32
32
logger = logging .getLogger (__name__ )
33
33
34
34
35
+ class InvalidEventSignatureError (RuntimeError ):
36
+ """Raised when the signature on an event is invalid.
37
+
38
+ The stringification of this exception is just the error message without reference
39
+ to the event id. The event id is available as a property.
40
+ """
41
+
42
+ def __init__ (self , message : str , event_id : str ):
43
+ super ().__init__ (message )
44
+ self .event_id = event_id
45
+
46
+
35
47
class FederationBase :
36
48
def __init__ (self , hs : "HomeServer" ):
37
49
self .hs = hs
@@ -59,20 +71,13 @@ async def _check_sigs_and_hash(
59
71
Returns:
60
72
* the original event if the checks pass
61
73
* a redacted version of the event (if the signature
62
- matched but the hash did not)
74
+ matched but the hash did not). In this case a warning will be logged.
63
75
64
76
Raises:
65
- SynapseError if the signature check failed.
77
+ InvalidEventSignatureError if the signature check failed. Nothing
78
+ will be logged in this case.
66
79
"""
67
- try :
68
- await _check_sigs_on_pdu (self .keyring , room_version , pdu )
69
- except SynapseError as e :
70
- logger .warning (
71
- "Signature check failed for %s: %s" ,
72
- pdu .event_id ,
73
- e ,
74
- )
75
- raise
80
+ await _check_sigs_on_pdu (self .keyring , room_version , pdu )
76
81
77
82
if not check_event_content_hash (pdu ):
78
83
# let's try to distinguish between failures because the event was
@@ -87,7 +92,7 @@ async def _check_sigs_and_hash(
87
92
if set (redacted_event .keys ()) == set (pdu .keys ()) and set (
88
93
redacted_event .content .keys ()
89
94
) == set (pdu .content .keys ()):
90
- logger .info (
95
+ logger .debug (
91
96
"Event %s seems to have been redacted; using our redacted copy" ,
92
97
pdu .event_id ,
93
98
)
@@ -116,12 +121,13 @@ async def _check_sigs_on_pdu(
116
121
) -> None :
117
122
"""Check that the given events are correctly signed
118
123
119
- Raise a SynapseError if the event wasn't correctly signed.
120
-
121
124
Args:
122
125
keyring: keyring object to do the checks
123
126
room_version: the room version of the PDUs
124
127
pdus: the events to be checked
128
+
129
+ Raises:
130
+ InvalidEventSignatureError if the event wasn't correctly signed.
125
131
"""
126
132
127
133
# we want to check that the event is signed by:
@@ -147,44 +153,38 @@ async def _check_sigs_on_pdu(
147
153
148
154
# First we check that the sender event is signed by the sender's domain
149
155
# (except if its a 3pid invite, in which case it may be sent by any server)
156
+ sender_domain = get_domain_from_id (pdu .sender )
150
157
if not _is_invite_via_3pid (pdu ):
151
158
try :
152
159
await keyring .verify_event_for_server (
153
- get_domain_from_id ( pdu . sender ) ,
160
+ sender_domain ,
154
161
pdu ,
155
162
pdu .origin_server_ts if room_version .enforce_key_validity else 0 ,
156
163
)
157
164
except Exception as e :
158
- errmsg = "event id %s: unable to verify signature for sender %s: %s" % (
165
+ raise InvalidEventSignatureError (
166
+ f"unable to verify signature for sender domain { sender_domain } : { e } " ,
159
167
pdu .event_id ,
160
- get_domain_from_id (pdu .sender ),
161
- e ,
162
- )
163
- raise SynapseError (403 , errmsg , Codes .FORBIDDEN )
168
+ ) from None
164
169
165
170
# now let's look for events where the sender's domain is different to the
166
171
# event id's domain (normally only the case for joins/leaves), and add additional
167
172
# checks. Only do this if the room version has a concept of event ID domain
168
173
# (ie, the room version uses old-style non-hash event IDs).
169
- if room_version .event_format == EventFormatVersions .V1 and get_domain_from_id (
170
- pdu .event_id
171
- ) != get_domain_from_id (pdu .sender ):
172
- try :
173
- await keyring .verify_event_for_server (
174
- get_domain_from_id (pdu .event_id ),
175
- pdu ,
176
- pdu .origin_server_ts if room_version .enforce_key_validity else 0 ,
177
- )
178
- except Exception as e :
179
- errmsg = (
180
- "event id %s: unable to verify signature for event id domain %s: %s"
181
- % (
182
- pdu .event_id ,
183
- get_domain_from_id (pdu .event_id ),
184
- e ,
174
+ if room_version .event_format == EventFormatVersions .V1 :
175
+ event_domain = get_domain_from_id (pdu .event_id )
176
+ if event_domain != sender_domain :
177
+ try :
178
+ await keyring .verify_event_for_server (
179
+ event_domain ,
180
+ pdu ,
181
+ pdu .origin_server_ts if room_version .enforce_key_validity else 0 ,
185
182
)
186
- )
187
- raise SynapseError (403 , errmsg , Codes .FORBIDDEN )
183
+ except Exception as e :
184
+ raise InvalidEventSignatureError (
185
+ f"unable to verify signature for event domain { event_domain } : { e } " ,
186
+ pdu .event_id ,
187
+ ) from None
188
188
189
189
# If this is a join event for a restricted room it may have been authorised
190
190
# via a different server from the sending server. Check those signatures.
@@ -204,15 +204,10 @@ async def _check_sigs_on_pdu(
204
204
pdu .origin_server_ts if room_version .enforce_key_validity else 0 ,
205
205
)
206
206
except Exception as e :
207
- errmsg = (
208
- "event id %s: unable to verify signature for authorising server %s: %s"
209
- % (
210
- pdu .event_id ,
211
- authorising_server ,
212
- e ,
213
- )
214
- )
215
- raise SynapseError (403 , errmsg , Codes .FORBIDDEN )
207
+ raise InvalidEventSignatureError (
208
+ f"unable to verify signature for authorising serve { authorising_server } : { e } " ,
209
+ pdu .event_id ,
210
+ ) from None
216
211
217
212
218
213
def _is_invite_via_3pid (event : EventBase ) -> bool :
0 commit comments