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.
 
 
 
 
 
 

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