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

Commit 230ff6e

Browse files
clokepbabolivier
authored andcommitted
Pass the proper type when uploading files. (#11927)
The Content-Length header should be treated as an int, not a string. This shouldn't have any user-facing change.
1 parent b8b7c86 commit 230ff6e

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

changelog.d/11927.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use the proper type for the Content-Length header in the `UploadResource`.

synapse/rest/media/v1/upload_resource.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ async def _async_render_OPTIONS(self, request: SynapseRequest) -> None:
4949

5050
async def _async_render_POST(self, request: SynapseRequest) -> None:
5151
requester = await self.auth.get_user_by_req(request)
52-
content_length = request.getHeader("Content-Length")
53-
if content_length is None:
52+
raw_content_length = request.getHeader("Content-Length")
53+
if raw_content_length is None:
5454
raise SynapseError(msg="Request must specify a Content-Length", code=400)
55-
if int(content_length) > self.max_upload_size:
55+
try:
56+
content_length = int(raw_content_length)
57+
except ValueError:
58+
raise SynapseError(msg="Content-Length value is invalid", code=400)
59+
if content_length > self.max_upload_size:
5660
raise SynapseError(
5761
msg="Upload request body is too large",
5862
code=413,
@@ -66,7 +70,8 @@ async def _async_render_POST(self, request: SynapseRequest) -> None:
6670
upload_name: Optional[str] = upload_name_bytes.decode("utf8")
6771
except UnicodeDecodeError:
6872
raise SynapseError(
69-
msg="Invalid UTF-8 filename parameter: %r" % (upload_name), code=400
73+
msg="Invalid UTF-8 filename parameter: %r" % (upload_name_bytes,),
74+
code=400,
7075
)
7176

7277
# If the name is falsey (e.g. an empty byte string) ensure it is None.

0 commit comments

Comments
 (0)