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

Commit

Permalink
Improve signature checking on some federation APIs (#6262)
Browse files Browse the repository at this point in the history
* commit '172f264ed':
  Improve signature checking on some federation APIs (#6262)
  • Loading branch information
anoadragon453 committed Mar 16, 2020
2 parents 6d1e88a + 172f264 commit 9528bab
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/6262.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve signature checking on some federation APIs.
7 changes: 2 additions & 5 deletions synapse/federation/federation_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ def sender_err(e, pdu_to_check):
pdu_to_check.sender_domain,
e.getErrorMessage(),
)
# XX not really sure if these are the right codes, but they are what
# we've done for ages
raise SynapseError(400, errmsg, Codes.UNAUTHORIZED)
raise SynapseError(403, errmsg, Codes.FORBIDDEN)

for p, d in zip(pdus_to_check_sender, more_deferreds):
d.addErrback(sender_err, p)
Expand Down Expand Up @@ -314,8 +312,7 @@ def event_err(e, pdu_to_check):
"event id %s: unable to verify signature for event id domain: %s"
% (pdu_to_check.pdu.event_id, e.getErrorMessage())
)
# XX as above: not really sure if these are the right codes
raise SynapseError(400, errmsg, Codes.UNAUTHORIZED)
raise SynapseError(403, errmsg, Codes.FORBIDDEN)

for p, d in zip(pdus_to_check_event_id, more_deferreds):
d.addErrback(event_err, p)
Expand Down
7 changes: 7 additions & 0 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ def on_invite_request(self, origin, content, room_version):
pdu = event_from_pdu_json(content, format_ver)
origin_host, _ = parse_server_name(origin)
yield self.check_server_matches_acl(origin_host, pdu.room_id)
pdu = yield self._check_sigs_and_hash(room_version, pdu)
ret_pdu = yield self.handler.on_invite_request(origin, pdu)
time_now = self._clock.time_msec()
return {"event": ret_pdu.get_pdu_json(time_now)}
Expand All @@ -386,6 +387,9 @@ def on_send_join_request(self, origin, content, room_id):
yield self.check_server_matches_acl(origin_host, pdu.room_id)

logger.debug("on_send_join_request: pdu sigs: %s", pdu.signatures)

pdu = yield self._check_sigs_and_hash(room_version, pdu)

res_pdus = yield self.handler.on_send_join_request(origin, pdu)
time_now = self._clock.time_msec()
return (
Expand Down Expand Up @@ -421,6 +425,9 @@ def on_send_leave_request(self, origin, content, room_id):
yield self.check_server_matches_acl(origin_host, pdu.room_id)

logger.debug("on_send_leave_request: pdu sigs: %s", pdu.signatures)

pdu = yield self._check_sigs_and_hash(room_version, pdu)

yield self.handler.on_send_leave_request(origin, pdu)
return 200, {}

Expand Down
20 changes: 18 additions & 2 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,6 @@ def on_make_join_request(self, origin, room_id, user_id):
Returns:
Deferred[FrozenEvent]
"""

if get_domain_from_id(user_id) != origin:
logger.info(
"Got /make_join request for user %r from different origin %s, ignoring",
Expand Down Expand Up @@ -1280,11 +1279,20 @@ def on_send_join_request(self, origin, pdu):
event = pdu

logger.debug(
"on_send_join_request: Got event: %s, signatures: %s",
"on_send_join_request from %s: Got event: %s, signatures: %s",
origin,
event.event_id,
event.signatures,
)

if get_domain_from_id(event.sender) != origin:
logger.info(
"Got /send_join request for user %r from different origin %s",
event.sender,
origin,
)
raise SynapseError(403, "User not from origin", Codes.FORBIDDEN)

event.internal_metadata.outlier = False
# Send this event on behalf of the origin server.
#
Expand Down Expand Up @@ -1510,6 +1518,14 @@ def on_send_leave_request(self, origin, pdu):
event.signatures,
)

if get_domain_from_id(event.sender) != origin:
logger.info(
"Got /send_leave request for user %r from different origin %s",
event.sender,
origin,
)
raise SynapseError(403, "User not from origin", Codes.FORBIDDEN)

event.internal_metadata.outlier = False

context = yield self._handle_new_event(origin, event)
Expand Down

0 comments on commit 9528bab

Please sign in to comment.