You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CSRF middleware doesn't set token in context when Sec-Fetch-Site validation passes (v4.15.0) #2874
In v4.15.0, when checkSecFetchSiteRequest() returns (true, nil) (e.g., for direct URL navigation where Sec-Fetch-Site: none), the middleware calls return next(c) without setting the CSRF token in context. This breaks handlers that need the token to render forms.
Version
Echo v4.15.0
Reproduction
Handler that expects CSRF token in context:
func (h*handler) RegisterPage(c echo.Context) error {
csrfToken, ok:=c.Get("csrf").(string)
if!ok {
returnecho.NewHTTPError(http.StatusInternalServerError, "CSRF token not found")
}
// Render form with CSRF token...
}
# Works in v4.14.0, fails in v4.15.0
curl -H "Sec-Fetch-Site: none" https://example.com/users/register
# Without header, both versions work
curl https://example.com/users/register
Expected Behavior
GET /users/register with Sec-Fetch-Site: none returns 200 with CSRF token in context and cookie set
The form can be rendered with the token for subsequent POST
Actual Behavior (v4.15.0)
GET /users/register with Sec-Fetch-Site: none returns 500 "CSRF token not found"
The token is never set in context because checkSecFetchSiteRequest() returns early
Root Cause
In middleware/csrf.go around lines 164-167, when checkSecFetchSiteRequest() returns (true, nil):
allow, err:=checkSecFetchSiteRequest(c)
iferr!=nil {
returnerr
}
ifallow {
returnnext(c) // <-- Returns without setting token in context!
}
The middleware correctly validates the request is safe but then returns without:
Generating/retrieving CSRF token
Setting the cookie
Setting the token in context
Suggested Fix
Even for "safe" requests per Sec-Fetch-Site validation, the middleware should still set the token in context and cookie:
ifallow {
// Still need to set token for form renderingtoken:=""ifk, err:=c.Cookie(config.CookieName); err!=nil {
token=randomString(config.TokenLength)
} else {
token=k.Value
}
// Set cookie and context even for "safe" requestsc.SetCookie(createCookie(config, token))
c.Set(config.ContextKey, token)
c.Response().Header().Add(echo.HeaderVary, echo.HeaderCookie)
returnnext(c)
}
Workaround
Downgrade to Echo v4.14.0 until this is fixed.
Impact
This breaks all server-rendered forms that use CSRF middleware when accessed via direct browser navigation (typing URL, bookmarks, clicking external links). The Sec-Fetch-Site: none header is automatically sent by all modern browsers in these scenarios.
Note for myself. In case handler is reached and csrfToken, ok := c.Get("csrf").(string) is not set, means that Sec-Fetch-Site header was deemed safe. And probably something like that
csrfToken, ok:=c.Get("csrf").(string)
if!ok {
csrfToken="UsingSecFetchSite"// dummy value for rendered forms
}
would be a workaround to allow rendered forms to work without downgrading.
but nevertheless we need to think what we can do to fix this situation for other users
changed the title [-]CSRF middleware doesn't set token in context when Sec-Fetch-Site validation passes[/-][+]CSRF middleware doesn't set token in context when Sec-Fetch-Site validation passes (v4.15.0)[/+]on Jan 21, 2026
Activity
aldas commentedon Jan 21, 2026
Note for myself. In case handler is reached and
csrfToken, ok := c.Get("csrf").(string)is not set, means thatSec-Fetch-Siteheader was deemed safe. And probably something like thatwould be a workaround to allow rendered forms to work without downgrading.
but nevertheless we need to think what we can do to fix this situation for other users
[-]CSRF middleware doesn't set token in context when Sec-Fetch-Site validation passes[/-][+]CSRF middleware doesn't set token in context when Sec-Fetch-Site validation passes (v4.15.0)[/+]auth: Add CSRF protection to all state-changing requests
Fix CSRF middleware not setting token in context when Sec-Fetch-Site …
Fix CSRF middleware to set token in context when Sec-Fetch-Site valid…
aldas commentedon Feb 22, 2026
Fix is released in v4.15.1