From bfd6ddf2c00b4e67ea1a59420d39f3662013e34a Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 1 Dec 2022 12:23:31 -0500 Subject: [PATCH] Unknown endpoints for /media should return JSON responses. This is a generalization of the previous commit since /media endpoints are routed to via Twisted's standard Resource routing, not via servlet routing. --- synapse/http/server.py | 17 +++++++++++++++ synapse/rest/media/v1/media_repository.py | 4 ++-- synapse/util/httpresourcetree.py | 25 ++--------------------- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/synapse/http/server.py b/synapse/http/server.py index 2f2431cc7c5a..9314454af106 100644 --- a/synapse/http/server.py +++ b/synapse/http/server.py @@ -570,6 +570,23 @@ def render_GET(self, request: Request) -> bytes: return super().render_GET(request) +class UnrecognizedRequestResource(resource.Resource): + """ + Similar to twisted.web.resource.NoResource, but returns a JSON 404 with an + errcode of M_UNRECOGNIZED. + """ + + def render(self, request: SynapseRequest) -> int: + f = failure.Failure(UnrecognizedRequestError(code=404)) + return_json_error(f, request, None) + # A response has already been sent but Twisted requires either NOT_DONE_YET + # or the response bytes as a return value. + return NOT_DONE_YET + + def getChild(self, name: str, request: Request) -> resource.Resource: + return self + + class RootRedirect(resource.Resource): """Redirects the root '/' path to another path.""" diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py index 40b0d39eb239..c70e1837afd0 100644 --- a/synapse/rest/media/v1/media_repository.py +++ b/synapse/rest/media/v1/media_repository.py @@ -24,7 +24,6 @@ import twisted.internet.error import twisted.web.http from twisted.internet.defer import Deferred -from twisted.web.resource import Resource from synapse.api.errors import ( FederationDeniedError, @@ -35,6 +34,7 @@ ) from synapse.config._base import ConfigError from synapse.config.repository import ThumbnailRequirement +from synapse.http.server import UnrecognizedRequestResource from synapse.http.site import SynapseRequest from synapse.logging.context import defer_to_thread from synapse.metrics.background_process_metrics import run_as_background_process @@ -1046,7 +1046,7 @@ async def _remove_local_media_from_disk( return removed_media, len(removed_media) -class MediaRepositoryResource(Resource): +class MediaRepositoryResource(UnrecognizedRequestResource): """File uploading and downloading. Uploads are POSTed to a resource which returns a token which is used to GET diff --git a/synapse/util/httpresourcetree.py b/synapse/util/httpresourcetree.py index f06011aac164..39fab4fe067e 100644 --- a/synapse/util/httpresourcetree.py +++ b/synapse/util/httpresourcetree.py @@ -15,34 +15,13 @@ import logging from typing import Dict -from twisted.python import failure from twisted.web.resource import Resource -from twisted.web.server import NOT_DONE_YET, Request -from synapse.api.errors import UnrecognizedRequestError -from synapse.http.server import return_json_error -from synapse.http.site import SynapseRequest +from synapse.http.server import UnrecognizedRequestResource logger = logging.getLogger(__name__) -class _UnrecognizedRequestResource(Resource): - """ - Similar to twisted.web.resource.NoResource, but returns a JSON 404 with an - errcode of M_UNRECOGNIZED. - """ - - def render(self, request: SynapseRequest) -> int: - f = failure.Failure(UnrecognizedRequestError(code=404)) - return_json_error(f, request, None) - # A response has already been sent but Twisted requires either NOT_DONE_YET - # or the response bytes as a return value. - return NOT_DONE_YET - - def getChild(self, name: str, request: Request) -> Resource: - return self - - def create_resource_tree( desired_tree: Dict[str, Resource], root_resource: Resource ) -> Resource: @@ -72,7 +51,7 @@ def create_resource_tree( for path_seg in full_path.split(b"/")[1:-1]: if path_seg not in last_resource.listNames(): # resource doesn't exist, so make a "dummy resource" - child_resource: Resource = _UnrecognizedRequestResource() + child_resource: Resource = UnrecognizedRequestResource() last_resource.putChild(path_seg, child_resource) res_id = _resource_id(last_resource, path_seg) resource_mappings[res_id] = child_resource