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

Return JSON errors instead of HTML errors for unknown resources. #11602

Merged
merged 7 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11602.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a long-standing bug that some unknown endpoints would return HTML 404 errors instead of JSON 400 errors.
clokep marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 14 additions & 3 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ class RootRedirect(resource.Resource):
"""Redirects the root '/' path to another path."""

def __init__(self, path: str):
resource.Resource.__init__(self)
super().__init__()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I see no reason not to make these changes still...)

self.url = path

def render_GET(self, request: Request) -> bytes:
Expand All @@ -539,7 +539,7 @@ def render_GET(self, request: Request) -> bytes:
def getChild(self, name: str, request: Request) -> resource.Resource:
if len(name) == 0:
return self # select ourselves as the child to render
return resource.Resource.getChild(self, name, request)
return super().getChild(name, request)


class OptionsResource(resource.Resource):
Expand All @@ -556,7 +556,18 @@ def render_OPTIONS(self, request: Request) -> bytes:
def getChildWithDefault(self, path: str, request: Request) -> resource.Resource:
if request.method == b"OPTIONS":
return self # select ourselves as the child to render
return resource.Resource.getChildWithDefault(self, path, request)
return super().getChildWithDefault(path, request)


class MissingResource(resource.Resource):
"""Similar to resource.NoResource, but returns a JSON error with proper CORS headers."""

def render(self, request: SynapseRequest) -> int:
return_json_error(failure.Failure(UnrecognizedRequestError()), request)
return NOT_DONE_YET

def getChild(self, name: str, request: Request) -> resource.Resource:
return self


class RootOptionsRedirectResource(OptionsResource, RootRedirect):
Expand Down
6 changes: 4 additions & 2 deletions synapse/util/httpresourcetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import logging
from typing import Dict

from twisted.web.resource import NoResource, Resource
from twisted.web.resource import Resource

from synapse.http.server import MissingResource

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -49,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 = NoResource()
child_resource: Resource = MissingResource()
last_resource.putChild(path_seg, child_resource)
res_id = _resource_id(last_resource, path_seg)
resource_mappings[res_id] = child_resource
Expand Down