diff options
author | Neil Alexander <neilalexander@users.noreply.github.com> | 2022-09-27 15:50:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-27 15:50:22 +0100 |
commit | 6c67552bf9eee18f656d731adf646aa09c5d7c92 (patch) | |
tree | 47d53ea2bd85838110750c209673a0c50c115f00 | |
parent | 249b32c4f3ee2e01e6f89435e0c7a5786d2ae3a1 (diff) |
Return `M_UNRECOGNIZED` for unknown CS API endpoints/actions (#2740)
Fixes #2739.
-rw-r--r-- | setup/base/base.go | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/setup/base/base.go b/setup/base/base.go index 0c7b222d..32716c76 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -392,17 +392,26 @@ func (b *BaseDendrite) configureHTTPErrors() { _, _ = w.Write([]byte(fmt.Sprintf("405 %s not allowed on this endpoint", r.Method))) } + clientNotFoundHandler := func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell + } + notFoundCORSHandler := httputil.WrapHandlerInCORS(http.NotFoundHandler()) notAllowedCORSHandler := httputil.WrapHandlerInCORS(http.HandlerFunc(notAllowedHandler)) for _, router := range []*mux.Router{ - b.PublicClientAPIMux, b.PublicMediaAPIMux, - b.DendriteAdminMux, b.SynapseAdminMux, - b.PublicWellKnownAPIMux, + b.PublicMediaAPIMux, b.DendriteAdminMux, + b.SynapseAdminMux, b.PublicWellKnownAPIMux, } { router.NotFoundHandler = notFoundCORSHandler router.MethodNotAllowedHandler = notAllowedCORSHandler } + + // Special case so that we don't upset clients on the CS API. + b.PublicClientAPIMux.NotFoundHandler = http.HandlerFunc(clientNotFoundHandler) + b.PublicClientAPIMux.MethodNotAllowedHandler = http.HandlerFunc(clientNotFoundHandler) } // SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on |