fix(tollbooth): default IPLookup in LimitByRequest to avoid silent bypass - #118
Open
desperatee wants to merge 1 commit into
Open
fix(tollbooth): default IPLookup in LimitByRequest to avoid silent bypass#118desperatee wants to merge 1 commit into
desperatee wants to merge 1 commit into
Conversation
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.
Summary
The two entry points to the limiter —
HTTPMiddlewareandLimitHandler— behave inconsistently for the most common configuration (tollbooth.NewLimiter(N, nil)with no explicitSetIPLookup):HTTPMiddleware"RemoteAddr"at line 353LimitHandlerRoot cause:
limiter.New()leavesexplicitIPLookupat its zero value (IPLookup{Name: ""}).RemoteIPFromIPLookuponly knows four names; an empty string returns"", andShouldSkipLimiterthen returnstruebecause the bucket cannot be addressed.HTTPMiddlewarehas this guard:LimitByRequest(called byLimitHandlerand the public API) does not.Reproduction
Fix
Apply the same guard in
LimitByRequestso both entry points behave identically.Severity
HIGH — silent rate-limit bypass, no log/metric signal, and the broken pattern matches the simplest example in any rate-limiting blog post about this library. Users who switch from
HTTPMiddlewaretoLimitHandler(e.g. to wrap a single endpoint) get a degradation with no warning.Proof of Concept
limiter.New()does not setexplicitIPLookup, leavingIPLookup{Name: ""}as the zero value.RemoteIPFromIPLookuponly handles"RemoteAddr","X-Forwarded-For","X-Real-IP", and"CF-Connecting-IP"— the empty-string name falls through toreturn "".ShouldSkipLimiterthen returnstrue(skip) because the per-key bucket cannot be addressed.HTTPMiddlewaredefends against this with aName == ""guard that defaults to"RemoteAddr".LimitHandler/LimitByRequestdo not — so the most common documented usage (tollbooth.LimitHandler(tollbooth.NewLimiter(N, nil), handler)) silently lets every request through.Test_Issue48is a vacuous pass: its configured limit (2 req/s) equals its actual send rate (one request every 500ms), so the test passes whether the limiter fires or is bypassed entirely.Steps to Reproduce
100/100 pass on master, ~1/100 pass after fix.