Wrap router with http.CrossOriginProtection for CSRF defence#119
Open
paskal wants to merge 1 commit intoumputun:masterfrom
Open
Wrap router with http.CrossOriginProtection for CSRF defence#119paskal wants to merge 1 commit intoumputun:masterfrom
paskal wants to merge 1 commit intoumputun:masterfrom
Conversation
Add Go 1.25's http.NewCrossOriginProtection().Handler to the global middleware chain. Previously the only CSRF defence was SameSite=Strict on the session cookie, which Firefox does not enforce by default and which subdomain attacks can bypass. The middleware checks Sec-Fetch-Site (forbidden header, set by all major browsers since 2023) with an Origin/Host fallback. Safe methods and non-browser POSTs (no Sec-Fetch-Site header, e.g. curl/scripts hitting /api/v1/) pass through unchanged.
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.
Adds Go 1.25's
http.NewCrossOriginProtection().Handlerto the global middleware chain inapp/server/server.go. One line of code, ~50 lines of test.Why
Today the only CSRF defence on the cookie session is
SameSite=Strict, which has known gaps:SameSite=Laxon its release channel and has no plans to change in 2025 -- explicitStrictis honoured but doesn't cover sites that only setLaxSameSiteentirely -- it operates on site (registrable domain), not origin, so an attacker controlling any*.safesecret.infosubdomain can issue same-site POSTs with the session cookie attachedSameSiteavailable across sites for 120s after navigationhttp.CrossOriginProtectionchecks the browser-setSec-Fetch-Siteheader (a forbidden header that JavaScript cannot forge, shipped in all major browsers since 2023) with anOriginvsHostfallback for older clients. UnlikeSameSite, it distinguishes same-origin from same-site -- subdomain attacks are blocked.OWASP elevated this algorithm from defence-in-depth to a primary defence in its CSRF cheatsheet in December 2025.
What changed
app/server/server.go-- one line in therouter.Use(...)blockapp/server/server_test.go-- newTestServer_crossOriginProtectioncovering same-origin / cross-site / origin-host-mismatch / non-browser cases for both web routes (/theme) and the API (/api/v1/message)Behaviour notes
Sec-Fetch-Site: same-originis sent and the middleware passes them through. Verified end-to-end via the new test.Sec-Fetch-Site-- the middleware treats this as a non-browser request and lets them through. The API was never cookie-authenticated so this is the correct behaviour./api/v1/message(e.g. an attacker page issuingfetch()without CORS) is now rejected before reaching the handler -- not a regression since CORS is not configured.References
SameSite=Laxdefault: bz#1617609