// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial // SPDX-FileCopyrightText: 2025-2026 Christian Galo package middleware import ( "net/http" "git.coopcloud.tech/wiki-cafe/member-console/internal/config" ) // hstsValue is the Strict-Transport-Security policy sent over https: one // year, subdomains included, no preload. const hstsValue = "max-age=31536000; includeSubDomains" // SecurityHeaders adds security and cache-control headers to all responses func SecureHeaders() Middleware { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Caching headers w.Header().Set("Cache-Control", "no-store") // XSSProtection provides protection against cross-site scripting attack (XSS) w.Header().Set("X-XSS-Protection", "1; mode=block") // ContentTypeNosniff provides protection against overriding Content-Type w.Header().Set("X-Content-Type-Options", "nosniff") // XFrameOptions prevents the page from being displayed in a frame w.Header().Set("X-Frame-Options", "DENY") // HSTS, only where the console is served over https. RFC 6797 // section 7.2 forbids sending it over non-secure transport // (browsers ignore it there anyway, section 8.1). The year is // the standard value: a shorter one re-opens the first-request // window every time a visitor is away longer than it (section // 11.2). includeSubDomains covers subdomains of the console's // own host, which nothing else uses, and closes cookie injection // from an insecure one (section 11.4); at an apex deployment it // would reach every subdomain, which production-deployment.md // says out loud. No preload: that is a shipped-list commitment. if config.ServesHTTPS() { w.Header().Set("Strict-Transport-Security", hstsValue) } // ReferrerPolicy sets the referrer information passed during navigation w.Header().Set("Referrer-Policy", "no-referrer") // CSP controls the resources the user agent is allowed to load for a page cspPolicy := "default-src 'self'; " + // Script comes only from this origin. The hypermedia library is // vendored into internal/embeds/static/ and served from /static/, // so the unpkg.com source this directive used to carry authorized // a CDN nothing loads from — a standing permission for // third-party script, flagged by the 2026-09 security audit. "script-src 'self'; " + "style-src 'self'; " + "img-src 'self' data:; " + "font-src 'self'; " + "connect-src 'self'; " + "object-src 'none'; " + "frame-ancestors 'none'; " + "form-action 'self'; " + "base-uri 'self';" // upgrade-insecure-requests only where the console is served // over https (base-url's scheme); on plain HTTP it would // upgrade every subresource to a scheme nothing listens on. if config.ServesHTTPS() { cspPolicy += "upgrade-insecure-requests;" } // Set Content-Security-Policy header w.Header().Set("Content-Security-Policy", cspPolicy) // Cross-Origin-Embedder-Policy prevents cross-origin resources from being loaded w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp") // Cross-Origin-Opener-Policy prevents cross-origin documents from being loaded w.Header().Set("Cross-Origin-Opener-Policy", "same-origin") // Cross-Origin-Resource-Policy prevents cross-origin resources from being loaded w.Header().Set("Cross-Origin-Resource-Policy", "same-origin") next.ServeHTTP(w, r) }) } } // MaxBodySize limits the maximum size of request bodies // size parameter is in bytes func MaxBodySize(maxSize int64) Middleware { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Skip restricting GET, HEAD, and OPTIONS requests as they shouldn't have bodies if r.Method == http.MethodGet || r.Method == http.MethodHead || r.Method == http.MethodOptions { next.ServeHTTP(w, r) return } // Check Content-Length header first for efficiency if r.ContentLength > maxSize { http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge) return } // If Content-Length is not set or potentially spoofed, use LimitReader r.Body = http.MaxBytesReader(w, r.Body, maxSize) // Continue to next middleware/handler next.ServeHTTP(w, r) }) } }