fix(proxy): add includeSubDomains to the Strict-Transport-Security header

Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
Sara Ahmadi committed 2026-11-24 16:55:55 +01:00
1 parent a2f9b5b637
commit b9a39ea2d5
2 files changed
+51

No files matched your search

@@ -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",
@@ -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 := `