Repository navigation
fix(limiter): Bearer-only identity + IP-keyed pre-auth login throttle #456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,29 @@ export function rateLimiter(req, res, next) { | |
| return next(); | ||
| } | ||
|
|
||
| // Identity: prefer API key, fall back to IP | ||
| const identity = req.headers.authorization?.slice(7)?.trim() | ||
| || req.ip | ||
| || "unknown"; | ||
| // Identity: a properly formed Bearer token, else the request IP. The | ||
| // recognition is EXACTLY apiKeyAuth's ("Bearer " prefix, case-sensitive, | ||
| // single space): anything looser re-opens the rotation bypass (e.g. | ||
| // "Bearer\t<rotating>" or "bearer <rotating>" are NOT Bearer for auth, | ||
| // so they must not mint limiter buckets either). Reproduced end-to-end | ||
| // in rate-limiter-behavior.test.js; remediation pinned in | ||
| // rate-limiter-identity.test.js. | ||
| // | ||
| // PRE-AUTHENTICATION ENDPOINTS are the exception: the limiter runs before | ||
| // authentication, so a syntactically valid Bearer token has NOT been | ||
| // validated at this point. On endpoints that authenticate by OTHER means | ||
| // (the login route validates the request-body password), an | ||
| // unauthenticated caller can rotate random Bearer values to mint a fresh | ||
| // bucket per password-guess. Those endpoints always key on the request | ||
| // IP — an unverified token must never become their throttle identity. | ||
| const PRE_AUTH_IP_IDENTITY_PATHS = ["/api/auth/login"]; | ||
| const useIpIdentity = PRE_AUTH_IP_IDENTITY_PATHS.includes(req.path); | ||
|
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an attacker posts to Useful? React with 👍 / 👎. |
||
|
|
||
| const authHeader = req.headers.authorization; | ||
| const bearerToken = !useIpIdentity && typeof authHeader === "string" && authHeader.startsWith("Bearer ") | ||
| ? authHeader.slice(7).trim() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [HIGH] .trim() on the sliced Bearer token breaks the claimed byte-for-byte parity with apiKeyAuth, leaving a whitespace-variant rotation bypass The diff's own invariant is 'recognition is EXACTLY apiKeyAuth's ... anything looser re-opens the rotation bypass', but the extraction applies .trim() to authHeader.slice(7). For 'Bearer ' (double space) or values with interior padding, the limiter keys on the trimmed token ('') while a strict single-space-prefix extraction in auth yields ' ' (invalid); under a /^Bearer\s+(.+)/ extraction the padded variants diverge differently but still diverge (e.g. trailing-tab). In both cases there exist whitespace variants where the limiter treats the value as a per-request-unique token identity but auth does not authenticate it: rotating 'Bearer x1', 'Bearer x2', ... against any non-login /api path mints a fresh bucket per request, and when a session cookie is present and auth falls back to session on a failed Bearer parse (the exact fallback the original Basic-header bypass exploited), the 130-requests-all-200 reproducer still succeeds. The whitespace cases are also unpinned by the new tests, which only use clean single-space tokens. Fix: extract the credential exactly as auth.js does (share one helper, no independent trim), and add identity tests pinning double-space and padded-token variants against the real apiKeyAuth. |
||
| : ""; | ||
| const identity = bearerToken || req.ip || "unknown"; | ||
|
|
||
| const key = `ratelimit:${identity}`; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| // tests/unit/rate-limiter-identity.test.js | ||
| // Production remediation for the identity bypass reproduced in | ||
| // rate-limiter-behavior.test.js: limiter identity now recognizes ONLY a | ||
| // properly formed Bearer scheme; every other Authorization form (and its | ||
| // absence) falls back to the request IP — so rotating non-Bearer headers can | ||
| // no longer mint fresh buckets for a session-authenticated caller. | ||
| // | ||
| // The behavior pins (threshold, exclusions, fail-open) live in the behavior | ||
| // suite against the same middleware; this suite proves the identity contract | ||
| // change end-to-end and in the middleware directly. | ||
|
|
||
| import { jest } from "@jest/globals"; | ||
|
|
||
| const redisState = { counts: new Map(), failWith: null }; | ||
|
|
||
| jest.unstable_mockModule("../../src/lib/queue.js", () => ({ | ||
| redis: { | ||
| incr: jest.fn(async (key) => { | ||
| if (redisState.failWith) throw redisState.failWith; | ||
| const next = (redisState.counts.get(key) ?? 0) + 1; | ||
| redisState.counts.set(key, next); | ||
| return next; | ||
| }), | ||
| pexpire: jest.fn(async () => 1), | ||
| get: jest.fn(async (key) => { | ||
| if (key.startsWith("gitwire:session:")) { | ||
| return key.endsWith(":valid-session") ? JSON.stringify({ principal: "session-user" }) : null; | ||
| } | ||
| return null; | ||
| }), | ||
| expire: jest.fn(async () => 1), | ||
| }, | ||
| })); | ||
| jest.unstable_mockModule("../../src/lib/logger.js", () => ({ | ||
| logger: { info: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn() }, | ||
| })); | ||
| jest.unstable_mockModule("../../config/index.js", () => ({ | ||
| config: { anthropic: { apiKey: "test" }, github: { appId: "1", privateKey: "k", clientId: "x", clientSecret: "x", webhookSecret: "s" }, server: { baseUrl: "x" } }, | ||
| })); | ||
|
|
||
| process.env.API_KEY = "unit-test-api-key"; | ||
|
|
||
| const { rateLimiter } = await import("../../src/middleware/rateLimiter.js"); | ||
| const { apiKeyAuth } = await import("../../src/middleware/auth.js"); | ||
|
|
||
| const express = (await import("express")).default; | ||
| const supertest = (await import("supertest")).default; | ||
|
|
||
| function buildApp() { | ||
| // Production mount order: limiter BEFORE authentication. | ||
| const app = express(); | ||
| app.use(rateLimiter); | ||
| app.use(apiKeyAuth); | ||
| app.get("/api/anything", (req, res) => res.json({ ok: true })); | ||
| return app; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| redisState.counts.clear(); | ||
| redisState.failWith = null; | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("REMEDIATION: rotating non-Bearer headers can no longer split buckets", () => { | ||
| it("THE REPRODUCER SCENARIO, FIXED: 130 requests from one session with rotating Basic headers hit 429 at request 121", async () => { | ||
| const app = buildApp(); | ||
| const sessionCookie = "gitwire-session=valid-session"; | ||
| let last = 200; | ||
| for (let i = 0; i < 130; i += 1) { | ||
| const res = await supertest(app) | ||
| .get("/api/anything") | ||
| .set("Authorization", `Basic rotating-value-${i}`) | ||
| .set("Cookie", sessionCookie); | ||
| if (i < 120) { | ||
| expect(res.status).toBe(200); | ||
| } | ||
| last = res.status; | ||
| if (res.status === 429) break; | ||
| } | ||
| expect(last).toBe(429); | ||
| // ONE shared bucket (the request IP), not 130. | ||
| expect(redisState.counts.size).toBe(1); | ||
| expect([...redisState.counts.values()][0]).toBe(121); | ||
| }); | ||
|
|
||
| it("header-absent session traffic shares the same IP bucket (unchanged semantics)", async () => { | ||
| const app = buildApp(); | ||
| const sessionCookie = "gitwire-session=valid-session"; | ||
| let saw429 = false; | ||
| for (let i = 0; i < 121; i += 1) { | ||
| const res = await supertest(app).get("/api/anything").set("Cookie", sessionCookie); | ||
| if (res.status === 429) { saw429 = true; break; } | ||
| expect(res.status).toBe(200); | ||
| } | ||
| expect(saw429).toBe(true); | ||
| expect(redisState.counts.size).toBe(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('PRE-AUTH FIX: rotating invalid Bearer tokens on /api/auth/login shares ONE IP bucket', () => { | ||
| // The limiter runs before authentication, so it cannot know a Bearer token | ||
| // is invalid. The login route authenticates by request-body password — an | ||
| // unauthenticated caller rotating syntactically-valid-but-random Bearer | ||
| // values would otherwise mint a fresh bucket per password guess. | ||
| function buildLoginApp() { | ||
| const app = express(); | ||
| app.use(rateLimiter); | ||
| app.use(express.json()); | ||
| // Minimal login route mirroring auth.js: 401 on wrong password. | ||
| app.post('/api/auth/login', (req, res) => res.status(401).json({ error: 'Invalid password' })); | ||
| return app; | ||
| } | ||
|
|
||
| it('END-TO-END: 130 login attempts with ROTATING random Bearer tokens hit 429 at request 121 on ONE shared bucket', async () => { | ||
| const app = buildLoginApp(); | ||
| let last = 200; | ||
| for (let i = 0; i < 130; i += 1) { | ||
| const res = await supertest(app) | ||
| .post('/api/auth/login') | ||
| .set('Authorization', "Bearer random-guess-token-" + i) | ||
| .send({ password: 'wrong-' + i }); | ||
| if (i < 120) { | ||
| expect(res.status).toBe(401); // still reaches the route and fails auth | ||
| } | ||
| last = res.status; | ||
| if (res.status === 429) break; | ||
| } | ||
| expect(last).toBe(429); | ||
| expect(redisState.counts.size).toBe(1); // ONE shared IP bucket | ||
| expect([...redisState.counts.values()][0]).toBe(121); | ||
| }); | ||
|
|
||
| it('login attempts WITHOUT Bearer headers share the same single bucket (unchanged semantics)', async () => { | ||
| const app = buildLoginApp(); | ||
| let saw429 = false; | ||
| for (let i = 0; i < 121; i += 1) { | ||
| const res = await supertest(app) | ||
| .post('/api/auth/login') | ||
| .send({ password: 'wrong' }); | ||
| if (res.status === 429) { saw429 = true; break; } | ||
| expect(res.status).toBe(401); | ||
| } | ||
| expect(saw429).toBe(true); | ||
| expect(redisState.counts.size).toBe(1); | ||
| }); | ||
|
|
||
| it('NON-login /api paths still derive identity from a well-formed Bearer token', async () => { | ||
| const app = buildLoginApp(); | ||
| app.get('/api/anything', (req, res) => res.json({ ok: true })); | ||
| const res = await supertest(app) | ||
| .get('/api/anything') | ||
| .set('Authorization', 'Bearer some-token'); | ||
| expect(res.status).toBe(200); | ||
| expect([...redisState.counts.keys()].join('|')).toContain('some-token'); | ||
| }); | ||
| }); | ||
|
|
||
| describe("identity derivation contract (middleware-level)", () => { | ||
| function runMiddleware({ headers = {}, ip } = {}) { | ||
| return new Promise((resolve) => { | ||
| const req = { path: "/api/anything", headers, ip, get: (h) => headers[h.toLowerCase()] }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [LOW] Test title contradicts its assertion: scheme is case-sensitive, not 'case-insensitive' The test 'a well-formed Bearer token IS the identity (case-insensitive scheme)' asserts the opposite of its title: 'bearer key-lower' (lowercase scheme) is expected to fall back to the IP bucket, i.e. the scheme recognition is case-sensitive, matching the middleware comment. The contradictory title will mislead future audits of the identity contract (exactly the kind of audit this PR's security reasoning depends on). Rename to something like 'a well-formed Bearer token IS the identity; a lowercase scheme is not Bearer and falls back to the IP'. |
||
| const res = { | ||
| statusCode: 200, | ||
| headers: {}, | ||
| setHeader(k, v) { this.headers[k] = v; }, | ||
| status(code) { this.statusCode = code; return this; }, | ||
| json(body) { this.body = body; resolve({ res, passed: false }); return this; }, | ||
| }; | ||
| rateLimiter(req, res, () => resolve({ res, passed: true })); | ||
| }); | ||
| } | ||
|
|
||
| it("a well-formed Bearer token IS the identity (case-insensitive scheme)", async () => { | ||
| await runMiddleware({ headers: { authorization: "Bearer key-one" }, ip: "10.0.0.1" }); | ||
| await runMiddleware({ headers: { authorization: "Bearer key-two" }, ip: "10.0.0.1" }); | ||
| // Two distinct buckets for two distinct tokens from one IP. | ||
| expect(redisState.counts.size).toBe(2); | ||
| expect([...redisState.counts.keys()].join("|")).toContain("key-one"); | ||
| expect([...redisState.counts.keys()].join("|")).toContain("key-two"); | ||
| // A LOWERCASE scheme is not Bearer for apiKeyAuth (starts-with "Bearer "), | ||
| // so it must not be Bearer for the limiter either — it falls to the IP. | ||
| redisState.counts.clear(); | ||
| await runMiddleware({ headers: { authorization: "bearer key-lower" }, ip: "10.0.0.2" }); | ||
| expect([...redisState.counts.keys()]).toEqual(["ratelimit:10.0.0.2"]); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["Basic value-1"], | ||
| ["basic value-2"], | ||
| ["Bearer"], // malformed: no token after scheme | ||
| ["Bearer "], // malformed: whitespace only | ||
| ["Bearer\tgap-token"], // malformed: tab not matched by \\s+? (regex ^Bearer\\s+ — tab IS \\s; assert below) | ||
| [""], // empty header value | ||
| ])("non-Bearer or malformed header %j falls back to the IP bucket", async (authorization) => { | ||
| redisState.counts.clear(); | ||
| const first = await runMiddleware({ headers: { authorization }, ip: "10.1.1.1" }); | ||
| expect(first.passed).toBe(true); | ||
| // Whatever the header said, the bucket is the IP. | ||
| expect([...redisState.counts.keys()]).toEqual(["ratelimit:10.1.1.1"]); | ||
| }); | ||
|
|
||
| it("Bearer-key separation still works at the threshold: key A saturates, key B unaffected, IP unaffected", async () => { | ||
| for (let i = 0; i < 120; i += 1) { | ||
| await runMiddleware({ headers: { authorization: "Bearer key-sat" }, ip: "10.2.2.2" }); | ||
| } | ||
| const saturated = await runMiddleware({ headers: { authorization: "Bearer key-sat" }, ip: "10.2.2.2" }); | ||
| expect(saturated.passed).toBe(false); | ||
| const otherKey = await runMiddleware({ headers: { authorization: "Bearer key-other" }, ip: "10.2.2.2" }); | ||
| expect(otherKey.passed).toBe(true); | ||
| const plainIp = await runMiddleware({ ip: "10.2.2.2" }); | ||
| expect(plainIp.passed).toBe(true); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[HIGH] Pre-auth path check is an exact req.path match — trailing slash or different case re-opens the login bucket-minting bypass
PRE_AUTH_IP_IDENTITY_PATHS.includes(req.path) is an exact string comparison, but Express routing is by default case-insensitive and trailing-slash tolerant (strict: false, caseSensitive: false). 'POST /api/auth/login/' and 'POST /API/AUTH/LOGIN' both reach the login route handler, yet their req.path values ('/api/auth/login/', '/API/AUTH/LOGIN') do not match the list, so useIpIdentity is false and the identity falls back to the (rotatable) Bearer token. An attacker rotating random 'Bearer ' values against the trailing-slash URL mints a fresh bucket per password guess — the exact bypass this PR fixes, one character away. The new tests only exercise the canonical path. Fix: normalize before comparing (e.g. req.path.replace(//+$/, '').toLowerCase()), or resolve the match with the same routing options the app mounts with; add tests for the trailing-slash and case variants. Also verify the list covers every pre-auth endpoint (register/refresh/password-reset if present) — a hardcoded exact-path list silently keys any future pre-auth route on unverified tokens.