fix(x402): fail-closed when URI is under a paid prefix but no rule matches#524
Closed
bussyjd wants to merge 1 commit into
Closed
fix(x402): fail-closed when URI is under a paid prefix but no rule matches#524bussyjd wants to merge 1 commit into
bussyjd wants to merge 1 commit into
Conversation
…tches
Today HandleVerify returns 200 whenever matchPaidRoute returns nil.
Combined with Traefik ForwardAuth's "200 = allow" semantics, this
means a misconfigured Middleware on a paid route OR a code bug where
the route was supposed to match but didn't silently makes the route
FREE — revenue loss with no signal.
- Adds paidPrefixes atomic.Pointer[[]string] to Verifier.
- Verifier.load() derives prefixes from cfg.Routes patterns:
"/services/foo/*" -> "/services/foo/" (trailing slash kept so
HasPrefix doesn't false-match /services/foobar/).
- HandleVerify: when matchRoute returns nil, check if URI is under
any tracked prefix. If yes -> 403. If no -> 200 (legitimately free).
Complementary to PR #519 (gate /readyz on informer sync):
- PR #519 ensures the pod isn't Ready until routes are loaded
(closes the bootstrap-window leak).
- This PR ensures that after Ready, any prefix the verifier KNOWS
about that doesn't have a matching rule is fail-closed
(closes the steady-state-bug leak).
Together they cover the "rule should match but doesn't" gap.
This was referenced May 24, 2026
Collaborator
Author
|
Superseded by bundle PR #536 — closing in favor of the consolidated merge target. Original branch and history preserved. |
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.
Why
HandleVerifyreturns 200 whenevermatchPaidRoutereturns nil. Traefik ForwardAuth treats 200 as "allow". So a misconfigured Middleware on a paid route, or a code bug where the route was supposed to match but didn't, silently makes the route FREE — revenue loss with no signal.Before
After
What changed
Verifier.paidPrefixes(atomic.Pointer to []string) — derived fromcfg.Routespatterns on eachload()Verifier.load()computes prefixes (sorted by length descending)Verifier.isUnderPaidPrefix(uri)— strings.HasPrefix lookupHandleVerify— when matchRoute returns nil, check prefix; fail-closed (403) if matched, 200 if notpatternToPrefix(pattern)helper — strips*suffix, keeps trailing slash so/services/foo/doesn't false-match/services/foobar/patternToPrefix, the fail-closed path, the free-route unchanged-behavior path, and the prefix-boundary caseComplementary to PR #519
Together they cover the "rule should match but doesn't" gap.
What this does NOT catch
/services/*are still free. The prefix tracker is derived from what's IN the route table, not what theoretically COULD be paid.Test plan
go build ./...cleango test ./internal/x402/...green (no existing tests needed adjustment — none were passing a URI under a tracked paid prefix)/services/foobar/xdoes NOT match/services/foo/*prefix)patternToPrefixunit test covers/foo/*, exact patterns (returns ""),/*, emptyStacks on
PR #513 /
feat/x402-marketplace-metrics. Complements PR #519. Rebase onto main after both merge.