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.
 
 
 
 
 
 

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