Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Allow backslashes in event field filters #4083

Merged
merged 2 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4083.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug which prevented backslashes being used in event field filters
5 changes: 4 additions & 1 deletion synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@
# events a lot easier as we can then use a negative lookbehind
# assertion to split '\.' If we allowed \\ then it would
# incorrectly split '\\.' See synapse.events.utils.serialize_event
"pattern": "^((?!\\\).)*$"
#
# Note that because this is a regular expression, we have to escape
# each backslash in the pattern.
"pattern": r"^((?!\\\\).)*$"
}
}
},
Expand Down
12 changes: 11 additions & 1 deletion tests/api/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_errors_on_invalid_filters(self):
invalid_filters = [
{"boom": {}},
{"account_data": "Hello World"},
{"event_fields": ["\\foo"]},
{"event_fields": [r"\\foo"]},
{"room": {"timeline": {"limit": 0}, "state": {"not_bars": ["*"]}}},
{"event_format": "other"},
{"room": {"not_rooms": ["#foo:pik-test"]}},
Expand Down Expand Up @@ -109,6 +109,16 @@ def test_valid_filters(self):
"event_format": "client",
"event_fields": ["type", "content", "sender"],
},

# a single backslash should be permitted (though it is debatable whether
# it should be permitted before anything other than `.`, and what that
# actually means)
#
# (note that event_fields is implemented in
# synapse.events.utils.serialize_event, and so whether this actually works
# is tested elsewhere. We just want to check that it is allowed through the
# filter validation)
{"event_fields": [r"foo\.bar"]},
]
for filter in valid_filters:
try:
Expand Down