Skip to content

Commit

Permalink
Handle null invite and knock room state
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston authored and sandhose committed Dec 3, 2024
1 parent 4daa533 commit d82e1ed
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions synapse/federation/transport/server/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ async def on_PUT(
event = content["event"]
invite_room_state = content.get("invite_room_state", [])

if not isinstance(invite_room_state, list):
invite_room_state = []

# Synapse expects invite_room_state to be in unsigned, as it is in v1
# API

Expand Down
3 changes: 3 additions & 0 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,9 @@ async def do_knock(
if stripped_room_state is None:
raise KeyError("Missing 'knock_room_state' field in send_knock response")

if not isinstance(stripped_room_state, list):
raise TypeError("'knock_room_state' has wrong type")

event.unsigned["knock_room_state"] = stripped_room_state

context = EventContext.for_outlier(self._storage_controllers)
Expand Down
16 changes: 11 additions & 5 deletions synapse/handlers/sliding_sync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,13 +815,19 @@ async def get_room_sync_data(

stripped_state = []
if invite_or_knock_event.membership == Membership.INVITE:
stripped_state.extend(
invite_or_knock_event.unsigned.get("invite_room_state", [])
invite_state = invite_or_knock_event.unsigned.get(
"invite_room_state", []
)
if not isinstance(invite_state, list):
invite_state = []

stripped_state.extend(invite_state)
elif invite_or_knock_event.membership == Membership.KNOCK:
stripped_state.extend(
invite_or_knock_event.unsigned.get("knock_room_state", [])
)
knock_state = invite_or_knock_event.unsigned.get("knock_room_state", [])
if not isinstance(knock_state, list):
knock_state = []

stripped_state.extend(knock_state)

stripped_state.append(strip_event(invite_or_knock_event))

Expand Down
8 changes: 6 additions & 2 deletions synapse/push/push_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ async def get_context_for_event(

room_state = []
if ev.content.get("membership") == Membership.INVITE:
room_state = ev.unsigned.get("invite_room_state", [])
invite_room_state = ev.unsigned.get("invite_room_state", [])
if isinstance(invite_room_state, list):
room_state = invite_room_state
elif ev.content.get("membership") == Membership.KNOCK:
room_state = ev.unsigned.get("knock_room_state", [])
knock_room_state = ev.unsigned.get("knock_room_state", [])
if isinstance(knock_room_state, list):
room_state = knock_room_state

# Ideally we'd reuse the logic in `calculate_room_name`, but that gets
# complicated to handle partial events vs pulling events from the DB.
Expand Down
12 changes: 10 additions & 2 deletions synapse/rest/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,12 @@ async def encode_invited(
)
unsigned = dict(invite.get("unsigned", {}))
invite["unsigned"] = unsigned
invited_state = list(unsigned.pop("invite_room_state", []))

invited_state = unsigned.pop("invite_room_state", [])
if not isinstance(invited_state, list):
invited_state = []

invited_state = list(invited_state)
invited_state.append(invite)
invited[room.room_id] = {"invite_state": {"events": invited_state}}

Expand Down Expand Up @@ -476,7 +481,10 @@ async def encode_knocked(
# Extract the stripped room state from the unsigned dict
# This is for clients to get a little bit of information about
# the room they've knocked on, without revealing any sensitive information
knocked_state = list(unsigned.pop("knock_room_state", []))
knocked_state = unsigned.pop("knock_room_state", [])
if not isinstance(knocked_state, list):
knocked_state = []
knocked_state = list(knocked_state)

# Append the actual knock membership event itself as well. This provides
# the client with:
Expand Down

0 comments on commit d82e1ed

Please sign in to comment.