From b370d63cb3aaa0c9e2da9ec61cf38a58f2d50f7a Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 17 Dec 2021 11:38:35 -0500 Subject: [PATCH 1/7] Use super() in some places. --- synapse/http/server.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/synapse/http/server.py b/synapse/http/server.py index 4fd5660a0819..7bbbe7648b8f 100644 --- a/synapse/http/server.py +++ b/synapse/http/server.py @@ -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__() self.url = path def render_GET(self, request: Request) -> bytes: @@ -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): @@ -556,7 +556,7 @@ 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 RootOptionsRedirectResource(OptionsResource, RootRedirect): From a7eb946628946d609dd5c6504280c23e5df4f335 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 17 Dec 2021 11:39:13 -0500 Subject: [PATCH 2/7] Return JSON errors instead of HTML errors for unknown resources. --- synapse/http/server.py | 11 +++++++++++ synapse/util/httpresourcetree.py | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/synapse/http/server.py b/synapse/http/server.py index 7bbbe7648b8f..e15cd2438b28 100644 --- a/synapse/http/server.py +++ b/synapse/http/server.py @@ -559,6 +559,17 @@ def getChildWithDefault(self, path: str, request: Request) -> resource.Resource: 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): pass diff --git a/synapse/util/httpresourcetree.py b/synapse/util/httpresourcetree.py index a0606851f7fb..55b0ff9b549b 100644 --- a/synapse/util/httpresourcetree.py +++ b/synapse/util/httpresourcetree.py @@ -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__) @@ -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 From 0982bbc16b2f0fbc73b3938bc346af383556ec3f Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 17 Dec 2021 11:50:18 -0500 Subject: [PATCH 3/7] Newsfragment --- changelog.d/11602.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11602.bugfix diff --git a/changelog.d/11602.bugfix b/changelog.d/11602.bugfix new file mode 100644 index 000000000000..964b9f5f7c91 --- /dev/null +++ b/changelog.d/11602.bugfix @@ -0,0 +1 @@ +Fix a long-standing bug that some unknown endpoints would return HTML 404 errors instead of JSON 400 errors. From a51b6f69240d53de050bc5363e37245bf3315994 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 17 Dec 2021 13:34:58 -0500 Subject: [PATCH 4/7] Fix changelog. Co-authored-by: reivilibre --- changelog.d/11602.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/11602.bugfix b/changelog.d/11602.bugfix index 964b9f5f7c91..a05765b75157 100644 --- a/changelog.d/11602.bugfix +++ b/changelog.d/11602.bugfix @@ -1 +1 @@ -Fix a long-standing bug that some unknown endpoints would return HTML 404 errors instead of JSON 400 errors. +Fix a long-standing bug that some unknown endpoints would return HTTP 404 errors instead of JSON `M_UNRECOGNIZED` errors. From 80ee55a05cfef9a91fe93e01ade179ee106669a5 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 17 Dec 2021 13:56:53 -0500 Subject: [PATCH 5/7] Fixup newsfragment. --- changelog.d/11602.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/11602.bugfix b/changelog.d/11602.bugfix index a05765b75157..e0dfbf1a1520 100644 --- a/changelog.d/11602.bugfix +++ b/changelog.d/11602.bugfix @@ -1 +1 @@ -Fix a long-standing bug that some unknown endpoints would return HTTP 404 errors instead of JSON `M_UNRECOGNIZED` errors. +Fix a long-standing bug that some unknown endpoints would return HTML error pages instead of JSON `M_UNRECOGNIZED` errors. From e58423668d07e59732bb7e55411749bbb2e48ff6 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 20 Dec 2021 10:26:12 -0500 Subject: [PATCH 6/7] Revert "Return JSON errors instead of HTML errors for unknown resources." This reverts commit a7eb946628946d609dd5c6504280c23e5df4f335. --- synapse/http/server.py | 11 ----------- synapse/util/httpresourcetree.py | 6 ++---- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/synapse/http/server.py b/synapse/http/server.py index e15cd2438b28..7bbbe7648b8f 100644 --- a/synapse/http/server.py +++ b/synapse/http/server.py @@ -559,17 +559,6 @@ def getChildWithDefault(self, path: str, request: Request) -> resource.Resource: 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): pass diff --git a/synapse/util/httpresourcetree.py b/synapse/util/httpresourcetree.py index 55b0ff9b549b..a0606851f7fb 100644 --- a/synapse/util/httpresourcetree.py +++ b/synapse/util/httpresourcetree.py @@ -15,9 +15,7 @@ import logging from typing import Dict -from twisted.web.resource import Resource - -from synapse.http.server import MissingResource +from twisted.web.resource import NoResource, Resource logger = logging.getLogger(__name__) @@ -51,7 +49,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 = MissingResource() + child_resource: Resource = NoResource() last_resource.putChild(path_seg, child_resource) res_id = _resource_id(last_resource, path_seg) resource_mappings[res_id] = child_resource From 98e74a633dbc45240506d838ea29dd6aaa57b295 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 20 Dec 2021 10:26:43 -0500 Subject: [PATCH 7/7] Consistently register the client resources under /_matrix/client instead of each API version. --- synapse/app/homeserver.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index dd76e0732108..177ce040e857 100644 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -27,6 +27,7 @@ import synapse.config.logger from synapse import events from synapse.api.urls import ( + CLIENT_API_PREFIX, FEDERATION_PREFIX, LEGACY_MEDIA_PREFIX, MEDIA_R0_PREFIX, @@ -192,13 +193,7 @@ def _configure_named_resource( resources.update( { - "/_matrix/client/api/v1": client_resource, - "/_matrix/client/r0": client_resource, - "/_matrix/client/v1": client_resource, - "/_matrix/client/v3": client_resource, - "/_matrix/client/unstable": client_resource, - "/_matrix/client/v2_alpha": client_resource, - "/_matrix/client/versions": client_resource, + CLIENT_API_PREFIX: client_resource, "/.well-known": well_known_resource(self), "/_synapse/admin": AdminRestResource(self), **build_synapse_client_resource_tree(self),