Skip to content

fix MSC3912-relation-based-redaction for room versions > 10#19782

Open
jason-famedly wants to merge 7 commits into
element-hq:developfrom
famedly:jason/fix-msc3912-4v12
Open

fix MSC3912-relation-based-redaction for room versions > 10#19782
jason-famedly wants to merge 7 commits into
element-hq:developfrom
famedly:jason/fix-msc3912-4v12

Conversation

@jason-famedly

@jason-famedly jason-famedly commented May 14, 2026

Copy link
Copy Markdown
Contributor

Room version 11 moved the redacts key for redaction events from top-level to inside the contents object of an Event. Incorporate the change needed to maintain this MSC remains operational.

This fixes some MSC3912 behavior when used with room versions > 10.

Needed to pass room version 11 & 12 unit tests. 3 fail without this

Test results before
tests.rest.client.test_redactions
  RedactionsTestCase
    test_redact_all_relations ...                                        [FAIL]
    test_redact_create_event ...                                           [OK]
    test_redact_event_as_moderator ...                                     [OK]
    test_redact_event_as_moderator_ratelimit ...                           [OK]
    test_redact_event_as_normal ...                                        [OK]
    test_redact_nonexistent_event ...                                      [OK]
    test_redact_relations_no_perms ...                                   [FAIL]
    test_redact_relations_txn_id_reuse ...                                 [OK]
    test_redact_relations_with_types ...                                 [FAIL]
    test_redaction_content_0 ...                                           [OK]
    test_redaction_content_1 ...                                           [OK]
    test_redaction_content_2 ...                                           [OK]

===============================================================================
[FAIL]
Traceback (most recent call last):
  File "/home/jason/dev/famedly/synapse-upstreaming/tests/rest/client/test_redactions.py", line 414, in test_redact_all_relations
    self.assertIn("redacted_because", event_dict, event_dict)
  File "/home/jason/.cache/pypoetry/virtualenvs/matrix-synapse-vUbQZmJ1-py3.13/lib/python3.13/site-packages/twisted/trial/_synctest.py", line 509, in assertIn
    raise self.failureException(msg or f"{containee!r} not in {container!r}")
twisted.trial.unittest.FailTest: {'age': 500, 'content': {'body': ' * hello world', 'm.new_content': {'body': 'hello world', 'msgtype': 'm.text'}, 'm.relates_to': {'event_id': '$e4aURQ2oCjklgu0CFv6rq7VXBKTizeXDvw3sq69oLbI', 'rel_type': 'm.replace'}, 'msgtype': 'm.text'}, 'event_id': '$DYlYk9M5ayjHEErwMBgm-x0kgLA9-MuJYXE7lhn31Cg', 'origin_server_ts': 1101, 'room_id': '!qoN5VJOacdt4kvnXFBjqdo1isnshVBVA7-aAYRL79fs', 'sender': '@user1:test', 'type': 'm.room.message', 'unsigned': {'age': 500, 'membership': 'join', 'transaction_id': 'm1778701690.8760948'}, 'user_id': '@user1:test'}

tests.rest.client.test_redactions.RedactionsTestCase.test_redact_all_relations
===============================================================================
[FAIL]
Traceback (most recent call last):
  File "/home/jason/dev/famedly/synapse-upstreaming/tests/rest/client/test_redactions.py", line 503, in test_redact_relations_no_perms
    self.assertIn("redacted_because", event_dict, event_dict)
  File "/home/jason/.cache/pypoetry/virtualenvs/matrix-synapse-vUbQZmJ1-py3.13/lib/python3.13/site-packages/twisted/trial/_synctest.py", line 509, in assertIn
    raise self.failureException(msg or f"{containee!r} not in {container!r}")
twisted.trial.unittest.FailTest: {'age': 300, 'content': {'body': 'message 2', 'm.relates_to': {'event_id': '$9cTuhYe4gRPI_WdH4ISZUAoo_3MfSvDkjy4zVtfkLzs', 'rel_type': 'm.thread'}, 'msgtype': 'm.text'}, 'event_id': '$Eg0Rlk1_KVNVZPuhutegwaE1NkRLmuuQJ4Zdm9_ywRE', 'origin_server_ts': 1201, 'room_id': '!qoN5VJOacdt4kvnXFBjqdo1isnshVBVA7-aAYRL79fs', 'sender': '@otheruser:test', 'type': 'm.room.message', 'unsigned': {'age': 300, 'membership': 'join', 'transaction_id': 'm1778701693.5619848'}, 'user_id': '@otheruser:test'}

tests.rest.client.test_redactions.RedactionsTestCase.test_redact_relations_no_perms
===============================================================================
[FAIL]
Traceback (most recent call last):
  File "/home/jason/dev/famedly/synapse-upstreaming/tests/rest/client/test_redactions.py", line 316, in test_redact_relations_with_types
    self.assertIn("redacted_because", event_dict, event_dict)
  File "/home/jason/.cache/pypoetry/virtualenvs/matrix-synapse-vUbQZmJ1-py3.13/lib/python3.13/site-packages/twisted/trial/_synctest.py", line 509, in assertIn
    raise self.failureException(msg or f"{containee!r} not in {container!r}")
twisted.trial.unittest.FailTest: {'age': 500, 'content': {'body': ' * hello world', 'm.new_content': {'body': 'hello world', 'msgtype': 'm.text'}, 'm.relates_to': {'event_id': '$e4aURQ2oCjklgu0CFv6rq7VXBKTizeXDvw3sq69oLbI', 'rel_type': 'm.replace'}, 'msgtype': 'm.text'}, 'event_id': '$DYlYk9M5ayjHEErwMBgm-x0kgLA9-MuJYXE7lhn31Cg', 'origin_server_ts': 1101, 'room_id': '!qoN5VJOacdt4kvnXFBjqdo1isnshVBVA7-aAYRL79fs', 'sender': '@user1:test', 'type': 'm.room.message', 'unsigned': {'age': 500, 'membership': 'join', 'transaction_id': 'm1778701694.2228405'}, 'user_id': '@user1:test'}

tests.rest.client.test_redactions.RedactionsTestCase.test_redact_relations_with_types

A bit more context:
By using the raw initial EventBase from the original redaction request, this included the redacts key in contents for the original targeted event that needed redaction. This was not being changed or overwritten by the new redaction event that was being created to handle a related event, instead a new top-level redacts key was produced pointing at the correct targeted event ID. After construction of the EventBase for this new event(in create_and_send_nonmember_event()) the new EventBases redact property first reads the key in the content object and returns, which ignores the top-level redacts. So this mechanism was trying to redact the wrong(and already redacted!) original event per the request.

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)

@jason-famedly jason-famedly marked this pull request as ready for review May 14, 2026 16:48
@jason-famedly jason-famedly requested a review from a team as a code owner May 14, 2026 16:48

@anoadragon453 anoadragon453 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long delay on review. We're slowly getting through the backlog ⌛

Comment thread synapse/handlers/relations.py Outdated
Comment thread synapse/handlers/relations.py

@anoadragon453 anoadragon453 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple small things. Otherwise this looks nearly good to go!

Comment thread synapse/handlers/relations.py Outdated
Comment thread changelog.d/19782.misc Outdated

@anoadragon453 anoadragon453 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should've mentioned this earlier, but could you write a quick unit test or two to double-check that this does what we hope it does?

@jason-famedly

jason-famedly commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

I should've mentioned this earlier, but could you write a quick unit test or two to double-check that this does what we hope it does?

As part of the bring-up for defaulting to room version 12 work we are part of, this has been locally tested. Perhaps a parameterized or temporary commit that changes the room version for this whole block of tests? tests.rest.client.test_redactions.RedactionsTestCase was the ones that it fails on (see main description above for the spoiler with that data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants