Codebase list matrix-synapse / c880b8b
Fix origin handling for pushed transactions Use the actual origin for push transactions, rather than whatever the remote server claimed. Richard van der Hoff authored 5 years ago Andrej Shadura committed 5 years ago
5 changed file(s) with 32 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
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