Skip to content

Fix overly restrictive typeguards in get_rooms_that_allow_join#18066

Open
blackmad wants to merge 6 commits intoelement-hq:developfrom
blackmad:fix-hierarchy-bug
Open

Fix overly restrictive typeguards in get_rooms_that_allow_join#18066
blackmad wants to merge 6 commits intoelement-hq:developfrom
blackmad:fix-hierarchy-bug

Conversation

@blackmad
Copy link
Copy Markdown

@blackmad blackmad commented Jan 6, 2025

This was causing a bug that was only occuring when the server had modules loaded and there were unjoined space-visible rooms in a hierarchy.

This fix is a result of the following investigation:

Symptom: new 'space-visible' rooms are not showing up in /hierarchy Reason: because get_rooms_that_allow_join is checking that the allow key is a list, and it's coming back as a tuple synapse/handlers/event_auth.py#L329

Reason: when the room is created, the join_rules event creation logic calls check_event_allowed - synapse/handlers/message.py#L1289 - which calls event.freeze() (in synapse/module_api/callbacks/third_party_event_rules_callbacks.py#L294 ) which seems to be changing lists to tuples - so that when the event is being pulled from cache, the allow list is a tuple. But when I restart the server, it gets pulled from the DB and deserialized as a list.

Part of #18117

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)

This was causing a bug that was only occuring when the server had modules loaded and there were unjoined space-visible rooms in a hierarchy.

This fix is a result of the following investigation:
Symptom: new 'space-visible' rooms are not showing up in /hierarchy
Reason: because get_rooms_that_allow_join is checking that the allow key is a list, and it's coming back as a tuple https://github.com/element-hq/synapse/blob/develop/synapse/handlers/event_auth.py#L329

-> Reason: when the room is created, the join_rules event calls check_event_allowed - https://github.com/element-hq/synapse/blob/develop/synapse/handlers/message.py#L1289 - which calls event.freeze() which seems to be changing lists to tuples - https://github.com/element-hq/synapse/blob/develop/synapse/module_api/callbacks/third_party_event_rules_callbacks.py#L294
--> so that when the event is being pulled from cache, the allow list is a tuple. But when I restart the server, it gets pulled from the DB and deserialized as a list.
@blackmad blackmad requested a review from a team as a code owner January 6, 2025 10:27
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jan 6, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

# If allowed is of the wrong form, then only allow invited users.
allow_list = join_rules_event.content.get("allow", [])
if not isinstance(allow_list, list):
if not isinstance(allow_list, (list, tuple)):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great find @blackmad! I've created #18117 to track the rest of the instances of this happening in the Synapse codebase.

Comment thread synapse/handlers/event_auth.py Outdated
# If allowed is of the wrong form, then only allow invited users.
allow_list = join_rules_event.content.get("allow", [])
if not isinstance(allow_list, list):
if not isinstance(allow_list, (list, tuple)):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@blackmad I've just had a thought on how we can fix all of the cases mentioned in #18117 without having to modify any of the downstream code, see #18103 (comment)

Going to leave this PR until we figure out if that is a viable solution to all of this.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants