Skip to content

fix(middleware): reject empty tokens in CSRF validation - #3135

Merged
vishr merged 1 commit into
labstack:masterfrom
hoangtaiki:fix/csrf-empty-token-bypass
Oct 22, 2026
Merged

vishr merged 1 commit into
labstack:masterfrom
hoangtaiki:fix/csrf-empty-token-bypass

Conversation

@hoangtaiki

@hoangtaiki hoangtaiki commented Sep 30, 2026 •

Copy link
Copy Markdown

Summary

validateCSRFToken compares tokens with subtle.ConstantTimeCompare, which returns equal for two empty byte slices. Extractors in extractor.go treat a present-but-empty form/query/header value as a successfully extracted client token, so if the stored cookie token is ever an empty string, any request supplying an empty token passes CSRF validation without knowing the real token.

  • Reject token == "" || clientToken == "" before the constant-time compare in validateCSRFToken.

This can't happen from Echo's own default config alone (tokens are always 32 random bytes), but it is reachable if the stored cookie token ever becomes empty - for example an application clearing the CSRF cookie to "" instead of expiring it, or CookieSecure: false (the default) allowing the cookie to be overwritten over plain HTTP by a network attacker. Rejecting empty tokens outright removes this edge case entirely.

Test plan

  • go build ./...
  • go vet ./...
  • go test ./...
  • Added a regression test (TestCSRFWithConfig/nok,_POST_empty_cookie_token_+_empty_client_token_is_blocked) and verified it fails without the fix and passes with it

subtle.ConstantTimeCompare treats two empty byte slices as equal, so
validateCSRFToken("", "") returns true. Extractors in extractor.go
treat a present-but-empty query/form/header value as successfully
extracted, so an attacker can supply an empty client token with no
special access. If the stored cookie token is ever empty (e.g. an
app clears it to "" instead of expiring it, or CookieSecure is false
and the cookie is overwritten over plain HTTP), CSRF validation is
bypassed entirely. Reject empty tokens before the comparison.

@vishr vishr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the empty-token guard and regression test. Approving as defense in depth. Cookie-write capability also permits setting a matching nonempty token, so the empty case does not establish an additional exploitable vulnerability and will not receive a security advisory.

@vishr
vishr merged commit f9d7e77 into labstack:master Oct 22, 2026
@vishr

vishr commented Oct 22, 2026

Copy link
Copy Markdown
Member

Merged as defense in depth. The v4 backport is open at #3189 with the original contributor credit and regression coverage. No security advisory is being created for this empty-token case.

vishr added a commit that referenced this pull request Oct 22, 2026
Backport #3135 as defense in depth. Reject empty cookie or client tokens
before constant-time comparison, retaining normal valid-token behavior.

Co-authored-by: Harry Tran <hoang@platphormcorp.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants