aboutsummaryrefslogtreecommitdiff
path: root/setup/base
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2022-08-01 11:34:27 +0100
committerGitHub <noreply@github.com>2022-08-01 11:34:27 +0100
commitc7f7aec4d07d59120d37d5b16a900f6d608a75c4 (patch)
treeed2c78dbe7b01f5b2701893cb53e961ffd89bc5c /setup/base
parent645f31ae24610110ee2c3100d228677fb3166b26 (diff)
Set CORS headers for HTTP 404 and 405 errors (#2599)
* Set CORS headers for the 404s * Use custom handlers, plus one for HTTP 405 too * Tweak setup * Add to muxes too * Tidy up some more * Use built-in HTTP 404 handler * Don't bother setting it for federation-facing
Diffstat (limited to 'setup/base')
-rw-r--r--setup/base/base.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/setup/base/base.go b/setup/base/base.go
index 93ab87de..b21eeba4 100644
--- a/setup/base/base.go
+++ b/setup/base/base.go
@@ -369,6 +369,25 @@ func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationCli
return client
}
+func (b *BaseDendrite) 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)))
+ }
+
+ 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,
+ } {
+ router.NotFoundHandler = notFoundCORSHandler
+ router.MethodNotAllowedHandler = notAllowedCORSHandler
+ }
+}
+
// SetupAndServeHTTP sets up the HTTP server to serve endpoints registered on
// ApiMux under /api/ and adds a prometheus handler under /metrics.
func (b *BaseDendrite) SetupAndServeHTTP(
@@ -409,6 +428,8 @@ func (b *BaseDendrite) SetupAndServeHTTP(
}
}
+ b.configureHTTPErrors()
+
internalRouter.PathPrefix(httputil.InternalPathPrefix).Handler(b.InternalAPIMux)
if b.Cfg.Global.Metrics.Enabled {
internalRouter.Handle("/metrics", httputil.WrapHandlerInBasicAuth(promhttp.Handler(), b.Cfg.Global.Metrics.BasicAuth))