fix(middleware): reject CSRF TokenLookup that produces no extractors - #3067
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3067 +/- ##
==========================================
+ Coverage 93.34% 93.37% +0.03%
==========================================
Files 43 44 +1
Lines 4735 4787 +52
==========================================
+ Hits 4420 4470 +50
- Misses 192 193 +1
- Partials 123 124 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Problem
If
TokenLookupuses a source keyword that isn't recognized, CSRF silentlystops checking tokens.
The known sources are
header,query,param,cookie,form. A small typolike
Header:X-CSRF-Token(capital H) orcookies:_csrf(should becookie)matches none of them, and there's no
defaultcase, so it's just dropped. Ifevery source in the lookup is like that you end up with zero extractors and no
error. CSRF then loops over an empty list, finds nothing to validate, and lets
the request through. So a typo in the config turns CSRF off without any warning.
KeyAuth already guards against this:
CSRF just doesn't have the same check, so this PR adds it.
For context on impact: this only happens with a misconfigured
TokenLookup, andSec-Fetch-Sitestill blocks normal cross-site requests. But a securitymiddleware quietly doing nothing on a typo felt worth turning into an error.
Change
CSRFConfig.ToMiddleware()now returns an error whenTokenLookupproduces noextractors (e.g.
"echo csrf middleware could not create extractors from TokenLookup string"),matching KeyAuth.
CSRFWithConfigturns that error into a startup panic.Valid configs keep working; only a lookup where all sources are unknown is
rejected.