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

Commit

Permalink
Unknown endpoints for /media should return JSON responses.
Browse files Browse the repository at this point in the history
This is a generalization of the previous commit since /media endpoints
are routed to via Twisted's standard Resource routing, not via
servlet routing.
  • Loading branch information
clokep committed Dec 2, 2022
1 parent 3228d15 commit bfd6ddf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
17 changes: 17 additions & 0 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/media/v1/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 2 additions & 23 deletions synapse/util/httpresourcetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bfd6ddf

Please sign in to comment.