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

Commit

Permalink
Merge pull request #3641 from matrix-org/rav/room_id_check
Browse files Browse the repository at this point in the history
Validation for events/rooms in fed requests
  • Loading branch information
richvdh authored Aug 2, 2018
2 parents 6284f57 + a013404 commit a937497
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.d/3641.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a potential event disclosure issue
1 change: 1 addition & 0 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ def on_query_auth_request(self, origin, content, room_id, event_id):
ret = yield self.handler.on_query_auth(
origin,
event_id,
room_id,
signed_auth,
content.get("rejects", []),
content.get("missing", []),
Expand Down
29 changes: 23 additions & 6 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,11 @@ def on_send_leave_request(self, origin, pdu):
def get_state_for_pdu(self, room_id, event_id):
"""Returns the state at the event. i.e. not including said event.
"""

event = yield self.store.get_event(
event_id, allow_none=False, check_room_id=room_id,
)

state_groups = yield self.store.get_state_groups(
room_id, [event_id]
)
Expand All @@ -1359,8 +1364,7 @@ def get_state_for_pdu(self, room_id, event_id):
(e.type, e.state_key): e for e in state
}

event = yield self.store.get_event(event_id)
if event and event.is_state():
if event.is_state():
# Get previous state
if "replaces_state" in event.unsigned:
prev_id = event.unsigned["replaces_state"]
Expand Down Expand Up @@ -1391,6 +1395,10 @@ def get_state_for_pdu(self, room_id, event_id):
def get_state_ids_for_pdu(self, room_id, event_id):
"""Returns the state at the event. i.e. not including said event.
"""
event = yield self.store.get_event(
event_id, allow_none=False, check_room_id=room_id,
)

state_groups = yield self.store.get_state_groups_ids(
room_id, [event_id]
)
Expand All @@ -1399,8 +1407,7 @@ def get_state_ids_for_pdu(self, room_id, event_id):
_, state = state_groups.items().pop()
results = state

event = yield self.store.get_event(event_id)
if event and event.is_state():
if event.is_state():
# Get previous state
if "replaces_state" in event.unsigned:
prev_id = event.unsigned["replaces_state"]
Expand Down Expand Up @@ -1706,8 +1713,19 @@ def _prep_event(self, origin, event, state=None, auth_events=None):
defer.returnValue(context)

@defer.inlineCallbacks
def on_query_auth(self, origin, event_id, remote_auth_chain, rejects,
def on_query_auth(self, origin, event_id, room_id, remote_auth_chain, rejects,
missing):
in_room = yield self.auth.check_host_in_room(
room_id,
origin
)
if not in_room:
raise AuthError(403, "Host not in room.")

event = yield self.store.get_event(
event_id, allow_none=False, check_room_id=room_id
)

# Just go through and process each event in `remote_auth_chain`. We
# don't want to fall into the trap of `missing` being wrong.
for e in remote_auth_chain:
Expand All @@ -1717,7 +1735,6 @@ def on_query_auth(self, origin, event_id, remote_auth_chain, rejects,
pass

# Now get the current auth_chain for the event.
event = yield self.store.get_event(event_id)
local_auth_chain = yield self.store.get_auth_chain(
[auth_id for auth_id, _ in event.auth_events],
include_given=True
Expand Down
1 change: 1 addition & 0 deletions synapse/storage/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ def _get_backfill_events(self, txn, room_id, event_list, limit):
table="events",
keyvalues={
"event_id": event_id,
"room_id": room_id,
},
retcol="depth",
allow_none=True,
Expand Down
20 changes: 14 additions & 6 deletions synapse/storage/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from twisted.internet import defer

from synapse.api.errors import SynapseError
from synapse.api.errors import NotFoundError
# these are only included to make the type annotations work
from synapse.events import EventBase # noqa: F401
from synapse.events import FrozenEvent
Expand Down Expand Up @@ -76,7 +76,7 @@ def get_received_ts(self, event_id):
@defer.inlineCallbacks
def get_event(self, event_id, check_redacted=True,
get_prev_content=False, allow_rejected=False,
allow_none=False):
allow_none=False, check_room_id=None):
"""Get an event from the database by event_id.
Args:
Expand All @@ -87,7 +87,9 @@ def get_event(self, event_id, check_redacted=True,
include the previous states content in the unsigned field.
allow_rejected (bool): If True return rejected events.
allow_none (bool): If True, return None if no event found, if
False throw an exception.
False throw a NotFoundError
check_room_id (str|None): if not None, check the room of the found event.
If there is a mismatch, behave as per allow_none.
Returns:
Deferred : A FrozenEvent.
Expand All @@ -99,10 +101,16 @@ def get_event(self, event_id, check_redacted=True,
allow_rejected=allow_rejected,
)

if not events and not allow_none:
raise SynapseError(404, "Could not find event %s" % (event_id,))
event = events[0] if events else None

defer.returnValue(events[0] if events else None)
if event is not None and check_room_id is not None:
if event.room_id != check_room_id:
event = None

if event is None and not allow_none:
raise NotFoundError("Could not find event %s" % (event_id,))

defer.returnValue(event)

@defer.inlineCallbacks
def get_events(self, event_ids, check_redacted=True,
Expand Down

0 comments on commit a937497

Please sign in to comment.