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

Commit f43c66d

Browse files
committed
Merge branch 'develop' of github.com:matrix-org/synapse into anoa/info-mainline-no-check-password-reset
2 parents fe443ac + 12f0d18 commit f43c66d

File tree

19 files changed

+404
-118
lines changed

19 files changed

+404
-118
lines changed

changelog.d/8317.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support testing the local Synapse checkout against the [Complement homeserver test suite](https://github.com/matrix-org/complement/).

changelog.d/8400.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect handling of timeouts on outgoing HTTP requests.

changelog.d/8404.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not include appservice users when calculating the total MAU for a server.

changelog.d/8406.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add prometheus metrics for replication requests.

changelog.d/8417.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a config option to specify a whitelist of domains that a user can be redirected to after validating their email or phone number.

scripts-dev/complement.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#! /bin/bash -eu
2+
# This script is designed for developers who want to test their code
3+
# against Complement.
4+
#
5+
# It makes a Synapse image which represents the current checkout,
6+
# then downloads Complement and runs it with that image.
7+
8+
cd "$(dirname $0)/.."
9+
10+
# Build the base Synapse image from the local checkout
11+
docker build -t matrixdotorg/synapse:latest -f docker/Dockerfile .
12+
13+
# Download Complement
14+
wget -N https://github.com/matrix-org/complement/archive/master.tar.gz
15+
tar -xzf master.tar.gz
16+
cd complement-master
17+
18+
# Build the Synapse image from Complement, based on the above image we just built
19+
docker build -t complement-synapse -f dockerfiles/Synapse.Dockerfile ./dockerfiles
20+
21+
# Run the tests on the resulting image!
22+
COMPLEMENT_BASE_IMAGE=complement-synapse go test -v -count=1 ./tests

synapse/handlers/identity.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
import urllib.parse
2222
from typing import Awaitable, Callable, Dict, List, Optional, Tuple
2323

24-
from twisted.internet.error import TimeoutError
25-
2624
from synapse.api.errors import (
2725
CodeMessageException,
2826
Codes,
2927
HttpResponseException,
3028
SynapseError,
3129
)
3230
from synapse.config.emailconfig import ThreepidBehaviour
31+
from synapse.http import RequestTimedOutError
3332
from synapse.http.client import SimpleHttpClient
3433
from synapse.types import JsonDict, Requester
3534
from synapse.util import json_decoder
@@ -93,7 +92,7 @@ async def threepid_from_creds(
9392

9493
try:
9594
data = await self.http_client.get_json(url, query_params)
96-
except TimeoutError:
95+
except RequestTimedOutError:
9796
raise SynapseError(500, "Timed out contacting identity server")
9897
except HttpResponseException as e:
9998
logger.info(
@@ -173,7 +172,7 @@ async def bind_threepid(
173172
if e.code != 404 or not use_v2:
174173
logger.error("3PID bind failed with Matrix error: %r", e)
175174
raise e.to_synapse_error()
176-
except TimeoutError:
175+
except RequestTimedOutError:
177176
raise SynapseError(500, "Timed out contacting identity server")
178177
except CodeMessageException as e:
179178
data = json_decoder.decode(e.msg) # XXX WAT?
@@ -273,7 +272,7 @@ async def try_unbind_threepid_with_id_server(
273272
else:
274273
logger.error("Failed to unbind threepid on identity server: %s", e)
275274
raise SynapseError(500, "Failed to contact identity server")
276-
except TimeoutError:
275+
except RequestTimedOutError:
277276
raise SynapseError(500, "Timed out contacting identity server")
278277

279278
await self.store.remove_user_bound_threepid(
@@ -419,7 +418,7 @@ async def requestEmailToken(
419418
except HttpResponseException as e:
420419
logger.info("Proxied requestToken failed: %r", e)
421420
raise e.to_synapse_error()
422-
except TimeoutError:
421+
except RequestTimedOutError:
423422
raise SynapseError(500, "Timed out contacting identity server")
424423

425424
async def requestMsisdnToken(
@@ -471,7 +470,7 @@ async def requestMsisdnToken(
471470
except HttpResponseException as e:
472471
logger.info("Proxied requestToken failed: %r", e)
473472
raise e.to_synapse_error()
474-
except TimeoutError:
473+
except RequestTimedOutError:
475474
raise SynapseError(500, "Timed out contacting identity server")
476475

477476
assert self.hs.config.public_baseurl
@@ -553,7 +552,7 @@ async def proxy_msisdn_submit_token(
553552
id_server + "/_matrix/identity/api/v1/validate/msisdn/submitToken",
554553
body,
555554
)
556-
except TimeoutError:
555+
except RequestTimedOutError:
557556
raise SynapseError(500, "Timed out contacting identity server")
558557
except HttpResponseException as e:
559558
logger.warning("Error contacting msisdn account_threepid_delegate: %s", e)
@@ -627,7 +626,7 @@ async def _lookup_3pid_v1(
627626
# require or validate it. See the following for context:
628627
# https://github.com/matrix-org/synapse/issues/5253#issuecomment-666246950
629628
return data["mxid"]
630-
except TimeoutError:
629+
except RequestTimedOutError:
631630
raise SynapseError(500, "Timed out contacting identity server")
632631
except IOError as e:
633632
logger.warning("Error from v1 identity server lookup: %s" % (e,))
@@ -655,7 +654,7 @@ async def _lookup_3pid_v2(
655654
"%s%s/_matrix/identity/v2/hash_details" % (id_server_scheme, id_server),
656655
{"access_token": id_access_token},
657656
)
658-
except TimeoutError:
657+
except RequestTimedOutError:
659658
raise SynapseError(500, "Timed out contacting identity server")
660659

661660
if not isinstance(hash_details, dict):
@@ -727,7 +726,7 @@ async def _lookup_3pid_v2(
727726
},
728727
headers=headers,
729728
)
730-
except TimeoutError:
729+
except RequestTimedOutError:
731730
raise SynapseError(500, "Timed out contacting identity server")
732731
except Exception as e:
733732
logger.warning("Error when performing a v2 3pid lookup: %s", e)
@@ -823,7 +822,7 @@ async def ask_id_server_for_third_party_invite(
823822
invite_config,
824823
{"Authorization": create_id_access_token_header(id_access_token)},
825824
)
826-
except TimeoutError:
825+
except RequestTimedOutError:
827826
raise SynapseError(500, "Timed out contacting identity server")
828827
except HttpResponseException as e:
829828
if e.code != 404:
@@ -841,7 +840,7 @@ async def ask_id_server_for_third_party_invite(
841840
data = await self.blacklisting_http_client.post_json_get_json(
842841
url, invite_config
843842
)
844-
except TimeoutError:
843+
except RequestTimedOutError:
845844
raise SynapseError(500, "Timed out contacting identity server")
846845
except HttpResponseException as e:
847846
logger.warning(

synapse/http/__init__.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import re
1717

1818
from twisted.internet import task
19-
from twisted.internet.defer import CancelledError
20-
from twisted.python import failure
2119
from twisted.web.client import FileBodyProducer
2220

2321
from synapse.api.errors import SynapseError
@@ -26,19 +24,8 @@
2624
class RequestTimedOutError(SynapseError):
2725
"""Exception representing timeout of an outbound request"""
2826

29-
def __init__(self):
30-
super().__init__(504, "Timed out")
31-
32-
33-
def cancelled_to_request_timed_out_error(value, timeout):
34-
"""Turns CancelledErrors into RequestTimedOutErrors.
35-
36-
For use with async.add_timeout_to_deferred
37-
"""
38-
if isinstance(value, failure.Failure):
39-
value.trap(CancelledError)
40-
raise RequestTimedOutError()
41-
return value
27+
def __init__(self, msg):
28+
super().__init__(504, msg)
4229

4330

4431
ACCESS_TOKEN_RE = re.compile(r"(\?.*access(_|%5[Ff])token=)[^&]*(.*)$")

synapse/http/client.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16-
1716
import logging
1817
import urllib
1918
from io import BytesIO
@@ -38,25 +37,26 @@
3837

3938
from OpenSSL import SSL
4039
from OpenSSL.SSL import VERIFY_NONE
41-
from twisted.internet import defer, protocol, ssl
40+
from twisted.internet import defer, error as twisted_error, protocol, ssl
4241
from twisted.internet.interfaces import (
4342
IReactorPluggableNameResolver,
4443
IResolutionReceiver,
4544
)
4645
from twisted.internet.task import Cooperator
4746
from twisted.python.failure import Failure
4847
from twisted.web._newclient import ResponseDone
49-
from twisted.web.client import Agent, HTTPConnectionPool, readBody
48+
from twisted.web.client import (
49+
Agent,
50+
HTTPConnectionPool,
51+
ResponseNeverReceived,
52+
readBody,
53+
)
5054
from twisted.web.http import PotentialDataLoss
5155
from twisted.web.http_headers import Headers
5256
from twisted.web.iweb import IResponse
5357

5458
from synapse.api.errors import Codes, HttpResponseException, SynapseError
55-
from synapse.http import (
56-
QuieterFileBodyProducer,
57-
cancelled_to_request_timed_out_error,
58-
redact_uri,
59-
)
59+
from synapse.http import QuieterFileBodyProducer, RequestTimedOutError, redact_uri
6060
from synapse.http.proxyagent import ProxyAgent
6161
from synapse.logging.context import make_deferred_yieldable
6262
from synapse.logging.opentracing import set_tag, start_active_span, tags
@@ -332,8 +332,6 @@ async def request(
332332
RequestTimedOutError if the request times out before the headers are read
333333
334334
"""
335-
# A small wrapper around self.agent.request() so we can easily attach
336-
# counters to it
337335
outgoing_requests_counter.labels(method).inc()
338336

339337
# log request but strip `access_token` (AS requests for example include this)
@@ -362,15 +360,17 @@ async def request(
362360
data=body_producer,
363361
headers=headers,
364362
**self._extra_treq_args
365-
)
363+
) # type: defer.Deferred
364+
366365
# we use our own timeout mechanism rather than treq's as a workaround
367366
# for https://twistedmatrix.com/trac/ticket/9534.
368367
request_deferred = timeout_deferred(
369-
request_deferred,
370-
60,
371-
self.hs.get_reactor(),
372-
cancelled_to_request_timed_out_error,
368+
request_deferred, 60, self.hs.get_reactor(),
373369
)
370+
371+
# turn timeouts into RequestTimedOutErrors
372+
request_deferred.addErrback(_timeout_to_request_timed_out_error)
373+
374374
response = await make_deferred_yieldable(request_deferred)
375375

376376
incoming_responses_counter.labels(method, response.code).inc()
@@ -410,7 +410,7 @@ async def post_urlencoded_get_json(
410410
parsed json
411411
412412
Raises:
413-
RequestTimedOutException: if there is a timeout before the response headers
413+
RequestTimedOutError: if there is a timeout before the response headers
414414
are received. Note there is currently no timeout on reading the response
415415
body.
416416
@@ -461,7 +461,7 @@ async def post_json_get_json(
461461
parsed json
462462
463463
Raises:
464-
RequestTimedOutException: if there is a timeout before the response headers
464+
RequestTimedOutError: if there is a timeout before the response headers
465465
are received. Note there is currently no timeout on reading the response
466466
body.
467467
@@ -506,7 +506,7 @@ async def get_json(
506506
Returns:
507507
Succeeds when we get a 2xx HTTP response, with the HTTP body as JSON.
508508
Raises:
509-
RequestTimedOutException: if there is a timeout before the response headers
509+
RequestTimedOutError: if there is a timeout before the response headers
510510
are received. Note there is currently no timeout on reading the response
511511
body.
512512
@@ -538,7 +538,7 @@ async def put_json(
538538
Returns:
539539
Succeeds when we get a 2xx HTTP response, with the HTTP body as JSON.
540540
Raises:
541-
RequestTimedOutException: if there is a timeout before the response headers
541+
RequestTimedOutError: if there is a timeout before the response headers
542542
are received. Note there is currently no timeout on reading the response
543543
body.
544544
@@ -586,7 +586,7 @@ async def get_raw(
586586
Succeeds when we get a 2xx HTTP response, with the
587587
HTTP body as bytes.
588588
Raises:
589-
RequestTimedOutException: if there is a timeout before the response headers
589+
RequestTimedOutError: if there is a timeout before the response headers
590590
are received. Note there is currently no timeout on reading the response
591591
body.
592592
@@ -631,7 +631,7 @@ async def get_file(
631631
headers, absolute URI of the response and HTTP response code.
632632
633633
Raises:
634-
RequestTimedOutException: if there is a timeout before the response headers
634+
RequestTimedOutError: if there is a timeout before the response headers
635635
are received. Note there is currently no timeout on reading the response
636636
body.
637637
@@ -684,6 +684,18 @@ async def get_file(
684684
)
685685

686686

687+
def _timeout_to_request_timed_out_error(f: Failure):
688+
if f.check(twisted_error.TimeoutError, twisted_error.ConnectingCancelledError):
689+
# The TCP connection has its own timeout (set by the 'connectTimeout' param
690+
# on the Agent), which raises twisted_error.TimeoutError exception.
691+
raise RequestTimedOutError("Timeout connecting to remote server")
692+
elif f.check(defer.TimeoutError, ResponseNeverReceived):
693+
# this one means that we hit our overall timeout on the request
694+
raise RequestTimedOutError("Timeout waiting for response from remote server")
695+
696+
return f
697+
698+
687699
# XXX: FIXME: This is horribly copy-pasted from matrixfederationclient.
688700
# The two should be factored out.
689701

0 commit comments

Comments
 (0)