Repository: matrix-org/synapse

Issue:
Cannot send new events into rooms with floating-point (and stringy?) power levels
Internal users: https://sentry.tools.element.io/organizations/element/issues/34032/?project=2&query=is%3Aunresolved&sort=freq&statsPeriod=14d

Sentry's report is terse here: it does not include local vars from the call stack.

```
Failed handle request via "RoomSendEventRestServlet": "<XForwardedForRequest at 0x7f8348f30f40 method='PUT' uri='/_matrix/client/r0/rooms/<room ID>/send/m.room.encrypted/<txn ID>' clientproto='HTTP/1.1' site='12101'>"
```

```
TypeError: argument 'notification_power_levels': 'float' object cannot be interpreted as an integer
  File "/home/synapse/src/synapse/http/server.py", line 306, in _async_render_wrapper
  File "/home/synapse/src/synapse/http/server.py", line 512, in _async_render
  File "/home/synapse/src/synapse/rest/client/room.py", line 353, in on_POST
  File "/home/synapse/src/synapse/handlers/message.py", line 1020, in create_and_send_nonmember_event
  File "/home/synapse/src/synapse/util/metrics.py", line 113, in measured_func
  File "/home/synapse/src/synapse/handlers/message.py", line 1382, in handle_new_client_event
  File "/home/synapse/env-pyston-poetry/lib/python3.8-pyston2.3/site-packages/twisted/internet/defer.py", line 1660, in _inlineCallbacks
  File "/home/synapse/src/synapse/handlers/message.py", line 1436, in _persist_events
  File "/home/synapse/src/synapse/util/metrics.py", line 113, in measured_func
  File "/home/synapse/src/synapse/push/bulk_push_rule_evaluator.py", line 292, in action_for_event_by_user
```

The deepest call is

https://github.com/matrix-org/synapse/blob/b4ec4f5e71a87d5bdc840a4220dfd9a34c54c847/synapse/push/bulk_push_rule_evaluator.py#L292-L299

which invokes the new Rust mechanism for push stuff:

https://github.com/matrix-org/synapse/blob/ebd9e2dac6495a1857617d1a76c9259a988f8bb4/rust/src/push/evaluator.rs#L64-L90

I am guessing that

- the error message `'float' object cannot be interpreted as an integer` comes from Py03's translation layer. (#13838)
- the power levels event used in that room has floating power levels (and therefore is bogus, but not prohibited until room v10)
- people are unable to send messages in that room from Synapse  if they believe that power levels event to be in the current state

Will investigate a little more.






Discussion:
```
matrix=> select json::json->'content'->'notifications' from current_state_events AS cse join event_json using(event_id) where cse.room_id = '<ROOM ID>' and cse.type = 'm.room.power_levels';
    ?column?    
════════════════
 {"room":100.0}
(1 row)

```

Right. Well that's not good.
The event in question was created at `2020-12-29 08:52:34+00` according to its `origin_server_ts` so MSC3667 doesn't apply.

We now check that the power level values are integers in sufficiently new rooms.

https://github.com/matrix-org/synapse/blob/c2fe48a6ffb99f553f3eaecb8f15bcbedb58add0/synapse/event_auth.py#L783-L807
No idea how we even got floats in the power levels content in the first place, given that https://github.com/matrix-org/synapse/blob/06df5d4250f54d5a95b0c90bfc9352ec6f02c520/synapse/events/utils.py#L662-L664 exists. But maybe it didn't at the time.

Resolving patch:
diff --git a/synapse/push/bulk_push_rule_evaluator.py b/synapse/push/bulk_push_rule_evaluator.py
--- a/synapse/push/bulk_push_rule_evaluator.py
+++ b/synapse/push/bulk_push_rule_evaluator.py
@@ -289,11 +289,18 @@ async def action_for_event_by_user(
             if relation.rel_type == RelationTypes.THREAD:
                 thread_id = relation.parent_id
 
+        # It's possible that old room versions have non-integer power levels (floats or
+        # strings). Workaround this by explicitly converting to int.
+        notification_levels = power_levels.get("notifications", {})
+        if not event.room_version.msc3667_int_only_power_levels:
+            for user_id, level in notification_levels.items():
+                notification_levels[user_id] = int(level)
+
         evaluator = PushRuleEvaluator(
             _flatten_dict(event),
             room_member_count,
             sender_power_level,
-            power_levels.get("notifications", {}),
+            notification_levels,
             relations,
             self._relations_match_enabled,
         )


Test patch:
diff --git a/tests/push/test_bulk_push_rule_evaluator.py b/tests/push/test_bulk_push_rule_evaluator.py
new file mode 100644
--- /dev/null
+++ b/tests/push/test_bulk_push_rule_evaluator.py
@@ -0,0 +1,74 @@
+from unittest.mock import patch
+
+from synapse.api.room_versions import RoomVersions
+from synapse.push.bulk_push_rule_evaluator import BulkPushRuleEvaluator
+from synapse.rest import admin
+from synapse.rest.client import login, register, room
+from synapse.types import create_requester
+
+from tests import unittest
+
+
+class TestBulkPushRuleEvaluator(unittest.HomeserverTestCase):
+
+    servlets = [
+        admin.register_servlets_for_client_rest_resource,
+        room.register_servlets,
+        login.register_servlets,
+        register.register_servlets,
+    ]
+
+    def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
+        """We should convert floats and strings to integers before passing to Rust.
+
+        Reproduces #14060.
+
+        A lack of validation: the gift that keeps on giving.
+        """
+        # Create a new user and room.
+        alice = self.register_user("alice", "pass")
+        token = self.login(alice, "pass")
+
+        room_id = self.helper.create_room_as(
+            alice, room_version=RoomVersions.V9.identifier, tok=token
+        )
+
+        # Alter the power levels in that room to include stringy and floaty levels.
+        # We need to suppress the validation logic or else it will reject these dodgy
+        # values. (Presumably this validation was not always present.)
+        event_creation_handler = self.hs.get_event_creation_handler()
+        requester = create_requester(alice)
+        with patch("synapse.events.validator.validate_canonicaljson"), patch(
+            "synapse.events.validator.jsonschema.validate"
+        ):
+            self.helper.send_state(
+                room_id,
+                "m.room.power_levels",
+                {
+                    "users": {alice: "100"},  # stringy
+                    "notifications": {"room": 100.0},  # float
+                },
+                token,
+                state_key="",
+            )
+
+        # Create a new message event, and try to evaluate it under the dodgy
+        # power level event.
+        event, context = self.get_success(
+            event_creation_handler.create_event(
+                requester,
+                {
+                    "type": "m.room.message",
+                    "room_id": room_id,
+                    "content": {
+                        "msgtype": "m.text",
+                        "body": "helo",
+                    },
+                    "sender": alice,
+                },
+            )
+        )
+
+        bulk_evaluator = BulkPushRuleEvaluator(self.hs)
+        # should not raise
+        self.get_success(bulk_evaluator.action_for_event_by_user(event, context))