The htmx login/registration partials answered success with an inline <script> that stashed the auth token and set window.location — refused by hash-mode script-src (no 'unsafe-inline'), so the user saw "Login successful! Redirecting..." and nothing else (reported from the home- page login form; /login was equally broken). The same pattern lived in collections.go (delete → redirect) and devices.go (token regen → reload, whose script tag was also unclosed). Replacement, per the AGENTS.md CSP rules: - auth.go login/registration: the partial now carries data-auth-token + data-redirect (redirect is path-only — open-redirect hygiene — and escaped); web/src/auth_forms.ts (new, armed from main.ts on every page) listens for htmx:afterSwap, stashes the token, and navigates. - collections.go delete: data-redirect, same listener. - devices.go token regen: htmx's native HX-Refresh response header. - The dead localStorage 'user'/'refreshToken' writes are dropped — no reader anywhere in web/src; only 'token' is load-bearing (19 modules). Tripwire extension: internal/middleware/csp_go_sources_test.go scans non-test Go sources for literal <script> tags and native on*= attributes (hand-written files only — templ codegen is owned by the .templ scan), verified to fire on a reintroduced probe. AGENTS.md CSP section notes the Go-emitted-partial vector. Verified live: home-page login form → /dashboard with token stashed and dashboard loading clean; /register form → /dashboard; collections and devices share the same mechanisms (data-redirect / HX-Refresh). Gates: make test (9 pkgs incl. both tripwires), vitest 155, Playwright 20, tsc/vp clean.
46 lines
2.2 KiB
Go
46 lines
2.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"github.com/labstack/echo/v5"
|
|
)
|
|
|
|
// ScriptHashThemeFlashGuard is the CSP script-src hash of the theme flash
|
|
// guard's inline script (the app's only inline script). Enforced against
|
|
// the rendered component by security_headers_test.go.
|
|
const ScriptHashThemeFlashGuard = "'sha256-JYIVpFyKdLUtWizMha0v69f6yHds+GbJRBWjvpuECBY='"
|
|
|
|
// SecurityHeadersMiddleware adds security headers to all responses
|
|
func SecurityHeadersMiddleware() echo.MiddlewareFunc {
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c *echo.Context) error {
|
|
// Add security headers
|
|
c.Response().Header().Set("X-Content-Type-Options", "nosniff")
|
|
c.Response().Header().Set("X-Frame-Options", "DENY")
|
|
c.Response().Header().Set("X-XSS-Protection", "1; mode=block")
|
|
c.Response().Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
|
|
// script-src notes: 'unsafe-inline' is required by the pre-paint
|
|
// CSP-safe Alpine build (@alpinejs/csp) needs no eval — do NOT
|
|
// add 'unsafe-eval' here without removing that build.
|
|
//
|
|
// The inline script allow-list: the theme flash guard is the
|
|
// ONLY inline script in the app, and its byte-stable content is
|
|
// hashed into script-src (sha256 of the bytes between the
|
|
// script tags as rendered by
|
|
// templates.ThemeFlashGuard()). Editing the guard requires
|
|
// recomputing the hash — enforced by
|
|
// internal/middleware/security_headers_test.go, which renders
|
|
// the component and asserts the hash matches.
|
|
//
|
|
// style-src keeps 'unsafe-inline' for htmx-injected styles and
|
|
// Alpine :style attribute writes.
|
|
// font/img blob: — the reader renders EPUB-embedded fonts and
|
|
// images from blob: URLs inside the foliate content frame.
|
|
c.Response().Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self' "+ScriptHashThemeFlashGuard+"; style-src 'self' 'unsafe-inline'; img-src 'self' data: https: blob:; font-src 'self' data: blob:; frame-src 'self' blob:")
|
|
c.Response().Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
|
|
c.Response().Header().Set("Permissions-Policy", "geolocation=(), microphone=(), camera=()")
|
|
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|