Apply a timeout to reading the body when fetching a file. (#11784)
This prevents the URL preview code from reading a stream forever.
This commit is contained in:
parent
ec2271ac50
commit
02d99f044e
|
@ -0,0 +1 @@
|
||||||
|
Fix a long-standing bug that media streams could cause long-lived connections when generating URL previews.
|
|
@ -731,15 +731,24 @@ class SimpleHttpClient:
|
||||||
# straight back in again
|
# straight back in again
|
||||||
|
|
||||||
try:
|
try:
|
||||||
length = await make_deferred_yieldable(
|
d = read_body_with_max_size(response, output_stream, max_size)
|
||||||
read_body_with_max_size(response, output_stream, max_size)
|
|
||||||
)
|
# Ensure that the body is not read forever.
|
||||||
|
d = timeout_deferred(d, 30, self.hs.get_reactor())
|
||||||
|
|
||||||
|
length = await make_deferred_yieldable(d)
|
||||||
except BodyExceededMaxSize:
|
except BodyExceededMaxSize:
|
||||||
raise SynapseError(
|
raise SynapseError(
|
||||||
HTTPStatus.BAD_GATEWAY,
|
HTTPStatus.BAD_GATEWAY,
|
||||||
"Requested file is too large > %r bytes" % (max_size,),
|
"Requested file is too large > %r bytes" % (max_size,),
|
||||||
Codes.TOO_LARGE,
|
Codes.TOO_LARGE,
|
||||||
)
|
)
|
||||||
|
except defer.TimeoutError:
|
||||||
|
raise SynapseError(
|
||||||
|
HTTPStatus.BAD_GATEWAY,
|
||||||
|
"Requested file took too long to download",
|
||||||
|
Codes.TOO_LARGE,
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise SynapseError(
|
raise SynapseError(
|
||||||
HTTPStatus.BAD_GATEWAY, ("Failed to download remote body: %s" % e)
|
HTTPStatus.BAD_GATEWAY, ("Failed to download remote body: %s" % e)
|
||||||
|
|
Loading…
Reference in New Issue