You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

810 lines
28 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2021 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import collections.abc
  16. import re
  17. from typing import (
  18. TYPE_CHECKING,
  19. Any,
  20. Awaitable,
  21. Callable,
  22. Dict,
  23. Iterable,
  24. List,
  25. Mapping,
  26. Match,
  27. MutableMapping,
  28. Optional,
  29. Union,
  30. )
  31. import attr
  32. from canonicaljson import encode_canonical_json
  33. from synapse.api.constants import (
  34. MAX_PDU_SIZE,
  35. EventContentFields,
  36. EventTypes,
  37. RelationTypes,
  38. )
  39. from synapse.api.errors import Codes, SynapseError
  40. from synapse.api.room_versions import RoomVersion
  41. from synapse.types import JsonDict, Requester
  42. from . import EventBase
  43. if TYPE_CHECKING:
  44. from synapse.handlers.relations import BundledAggregations
  45. from synapse.server import HomeServer
  46. # Split strings on "." but not "\." (or "\\\.").
  47. SPLIT_FIELD_REGEX = re.compile(r"\\*\.")
  48. # Find escaped characters, e.g. those with a \ in front of them.
  49. ESCAPE_SEQUENCE_PATTERN = re.compile(r"\\(.)")
  50. CANONICALJSON_MAX_INT = (2**53) - 1
  51. CANONICALJSON_MIN_INT = -CANONICALJSON_MAX_INT
  52. # Module API callback that allows adding fields to the unsigned section of
  53. # events that are sent to clients.
  54. ADD_EXTRA_FIELDS_TO_UNSIGNED_CLIENT_EVENT_CALLBACK = Callable[
  55. [EventBase], Awaitable[JsonDict]
  56. ]
  57. def prune_event(event: EventBase) -> EventBase:
  58. """Returns a pruned version of the given event, which removes all keys we
  59. don't know about or think could potentially be dodgy.
  60. This is used when we "redact" an event. We want to remove all fields that
  61. the user has specified, but we do want to keep necessary information like
  62. type, state_key etc.
  63. """
  64. pruned_event_dict = prune_event_dict(event.room_version, event.get_dict())
  65. from . import make_event_from_dict
  66. pruned_event = make_event_from_dict(
  67. pruned_event_dict, event.room_version, event.internal_metadata.get_dict()
  68. )
  69. # copy the internal fields
  70. pruned_event.internal_metadata.stream_ordering = (
  71. event.internal_metadata.stream_ordering
  72. )
  73. pruned_event.internal_metadata.outlier = event.internal_metadata.outlier
  74. # Mark the event as redacted
  75. pruned_event.internal_metadata.redacted = True
  76. return pruned_event
  77. def prune_event_dict(room_version: RoomVersion, event_dict: JsonDict) -> JsonDict:
  78. """Redacts the event_dict in the same way as `prune_event`, except it
  79. operates on dicts rather than event objects
  80. Returns:
  81. A copy of the pruned event dict
  82. """
  83. allowed_keys = [
  84. "event_id",
  85. "sender",
  86. "room_id",
  87. "hashes",
  88. "signatures",
  89. "content",
  90. "type",
  91. "state_key",
  92. "depth",
  93. "prev_events",
  94. "auth_events",
  95. "origin_server_ts",
  96. ]
  97. # Earlier room versions from had additional allowed keys.
  98. if not room_version.updated_redaction_rules:
  99. allowed_keys.extend(["prev_state", "membership", "origin"])
  100. event_type = event_dict["type"]
  101. new_content = {}
  102. def add_fields(*fields: str) -> None:
  103. for field in fields:
  104. if field in event_dict["content"]:
  105. new_content[field] = event_dict["content"][field]
  106. if event_type == EventTypes.Member:
  107. add_fields("membership")
  108. if room_version.restricted_join_rule_fix:
  109. add_fields(EventContentFields.AUTHORISING_USER)
  110. if room_version.updated_redaction_rules:
  111. # Preserve the signed field under third_party_invite.
  112. third_party_invite = event_dict["content"].get("third_party_invite")
  113. if isinstance(third_party_invite, collections.abc.Mapping):
  114. new_content["third_party_invite"] = {}
  115. if "signed" in third_party_invite:
  116. new_content["third_party_invite"]["signed"] = third_party_invite[
  117. "signed"
  118. ]
  119. elif event_type == EventTypes.Create:
  120. if room_version.updated_redaction_rules:
  121. # MSC2176 rules state that create events cannot have their `content` redacted.
  122. new_content = event_dict["content"]
  123. elif not room_version.implicit_room_creator:
  124. # Some room versions give meaning to `creator`
  125. add_fields("creator")
  126. elif event_type == EventTypes.JoinRules:
  127. add_fields("join_rule")
  128. if room_version.restricted_join_rule:
  129. add_fields("allow")
  130. elif event_type == EventTypes.PowerLevels:
  131. add_fields(
  132. "users",
  133. "users_default",
  134. "events",
  135. "events_default",
  136. "state_default",
  137. "ban",
  138. "kick",
  139. "redact",
  140. )
  141. if room_version.updated_redaction_rules:
  142. add_fields("invite")
  143. elif event_type == EventTypes.Aliases and room_version.special_case_aliases_auth:
  144. add_fields("aliases")
  145. elif event_type == EventTypes.RoomHistoryVisibility:
  146. add_fields("history_visibility")
  147. elif event_type == EventTypes.Redaction and room_version.updated_redaction_rules:
  148. add_fields("redacts")
  149. # Protect the rel_type and event_id fields under the m.relates_to field.
  150. if room_version.msc3389_relation_redactions:
  151. relates_to = event_dict["content"].get("m.relates_to")
  152. if isinstance(relates_to, collections.abc.Mapping):
  153. new_relates_to = {}
  154. for field in ("rel_type", "event_id"):
  155. if field in relates_to:
  156. new_relates_to[field] = relates_to[field]
  157. # Only include a non-empty relates_to field.
  158. if new_relates_to:
  159. new_content["m.relates_to"] = new_relates_to
  160. allowed_fields = {k: v for k, v in event_dict.items() if k in allowed_keys}
  161. allowed_fields["content"] = new_content
  162. unsigned: JsonDict = {}
  163. allowed_fields["unsigned"] = unsigned
  164. event_unsigned = event_dict.get("unsigned", {})
  165. if "age_ts" in event_unsigned:
  166. unsigned["age_ts"] = event_unsigned["age_ts"]
  167. if "replaces_state" in event_unsigned:
  168. unsigned["replaces_state"] = event_unsigned["replaces_state"]
  169. return allowed_fields
  170. def _copy_field(src: JsonDict, dst: JsonDict, field: List[str]) -> None:
  171. """Copy the field in 'src' to 'dst'.
  172. For example, if src={"foo":{"bar":5}} and dst={}, and field=["foo","bar"]
  173. then dst={"foo":{"bar":5}}.
  174. Args:
  175. src: The dict to read from.
  176. dst: The dict to modify.
  177. field: List of keys to drill down to in 'src'.
  178. """
  179. if len(field) == 0: # this should be impossible
  180. return
  181. if len(field) == 1: # common case e.g. 'origin_server_ts'
  182. if field[0] in src:
  183. dst[field[0]] = src[field[0]]
  184. return
  185. # Else is a nested field e.g. 'content.body'
  186. # Pop the last field as that's the key to move across and we need the
  187. # parent dict in order to access the data. Drill down to the right dict.
  188. key_to_move = field.pop(-1)
  189. sub_dict = src
  190. for sub_field in field: # e.g. sub_field => "content"
  191. if sub_field in sub_dict and isinstance(
  192. sub_dict[sub_field], collections.abc.Mapping
  193. ):
  194. sub_dict = sub_dict[sub_field]
  195. else:
  196. return
  197. if key_to_move not in sub_dict:
  198. return
  199. # Insert the key into the output dictionary, creating nested objects
  200. # as required. We couldn't do this any earlier or else we'd need to delete
  201. # the empty objects if the key didn't exist.
  202. sub_out_dict = dst
  203. for sub_field in field:
  204. sub_out_dict = sub_out_dict.setdefault(sub_field, {})
  205. sub_out_dict[key_to_move] = sub_dict[key_to_move]
  206. def _escape_slash(m: Match[str]) -> str:
  207. """
  208. Replacement function; replace a backslash-backslash or backslash-dot with the
  209. second character. Leaves any other string alone.
  210. """
  211. if m.group(1) in ("\\", "."):
  212. return m.group(1)
  213. return m.group(0)
  214. def _split_field(field: str) -> List[str]:
  215. """
  216. Splits strings on unescaped dots and removes escaping.
  217. Args:
  218. field: A string representing a path to a field.
  219. Returns:
  220. A list of nested fields to traverse.
  221. """
  222. # Convert the field and remove escaping:
  223. #
  224. # 1. "content.body.thing\.with\.dots"
  225. # 2. ["content", "body", "thing\.with\.dots"]
  226. # 3. ["content", "body", "thing.with.dots"]
  227. # Find all dots (and their preceding backslashes). If the dot is unescaped
  228. # then emit a new field part.
  229. result = []
  230. prev_start = 0
  231. for match in SPLIT_FIELD_REGEX.finditer(field):
  232. # If the match is an *even* number of characters than the dot was escaped.
  233. if len(match.group()) % 2 == 0:
  234. continue
  235. # Add a new part (up to the dot, exclusive) after escaping.
  236. result.append(
  237. ESCAPE_SEQUENCE_PATTERN.sub(
  238. _escape_slash, field[prev_start : match.end() - 1]
  239. )
  240. )
  241. prev_start = match.end()
  242. # Add any part of the field after the last unescaped dot. (Note that if the
  243. # character is a dot this correctly adds a blank string.)
  244. result.append(re.sub(r"\\(.)", _escape_slash, field[prev_start:]))
  245. return result
  246. def only_fields(dictionary: JsonDict, fields: List[str]) -> JsonDict:
  247. """Return a new dict with only the fields in 'dictionary' which are present
  248. in 'fields'.
  249. If there are no event fields specified then all fields are included.
  250. The entries may include '.' characters to indicate sub-fields.
  251. So ['content.body'] will include the 'body' field of the 'content' object.
  252. A literal '.' or '\' character in a field name may be escaped using a '\'.
  253. Args:
  254. dictionary: The dictionary to read from.
  255. fields: A list of fields to copy over. Only shallow refs are
  256. taken.
  257. Returns:
  258. A new dictionary with only the given fields. If fields was empty,
  259. the same dictionary is returned.
  260. """
  261. if len(fields) == 0:
  262. return dictionary
  263. # for each field, convert it:
  264. # ["content.body.thing\.with\.dots"] => [["content", "body", "thing\.with\.dots"]]
  265. split_fields = [_split_field(f) for f in fields]
  266. output: JsonDict = {}
  267. for field_array in split_fields:
  268. _copy_field(dictionary, output, field_array)
  269. return output
  270. def format_event_raw(d: JsonDict) -> JsonDict:
  271. return d
  272. def format_event_for_client_v1(d: JsonDict) -> JsonDict:
  273. d = format_event_for_client_v2(d)
  274. sender = d.get("sender")
  275. if sender is not None:
  276. d["user_id"] = sender
  277. copy_keys = (
  278. "age",
  279. "redacted_because",
  280. "replaces_state",
  281. "prev_content",
  282. "invite_room_state",
  283. "knock_room_state",
  284. )
  285. for key in copy_keys:
  286. if key in d["unsigned"]:
  287. d[key] = d["unsigned"][key]
  288. return d
  289. def format_event_for_client_v2(d: JsonDict) -> JsonDict:
  290. drop_keys = (
  291. "auth_events",
  292. "prev_events",
  293. "hashes",
  294. "signatures",
  295. "depth",
  296. "origin",
  297. "prev_state",
  298. )
  299. for key in drop_keys:
  300. d.pop(key, None)
  301. return d
  302. def format_event_for_client_v2_without_room_id(d: JsonDict) -> JsonDict:
  303. d = format_event_for_client_v2(d)
  304. d.pop("room_id", None)
  305. return d
  306. @attr.s(slots=True, frozen=True, auto_attribs=True)
  307. class SerializeEventConfig:
  308. as_client_event: bool = True
  309. # Function to convert from federation format to client format
  310. event_format: Callable[[JsonDict], JsonDict] = format_event_for_client_v1
  311. # The entity that requested the event. This is used to determine whether to include
  312. # the transaction_id in the unsigned section of the event.
  313. requester: Optional[Requester] = None
  314. # List of event fields to include. If empty, all fields will be returned.
  315. only_event_fields: Optional[List[str]] = None
  316. # Some events can have stripped room state stored in the `unsigned` field.
  317. # This is required for invite and knock functionality. If this option is
  318. # False, that state will be removed from the event before it is returned.
  319. # Otherwise, it will be kept.
  320. include_stripped_room_state: bool = False
  321. _DEFAULT_SERIALIZE_EVENT_CONFIG = SerializeEventConfig()
  322. def serialize_event(
  323. e: Union[JsonDict, EventBase],
  324. time_now_ms: int,
  325. *,
  326. config: SerializeEventConfig = _DEFAULT_SERIALIZE_EVENT_CONFIG,
  327. ) -> JsonDict:
  328. """Serialize event for clients
  329. Args:
  330. e
  331. time_now_ms
  332. config: Event serialization config
  333. Returns:
  334. The serialized event dictionary.
  335. """
  336. # FIXME(erikj): To handle the case of presence events and the like
  337. if not isinstance(e, EventBase):
  338. return e
  339. time_now_ms = int(time_now_ms)
  340. # Should this strip out None's?
  341. d = dict(e.get_dict().items())
  342. d["event_id"] = e.event_id
  343. if "age_ts" in d["unsigned"]:
  344. d["unsigned"]["age"] = time_now_ms - d["unsigned"]["age_ts"]
  345. del d["unsigned"]["age_ts"]
  346. if "redacted_because" in e.unsigned:
  347. d["unsigned"]["redacted_because"] = serialize_event(
  348. e.unsigned["redacted_because"],
  349. time_now_ms,
  350. config=config,
  351. )
  352. # If we have a txn_id saved in the internal_metadata, we should include it in the
  353. # unsigned section of the event if it was sent by the same session as the one
  354. # requesting the event.
  355. txn_id: Optional[str] = getattr(e.internal_metadata, "txn_id", None)
  356. if (
  357. txn_id is not None
  358. and config.requester is not None
  359. and config.requester.user.to_string() == e.sender
  360. ):
  361. # Some events do not have the device ID stored in the internal metadata,
  362. # this includes old events as well as those created by appservice, guests,
  363. # or with tokens minted with the admin API. For those events, fallback
  364. # to using the access token instead.
  365. event_device_id: Optional[str] = getattr(e.internal_metadata, "device_id", None)
  366. if event_device_id is not None:
  367. if event_device_id == config.requester.device_id:
  368. d["unsigned"]["transaction_id"] = txn_id
  369. else:
  370. # Fallback behaviour: only include the transaction ID if the event
  371. # was sent from the same access token.
  372. #
  373. # For regular users, the access token ID can be used to determine this.
  374. # This includes access tokens minted with the admin API.
  375. #
  376. # For guests and appservice users, we can't check the access token ID
  377. # so assume it is the same session.
  378. event_token_id: Optional[int] = getattr(
  379. e.internal_metadata, "token_id", None
  380. )
  381. if (
  382. (
  383. event_token_id is not None
  384. and config.requester.access_token_id is not None
  385. and event_token_id == config.requester.access_token_id
  386. )
  387. or config.requester.is_guest
  388. or config.requester.app_service
  389. ):
  390. d["unsigned"]["transaction_id"] = txn_id
  391. # invite_room_state and knock_room_state are a list of stripped room state events
  392. # that are meant to provide metadata about a room to an invitee/knocker. They are
  393. # intended to only be included in specific circumstances, such as down sync, and
  394. # should not be included in any other case.
  395. if not config.include_stripped_room_state:
  396. d["unsigned"].pop("invite_room_state", None)
  397. d["unsigned"].pop("knock_room_state", None)
  398. if config.as_client_event:
  399. d = config.event_format(d)
  400. # If the event is a redaction, the field with the redacted event ID appears
  401. # in a different location depending on the room version. e.redacts handles
  402. # fetching from the proper location; copy it to the other location for forwards-
  403. # and backwards-compatibility with clients.
  404. if e.type == EventTypes.Redaction and e.redacts is not None:
  405. if e.room_version.updated_redaction_rules:
  406. d["redacts"] = e.redacts
  407. else:
  408. d["content"] = dict(d["content"])
  409. d["content"]["redacts"] = e.redacts
  410. only_event_fields = config.only_event_fields
  411. if only_event_fields:
  412. if not isinstance(only_event_fields, list) or not all(
  413. isinstance(f, str) for f in only_event_fields
  414. ):
  415. raise TypeError("only_event_fields must be a list of strings")
  416. d = only_fields(d, only_event_fields)
  417. return d
  418. class EventClientSerializer:
  419. """Serializes events that are to be sent to clients.
  420. This is used for bundling extra information with any events to be sent to
  421. clients.
  422. """
  423. def __init__(self, hs: "HomeServer") -> None:
  424. self._store = hs.get_datastores().main
  425. self._add_extra_fields_to_unsigned_client_event_callbacks: List[
  426. ADD_EXTRA_FIELDS_TO_UNSIGNED_CLIENT_EVENT_CALLBACK
  427. ] = []
  428. async def serialize_event(
  429. self,
  430. event: Union[JsonDict, EventBase],
  431. time_now: int,
  432. *,
  433. config: SerializeEventConfig = _DEFAULT_SERIALIZE_EVENT_CONFIG,
  434. bundle_aggregations: Optional[Dict[str, "BundledAggregations"]] = None,
  435. ) -> JsonDict:
  436. """Serializes a single event.
  437. Args:
  438. event: The event being serialized.
  439. time_now: The current time in milliseconds
  440. config: Event serialization config
  441. bundle_aggregations: A map from event_id to the aggregations to be bundled
  442. into the event.
  443. Returns:
  444. The serialized event
  445. """
  446. # To handle the case of presence events and the like
  447. if not isinstance(event, EventBase):
  448. return event
  449. serialized_event = serialize_event(event, time_now, config=config)
  450. new_unsigned = {}
  451. for callback in self._add_extra_fields_to_unsigned_client_event_callbacks:
  452. u = await callback(event)
  453. new_unsigned.update(u)
  454. if new_unsigned:
  455. # We do the `update` this way round so that modules can't clobber
  456. # existing fields.
  457. new_unsigned.update(serialized_event["unsigned"])
  458. serialized_event["unsigned"] = new_unsigned
  459. # Check if there are any bundled aggregations to include with the event.
  460. if bundle_aggregations:
  461. if event.event_id in bundle_aggregations:
  462. await self._inject_bundled_aggregations(
  463. event,
  464. time_now,
  465. config,
  466. bundle_aggregations,
  467. serialized_event,
  468. )
  469. return serialized_event
  470. async def _inject_bundled_aggregations(
  471. self,
  472. event: EventBase,
  473. time_now: int,
  474. config: SerializeEventConfig,
  475. bundled_aggregations: Dict[str, "BundledAggregations"],
  476. serialized_event: JsonDict,
  477. ) -> None:
  478. """Potentially injects bundled aggregations into the unsigned portion of the serialized event.
  479. Args:
  480. event: The event being serialized.
  481. time_now: The current time in milliseconds
  482. config: Event serialization config
  483. bundled_aggregations: Bundled aggregations to be injected.
  484. A map from event_id to aggregation data. Must contain at least an
  485. entry for `event`.
  486. While serializing the bundled aggregations this map may be searched
  487. again for additional events in a recursive manner.
  488. serialized_event: The serialized event which may be modified.
  489. """
  490. # We have already checked that aggregations exist for this event.
  491. event_aggregations = bundled_aggregations[event.event_id]
  492. # The JSON dictionary to be added under the unsigned property of the event
  493. # being serialized.
  494. serialized_aggregations = {}
  495. if event_aggregations.references:
  496. serialized_aggregations[
  497. RelationTypes.REFERENCE
  498. ] = event_aggregations.references
  499. if event_aggregations.replace:
  500. # Include information about it in the relations dict.
  501. #
  502. # Matrix spec v1.5 (https://spec.matrix.org/v1.5/client-server-api/#server-side-aggregation-of-mreplace-relationships)
  503. # said that we should only include the `event_id`, `origin_server_ts` and
  504. # `sender` of the edit; however MSC3925 proposes extending it to the whole
  505. # of the edit, which is what we do here.
  506. serialized_aggregations[RelationTypes.REPLACE] = await self.serialize_event(
  507. event_aggregations.replace,
  508. time_now,
  509. config=config,
  510. )
  511. # Include any threaded replies to this event.
  512. if event_aggregations.thread:
  513. thread = event_aggregations.thread
  514. serialized_latest_event = await self.serialize_event(
  515. thread.latest_event,
  516. time_now,
  517. config=config,
  518. bundle_aggregations=bundled_aggregations,
  519. )
  520. thread_summary = {
  521. "latest_event": serialized_latest_event,
  522. "count": thread.count,
  523. "current_user_participated": thread.current_user_participated,
  524. }
  525. serialized_aggregations[RelationTypes.THREAD] = thread_summary
  526. # Include the bundled aggregations in the event.
  527. if serialized_aggregations:
  528. # There is likely already an "unsigned" field, but a filter might
  529. # have stripped it off (via the event_fields option). The server is
  530. # allowed to return additional fields, so add it back.
  531. serialized_event.setdefault("unsigned", {}).setdefault(
  532. "m.relations", {}
  533. ).update(serialized_aggregations)
  534. async def serialize_events(
  535. self,
  536. events: Iterable[Union[JsonDict, EventBase]],
  537. time_now: int,
  538. *,
  539. config: SerializeEventConfig = _DEFAULT_SERIALIZE_EVENT_CONFIG,
  540. bundle_aggregations: Optional[Dict[str, "BundledAggregations"]] = None,
  541. ) -> List[JsonDict]:
  542. """Serializes multiple events.
  543. Args:
  544. event
  545. time_now: The current time in milliseconds
  546. config: Event serialization config
  547. bundle_aggregations: Whether to include the bundled aggregations for this
  548. event. Only applies to non-state events. (State events never include
  549. bundled aggregations.)
  550. Returns:
  551. The list of serialized events
  552. """
  553. return [
  554. await self.serialize_event(
  555. event,
  556. time_now,
  557. config=config,
  558. bundle_aggregations=bundle_aggregations,
  559. )
  560. for event in events
  561. ]
  562. def register_add_extra_fields_to_unsigned_client_event_callback(
  563. self, callback: ADD_EXTRA_FIELDS_TO_UNSIGNED_CLIENT_EVENT_CALLBACK
  564. ) -> None:
  565. """Register a callback that returns additions to the unsigned section of
  566. serialized events.
  567. """
  568. self._add_extra_fields_to_unsigned_client_event_callbacks.append(callback)
  569. _PowerLevel = Union[str, int]
  570. PowerLevelsContent = Mapping[str, Union[_PowerLevel, Mapping[str, _PowerLevel]]]
  571. def copy_and_fixup_power_levels_contents(
  572. old_power_levels: PowerLevelsContent,
  573. ) -> Dict[str, Union[int, Dict[str, int]]]:
  574. """Copy the content of a power_levels event, unfreezing immutabledicts along the way.
  575. We accept as input power level values which are strings, provided they represent an
  576. integer, e.g. `"`100"` instead of 100. Such strings are converted to integers
  577. in the returned dictionary (hence "fixup" in the function name).
  578. Note that future room versions will outlaw such stringy power levels (see
  579. https://github.com/matrix-org/matrix-spec/issues/853).
  580. Raises:
  581. TypeError if the input does not look like a valid power levels event content
  582. """
  583. if not isinstance(old_power_levels, collections.abc.Mapping):
  584. raise TypeError("Not a valid power-levels content: %r" % (old_power_levels,))
  585. power_levels: Dict[str, Union[int, Dict[str, int]]] = {}
  586. for k, v in old_power_levels.items():
  587. if isinstance(v, collections.abc.Mapping):
  588. h: Dict[str, int] = {}
  589. power_levels[k] = h
  590. for k1, v1 in v.items():
  591. _copy_power_level_value_as_integer(v1, h, k1)
  592. else:
  593. _copy_power_level_value_as_integer(v, power_levels, k)
  594. return power_levels
  595. def _copy_power_level_value_as_integer(
  596. old_value: object,
  597. power_levels: MutableMapping[str, Any],
  598. key: str,
  599. ) -> None:
  600. """Set `power_levels[key]` to the integer represented by `old_value`.
  601. :raises TypeError: if `old_value` is neither an integer nor a base-10 string
  602. representation of an integer.
  603. """
  604. if type(old_value) is int: # noqa: E721
  605. power_levels[key] = old_value
  606. return
  607. if isinstance(old_value, str):
  608. try:
  609. parsed_value = int(old_value, base=10)
  610. except ValueError:
  611. # Fall through to the final TypeError.
  612. pass
  613. else:
  614. power_levels[key] = parsed_value
  615. return
  616. raise TypeError(f"Invalid power_levels value for {key}: {old_value}")
  617. def validate_canonicaljson(value: Any) -> None:
  618. """
  619. Ensure that the JSON object is valid according to the rules of canonical JSON.
  620. See the appendix section 3.1: Canonical JSON.
  621. This rejects JSON that has:
  622. * An integer outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1]
  623. * Floats
  624. * NaN, Infinity, -Infinity
  625. """
  626. if type(value) is int: # noqa: E721
  627. if value < CANONICALJSON_MIN_INT or CANONICALJSON_MAX_INT < value:
  628. raise SynapseError(400, "JSON integer out of range", Codes.BAD_JSON)
  629. elif isinstance(value, float):
  630. # Note that Infinity, -Infinity, and NaN are also considered floats.
  631. raise SynapseError(400, "Bad JSON value: float", Codes.BAD_JSON)
  632. elif isinstance(value, collections.abc.Mapping):
  633. for v in value.values():
  634. validate_canonicaljson(v)
  635. elif isinstance(value, (list, tuple)):
  636. for i in value:
  637. validate_canonicaljson(i)
  638. elif not isinstance(value, (bool, str)) and value is not None:
  639. # Other potential JSON values (bool, None, str) are safe.
  640. raise SynapseError(400, "Unknown JSON value", Codes.BAD_JSON)
  641. def maybe_upsert_event_field(
  642. event: EventBase, container: JsonDict, key: str, value: object
  643. ) -> bool:
  644. """Upsert an event field, but only if this doesn't make the event too large.
  645. Returns true iff the upsert took place.
  646. """
  647. if key in container:
  648. old_value: object = container[key]
  649. container[key] = value
  650. # NB: here and below, we assume that passing a non-None `time_now` argument to
  651. # get_pdu_json doesn't increase the size of the encoded result.
  652. upsert_okay = len(encode_canonical_json(event.get_pdu_json())) <= MAX_PDU_SIZE
  653. if not upsert_okay:
  654. container[key] = old_value
  655. else:
  656. container[key] = value
  657. upsert_okay = len(encode_canonical_json(event.get_pdu_json())) <= MAX_PDU_SIZE
  658. if not upsert_okay:
  659. del container[key]
  660. return upsert_okay