From 3af541879f0876c8ff88130022d45feabf4fba99 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 7 Feb 2022 08:44:43 -0500 Subject: [PATCH 1/3] Pass the integer content-length. --- synapse/rest/media/v1/upload_resource.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/synapse/rest/media/v1/upload_resource.py b/synapse/rest/media/v1/upload_resource.py index 8162094cf688..25a19f65230b 100644 --- a/synapse/rest/media/v1/upload_resource.py +++ b/synapse/rest/media/v1/upload_resource.py @@ -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, From 324c7b8a8057db1a4ef5fa2bd025ca93c18768a1 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 7 Feb 2022 08:45:09 -0500 Subject: [PATCH 2/3] Do not reference an invalid variable. --- synapse/rest/media/v1/upload_resource.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/synapse/rest/media/v1/upload_resource.py b/synapse/rest/media/v1/upload_resource.py index 25a19f65230b..fde28d08cb8e 100644 --- a/synapse/rest/media/v1/upload_resource.py +++ b/synapse/rest/media/v1/upload_resource.py @@ -70,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. From ed205174fa2811071a2c69e90c5334d568fb0fb3 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 7 Feb 2022 08:49:42 -0500 Subject: [PATCH 3/3] Newsfragment --- changelog.d/11927.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11927.misc diff --git a/changelog.d/11927.misc b/changelog.d/11927.misc new file mode 100644 index 000000000000..22c58521c92c --- /dev/null +++ b/changelog.d/11927.misc @@ -0,0 +1 @@ +Use the proper type for the Content-Length header in the `UploadResource`.