Replace hand-rolled CSRF tokens with http.CrossOriginProtection#45
Merged
Conversation
Drop the per-request CSRF token cookie + hidden form field in favour of Go 1.25's http.CrossOriginProtection middleware, which checks the browser-set Sec-Fetch-Site header (with an Origin/Host fallback) and needs no client-side instrumentation. Net change: ~190 LoC removed.
umputun
approved these changes
Apr 17, 2026
Owner
umputun
left a comment
There was a problem hiding this comment.
lgtm - swapping 190 LoC of hand-rolled crypto/cookie plumbing for one stdlib line is a win, and the new middleware actually expands CSRF coverage: the old double-submit was only on /login, now /upload, /download-selected, /partials/selection-status are protected too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Drops the per-request CSRF token cookie + hidden form field in favour of Go 1.25's
http.CrossOriginProtectionmiddleware (one line inserver/server.go). The middleware checks the browser-setSec-Fetch-Siteheader with anOriginvsHostfallback for older clients, so no client-side instrumentation is needed.Why
Sec-Fetch-Siteis a forbidden header -- the browser sets it and JS cannot forge it. It's been shipped in all major browsers since 2023 (96%+ global coverage per Can I Use), and OWASP elevated it from defence-in-depth to a primary defence in its CSRF cheatsheet in December 2025. Compared to the hand-rolled double-submit token it replaces:CSRFTokenfield, hidden<input>)csrf_tokenset/clear lifecycle, 5-min TTL)SameSite=Laxpermits) are blockedWhat changed
server/server.go--router.Use(http.NewCrossOriginProtection().Handler)added to the existing middleware chainserver/auth.go-- removedgenerateCSRFToken,CSRFTokentemplate field, allcsrf_tokencookie lifecycle code inhandleLoginPage/handleLoginSubmit/renderLoginErrorserver/templates/login.html-- dropped the hiddencsrf_tokeninputserver/auth_test.go-- newTestCrossOriginProtectioncovering same-origin / cross-site / origin-host-mismatch / non-browser cases; obsolete "missing/invalid CSRF token" subtests removedmain_test.go-- integration tests no longer thread a CSRF token through the login flowNet: ~190 LoC removed.
References for context
Notes
SameSite=Laxon the auth session cookie remains as defence-in-depth.Originhost againstr.Host(HSTS recommended to prevent HTTP→HTTPS downgrades). TheSec-Fetch-Sitepath is checked first and is unaffected by proxy schemes.