fix(tollbooth): correctly read context value in ShouldSkipLimiter (bypass) - #117
Open
desperatee wants to merge 1 commit into
Open
fix(tollbooth): correctly read context value in ShouldSkipLimiter (bypass)#117desperatee 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
ShouldSkipLimiterhas two bugs in the context-value branch that combine into a complete bypass of any rate limiter configured withSetContextValue:Bug 1 —
r.Header.Getinstead ofr.Context().Value(line 160)The adjacent first-phase check on line 142 and
BuildKeyson line 247 both correctly user.Context().Value(contextKey). This is the only site that reads fromr.Header— almost certainly a copy-paste from a sibling function.Bug 2 —
fmt.Sprintf("%v", nil) != ""is always true (line 142)fmt.Sprintf("%v", nil)returns"<nil>"(5 chars), not"". The guard intended to skip when the context key is absent fires for every request, including those genuinely missing the key.Combined behavior
For a limiter configured with
lmt.SetContextValue("access-level", []string{"basic"}):access-level=basicaccess-level=premiumaccess-levelEffect: any
SetContextValuelimiter is a no-op rate limiter.Fix
fmt.Sprintf("%v", r.Context().Value(contextKey)) == contextValuer.Context().Value(contextKey) != nilSeverity
CRITICAL — silent rate-limit bypass with no log signal. Any deployment using
SetContextValuefor per-tier limits (typical SaaS pattern) is unprotected.Proof of Concept
ShouldSkipLimiteris responsible for filtering which requests should bypass the limiter based on context values configured viaSetContextValue. Two bugs combine to make the filter unconditionally bypass the limiter for any non-emptycontextValueslist:r.Header.Get(contextKey)instead of the context value. The adjacent first-phase check at line 142 andBuildKeysat line 247 correctly callr.Context().Value(contextKey)— only this one site reads from the wrong source.fmt.Sprintf("%v", nil) != "", which evaluates to"<nil>" != ""(always true), so the first-phase "absent-key skip" never fires for missing context keys.Net effect: any limiter configured with
SetContextValue("key", []string{"some-value"})skips rate limiting for all requests — including authenticated, anonymous, abusive, and trusted alike.Steps to Reproduce
Both tests fail on master and pass after this fix.