feat(rate-limit): key by authKey hash, not just IP#65
Merged
Conversation
express-rate-limit defaults to keying by IP, which has two
problems against an authenticated API:
1. Shared-IP false positives. Mobile-carrier NAT, corporate
egress, a household behind one router, k8s pods sharing a
SNAT — all of these put legitimate clients on a shared
budget. A noisy neighbor poisons everyone else.
2. Brute-force pivot. An attacker rotating source IPs (vps
pool, residential proxy, tor) stretches a per-IP budget
by however many addresses they can spin up.
The new key generator looks at the `authKey` header:
- present → key by `k:<sha256-prefix-of-authKey>`
- absent → key by `ip:<remote_address>` (the brute-force path)
Authenticated clients get their own per-key budget regardless
of egress IP. Anonymous requests (someone fishing for a valid
key) still get per-IP keying — the right granularity there.
Extracted into app/middleware/rate-limit-key.js so the keying
logic is unit-testable. server.js just imports it.
Tests: 7 cases in tests/unit/rate-limit-key.test.js — verifies
deterministic mapping, separation between distinct authKeys, IP
fallback, and that the raw authKey never appears in the
returned key (in case the rate-limiter store becomes redis-
backed later).
Suite: 33 files / 241 passing + 4 integration skipped (was 32 / 234).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
express-rate-limit's default IP keying causes shared-IP false positives (mobile carrier NAT, corp egress) and is easy to dodge by rotating source IPs. Switching to:
k:<sha256-prefix-of-authKey>(deterministic, separates budgets per credential)Extracted into
app/middleware/rate-limit-key.jsfor testability.Test plan
Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/