utils.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. MutableMapping,
  26. Optional,
  27. Union,
  28. )
  29. import attr
  30. from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
  31. from synapse.api.errors import Codes, SynapseError
  32. from synapse.api.room_versions import RoomVersion
  33. from synapse.types import JsonDict
  34. from synapse.util.frozenutils import unfreeze
  35. from . import EventBase
  36. if TYPE_CHECKING:
  37. from synapse.handlers.relations import BundledAggregations
  38. # Split strings on "." but not "\." This uses a negative lookbehind assertion for '\'
  39. # (?<!stuff) matches if the current position in the string is not preceded
  40. # by a match for 'stuff'.
  41. # TODO: This is fast, but fails to handle "foo\\.bar" which should be treated as
  42. # the literal fields "foo\" and "bar" but will instead be treated as "foo\\.bar"
  43. SPLIT_FIELD_REGEX = re.compile(r"(?<!\\)\.")
  44. CANONICALJSON_MAX_INT = (2**53) - 1
  45. CANONICALJSON_MIN_INT = -CANONICALJSON_MAX_INT
  46. def prune_event(event: EventBase) -> EventBase:
  47. """Returns a pruned version of the given event, which removes all keys we
  48. don't know about or think could potentially be dodgy.
  49. This is used when we "redact" an event. We want to remove all fields that
  50. the user has specified, but we do want to keep necessary information like
  51. type, state_key etc.
  52. """
  53. pruned_event_dict = prune_event_dict(event.room_version, event.get_dict())
  54. from . import make_event_from_dict
  55. pruned_event = make_event_from_dict(
  56. pruned_event_dict, event.room_version, event.internal_metadata.get_dict()
  57. )
  58. # copy the internal fields
  59. pruned_event.internal_metadata.stream_ordering = (
  60. event.internal_metadata.stream_ordering
  61. )
  62. pruned_event.internal_metadata.outlier = event.internal_metadata.outlier
  63. # Mark the event as redacted
  64. pruned_event.internal_metadata.redacted = True
  65. return pruned_event
  66. def prune_event_dict(room_version: RoomVersion, event_dict: JsonDict) -> JsonDict:
  67. """Redacts the event_dict in the same way as `prune_event`, except it
  68. operates on dicts rather than event objects
  69. Returns:
  70. A copy of the pruned event dict
  71. """
  72. allowed_keys = [
  73. "event_id",
  74. "sender",
  75. "room_id",
  76. "hashes",
  77. "signatures",
  78. "content",
  79. "type",
  80. "state_key",
  81. "depth",
  82. "prev_events",
  83. "auth_events",
  84. "origin",
  85. "origin_server_ts",
  86. ]
  87. # Room versions from before MSC2176 had additional allowed keys.
  88. if not room_version.msc2176_redaction_rules:
  89. allowed_keys.extend(["prev_state", "membership"])
  90. event_type = event_dict["type"]
  91. new_content = {}
  92. def add_fields(*fields: str) -> None:
  93. for field in fields:
  94. if field in event_dict["content"]:
  95. new_content[field] = event_dict["content"][field]
  96. if event_type == EventTypes.Member:
  97. add_fields("membership")
  98. if room_version.msc3375_redaction_rules:
  99. add_fields(EventContentFields.AUTHORISING_USER)
  100. elif event_type == EventTypes.Create:
  101. # MSC2176 rules state that create events cannot be redacted.
  102. if room_version.msc2176_redaction_rules:
  103. return event_dict
  104. add_fields("creator")
  105. elif event_type == EventTypes.JoinRules:
  106. add_fields("join_rule")
  107. if room_version.msc3083_join_rules:
  108. add_fields("allow")
  109. elif event_type == EventTypes.PowerLevels:
  110. add_fields(
  111. "users",
  112. "users_default",
  113. "events",
  114. "events_default",
  115. "state_default",
  116. "ban",
  117. "kick",
  118. "redact",
  119. )
  120. if room_version.msc2176_redaction_rules:
  121. add_fields("invite")
  122. if room_version.msc2716_historical:
  123. add_fields("historical")
  124. elif event_type == EventTypes.Aliases and room_version.special_case_aliases_auth:
  125. add_fields("aliases")
  126. elif event_type == EventTypes.RoomHistoryVisibility:
  127. add_fields("history_visibility")
  128. elif event_type == EventTypes.Redaction and room_version.msc2176_redaction_rules:
  129. add_fields("redacts")
  130. elif room_version.msc2716_redactions and event_type == EventTypes.MSC2716_INSERTION:
  131. add_fields(EventContentFields.MSC2716_NEXT_BATCH_ID)
  132. elif room_version.msc2716_redactions and event_type == EventTypes.MSC2716_BATCH:
  133. add_fields(EventContentFields.MSC2716_BATCH_ID)
  134. elif room_version.msc2716_redactions and event_type == EventTypes.MSC2716_MARKER:
  135. add_fields(EventContentFields.MSC2716_MARKER_INSERTION)
  136. allowed_fields = {k: v for k, v in event_dict.items() if k in allowed_keys}
  137. allowed_fields["content"] = new_content
  138. unsigned: JsonDict = {}
  139. allowed_fields["unsigned"] = unsigned
  140. event_unsigned = event_dict.get("unsigned", {})
  141. if "age_ts" in event_unsigned:
  142. unsigned["age_ts"] = event_unsigned["age_ts"]
  143. if "replaces_state" in event_unsigned:
  144. unsigned["replaces_state"] = event_unsigned["replaces_state"]
  145. return allowed_fields
  146. def _copy_field(src: JsonDict, dst: JsonDict, field: List[str]) -> None:
  147. """Copy the field in 'src' to 'dst'.
  148. For example, if src={"foo":{"bar":5}} and dst={}, and field=["foo","bar"]
  149. then dst={"foo":{"bar":5}}.
  150. Args:
  151. src: The dict to read from.
  152. dst: The dict to modify.
  153. field: List of keys to drill down to in 'src'.
  154. """
  155. if len(field) == 0: # this should be impossible
  156. return
  157. if len(field) == 1: # common case e.g. 'origin_server_ts'
  158. if field[0] in src:
  159. dst[field[0]] = src[field[0]]
  160. return
  161. # Else is a nested field e.g. 'content.body'
  162. # Pop the last field as that's the key to move across and we need the
  163. # parent dict in order to access the data. Drill down to the right dict.
  164. key_to_move = field.pop(-1)
  165. sub_dict = src
  166. for sub_field in field: # e.g. sub_field => "content"
  167. if sub_field in sub_dict and isinstance(
  168. sub_dict[sub_field], collections.abc.Mapping
  169. ):
  170. sub_dict = sub_dict[sub_field]
  171. else:
  172. return
  173. if key_to_move not in sub_dict:
  174. return
  175. # Insert the key into the output dictionary, creating nested objects
  176. # as required. We couldn't do this any earlier or else we'd need to delete
  177. # the empty objects if the key didn't exist.
  178. sub_out_dict = dst
  179. for sub_field in field:
  180. sub_out_dict = sub_out_dict.setdefault(sub_field, {})
  181. sub_out_dict[key_to_move] = sub_dict[key_to_move]
  182. def only_fields(dictionary: JsonDict, fields: List[str]) -> JsonDict:
  183. """Return a new dict with only the fields in 'dictionary' which are present
  184. in 'fields'.
  185. If there are no event fields specified then all fields are included.
  186. The entries may include '.' characters to indicate sub-fields.
  187. So ['content.body'] will include the 'body' field of the 'content' object.
  188. A literal '.' character in a field name may be escaped using a '\'.
  189. Args:
  190. dictionary: The dictionary to read from.
  191. fields: A list of fields to copy over. Only shallow refs are
  192. taken.
  193. Returns:
  194. A new dictionary with only the given fields. If fields was empty,
  195. the same dictionary is returned.
  196. """
  197. if len(fields) == 0:
  198. return dictionary
  199. # for each field, convert it:
  200. # ["content.body.thing\.with\.dots"] => [["content", "body", "thing\.with\.dots"]]
  201. split_fields = [SPLIT_FIELD_REGEX.split(f) for f in fields]
  202. # for each element of the output array of arrays:
  203. # remove escaping so we can use the right key names.
  204. split_fields[:] = [
  205. [f.replace(r"\.", r".") for f in field_array] for field_array in split_fields
  206. ]
  207. output: JsonDict = {}
  208. for field_array in split_fields:
  209. _copy_field(dictionary, output, field_array)
  210. return output
  211. def format_event_raw(d: JsonDict) -> JsonDict:
  212. return d
  213. def format_event_for_client_v1(d: JsonDict) -> JsonDict:
  214. d = format_event_for_client_v2(d)
  215. sender = d.get("sender")
  216. if sender is not None:
  217. d["user_id"] = sender
  218. copy_keys = (
  219. "age",
  220. "redacted_because",
  221. "replaces_state",
  222. "prev_content",
  223. "invite_room_state",
  224. "knock_room_state",
  225. )
  226. for key in copy_keys:
  227. if key in d["unsigned"]:
  228. d[key] = d["unsigned"][key]
  229. return d
  230. def format_event_for_client_v2(d: JsonDict) -> JsonDict:
  231. drop_keys = (
  232. "auth_events",
  233. "prev_events",
  234. "hashes",
  235. "signatures",
  236. "depth",
  237. "origin",
  238. "prev_state",
  239. )
  240. for key in drop_keys:
  241. d.pop(key, None)
  242. return d
  243. def format_event_for_client_v2_without_room_id(d: JsonDict) -> JsonDict:
  244. d = format_event_for_client_v2(d)
  245. d.pop("room_id", None)
  246. return d
  247. @attr.s(slots=True, frozen=True, auto_attribs=True)
  248. class SerializeEventConfig:
  249. as_client_event: bool = True
  250. # Function to convert from federation format to client format
  251. event_format: Callable[[JsonDict], JsonDict] = format_event_for_client_v1
  252. # ID of the user's auth token - used for namespacing of transaction IDs
  253. token_id: Optional[int] = None
  254. # List of event fields to include. If empty, all fields will be returned.
  255. only_event_fields: Optional[List[str]] = None
  256. # Some events can have stripped room state stored in the `unsigned` field.
  257. # This is required for invite and knock functionality. If this option is
  258. # False, that state will be removed from the event before it is returned.
  259. # Otherwise, it will be kept.
  260. include_stripped_room_state: bool = False
  261. _DEFAULT_SERIALIZE_EVENT_CONFIG = SerializeEventConfig()
  262. def serialize_event(
  263. e: Union[JsonDict, EventBase],
  264. time_now_ms: int,
  265. *,
  266. config: SerializeEventConfig = _DEFAULT_SERIALIZE_EVENT_CONFIG,
  267. ) -> JsonDict:
  268. """Serialize event for clients
  269. Args:
  270. e
  271. time_now_ms
  272. config: Event serialization config
  273. Returns:
  274. The serialized event dictionary.
  275. """
  276. # FIXME(erikj): To handle the case of presence events and the like
  277. if not isinstance(e, EventBase):
  278. return e
  279. time_now_ms = int(time_now_ms)
  280. # Should this strip out None's?
  281. d = {k: v for k, v in e.get_dict().items()}
  282. d["event_id"] = e.event_id
  283. if "age_ts" in d["unsigned"]:
  284. d["unsigned"]["age"] = time_now_ms - d["unsigned"]["age_ts"]
  285. del d["unsigned"]["age_ts"]
  286. if "redacted_because" in e.unsigned:
  287. d["unsigned"]["redacted_because"] = serialize_event(
  288. e.unsigned["redacted_because"], time_now_ms, config=config
  289. )
  290. if config.token_id is not None:
  291. if config.token_id == getattr(e.internal_metadata, "token_id", None):
  292. txn_id = getattr(e.internal_metadata, "txn_id", None)
  293. if txn_id is not None:
  294. d["unsigned"]["transaction_id"] = txn_id
  295. # invite_room_state and knock_room_state are a list of stripped room state events
  296. # that are meant to provide metadata about a room to an invitee/knocker. They are
  297. # intended to only be included in specific circumstances, such as down sync, and
  298. # should not be included in any other case.
  299. if not config.include_stripped_room_state:
  300. d["unsigned"].pop("invite_room_state", None)
  301. d["unsigned"].pop("knock_room_state", None)
  302. if config.as_client_event:
  303. d = config.event_format(d)
  304. only_event_fields = config.only_event_fields
  305. if only_event_fields:
  306. if not isinstance(only_event_fields, list) or not all(
  307. isinstance(f, str) for f in only_event_fields
  308. ):
  309. raise TypeError("only_event_fields must be a list of strings")
  310. d = only_fields(d, only_event_fields)
  311. return d
  312. class EventClientSerializer:
  313. """Serializes events that are to be sent to clients.
  314. This is used for bundling extra information with any events to be sent to
  315. clients.
  316. """
  317. def serialize_event(
  318. self,
  319. event: Union[JsonDict, EventBase],
  320. time_now: int,
  321. *,
  322. config: SerializeEventConfig = _DEFAULT_SERIALIZE_EVENT_CONFIG,
  323. bundle_aggregations: Optional[Dict[str, "BundledAggregations"]] = None,
  324. apply_edits: bool = True,
  325. ) -> JsonDict:
  326. """Serializes a single event.
  327. Args:
  328. event: The event being serialized.
  329. time_now: The current time in milliseconds
  330. config: Event serialization config
  331. bundle_aggregations: A map from event_id to the aggregations to be bundled
  332. into the event.
  333. apply_edits: Whether the content of the event should be modified to reflect
  334. any replacement in `bundle_aggregations[<event_id>].replace`.
  335. Returns:
  336. The serialized event
  337. """
  338. # To handle the case of presence events and the like
  339. if not isinstance(event, EventBase):
  340. return event
  341. serialized_event = serialize_event(event, time_now, config=config)
  342. # Check if there are any bundled aggregations to include with the event.
  343. if bundle_aggregations:
  344. if event.event_id in bundle_aggregations:
  345. self._inject_bundled_aggregations(
  346. event,
  347. time_now,
  348. config,
  349. bundle_aggregations,
  350. serialized_event,
  351. apply_edits=apply_edits,
  352. )
  353. return serialized_event
  354. def _apply_edit(
  355. self, orig_event: EventBase, serialized_event: JsonDict, edit: EventBase
  356. ) -> None:
  357. """Replace the content, preserving existing relations of the serialized event.
  358. Args:
  359. orig_event: The original event.
  360. serialized_event: The original event, serialized. This is modified.
  361. edit: The event which edits the above.
  362. """
  363. # Ensure we take copies of the edit content, otherwise we risk modifying
  364. # the original event.
  365. edit_content = edit.content.copy()
  366. # Unfreeze the event content if necessary, so that we may modify it below
  367. edit_content = unfreeze(edit_content)
  368. serialized_event["content"] = edit_content.get("m.new_content", {})
  369. # Check for existing relations
  370. relates_to = orig_event.content.get("m.relates_to")
  371. if relates_to:
  372. # Keep the relations, ensuring we use a dict copy of the original
  373. serialized_event["content"]["m.relates_to"] = relates_to.copy()
  374. else:
  375. serialized_event["content"].pop("m.relates_to", None)
  376. def _inject_bundled_aggregations(
  377. self,
  378. event: EventBase,
  379. time_now: int,
  380. config: SerializeEventConfig,
  381. bundled_aggregations: Dict[str, "BundledAggregations"],
  382. serialized_event: JsonDict,
  383. apply_edits: bool,
  384. ) -> None:
  385. """Potentially injects bundled aggregations into the unsigned portion of the serialized event.
  386. Args:
  387. event: The event being serialized.
  388. time_now: The current time in milliseconds
  389. config: Event serialization config
  390. bundled_aggregations: Bundled aggregations to be injected.
  391. A map from event_id to aggregation data. Must contain at least an
  392. entry for `event`.
  393. While serializing the bundled aggregations this map may be searched
  394. again for additional events in a recursive manner.
  395. serialized_event: The serialized event which may be modified.
  396. apply_edits: Whether the content of the event should be modified to reflect
  397. any replacement in `aggregations.replace`.
  398. """
  399. # We have already checked that aggregations exist for this event.
  400. event_aggregations = bundled_aggregations[event.event_id]
  401. # The JSON dictionary to be added under the unsigned property of the event
  402. # being serialized.
  403. serialized_aggregations = {}
  404. if event_aggregations.annotations:
  405. serialized_aggregations[
  406. RelationTypes.ANNOTATION
  407. ] = event_aggregations.annotations
  408. if event_aggregations.references:
  409. serialized_aggregations[
  410. RelationTypes.REFERENCE
  411. ] = event_aggregations.references
  412. if event_aggregations.replace:
  413. # If there is an edit, optionally apply it to the event.
  414. edit = event_aggregations.replace
  415. if apply_edits:
  416. self._apply_edit(event, serialized_event, edit)
  417. # Include information about it in the relations dict.
  418. serialized_aggregations[RelationTypes.REPLACE] = {
  419. "event_id": edit.event_id,
  420. "origin_server_ts": edit.origin_server_ts,
  421. "sender": edit.sender,
  422. }
  423. # Include any threaded replies to this event.
  424. if event_aggregations.thread:
  425. thread = event_aggregations.thread
  426. serialized_latest_event = self.serialize_event(
  427. thread.latest_event,
  428. time_now,
  429. config=config,
  430. bundle_aggregations=bundled_aggregations,
  431. )
  432. thread_summary = {
  433. "latest_event": serialized_latest_event,
  434. "count": thread.count,
  435. "current_user_participated": thread.current_user_participated,
  436. }
  437. serialized_aggregations[RelationTypes.THREAD] = thread_summary
  438. # Include the bundled aggregations in the event.
  439. if serialized_aggregations:
  440. # There is likely already an "unsigned" field, but a filter might
  441. # have stripped it off (via the event_fields option). The server is
  442. # allowed to return additional fields, so add it back.
  443. serialized_event.setdefault("unsigned", {}).setdefault(
  444. "m.relations", {}
  445. ).update(serialized_aggregations)
  446. def serialize_events(
  447. self,
  448. events: Iterable[Union[JsonDict, EventBase]],
  449. time_now: int,
  450. *,
  451. config: SerializeEventConfig = _DEFAULT_SERIALIZE_EVENT_CONFIG,
  452. bundle_aggregations: Optional[Dict[str, "BundledAggregations"]] = None,
  453. ) -> List[JsonDict]:
  454. """Serializes multiple events.
  455. Args:
  456. event
  457. time_now: The current time in milliseconds
  458. config: Event serialization config
  459. bundle_aggregations: Whether to include the bundled aggregations for this
  460. event. Only applies to non-state events. (State events never include
  461. bundled aggregations.)
  462. Returns:
  463. The list of serialized events
  464. """
  465. return [
  466. self.serialize_event(
  467. event,
  468. time_now,
  469. config=config,
  470. bundle_aggregations=bundle_aggregations,
  471. )
  472. for event in events
  473. ]
  474. _PowerLevel = Union[str, int]
  475. def copy_and_fixup_power_levels_contents(
  476. old_power_levels: Mapping[str, Union[_PowerLevel, Mapping[str, _PowerLevel]]]
  477. ) -> Dict[str, Union[int, Dict[str, int]]]:
  478. """Copy the content of a power_levels event, unfreezing frozendicts along the way.
  479. We accept as input power level values which are strings, provided they represent an
  480. integer, e.g. `"`100"` instead of 100. Such strings are converted to integers
  481. in the returned dictionary (hence "fixup" in the function name).
  482. Note that future room versions will outlaw such stringy power levels (see
  483. https://github.com/matrix-org/matrix-spec/issues/853).
  484. Raises:
  485. TypeError if the input does not look like a valid power levels event content
  486. """
  487. if not isinstance(old_power_levels, collections.abc.Mapping):
  488. raise TypeError("Not a valid power-levels content: %r" % (old_power_levels,))
  489. power_levels: Dict[str, Union[int, Dict[str, int]]] = {}
  490. for k, v in old_power_levels.items():
  491. if isinstance(v, collections.abc.Mapping):
  492. h: Dict[str, int] = {}
  493. power_levels[k] = h
  494. for k1, v1 in v.items():
  495. _copy_power_level_value_as_integer(v1, h, k1)
  496. else:
  497. _copy_power_level_value_as_integer(v, power_levels, k)
  498. return power_levels
  499. def _copy_power_level_value_as_integer(
  500. old_value: object,
  501. power_levels: MutableMapping[str, Any],
  502. key: str,
  503. ) -> None:
  504. """Set `power_levels[key]` to the integer represented by `old_value`.
  505. :raises TypeError: if `old_value` is not an integer, nor a base-10 string
  506. representation of an integer.
  507. """
  508. if isinstance(old_value, int):
  509. power_levels[key] = old_value
  510. return
  511. if isinstance(old_value, str):
  512. try:
  513. parsed_value = int(old_value, base=10)
  514. except ValueError:
  515. # Fall through to the final TypeError.
  516. pass
  517. else:
  518. power_levels[key] = parsed_value
  519. return
  520. raise TypeError(f"Invalid power_levels value for {key}: {old_value}")
  521. def validate_canonicaljson(value: Any) -> None:
  522. """
  523. Ensure that the JSON object is valid according to the rules of canonical JSON.
  524. See the appendix section 3.1: Canonical JSON.
  525. This rejects JSON that has:
  526. * An integer outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1]
  527. * Floats
  528. * NaN, Infinity, -Infinity
  529. """
  530. if isinstance(value, int):
  531. if value < CANONICALJSON_MIN_INT or CANONICALJSON_MAX_INT < value:
  532. raise SynapseError(400, "JSON integer out of range", Codes.BAD_JSON)
  533. elif isinstance(value, float):
  534. # Note that Infinity, -Infinity, and NaN are also considered floats.
  535. raise SynapseError(400, "Bad JSON value: float", Codes.BAD_JSON)
  536. elif isinstance(value, collections.abc.Mapping):
  537. for v in value.values():
  538. validate_canonicaljson(v)
  539. elif isinstance(value, (list, tuple)):
  540. for i in value:
  541. validate_canonicaljson(i)
  542. elif not isinstance(value, (bool, str)) and value is not None:
  543. # Other potential JSON values (bool, None, str) are safe.
  544. raise SynapseError(400, "Unknown JSON value", Codes.BAD_JSON)