Codebase list matrix-synapse / 41bd959
Imported Upstream version 0.33.3.1 Richard van der Hoff 5 years ago
9 changed file(s) with 158 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
22 .gitignore
33 demo/etc
44 tox.ini
5 synctl
65 .git/*
76 .tox/*
0 Synapse 0.33.3.1 (2018-09-06)
1 =============================
2
3 SECURITY FIXES
4 --------------
5
6 - Fix an issue where event signatures were not always correctly validated ([\#3796](https://github.com/matrix-org/synapse/issues/3796))
7 - Fix an issue where server_acls could be circumvented for incoming events ([\#3796](https://github.com/matrix-org/synapse/issues/3796))
8
9 Internal Changes
10 ----------------
11
12 - Unignore synctl in .dockerignore to fix docker builds ([\#3802](https://github.com/matrix-org/synapse/issues/3802))
13
014 Synapse 0.33.3 (2018-08-22)
115 ===========================
216
1616 """ This is a reference implementation of a Matrix home server.
1717 """
1818
19 __version__ = "0.33.3"
19 __version__ = "0.33.3.1"
1212 # See the License for the specific language governing permissions and
1313 # limitations under the License.
1414 import logging
15 from collections import namedtuple
1516
1617 import six
1718
1819 from twisted.internet import defer
19
20 from synapse.api.constants import MAX_DEPTH
20 from twisted.internet.defer import DeferredList
21
22 from synapse.api.constants import MAX_DEPTH, EventTypes, Membership
2123 from synapse.api.errors import Codes, SynapseError
2224 from synapse.crypto.event_signing import check_event_content_hash
2325 from synapse.events import FrozenEvent
2426 from synapse.events.utils import prune_event
2527 from synapse.http.servlet import assert_params_in_dict
28 from synapse.types import get_domain_from_id
2629 from synapse.util import logcontext, unwrapFirstError
2730
2831 logger = logging.getLogger(__name__)
132135 * throws a SynapseError if the signature check failed.
133136 The deferreds run their callbacks in the sentinel logcontext.
134137 """
135
136 redacted_pdus = [
137 prune_event(pdu)
138 for pdu in pdus
139 ]
140
141 deferreds = self.keyring.verify_json_objects_for_server([
142 (p.origin, p.get_pdu_json())
143 for p in redacted_pdus
144 ])
138 deferreds = _check_sigs_on_pdus(self.keyring, pdus)
145139
146140 ctx = logcontext.LoggingContext.current_context()
147141
148 def callback(_, pdu, redacted):
142 def callback(_, pdu):
149143 with logcontext.PreserveLoggingContext(ctx):
150144 if not check_event_content_hash(pdu):
151145 logger.warn(
152146 "Event content has been tampered, redacting %s: %s",
153147 pdu.event_id, pdu.get_pdu_json()
154148 )
155 return redacted
149 return prune_event(pdu)
156150
157151 if self.spam_checker.check_event_for_spam(pdu):
158152 logger.warn(
159153 "Event contains spam, redacting %s: %s",
160154 pdu.event_id, pdu.get_pdu_json()
161155 )
162 return redacted
156 return prune_event(pdu)
163157
164158 return pdu
165159
172166 )
173167 return failure
174168
175 for deferred, pdu, redacted in zip(deferreds, pdus, redacted_pdus):
169 for deferred, pdu in zip(deferreds, pdus):
176170 deferred.addCallbacks(
177171 callback, errback,
178 callbackArgs=[pdu, redacted],
172 callbackArgs=[pdu],
179173 errbackArgs=[pdu],
180174 )
181175
182176 return deferreds
177
178
179 class PduToCheckSig(namedtuple("PduToCheckSig", [
180 "pdu", "redacted_pdu_json", "event_id_domain", "sender_domain", "deferreds",
181 ])):
182 pass
183
184
185 def _check_sigs_on_pdus(keyring, pdus):
186 """Check that the given events are correctly signed
187
188 Args:
189 keyring (synapse.crypto.Keyring): keyring object to do the checks
190 pdus (Collection[EventBase]): the events to be checked
191
192 Returns:
193 List[Deferred]: a Deferred for each event in pdus, which will either succeed if
194 the signatures are valid, or fail (with a SynapseError) if not.
195 """
196
197 # (currently this is written assuming the v1 room structure; we'll probably want a
198 # separate function for checking v2 rooms)
199
200 # we want to check that the event is signed by:
201 #
202 # (a) the server which created the event_id
203 #
204 # (b) the sender's server.
205 #
206 # - except in the case of invites created from a 3pid invite, which are exempt
207 # from this check, because the sender has to match that of the original 3pid
208 # invite, but the event may come from a different HS, for reasons that I don't
209 # entirely grok (why do the senders have to match? and if they do, why doesn't the
210 # joining server ask the inviting server to do the switcheroo with
211 # exchange_third_party_invite?).
212 #
213 # That's pretty awful, since redacting such an invite will render it invalid
214 # (because it will then look like a regular invite without a valid signature),
215 # and signatures are *supposed* to be valid whether or not an event has been
216 # redacted. But this isn't the worst of the ways that 3pid invites are broken.
217 #
218 # let's start by getting the domain for each pdu, and flattening the event back
219 # to JSON.
220 pdus_to_check = [
221 PduToCheckSig(
222 pdu=p,
223 redacted_pdu_json=prune_event(p).get_pdu_json(),
224 event_id_domain=get_domain_from_id(p.event_id),
225 sender_domain=get_domain_from_id(p.sender),
226 deferreds=[],
227 )
228 for p in pdus
229 ]
230
231 # first make sure that the event is signed by the event_id's domain
232 deferreds = keyring.verify_json_objects_for_server([
233 (p.event_id_domain, p.redacted_pdu_json)
234 for p in pdus_to_check
235 ])
236
237 for p, d in zip(pdus_to_check, deferreds):
238 p.deferreds.append(d)
239
240 # now let's look for events where the sender's domain is different to the
241 # event id's domain (normally only the case for joins/leaves), and add additional
242 # checks.
243 pdus_to_check_sender = [
244 p for p in pdus_to_check
245 if p.sender_domain != p.event_id_domain and not _is_invite_via_3pid(p.pdu)
246 ]
247
248 more_deferreds = keyring.verify_json_objects_for_server([
249 (p.sender_domain, p.redacted_pdu_json)
250 for p in pdus_to_check_sender
251 ])
252
253 for p, d in zip(pdus_to_check_sender, more_deferreds):
254 p.deferreds.append(d)
255
256 # replace lists of deferreds with single Deferreds
257 return [_flatten_deferred_list(p.deferreds) for p in pdus_to_check]
258
259
260 def _flatten_deferred_list(deferreds):
261 """Given a list of one or more deferreds, either return the single deferred, or
262 combine into a DeferredList.
263 """
264 if len(deferreds) > 1:
265 return DeferredList(deferreds, fireOnOneErrback=True, consumeErrors=True)
266 else:
267 assert len(deferreds) == 1
268 return deferreds[0]
269
270
271 def _is_invite_via_3pid(event):
272 return (
273 event.type == EventTypes.Member
274 and event.membership == Membership.INVITE
275 and "third_party_invite" in event.content
276 )
183277
184278
185279 def event_from_pdu_json(pdu_json, outlier=False):
9898
9999 @defer.inlineCallbacks
100100 @log_function
101 def on_incoming_transaction(self, transaction_data):
101 def on_incoming_transaction(self, origin, transaction_data):
102102 # keep this as early as possible to make the calculated origin ts as
103103 # accurate as possible.
104104 request_time = self._clock.time_msec()
107107
108108 if not transaction.transaction_id:
109109 raise Exception("Transaction missing transaction_id")
110 if not transaction.origin:
111 raise Exception("Transaction missing origin")
112110
113111 logger.debug("[%s] Got transaction", transaction.transaction_id)
114112
115113 # use a linearizer to ensure that we don't process the same transaction
116114 # multiple times in parallel.
117115 with (yield self._transaction_linearizer.queue(
118 (transaction.origin, transaction.transaction_id),
116 (origin, transaction.transaction_id),
119117 )):
120118 result = yield self._handle_incoming_transaction(
121 transaction, request_time,
119 origin, transaction, request_time,
122120 )
123121
124122 defer.returnValue(result)
125123
126124 @defer.inlineCallbacks
127 def _handle_incoming_transaction(self, transaction, request_time):
125 def _handle_incoming_transaction(self, origin, transaction, request_time):
128126 """ Process an incoming transaction and return the HTTP response
129127
130128 Args:
129 origin (unicode): the server making the request
131130 transaction (Transaction): incoming transaction
132131 request_time (int): timestamp that the HTTP request arrived at
133132
134133 Returns:
135134 Deferred[(int, object)]: http response code and body
136135 """
137 response = yield self.transaction_actions.have_responded(transaction)
136 response = yield self.transaction_actions.have_responded(origin, transaction)
138137
139138 if response:
140139 logger.debug(
148147
149148 received_pdus_counter.inc(len(transaction.pdus))
150149
151 origin_host, _ = parse_server_name(transaction.origin)
150 origin_host, _ = parse_server_name(origin)
152151
153152 pdus_by_room = {}
154153
189188 event_id = pdu.event_id
190189 try:
191190 yield self._handle_received_pdu(
192 transaction.origin, pdu
191 origin, pdu
193192 )
194193 pdu_results[event_id] = {}
195194 except FederationError as e:
211210 if hasattr(transaction, "edus"):
212211 for edu in (Edu(**x) for x in transaction.edus):
213212 yield self.received_edu(
214 transaction.origin,
213 origin,
215214 edu.edu_type,
216215 edu.content
217216 )
223222 logger.debug("Returning: %s", str(response))
224223
225224 yield self.transaction_actions.set_response(
225 origin,
226226 transaction,
227227 200, response
228228 )
3535 self.store = datastore
3636
3737 @log_function
38 def have_responded(self, transaction):
38 def have_responded(self, origin, transaction):
3939 """ Have we already responded to a transaction with the same id and
4040 origin?
4141
4949 "transaction_id")
5050
5151 return self.store.get_received_txn_response(
52 transaction.transaction_id, transaction.origin
52 transaction.transaction_id, origin
5353 )
5454
5555 @log_function
56 def set_response(self, transaction, code, response):
56 def set_response(self, origin, transaction, code, response):
5757 """ Persist how we responded to a transaction.
5858
5959 Returns:
6565
6666 return self.store.set_received_txn_response(
6767 transaction.transaction_id,
68 transaction.origin,
68 origin,
6969 code,
7070 response,
7171 )
352352
353353 try:
354354 code, response = yield self.handler.on_incoming_transaction(
355 transaction_data
355 origin, transaction_data,
356356 )
357357 except Exception:
358358 logger.exception("on_incoming_transaction failed")
3232 )
3333
3434
35 def _expect_edu(destination, edu_type, content, origin="test"):
35 def _expect_edu_transaction(edu_type, content, origin="test"):
3636 return {
3737 "origin": origin,
3838 "origin_server_ts": 1000000,
4141 }
4242
4343
44 def _make_edu_json(origin, edu_type, content):
45 return json.dumps(_expect_edu("test", edu_type, content, origin=origin)).encode(
44 def _make_edu_transaction_json(edu_type, content):
45 return json.dumps(_expect_edu_transaction(edu_type, content)).encode(
4646 'utf8'
4747 )
4848
189189 call(
190190 "farm",
191191 path="/_matrix/federation/v1/send/1000000/",
192 data=_expect_edu(
193 "farm",
192 data=_expect_edu_transaction(
194193 "m.typing",
195194 content={
196195 "room_id": self.room_id,
220219
221220 self.assertEquals(self.event_source.get_current_key(), 0)
222221
223 yield self.mock_federation_resource.trigger(
222 (code, response) = yield self.mock_federation_resource.trigger(
224223 "PUT",
225224 "/_matrix/federation/v1/send/1000000/",
226 _make_edu_json(
227 "farm",
225 _make_edu_transaction_json(
228226 "m.typing",
229227 content={
230228 "room_id": self.room_id,
232230 "typing": True,
233231 },
234232 ),
235 federation_auth=True,
233 federation_auth_origin=b'farm',
236234 )
237235
238236 self.on_new_event.assert_has_calls(
263261 call(
264262 "farm",
265263 path="/_matrix/federation/v1/send/1000000/",
266 data=_expect_edu(
267 "farm",
264 data=_expect_edu_transaction(
268265 "m.typing",
269266 content={
270267 "room_id": self.room_id,
305305
306306 @patch('twisted.web.http.Request')
307307 @defer.inlineCallbacks
308 def trigger(self, http_method, path, content, mock_request, federation_auth=False):
308 def trigger(
309 self, http_method, path, content, mock_request,
310 federation_auth_origin=None,
311 ):
309312 """ Fire an HTTP event.
310313
311314 Args:
314317 content : The HTTP body
315318 mock_request : Mocked request to pass to the event so it can get
316319 content.
320 federation_auth_origin (bytes|None): domain to authenticate as, for federation
317321 Returns:
318322 A tuple of (code, response)
319323 Raises:
334338 mock_request.getClientIP.return_value = "-"
335339
336340 headers = {}
337 if federation_auth:
338 headers[b"Authorization"] = [b"X-Matrix origin=test,key=,sig="]
341 if federation_auth_origin is not None:
342 headers[b"Authorization"] = [
343 b"X-Matrix origin=%s,key=,sig=" % (federation_auth_origin, )
344 ]
339345 mock_request.requestHeaders.getRawHeaders = mock_getRawHeaders(headers)
340346
341347 # return the right path if the event requires it