Repository navigation
fix(limiter): pre-auth family coverage + advisory remediation + structural bearer lockstep (v8) #463
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
fix(limiter): pre-auth family coverage + advisory remediation + structural bearer lockstep (v8) #463
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import { config } from "../../config/index.js"; | ||
| import { logger } from "../lib/logger.js"; | ||
| import { redis } from "../lib/queue.js"; | ||
| import { extractBearerToken } from "./bearerToken.js"; | ||
|
|
||
| const SESSION_PREFIX = "gitwire:session:"; | ||
|
|
||
|
|
@@ -71,13 +72,11 @@ export async function apiKeyAuth(req, res, next) { | |
| return next(); | ||
| } | ||
|
|
||
| // Extract key from header or query param | ||
| let providedKey = null; | ||
|
|
||
| const authHeader = req.headers.authorization; | ||
| if (authHeader?.startsWith("Bearer ")) { | ||
| providedKey = authHeader.slice(7).trim(); | ||
| } | ||
| // Extract key from header or query param. | ||
| // Recognition lives in the shared bearerToken.js helper so apiKeyAuth and | ||
| // the rate limiter cannot drift apart (the limiter derives its identity | ||
|
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] Lockstep guarantee overstated: query-param API-key auth has no limiter identity counterpart The new comment in auth.js claims the limiter 'derives its identity from the exact same forms auth accepts', and the corpus test frames bearerToken.js as the single rule both mechanisms depend on. However, apiKeyAuth still extracts a key from a query parameter as well (the retained 'Extract key from header or query param' comment; only the header branch moved into the shared helper), while deriveLimiterIdentity considers only the Authorization header. Consequence: a caller authenticating via query-param key is throttled per-IP, so distinct keys behind one shared IP share a single bucket and one caller at that IP can exhaust another tenant's quota. This is pre-existing behavior (the old limiter also ignored query params), not a regression introduced here, but the newly added 'exact same forms' claim is inaccurate and could mislead future security audits. Either mirror the query-param key in the limiter identity or soften the comment to 'the same Authorization header forms'. |
||
| // from the exact same forms auth accepts). | ||
| const providedKey = extractBearerToken(req.headers.authorization) || null; | ||
|
|
||
| // Check session cookie if no Bearer token | ||
| if (!providedKey) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // src/middleware/bearerToken.js | ||
| // Bearer-token extraction shared by apiKeyAuth (auth.js) and the rate limiter | ||
| // (rateLimiter.js). Both MUST recognize exactly the same Authorization forms: | ||
| // a case-sensitive "Bearer " scheme prefix with a single literal space. A | ||
| // looser recognizer on the limiter side re-opens the bucket-rotation bypass | ||
| // (rotating non-Bearer headers to mint fresh buckets); a stricter one would | ||
| // throttle callers apiKeyAuth accepts. Before this module existed the two | ||
| // implementations were unlinked copies kept in sync by comments — the corpus | ||
| // contract test in rate-limiter-identity.test.js now pins this one function | ||
| // for both callers. | ||
|
|
||
| /** | ||
| * Extract the Bearer token from an Authorization header value. | ||
| * | ||
| * Recognition is apiKeyAuth-EXACT: case-sensitive "Bearer " prefix (single | ||
| * literal space), then slice(7).trim(). Returns "" for every other form — | ||
| * wrong case ("bearer x"), tab separator ("Bearer\tx"), bare "Bearer", | ||
| * whitespace-only token, or a missing/non-string header. | ||
| * | ||
| * @param {string | undefined} authHeader | ||
| * @returns {string} the token, or "" when the header is not a well-formed | ||
| * Bearer credential. | ||
| */ | ||
| export function extractBearerToken(authHeader) { | ||
| return typeof authHeader === "string" && authHeader.slice(0, 7) === "Bearer " | ||
| ? authHeader.slice(7).trim() | ||
| : ""; | ||
| } |
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.
[LOW] Major zod upgrade (3 to 4) bundled into a security patch without any migration changes
packages/web/package.json jumps zod from ^3.22.0 to ^4.6.6. Zod 4 ships breaking API changes (error-customization signatures, z.record arity requirements, removed/deprecated helpers, changed error shape). This PR touches no validation source code, so zod-4 compatibility is unverified from the diff; if the web package uses any affected API, request-time validation on auth/session/bootstrap routes could break after deploy. The stated advisory remediation (next bumps, body-parser pin) does not obviously require a zod major bump. Suggest confirming the web package is zod-4 compatible via typecheck and full test run, or splitting this bump into its own PR so a runtime regression cannot ride in under a security fix.