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

Do not reject /sync requests with unrecognised filter fields #14369

Merged
merged 2 commits into from
Nov 7, 2022
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/14369.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug where Synapse would raise an error when encountering an unrecognised field in a `/sync` filter, instead of ignoring it for forward compatibility.
8 changes: 4 additions & 4 deletions synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from synapse.server import HomeServer

FILTER_SCHEMA = {
"additionalProperties": False,
"additionalProperties": True, # Allow new fields for forward compatibility
"type": "object",
"properties": {
"limit": {"type": "number"},
Expand All @@ -63,7 +63,7 @@
}

ROOM_FILTER_SCHEMA = {
"additionalProperties": False,
"additionalProperties": True, # Allow new fields for forward compatibility
"type": "object",
"properties": {
"not_rooms": {"$ref": "#/definitions/room_id_array"},
Expand All @@ -77,7 +77,7 @@
}

ROOM_EVENT_FILTER_SCHEMA = {
"additionalProperties": False,
"additionalProperties": True, # Allow new fields for forward compatibility
"type": "object",
"properties": {
"limit": {"type": "number"},
Expand Down Expand Up @@ -143,7 +143,7 @@
},
},
},
"additionalProperties": False,
"additionalProperties": True, # Allow new fields for forward compatibility
}


Expand Down
21 changes: 19 additions & 2 deletions tests/api/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,36 @@ def prepare(self, reactor, clock, hs):
self.datastore = hs.get_datastores().main

def test_errors_on_invalid_filters(self):
# See USER_FILTER_SCHEMA for the filter schema.
invalid_filters = [
{"boom": {}},
# `account_data` must be a dictionary
{"account_data": "Hello World"},
# `event_fields` entries must not contain backslashes
{"event_fields": [r"\\foo"]},
{"room": {"timeline": {"limit": 0}, "state": {"not_bars": ["*"]}}},
# `event_format` must be "client" or "federation"
{"event_format": "other"},
# `not_rooms` must contain valid room IDs
{"room": {"not_rooms": ["#foo:pik-test"]}},
# `senders` must contain valid user IDs
{"presence": {"senders": ["@bar;pik.test.com"]}},
]
for filter in invalid_filters:
with self.assertRaises(SynapseError):
self.filtering.check_valid_filter(filter)

def test_ignores_unknown_filter_fields(self):
# For forward compatibility, we must ignore unknown filter fields.
# See USER_FILTER_SCHEMA for the filter schema.
filters = [
{"org.matrix.msc9999.future_option": True},
{"presence": {"org.matrix.msc9999.future_option": True}},
{"room": {"org.matrix.msc9999.future_option": True}},
{"room": {"timeline": {"org.matrix.msc9999.future_option": True}}},
]
for filter in filters:
self.filtering.check_valid_filter(filter)
# Must not raise.

def test_valid_filters(self):
valid_filters = [
{
Expand Down