diff --git a/services/proxy/pkg/middleware/security.go b/services/proxy/pkg/middleware/security.go index b8d3d82b52..0831679c08 100644 --- a/services/proxy/pkg/middleware/security.go +++ b/services/proxy/pkg/middleware/security.go @@ -147,6 +147,7 @@ func Security(cspConfig *config.CSP) func(h http.Handler) http.Handler { FrameDeny: true, ReferrerPolicy: "strict-origin-when-cross-origin", STSSeconds: 315360000, + STSIncludeSubdomains: true, STSPreload: true, PermittedCrossDomainPolicies: "none", RobotTag: "none", diff --git a/services/proxy/pkg/middleware/security_test.go b/services/proxy/pkg/middleware/security_test.go index e83e4f2371..cc681f6d14 100644 --- a/services/proxy/pkg/middleware/security_test.go +++ b/services/proxy/pkg/middleware/security_test.go @@ -1,12 +1,62 @@ package middleware import ( + "net/http" + "net/http/httptest" "testing" + "github.com/opencloud-eu/opencloud/services/proxy/pkg/config" "gotest.tools/v3/assert" "gotest.tools/v3/assert/cmp" ) +func TestSecurityStrictTransportSecurity(t *testing.T) { + tests := []struct { + name string + url string + headers map[string]string + want string + }{ + { + name: "https request gets the full header including subdomains", + url: "https://cloud.example.com/", + want: "max-age=315360000; includeSubDomains; preload", + }, + { + name: "plain http request gets no header", + url: "http://cloud.example.com/", + want: "", + }, + { + name: "plain http request with X-Forwarded-Proto gets no header", + url: "http://cloud.example.com/", + headers: map[string]string{"X-Forwarded-Proto": "https"}, + want: "", + }, + } + + cspConfig := &config.CSP{Directives: map[string][]string{"default-src": {"'none'"}}} + next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + handler := Security(cspConfig)(next) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, tt.url, nil) + for k, v := range tt.headers { + req.Header.Set(k, v) + } + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + + assert.Equal(t, rec.Code, http.StatusOK) + assert.Equal(t, rec.Header().Get("Strict-Transport-Security"), tt.want) + }) + } +} + func TestLoadCSPConfig(t *testing.T) { // setup test env presetYaml := `