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

Commit 02d99f0

Browse files
authored
Apply a timeout to reading the body when fetching a file. (#11784)
This prevents the URL preview code from reading a stream forever.
1 parent ec2271a commit 02d99f0

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

changelog.d/11784.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug that media streams could cause long-lived connections when generating URL previews.

synapse/http/client.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -731,15 +731,24 @@ async def get_file(
731731
# straight back in again
732732

733733
try:
734-
length = await make_deferred_yieldable(
735-
read_body_with_max_size(response, output_stream, max_size)
736-
)
734+
d = read_body_with_max_size(response, output_stream, max_size)
735+
736+
# Ensure that the body is not read forever.
737+
d = timeout_deferred(d, 30, self.hs.get_reactor())
738+
739+
length = await make_deferred_yieldable(d)
737740
except BodyExceededMaxSize:
738741
raise SynapseError(
739742
HTTPStatus.BAD_GATEWAY,
740743
"Requested file is too large > %r bytes" % (max_size,),
741744
Codes.TOO_LARGE,
742745
)
746+
except defer.TimeoutError:
747+
raise SynapseError(
748+
HTTPStatus.BAD_GATEWAY,
749+
"Requested file took too long to download",
750+
Codes.TOO_LARGE,
751+
)
743752
except Exception as e:
744753
raise SynapseError(
745754
HTTPStatus.BAD_GATEWAY, ("Failed to download remote body: %s" % e)

0 commit comments

Comments
 (0)