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

Do not error when thumbnailing invalid files #8236

Merged
merged 11 commits into from
Sep 9, 2020
32 changes: 25 additions & 7 deletions synapse/rest/media/v1/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from twisted.internet.interfaces import IConsumer
from twisted.protocols.basic import FileSender
from twisted.web.server import Request

from synapse.api.errors import Codes, SynapseError, cs_error
from synapse.http.server import finish_request, respond_with_json
Expand Down Expand Up @@ -69,13 +70,30 @@ def parse_media_id(request):
)


def respond_404(request):
respond_with_json(
request,
404,
cs_error("Not found %r" % (request.postpath,), code=Codes.NOT_FOUND),
send_cors=True,
)
def respond_404(request: Request):
respond_with_code(request, 404)


def respond_with_code(request: Request, code: int):
clokep marked this conversation as resolved.
Show resolved Hide resolved
"""
Sends an empty response with a status code.

Args:
request: The http request to respond to.
code: The HTTP response code.
"""
# could alternatively use request.notifyFinish() and flip a flag when
# the Deferred fires, but since the flag is RIGHT THERE it seems like
# a waste.
if request._disconnected:
logger.warning(
"Not sending response to request %s, already disconnected.", request
)
return

request.setResponseCode(code)
request.setHeader(b"Content-Length", b"0")
finish_request(request)


async def respond_with_file(
Expand Down
1 change: 1 addition & 0 deletions tests/rest/media/v1/test_media_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,4 @@ def _test_thumbnail(self, method, expected_body, expected_found):
else:
# A 404 with no body.
self.assertEqual(channel.code, 404)
self.assertNotIn("body", channel.result)