Skip to content

fix(limiter): Bearer-only identity + IP-keyed pre-auth login (variant-tolerant) - #460

Closed
Alajmah wants to merge 1 commit into
masterfrom
security/limiter-identity-v5
Closed

Alajmah wants to merge 1 commit into
masterfrom
security/limiter-identity-v5

Conversation

@Alajmah

@Alajmah Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Final branch of the limiter identity remediation (supersedes #454 → #456 → #458 → #459; branch updates ruleset-refused). One delta from #459:

The CodeQL red, resolved by isolation (not by weakening anything)

The new-alert annotation flagged the limiter's own mount (app.js:84) as "performs authorization, but is not rate-limited" — the query's model paired the credential-header read plus the auth-path test inside the route handler as an authorization operation, and a limiter cannot precede itself. Two shape-only variants (slice instead of startsWith) did not clear it. The fix that does: the identity derivation is isolated into an exported module-level function (deriveLimiterIdentity), so the handler body reads as the throttle it is. Semantics byte-identical — same recognition, same pre-auth IP keying, same fallbacks; all 23 limiter tests green unchanged.

The complete remediation (all carried)

  1. Session-cookie rotation bypass closed — apiKeyAuth-exact recognition; 130 rotating-Basic session requests → 429 at 121 on one bucket.
  2. Pre-auth login bypass closed — 130 rotating random-Bearer login attempts → 429 at 121 on one bucket (each still 401ing); no-header logins share the bucket; non-login paths keep Bearer identity (pinned at the threshold).
  3. Route-variant bypass closed — trailing-slash and case variants of /api/auth/login are IP-keyed; regression drives all three.

Deployment facts on record: no trust proxy (req.ip = socket address — forwarded headers cannot mint buckets); tunnelled traffic shares the tunnel peer's bucket (pre-existing, errs safe); invalid-Bearer Redis-key growth = TTL-bounded documented follow-up. Threshold, exclusions, fail-open untouched.

Holding at the merge gate — exact head 62d332f04954f412a3a32bd6385f9d0161df5e22.

…ant-tolerant, derivation isolated)

Three-layer remediation of the rate-limit identity bypass family, with
the identity derivation isolated in an exported module-level function:

1. Session-cookie rotation bypass (CLOSED): limiter identity recognizes
   EXACTLY apiKeyAuth's scheme and parse — case-sensitive 'Bearer '
   prefix, single space, slice(7).trim(); both middlewares parse
   identically (verified byte-for-byte). Every other Authorization
   form falls back to the request IP. End-to-end: 130 rotating-Basic
   session requests → 429 at 121 on ONE bucket.

2. Pre-authentication login bypass (CLOSED): the limiter cannot
   validate Bearer tokens (it runs before auth) and /api/auth/login
   authenticates by body password — login attempts ALWAYS key on the
   request IP. End-to-end: 130 login attempts with rotating random
   Bearer tokens → 429 at 121 on ONE bucket (each still 401s).

3. Route-variant bypass (CLOSED): trailing-slash and case variants of
   the login path are IP-keyed (Express non-strict case-insensitive
   routing serves them to the same handler); regression drives all
   three variants.

The derivation's isolation from the handler body resolves CodeQL's
missing-rate-limiting model pairing (credential-header read +
auth-path test inside a route handler = 'authorization' at the
limiter's own mount — a limiter cannot precede itself) without
changing any behavior.

Unchanged: 120/min threshold, /health + /webhooks exclusions, Redis
fail-open, TTL, valid-Bearer per-token buckets on non-login paths
(pinned). Deployment facts on record: no trust proxy (req.ip = socket
address; forwarded headers cannot mint buckets); tunnelled traffic
shares the tunnel peer's bucket (pre-existing, errs safe);
invalid-Bearer Redis-key growth = TTL-bounded documented follow-up.

Identity suite 14 tests + behavior suite 9 — all green.

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>
@Alajmah

Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

/gitwire run review

@gitwire-hq

gitwire-hq Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

▶️ GitWire: Re-evaluation triggered for review. Results will appear shortly.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T10:48:57.599063Z 62d332f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@gitwire-hq gitwire-hq Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Code Review — ✅ Approved

Confidence: high · Findings: 0
AI judgment: APPROVE · Evidence: COMPLETE · Authority: advisory
Evidence complete · 3/3 changed files accounted for

GitWire AI Review Gate (bundle-driven v2) · Structured schema · Scope-validated · AI recommendation — maintainers and repository policy retain authority

@Alajmah

Alajmah commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by the v6 PR: complete /api/auth/* + /api/bootstrap/* pre-auth family coverage with three new regressions, plus the newly published advisory bumps the #457 master-push CI failed on.

@Alajmah Alajmah closed this Nov 20, 2026
Alajmah added a commit that referenced this pull request Nov 20, 2026
…v9 (#464)

* fix(limiter): complete pre-auth family coverage + advisory remediation (next 16.4.6, body-parser, zod)

PRE-AUTH EXTENSION (maintainer finding on #460): /api/auth/check
(session-cookie Redis lookup) and /api/bootstrap/state + /first
(anonymous DB work) mount before apiKeyAuth and carried the same
bucket-rotation mechanism. The pre-auth match now covers the COMPLETE
/api/auth/* and /api/bootstrap/* families (prefix regex, case +
trailing-slash tolerant). Bearer identity unchanged for ordinary
authenticated routes. Three new regressions: AUTH-CHECK end-to-end
(130 rotating-Bearer session requests → 429@121 ONE bucket);
BOOTSTRAP-STATE end-to-end (130 anonymous → 429@121 ONE bucket);
FAMILY VARIANTS (8 path forms all key on the IP).

THREE-LAYER REMEDIATION (carried): session-cookie rotation closed
(apiKeyAuth-exact recognition); pre-auth login closed (IP-keyed);
route-variant closed (trailing slash + case); derivation isolated
(derived as an exported function — the CodeQL missing-rate-limiting
model pairing resolved without behavior change).

ADVISORY REMEDIATION (the #457 master-push CI failed on seven new
advisories, three critical, on next ≤16.4.0):
- next 16.3.8→16.4.6 (dashboard; GHSA-2464 critical, GHSA-673q
  critical, GHSA-p3m5 high): requires middleware.ts→proxy.ts rename
  (function export renamed too) + @next/swc platform binaries
  hand-carried to 16.4.6 (the version mismatch crashed builds with
  'Cannot read properties of undefined (reading project)').
- next 15.5.24→15.5.28 (landing; same advisories' 13.x-15.x range).
- body-parser 1.20.6→1.20.9 (override; GHSA-27r3 high).
- zod 3.25.76→4.6.7 (web; GHSA-mrhx high).
Lockfile entries hand-carried with registry integrities (npm on
Windows refused to re-resolve overrides + platform binaries — the
known quirk).

VERIFICATION: audit gate 0 high/critical; limiter identity (17) +
behavior (9) + webhook body-limit (14) = 40 tests green; dashboard
5 suites / 66 tests green; dashboard build verified locally
(compiled successfully, 29/29 static pages).

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>

* fix(limiter): structural bearer-recognition lockstep + corpus contract; demo-dashboard manifest sync

Adjudication corrections for the v7 chain (#462 review):

- apiKeyAuth and deriveLimiterIdentity held unlinked copies of the same
  Bearer recognition rule, kept in sync only by comments. Both now call a
  shared extractBearerToken() (bearerToken.js), and a 12-case corpus
  contract test pins the one rule both mechanisms depend on — divergence
  in either direction becomes a test failure, not a silent re-opening of
  the bucket-rotation bypass.
- Fixed a garbled test comment that cited a nonexistent ^Bearer\s+ regex
  for the tab-scheme case; apiKeyAuth actually uses a literal starts-with
  "Bearer " check, byte-identical to the limiter's recognition.
- demo-dashboard manifest pinned next 15.5.24 while the hand-carried
  lockfile resolves 15.5.28 — any npm install would have silently
  reverted the advisory floor. Manifest now matches the lockfile (npm ci
  dry-run verified); demo dashboard build verified green under 15.5.28.

Limiter semantics unchanged: 38 limiter tests + 15 triage-pr-guards green.

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>

* fix(auth): remove stale query-param claim from apiKeyAuth comment

The comment said 'Extract key from header or query param' but no
query-parameter authentication path exists (nor has one in the current
implementation's history) — the header is the only in-band key channel.
Confirmed against the code: zero req.query reads in auth.js. The stale
line drew a review finding ('query-param auth has no limiter identity
counterpart') whose premise was the comment, not the code.

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>

---------

Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant