MSC3873: Escape keys when flattening dicts. (#15004)

This disambiguates keys which attempt to match fields
with a dot in them (e.g. m.relates_to).

Disabled by default behind an experimental configuration flag.
1.103.0-whithout-watcha
Patrick Cloke 2 years ago committed by GitHub
parent a4126e2861
commit c951fbedcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      changelog.d/15004.feature
  2. 5
      synapse/config/experimental.py
  3. 30
      synapse/push/bulk_push_rule_evaluator.py
  4. 8
      tests/push/test_push_rule_evaluator.py

@ -0,0 +1 @@
Implement [MSC3873](https://github.com/matrix-org/matrix-spec-proposals/pull/3873) to unambiguate push rule keys with dots in them.

@ -169,6 +169,11 @@ class ExperimentalConfig(Config):
# MSC3925: do not replace events with their edits # MSC3925: do not replace events with their edits
self.msc3925_inhibit_edit = experimental.get("msc3925_inhibit_edit", False) self.msc3925_inhibit_edit = experimental.get("msc3925_inhibit_edit", False)
# MSC3873: Disambiguate event_match keys.
self.msc3783_escape_event_match_key = experimental.get(
"msc3783_escape_event_match_key", False
)
# MSC3952: Intentional mentions # MSC3952: Intentional mentions
self.msc3952_intentional_mentions = experimental.get( self.msc3952_intentional_mentions = experimental.get(
"msc3952_intentional_mentions", False "msc3952_intentional_mentions", False

@ -271,7 +271,10 @@ class BulkPushRuleEvaluator:
related_event_id, allow_none=True related_event_id, allow_none=True
) )
if related_event is not None: if related_event is not None:
related_events[relation_type] = _flatten_dict(related_event) related_events[relation_type] = _flatten_dict(
related_event,
msc3783_escape_event_match_key=self.hs.config.experimental.msc3783_escape_event_match_key,
)
reply_event_id = ( reply_event_id = (
event.content.get("m.relates_to", {}) event.content.get("m.relates_to", {})
@ -286,7 +289,10 @@ class BulkPushRuleEvaluator:
) )
if related_event is not None: if related_event is not None:
related_events["m.in_reply_to"] = _flatten_dict(related_event) related_events["m.in_reply_to"] = _flatten_dict(
related_event,
msc3783_escape_event_match_key=self.hs.config.experimental.msc3783_escape_event_match_key,
)
# indicate that this is from a fallback relation. # indicate that this is from a fallback relation.
if relation_type == "m.thread" and event.content.get( if relation_type == "m.thread" and event.content.get(
@ -405,7 +411,10 @@ class BulkPushRuleEvaluator:
room_mention = mentions.get("room") is True room_mention = mentions.get("room") is True
evaluator = PushRuleEvaluator( evaluator = PushRuleEvaluator(
_flatten_dict(event), _flatten_dict(
event,
msc3783_escape_event_match_key=self.hs.config.experimental.msc3783_escape_event_match_key,
),
has_mentions, has_mentions,
user_mentions, user_mentions,
room_mention, room_mention,
@ -493,6 +502,8 @@ def _flatten_dict(
d: Union[EventBase, Mapping[str, Any]], d: Union[EventBase, Mapping[str, Any]],
prefix: Optional[List[str]] = None, prefix: Optional[List[str]] = None,
result: Optional[Dict[str, str]] = None, result: Optional[Dict[str, str]] = None,
*,
msc3783_escape_event_match_key: bool = False,
) -> Dict[str, str]: ) -> Dict[str, str]:
""" """
Given a JSON dictionary (or event) which might contain sub dictionaries, Given a JSON dictionary (or event) which might contain sub dictionaries,
@ -521,11 +532,22 @@ def _flatten_dict(
if result is None: if result is None:
result = {} result = {}
for key, value in d.items(): for key, value in d.items():
if msc3783_escape_event_match_key:
# Escape periods in the key with a backslash (and backslashes with an
# extra backslash). This is since a period is used as a separator between
# nested fields.
key = key.replace("\\", "\\\\").replace(".", "\\.")
if isinstance(value, str): if isinstance(value, str):
result[".".join(prefix + [key])] = value.lower() result[".".join(prefix + [key])] = value.lower()
elif isinstance(value, Mapping): elif isinstance(value, Mapping):
# do not set `room_version` due to recursion considerations below # do not set `room_version` due to recursion considerations below
_flatten_dict(value, prefix=(prefix + [key]), result=result) _flatten_dict(
value,
prefix=(prefix + [key]),
result=result,
msc3783_escape_event_match_key=msc3783_escape_event_match_key,
)
# `room_version` should only ever be set when looking at the top level of an event # `room_version` should only ever be set when looking at the top level of an event
if ( if (

@ -48,6 +48,14 @@ class FlattenDictTestCase(unittest.TestCase):
input = {"foo": {"bar": "abc"}} input = {"foo": {"bar": "abc"}}
self.assertEqual({"foo.bar": "abc"}, _flatten_dict(input)) self.assertEqual({"foo.bar": "abc"}, _flatten_dict(input))
# If a field has a dot in it, escape it.
input = {"m.foo": {"b\\ar": "abc"}}
self.assertEqual({"m.foo.b\\ar": "abc"}, _flatten_dict(input))
self.assertEqual(
{"m\\.foo.b\\\\ar": "abc"},
_flatten_dict(input, msc3783_escape_event_match_key=True),
)
def test_non_string(self) -> None: def test_non_string(self) -> None:
"""Non-string items are dropped.""" """Non-string items are dropped."""
input: Dict[str, Any] = { input: Dict[str, Any] = {

Loading…
Cancel
Save