Repository navigation
Conversation
…ections Legacy token validation in CSRFConfig.ToMiddleware routes validation errors through config.ErrorHandler when configured, but the Sec-Fetch-Site check returned checkSecFetchSiteRequest errors directly, bypassing CSRFConfig.ErrorHandler for cross-site, same-site, and AllowSecFetchSiteFunc rejections. Invoke config.ErrorHandler(c, err) on Sec-Fetch-Site rejections when configured, and expand TestCSRFErrorHandling in middleware/csrf_test.go to cover Sec-Fetch-Site cross-site, same-site, and custom callback errors.
|
No actionable comments were generated in the recent review. Walkthrough
Change: Bug fix Merge Risk:
|
Summary
Invoke
CSRFConfig.ErrorHandlerwhencheckSecFetchSiteRequestrejects a request in the CSRF middleware (middleware/csrf.go), matching the legacy token validation error path.Root Cause
In
middleware/csrf.go, legacy token validation routes errors throughconfig.ErrorHandlerwhen configured:However, the
Sec-Fetch-Sitecheck at the top of the middleware returnederrdirectly without callingconfig.ErrorHandler:Because modern browsers send
Sec-Fetch-Siteheaders by default, applications configuringCSRFConfig.ErrorHandlerto return custom responses, record metrics, or redirect on CSRF failures never hadErrorHandlercalled forSec-Fetch-Site: cross-site,Sec-Fetch-Site: same-site, orAllowSecFetchSiteFuncrejections.Fix
config.checkSecFetchSiteRequest(c)throughconfig.ErrorHandler(c, err)whenconfig.ErrorHandler != nil(middleware/csrf.go).TestCSRFErrorHandlinginmiddleware/csrf_test.gointo a table-driven test covering missing token,Sec-Fetch-Site: cross-site,Sec-Fetch-Site: same-site, andAllowSecFetchSiteFunccustom errors.Verification
go test -race -v -count=1 ./middleware -run '^TestCSRF'(fails before the fix on all 3Sec-Fetch-Sitesubtests; passes after the fix).go test -race -count=1 ./middlewarego vet ./...Summary by CodeRabbit