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

Improve logging when signature checks fail #12925

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions synapse/federation/federation_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,13 @@ async def _check_sigs_and_hash(
Returns:
* the original event if the checks pass
* a redacted version of the event (if the signature
matched but the hash did not)
matched but the hash did not). In this case a warning will be logged.

Raises:
SynapseError if the signature check failed.
InvalidEventSignatureError if the signature check failed. Nothing
will be logged in this case.
"""
try:
await _check_sigs_on_pdu(self.keyring, room_version, pdu)
except InvalidEventSignatureError as e:
errmsg = f"event id {pdu.event_id}: {e}"
logger.warning("%s", errmsg)
raise SynapseError(403, errmsg, Codes.FORBIDDEN)
await _check_sigs_on_pdu(self.keyring, room_version, pdu)

if not check_event_content_hash(pdu):
# let's try to distinguish between failures because the event was
Expand Down
45 changes: 32 additions & 13 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
RoomVersions,
)
from synapse.events import EventBase, builder
from synapse.federation.federation_base import FederationBase, event_from_pdu_json
from synapse.federation.federation_base import (
FederationBase,
InvalidEventSignatureError,
event_from_pdu_json,
)
from synapse.federation.transport.client import SendJoinResponse
from synapse.http.types import QueryParams
from synapse.types import JsonDict, UserID, get_domain_from_id
Expand Down Expand Up @@ -319,7 +323,13 @@ async def get_pdu_from_destination_raw(
pdu = pdu_list[0]

# Check signatures are correct.
signed_pdu = await self._check_sigs_and_hash(room_version, pdu)
try:
signed_pdu = await self._check_sigs_and_hash(room_version, pdu)
except InvalidEventSignatureError as e:
errmsg = f"event id {pdu.event_id}: {e}"
logger.warning("%s", errmsg)
raise SynapseError(403, errmsg, Codes.FORBIDDEN)
Comment on lines +326 to +331
Copy link
Member Author

Choose a reason for hiding this comment

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

this (and the three other copies of the same change) should be a non-functional change; we're just pulling the functionality out of _check_sigs_and_hash


return signed_pdu

return None
Expand Down Expand Up @@ -552,20 +562,24 @@ async def _check_sigs_and_hash_and_fetch_one(

Returns:
The PDU (possibly redacted) if it has valid signatures and hashes.
None if no valid copy could be found.
"""

res = None
try:
res = await self._check_sigs_and_hash(room_version, pdu)
except SynapseError:
pass

if not res:
# Check local db.
res = await self.store.get_event(
pdu.event_id, allow_rejected=True, allow_none=True
return await self._check_sigs_and_hash(room_version, pdu)
except InvalidEventSignatureError as e:
logger.warning(
"Signature on retrieved event %s was invalid (%s). "
"Checking local store/orgin server",
pdu.event_id,
e,
)
Comment on lines +570 to 576
Copy link
Member Author

Choose a reason for hiding this comment

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

this was the real driver for the whole PR. I wanted a clearer error message when the sig check fails in this case.


# Check local db.
res = await self.store.get_event(
pdu.event_id, allow_rejected=True, allow_none=True
)

pdu_origin = get_domain_from_id(pdu.sender)
if not res and pdu_origin != origin:
try:
Expand Down Expand Up @@ -1040,9 +1054,14 @@ async def send_invite(
pdu = event_from_pdu_json(pdu_dict, room_version)

# Check signatures are correct.
pdu = await self._check_sigs_and_hash(room_version, pdu)
try:
pdu = await self._check_sigs_and_hash(room_version, pdu)
except InvalidEventSignatureError as e:
errmsg = f"event id {pdu.event_id}: {e}"
logger.warning("%s", errmsg)
raise SynapseError(403, errmsg, Codes.FORBIDDEN)

# FIXME: We should handle signature failures more gracefully.
# FIXME: We should handle signature failures more gracefully.

return pdu

Expand Down
25 changes: 20 additions & 5 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
from synapse.crypto.event_signing import compute_event_signature
from synapse.events import EventBase
from synapse.events.snapshot import EventContext
from synapse.federation.federation_base import FederationBase, event_from_pdu_json
from synapse.federation.federation_base import (
FederationBase,
InvalidEventSignatureError,
event_from_pdu_json,
)
from synapse.federation.persistence import TransactionActions
from synapse.federation.units import Edu, Transaction
from synapse.http.servlet import assert_params_in_dict
Expand Down Expand Up @@ -632,7 +636,12 @@ async def on_invite_request(
pdu = event_from_pdu_json(content, room_version)
origin_host, _ = parse_server_name(origin)
await self.check_server_matches_acl(origin_host, pdu.room_id)
pdu = await self._check_sigs_and_hash(room_version, pdu)
try:
pdu = await self._check_sigs_and_hash(room_version, pdu)
except InvalidEventSignatureError as e:
errmsg = f"event id {pdu.event_id}: {e}"
logger.warning("%s", errmsg)
raise SynapseError(403, errmsg, Codes.FORBIDDEN)
ret_pdu = await self.handler.on_invite_request(origin, pdu, room_version)
time_now = self._clock.time_msec()
return {"event": ret_pdu.get_pdu_json(time_now)}
Expand Down Expand Up @@ -865,7 +874,12 @@ async def _on_send_membership_event(
)
)

event = await self._check_sigs_and_hash(room_version, event)
try:
event = await self._check_sigs_and_hash(room_version, event)
except InvalidEventSignatureError as e:
errmsg = f"event id {event.event_id}: {e}"
logger.warning("%s", errmsg)
raise SynapseError(403, errmsg, Codes.FORBIDDEN)

return await self._federation_event_handler.on_send_membership_event(
origin, event
Expand Down Expand Up @@ -1017,8 +1031,9 @@ async def _handle_received_pdu(self, origin: str, pdu: EventBase) -> None:
# Check signature.
try:
pdu = await self._check_sigs_and_hash(room_version, pdu)
except SynapseError as e:
raise FederationError("ERROR", e.code, e.msg, affected=pdu.event_id)
except InvalidEventSignatureError as e:
logger.warning("event id %s: %s", pdu.event_id, e)
raise FederationError("ERROR", 403, str(e), affected=pdu.event_id)

if await self._spam_checker.should_drop_federated_event(pdu):
logger.warning(
Expand Down