Skip to content

Commit ca93d66

Browse files
committed
docs(auth): fix misleading withSecurityHeaders CONSUMER NOTE
Per Copilot review on PR #290 (post-merge), the previous note suggested custom HTML handlers could fix CSP blocking by "moving scripts/styles to external files served from 'self'". That's wrong: the wrapper applies default-src 'none', so even self-hosted scripts/styles/forms are blocked, and sandbox additionally disables script execution and form submission outright. The only viable path for an HTML custom handler is option (a): override the CSP on its own response. The note now spells out what the wrapper actually does and gives a concrete example of a relaxed CSP for a form-based custom provider. Docstring-only change in both v1 (auth.go) and v2 (v2/auth.go). No behavior change.
1 parent b19c8d7 commit ca93d66

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

auth.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,14 @@ func (s *Service) Handlers() (authHandler, avatarHandler http.Handler) {
258258
//
259259
// CONSUMER NOTE: custom providers added via Service.AddCustomHandler / AddProvider
260260
// are also wrapped. If a custom provider renders HTML (login forms, JS-based flows,
261-
// the dev_provider's login page, etc.), the strict CSP will block inline scripts and
262-
// event handlers on those pages. Such providers should either (a) override the CSP
263-
// for their own response by calling w.Header().Set("Content-Security-Policy", ...)
264-
// before writing — Set replaces the wrapper's value — or (b) move any required
265-
// scripts/styles to external files served from 'self'.
261+
// the dev_provider's login page, etc.), this CSP will block everything on the page:
262+
// default-src 'none' rules out scripts, styles, fonts, images and form targets even
263+
// when served from 'self', and the sandbox directive disables form submission and
264+
// script execution outright. Such providers must override the CSP for their own
265+
// response by calling w.Header().Set("Content-Security-Policy", ...) before writing
266+
// — Set replaces the wrapper's value — relaxing only the directives the page
267+
// actually needs (for example script-src 'self'; style-src 'self'; form-action 'self';
268+
// sandbox allow-forms allow-scripts).
266269
func withSecurityHeaders(next http.Handler) http.Handler {
267270
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
268271
w.Header().Set("Content-Security-Policy", "default-src 'none'; sandbox; frame-ancestors 'none'")

v2/auth.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,14 @@ func (s *Service) Handlers() (authHandler, avatarHandler http.Handler) {
259259
//
260260
// CONSUMER NOTE: custom providers added via Service.AddCustomHandler / AddProvider
261261
// are also wrapped. If a custom provider renders HTML (login forms, JS-based flows,
262-
// the dev_provider's login page, etc.), the strict CSP will block inline scripts and
263-
// event handlers on those pages. Such providers should either (a) override the CSP
264-
// for their own response by calling w.Header().Set("Content-Security-Policy", ...)
265-
// before writing — Set replaces the wrapper's value — or (b) move any required
266-
// scripts/styles to external files served from 'self'.
262+
// the dev_provider's login page, etc.), this CSP will block everything on the page:
263+
// default-src 'none' rules out scripts, styles, fonts, images and form targets even
264+
// when served from 'self', and the sandbox directive disables form submission and
265+
// script execution outright. Such providers must override the CSP for their own
266+
// response by calling w.Header().Set("Content-Security-Policy", ...) before writing
267+
// — Set replaces the wrapper's value — relaxing only the directives the page
268+
// actually needs (for example script-src 'self'; style-src 'self'; form-action 'self';
269+
// sandbox allow-forms allow-scripts).
267270
func withSecurityHeaders(next http.Handler) http.Handler {
268271
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
269272
w.Header().Set("Content-Security-Policy", "default-src 'none'; sandbox; frame-ancestors 'none'")

0 commit comments

Comments
 (0)