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

Commit 510d4b0

Browse files
author
David Robertson
authored
Handle malformed values of notification.room in power level events (#14942)
* Better test for bad values in power levels events The previous test only checked that Synapse didn't raise an exception, but didn't check that we had correctly interpreted the value of the dodgy power level. It also conflated two things: bad room notification levels, and bad user levels. There _is_ logic for converting the latter to integers, but we should test it separately. * Check we ignore types that don't convert to int * Handle `None` values in `notifications.room` * Changelog * Also test that bad values are rejected by event auth * Docstring * linter scripttttttttt
1 parent 43c7d81 commit 510d4b0

File tree

4 files changed

+128
-17
lines changed

4 files changed

+128
-17
lines changed

changelog.d/14942.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug introduced in Synapse 1.68.0 where we were unable to service remote joins in rooms with `@room` notification levels set to `null` in their (malformed) power levels.

synapse/push/bulk_push_rule_evaluator.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
}
7070

7171

72+
SENTINEL = object()
73+
74+
7275
def _should_count_as_unread(event: EventBase, context: EventContext) -> bool:
7376
# Exclude rejected and soft-failed events.
7477
if context.rejected or event.internal_metadata.is_soft_failed():
@@ -343,11 +346,21 @@ async def _action_for_event_by_user(
343346
related_events = await self._related_events(event)
344347

345348
# It's possible that old room versions have non-integer power levels (floats or
346-
# strings). Workaround this by explicitly converting to int.
349+
# strings; even the occasional `null`). For old rooms, we interpret these as if
350+
# they were integers. Do this here for the `@room` power level threshold.
351+
# Note that this is done automatically for the sender's power level by
352+
# _get_power_levels_and_sender_level in its call to get_user_power_level
353+
# (even for room V10.)
347354
notification_levels = power_levels.get("notifications", {})
348355
if not event.room_version.msc3667_int_only_power_levels:
349-
for user_id, level in notification_levels.items():
350-
notification_levels[user_id] = int(level)
356+
keys = list(notification_levels.keys())
357+
for key in keys:
358+
level = notification_levels.get(key, SENTINEL)
359+
if level is not SENTINEL and type(level) is not int:
360+
try:
361+
notification_levels[key] = int(level)
362+
except (TypeError, ValueError):
363+
del notification_levels[key]
351364

352365
# Pull out any user and room mentions.
353366
mentions = event.content.get(EventContentFields.MSC3952_MENTIONS)

tests/push/test_bulk_push_rule_evaluator.py

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from typing import Any
1616
from unittest.mock import patch
1717

18+
from parameterized import parameterized
19+
1820
from twisted.test.proto_helpers import MemoryReactor
1921

2022
from synapse.api.constants import EventContentFields
@@ -48,35 +50,84 @@ def prepare(
4850
self.requester = create_requester(self.alice)
4951

5052
self.room_id = self.helper.create_room_as(
51-
self.alice, room_version=RoomVersions.V9.identifier, tok=self.token
53+
# This is deliberately set to V9, because we want to test the logic which
54+
# handles stringy power levels. Stringy power levels were outlawed in V10.
55+
self.alice,
56+
room_version=RoomVersions.V9.identifier,
57+
tok=self.token,
5258
)
5359

5460
self.event_creation_handler = self.hs.get_event_creation_handler()
5561

56-
def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
57-
"""We should convert floats and strings to integers before passing to Rust.
62+
@parameterized.expand(
63+
[
64+
# The historically-permitted bad values. Alice's notification should be
65+
# allowed if this threshold is at or below her power level (60)
66+
("100", False),
67+
("0", True),
68+
(12.34, True),
69+
(60.0, True),
70+
(67.89, False),
71+
# Values that int(...) would not successfully cast should be ignored.
72+
# The room notification level should then default to 50, per the spec, so
73+
# Alice's notification is allowed.
74+
(None, True),
75+
# We haven't seen `"room": []` or `"room": {}` in the wild (yet), but
76+
# let's check them for paranoia's sake.
77+
([], True),
78+
({}, True),
79+
]
80+
)
81+
def test_action_for_event_by_user_handles_noninteger_room_power_levels(
82+
self, bad_room_level: object, should_permit: bool
83+
) -> None:
84+
"""We should convert strings in `room` to integers before passing to Rust.
85+
86+
Test this as follows:
87+
- Create a room as Alice and invite two other users Bob and Charlie.
88+
- Set PLs so that Alice has PL 60 and `notifications.room` is set to a bad value.
89+
- Have Alice create a message notifying @room.
90+
- Evaluate notification actions for that message. This should not raise.
91+
- Look in the DB to see if that message triggered a highlight for Bob.
92+
93+
The test is parameterised with two arguments:
94+
- the bad power level value for "room", before JSON serisalistion
95+
- whether Bob should expect the message to be highlighted
5896
5997
Reproduces #14060.
6098
6199
A lack of validation: the gift that keeps on giving.
62100
"""
63-
64-
# Alter the power levels in that room to include stringy and floaty levels.
65-
# We need to suppress the validation logic or else it will reject these dodgy
66-
# values. (Presumably this validation was not always present.)
101+
# Join another user to the room, so that there is someone to see Alice's
102+
# @room notification.
103+
bob = self.register_user("bob", "pass")
104+
bob_token = self.login(bob, "pass")
105+
self.helper.join(self.room_id, bob, tok=bob_token)
106+
107+
# Alter the power levels in that room to include the bad @room notification
108+
# level. We need to suppress
109+
#
110+
# - canonicaljson validation, because canonicaljson forbids floats;
111+
# - the event jsonschema validation, because it will forbid bad values; and
112+
# - the auth rules checks, because they stop us from creating power levels
113+
# with `"room": null`. (We want to test this case, because we have seen it
114+
# in the wild.)
115+
#
116+
# We have seen stringy and null values for "room" in the wild, so presumably
117+
# some of this validation was missing in the past.
67118
with patch("synapse.events.validator.validate_canonicaljson"), patch(
68119
"synapse.events.validator.jsonschema.validate"
69-
):
70-
self.helper.send_state(
120+
), patch("synapse.handlers.event_auth.check_state_dependent_auth_rules"):
121+
pl_event_id = self.helper.send_state(
71122
self.room_id,
72123
"m.room.power_levels",
73124
{
74-
"users": {self.alice: "100"}, # stringy
75-
"notifications": {"room": 100.0}, # float
125+
"users": {self.alice: 60},
126+
"notifications": {"room": bad_room_level},
76127
},
77128
self.token,
78129
state_key="",
79-
)
130+
)["event_id"]
80131

81132
# Create a new message event, and try to evaluate it under the dodgy
82133
# power level event.
@@ -88,17 +139,33 @@ def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
88139
"room_id": self.room_id,
89140
"content": {
90141
"msgtype": "m.text",
91-
"body": "helo",
142+
"body": "helo @room",
92143
},
93144
"sender": self.alice,
94145
},
146+
prev_event_ids=[pl_event_id],
95147
)
96148
)
97149

98150
bulk_evaluator = BulkPushRuleEvaluator(self.hs)
99151
# should not raise
100152
self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))
101153

154+
# Did Bob see Alice's @room notification?
155+
highlighted_actions = self.get_success(
156+
self.hs.get_datastores().main.db_pool.simple_select_list(
157+
table="event_push_actions_staging",
158+
keyvalues={
159+
"event_id": event.event_id,
160+
"user_id": bob,
161+
"highlight": 1,
162+
},
163+
retcols=("*",),
164+
desc="get_event_push_actions_staging",
165+
)
166+
)
167+
self.assertEqual(len(highlighted_actions), int(should_permit))
168+
102169
@override_config({"push": {"enabled": False}})
103170
def test_action_for_event_by_user_disabled_by_config(self) -> None:
104171
"""Ensure that push rules are not calculated when disabled in the config"""

tests/test_event_auth.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
import unittest
16-
from typing import Collection, Dict, Iterable, List, Optional
16+
from typing import Any, Collection, Dict, Iterable, List, Optional
1717

1818
from parameterized import parameterized
1919

@@ -728,6 +728,36 @@ def test_room_v10_rejects_string_power_levels(self) -> None:
728728
pl_event.room_version, pl_event2, {("fake_type", "fake_key"): pl_event}
729729
)
730730

731+
def test_room_v10_rejects_other_non_integer_power_levels(self) -> None:
732+
"""We should reject PLs that are non-integer, non-string JSON values.
733+
734+
test_room_v10_rejects_string_power_levels above handles the string case.
735+
"""
736+
737+
def create_event(pl_event_content: Dict[str, Any]) -> EventBase:
738+
return make_event_from_dict(
739+
{
740+
"room_id": TEST_ROOM_ID,
741+
**_maybe_get_event_id_dict_for_room_version(RoomVersions.V10),
742+
"type": "m.room.power_levels",
743+
"sender": "@test:test.com",
744+
"state_key": "",
745+
"content": pl_event_content,
746+
"signatures": {"test.com": {"ed25519:0": "some9signature"}},
747+
},
748+
room_version=RoomVersions.V10,
749+
)
750+
751+
contents: Iterable[Dict[str, Any]] = [
752+
{"notifications": {"room": None}},
753+
{"users": {"@alice:wonderland": []}},
754+
{"users_default": {}},
755+
]
756+
for content in contents:
757+
event = create_event(content)
758+
with self.assertRaises(SynapseError):
759+
event_auth._check_power_levels(event.room_version, event, {})
760+
731761

732762
# helpers for making events
733763
TEST_DOMAIN = "example.com"

0 commit comments

Comments
 (0)