A continuation of HomeBox the inventory and organization system built for the Home User
fix: use standard Cross-Origin header names in SecurityHeaders (#1682)
Ajithkumar003-devc61808ad8974
diff --git a/backend/internal/web/mid/security.go b/backend/internal/web/mid/security.goindex 3a6c8aea..1393ea0f 100644--- a/backend/internal/web/mid/security.go+++ b/backend/internal/web/mid/security.go@@ -12,9 +12,9 @@ import (func SecurityHeaders() func(http.Handler) http.Handler {return func(h http.Handler) http.Handler {return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- w.Header().Set("Content-Origin-Embedder-Policy", "require-corp")- w.Header().Set("Content-Origin-Opener-Policy", "same-origin")- w.Header().Set("Content-Origin-Resource-Policy", "same-site")+ w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")+ w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")+ w.Header().Set("Cross-Origin-Resource-Policy", "same-site")w.Header().Set("Permissions-Policy", "accelerometer=(), autoplay=(), camera=(self), cross-origin-isolated=(), display-capture=(), encrypted-media=(), fullscreen=(), geolocation=(), gyroscope=(), keyboard-map=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(self), usb=(), web-share=(), xr-spatial-tracking=(), clipboard-read=(), clipboard-write=(self), gamepad=(), hid=(), idle-detection=(), interest-cohort=(), serial=(), unload=()")w.Header().Set("Referrer-Policy", "no-referrer")w.Header().Set("X-Content-Type-Options", "nosniff")diff --git a/backend/internal/web/mid/security_test.go b/backend/internal/web/mid/security_test.gonew file mode 100644index 00000000..91e6da3d--- /dev/null+++ b/backend/internal/web/mid/security_test.go@@ -0,0 +1,46 @@+package mid++import (+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/stretchr/testify/assert"+)++// The cross-origin isolation headers were spelled "Content-Origin-*" instead of+// "Cross-Origin-*". Browsers ignore header names they do not recognise, so all+// three policies were silently inert rather than merely misconfigured, and+// nothing in the response looked wrong enough to notice. This is issue #1665.+func TestSecurityHeadersUsesStandardCrossOriginNames(t *testing.T) {+ rec := httptest.NewRecorder()++ handler := SecurityHeaders()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusOK)+ }))+ handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))++ want := map[string]string{+ "Cross-Origin-Embedder-Policy": "require-corp",+ "Cross-Origin-Opener-Policy": "same-origin",+ "Cross-Origin-Resource-Policy": "same-site",+ "Referrer-Policy": "no-referrer",+ "X-Content-Type-Options": "nosniff",+ "X-Frame-Options": "DENY",+ "X-Permitted-Cross-Domain-Policies": "none",+ }+ for name, value := range want {+ assert.Equal(t, value, rec.Header().Get(name), "%s should be set", name)+ }++ // Guard against the misspelling coming back: a stray "Content-Origin-*"+ // header is indistinguishable from a working one without checking the+ // exact name, which is what let this go unnoticed in the first place.+ for _, name := range []string{+ "Content-Origin-Embedder-Policy",+ "Content-Origin-Opener-Policy",+ "Content-Origin-Resource-Policy",+ } {+ assert.Empty(t, rec.Header().Values(name), "%s is not a real header and should not be sent", name)+ }+}