Skip to content
Snippets Groups Projects
Unverified Commit 314ca4c8 authored by Patrick Cloke's avatar Patrick Cloke Committed by GitHub
Browse files

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.
parent e03dde25
No related branches found
No related tags found
No related merge requests found
Use the proper type for the Content-Length header in the `UploadResource`.
...@@ -49,10 +49,14 @@ class UploadResource(DirectServeJsonResource): ...@@ -49,10 +49,14 @@ class UploadResource(DirectServeJsonResource):
async def _async_render_POST(self, request: SynapseRequest) -> None: async def _async_render_POST(self, request: SynapseRequest) -> None:
requester = await self.auth.get_user_by_req(request) requester = await self.auth.get_user_by_req(request)
content_length = request.getHeader("Content-Length") raw_content_length = request.getHeader("Content-Length")
if content_length is None: if raw_content_length is None:
raise SynapseError(msg="Request must specify a Content-Length", code=400) 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( raise SynapseError(
msg="Upload request body is too large", msg="Upload request body is too large",
code=413, code=413,
...@@ -66,7 +70,8 @@ class UploadResource(DirectServeJsonResource): ...@@ -66,7 +70,8 @@ class UploadResource(DirectServeJsonResource):
upload_name: Optional[str] = upload_name_bytes.decode("utf8") upload_name: Optional[str] = upload_name_bytes.decode("utf8")
except UnicodeDecodeError: except UnicodeDecodeError:
raise SynapseError( 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. # If the name is falsey (e.g. an empty byte string) ensure it is None.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment