fix(rate-limit): key IPv6 rate buckets by /64 instead of /128 (#205) - #237
fix(rate-limit): key IPv6 rate buckets by /64 instead of /128 (#205)#237dcccrypto wants to merge 1 commit into
Conversation
normalizeIp() only unwrapped ::ffff:-mapped addresses, so real IPv6 clients
were bucketed on the full 128-bit address. A host controlling a /64 — the
standard cloud and residential allocation — could rotate source addresses to
mint a fresh quota per request:
4 addresses inside one /64 -> OLD: 4 buckets = 400 req/min
NEW: 1 bucket = 100 req/min
Adds src/utils/ip-key.ts with a shared rateLimitKey() and uses it for the
read: and write: buckets. `ip` itself is untouched, so logs and the
could-not-determine-IP path keep full address fidelity. IPv4 behaviour is
unchanged, including ::ffff: unwrapping.
No new dependency. #205 suggested a vetted IP library, but adding one means
regenerating pnpm-lock.yaml — the exact file broken by #232, which pins
@percolatorct/sdk to a local filesystem path. Instead the hand-written
expansion is cross-checked in tests against Node's built-in net.BlockList:
BlockList masks host bits itself, so addSubnet(a,64)+check(b) is an
independent answer to "same /64?" that never consults our implementation.
361 address pairs, 0 mismatches.
Also covers compressed forms, embedded IPv4 tails, zone ids, and unparseable
input (which falls back to a per-value bucket — the pre-#205 behaviour —
because collapsing malformed input into one shared key would let it throttle
unrelated clients).
Suite 255 -> 263 passed, no new failures. tsc: 26 pre-existing errors before
and after, none in the files touched here. The one failing file
(tests/sdk-smoke.test.ts) fails identically on clean origin/main.
Scope: rate limiter only. See the PR for why the WS per-IP counters and the
IP blocklist are follow-ups rather than part of this change.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Warning Review limit reached
Next review available in: 5 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Action performedReview finished.
|
Addresses the rate limiter portion of #205. WS counters and the IP blocklist are deliberately left as follow-ups — reasoning at the bottom.
The bug
normalizeIp()only unwrapped::ffff:-mapped addresses, so real IPv6 clients were bucketed on the full 128-bit address. A host controlling a/64— the standard cloud and residential allocation — rotates source addresses and mints a fresh quota per request.Measured directly against the pre-fix implementation:
This is the enabler for the expensive endpoints
/candles,/chartand WSsubscribestorms.The fix
New
src/utils/ip-key.tsexportingrateLimitKey(), used for theread:andwrite:buckets.ipitself is untouched, so logs and the could-not-determine-IP path keep full address fidelity. IPv4 behaviour is unchanged, including::ffff:unwrapping.No new dependency — and why
#205 recommends a vetted IP library rather than a hand-rolled parser. That is good advice I chose not to take here, for a concrete reason: adding a dependency means regenerating
pnpm-lock.yaml— the exact file broken by #232, which pins@percolatorct/sdkto a local filesystem path. Regenerating it on a developer machine re-bakes that path and would deepen the blocker my PR #233 is trying to clear.Instead the hand-written expansion is cross-checked against Node's built-in
net.BlockList.BlockList.addSubnet(a, 64)masks host bits itself, socheck(b)is an independent answer to "areaandbin the same /64?" that never consults our implementation:That covers compressed forms (
2001:db8::1), fully expanded forms, embedded IPv4 tails (2001:db8::1.2.3.4), zone ids (fe80::2%eth0),::, and::1.If a reviewer would still rather take the library, I am happy to swap it in once #233 lands and the lockfile is safe to regenerate.
How to test
tests/utils/ip-key.test.ts— 8 tests:net.BlockListon every pair::ffff:1.2.3.41.2.3.4fe80::2%eth0≡fe80::2Unparseable input keeps a per-value bucket (the pre-#205 behaviour) rather than collapsing into one shared key — otherwise a malformed value could throttle unrelated clients.
origin/maintscerrorsThe one failing file (
tests/sdk-smoke.test.ts) fails identically on cleanorigin/main. CI will also still be red at install until #233 lands.Why WS counters and the blocklist are NOT here
#205 says all three
normalizeIpcopies "must be fixed together". I looked at doing that and think it should be sequenced instead:The WS per-IP counters are 9 call sites (
ws.ts:654–1144) split across two variables (clientIpat upgrade,client.ipafterwards), with increment/decrement pairs spanning both. Changing them piecemeal causes permanent counter drift — a decrement under a different key than its increment leaks the slot forever. That needs to be one atomic change (most likely aclient.ipKeyfield), not a find-and-replace bolted onto this PR.ws.tsis already modified by my unreviewed fix(ws): fail closed when WS_AUTH_SECRET is unset (#212) #236. Landing a second 9-site change to the same file before that is reviewed just creates conflicts for the reviewer.The blocklist is a different question, not a bucketing one.
ip-blocklist.tsusesnormalizeIpfor exact matching, so switching it to a /64 key would silently widen every configured block from one address to an entire allocation. That is IPv6 addresses cannot be blocklisted or auth-banned — no fail-closed behaviour #208's call to make (it asks for either real IPv6 CIDR support or fail-closed), and it is a policy decision rather than a key change.net.BlockList— already proven correct here as the test oracle — is a strong candidate for implementing it with no dependency either.rateLimitKey()is exported and ready for both follow-ups.🤖 Generated with Claude Code