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

Pass the proper type when uploading files. #11927

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11927.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use the proper type for the Content-Length header in the `UploadResource`.
13 changes: 9 additions & 4 deletions synapse/rest/media/v1/upload_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ async def _async_render_OPTIONS(self, request: SynapseRequest) -> None:

async def _async_render_POST(self, request: SynapseRequest) -> None:
requester = await self.auth.get_user_by_req(request)
content_length = request.getHeader("Content-Length")
if content_length is None:
raw_content_length = request.getHeader("Content-Length")
if raw_content_length is None:
raise SynapseError(msg="Request must specify a Content-Length", code=400)
if int(content_length) > self.max_upload_size:
try:
content_length = int(raw_content_length)
except ValueError:
raise SynapseError(msg="Content-Length value is invalid", code=400)
if content_length > self.max_upload_size:
raise SynapseError(
msg="Upload request body is too large",
code=413,
Expand All @@ -66,7 +70,8 @@ async def _async_render_POST(self, request: SynapseRequest) -> None:
upload_name: Optional[str] = upload_name_bytes.decode("utf8")
except UnicodeDecodeError:
raise SynapseError(
msg="Invalid UTF-8 filename parameter: %r" % (upload_name), code=400
msg="Invalid UTF-8 filename parameter: %r" % (upload_name_bytes,),
code=400,
)

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