From 59eaec4fd8e35e84585645cb1dff48373d8cf572 Mon Sep 17 00:00:00 2001 From: Till Faelligen <2353100+S7evinK@users.noreply.github.com> Date: Thu, 16 Mar 2023 12:43:40 +0100 Subject: [PATCH] Make Dendrite spec compliant, fix #2903 --- internal/httputil/routing.go | 41 +++++++++++++++--------------------- setup/base/base.go | 3 +++ 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/internal/httputil/routing.go b/internal/httputil/routing.go index 5522514b4..c733c8ce7 100644 --- a/internal/httputil/routing.go +++ b/internal/httputil/routing.go @@ -15,7 +15,6 @@ package httputil import ( - "fmt" "net/http" "net/url" @@ -64,31 +63,25 @@ func NewRouters() Routers { return r } +var NotAllowedHandler = WrapHandlerInCORS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusMethodNotAllowed) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}`)) // nolint:misspell +})) + +var NotFoundCORSHandler = WrapHandlerInCORS(http.HandlerFunc(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 +})) + func (r *Routers) configureHTTPErrors() { - notAllowedHandler := func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusMethodNotAllowed) - _, _ = 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 := WrapHandlerInCORS(http.NotFoundHandler()) - notAllowedCORSHandler := WrapHandlerInCORS(http.HandlerFunc(notAllowedHandler)) - for _, router := range []*mux.Router{ - r.Media, r.DendriteAdmin, - r.SynapseAdmin, r.WellKnown, - r.Static, + r.Client, r.Federation, r.Keys, + r.Media, r.WellKnown, r.Static, + r.DendriteAdmin, r.SynapseAdmin, } { - router.NotFoundHandler = notFoundCORSHandler - router.MethodNotAllowedHandler = notAllowedCORSHandler + router.NotFoundHandler = NotFoundCORSHandler + router.MethodNotAllowedHandler = NotAllowedHandler } - - // Special case so that we don't upset clients on the CS API. - r.Client.NotFoundHandler = http.HandlerFunc(clientNotFoundHandler) - r.Client.MethodNotAllowedHandler = http.HandlerFunc(clientNotFoundHandler) } diff --git a/setup/base/base.go b/setup/base/base.go index 021e7168c..ed3445019 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -353,6 +353,9 @@ func (b *BaseDendrite) SetupAndServeHTTP( b.startupLock.Unlock() + externalRouter.NotFoundHandler = httputil.NotFoundCORSHandler + externalRouter.MethodNotAllowedHandler = httputil.NotAllowedHandler + if externalHTTPAddr.Enabled() { go func() { var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once