Skip to content

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

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

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

Conversation

@Alajmah

@Alajmah Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Final branch of the limiter identity remediation (supersedes #454 → #456; branch updates ruleset-refused). Adds the review-round corrections on top of everything prior:

The two review HIGHs, one fixed and one refuted with proof

  • Trailing-slash/case variant (real, FIXED): Express's default non-strict, case-insensitive-to-route matching serves /api/auth/login/ and /API/AUTH/LOGIN to the same login handler — an exact-equality path list would re-open the Bearer-mint on the slashed variant. The pre-auth check is now a trailing-slash-and-case tolerant regex; the regression drives all three variants and asserts the bucket is the IP, never the token.
  • .trim() parity (refuted with the source): apiKeyAuth itself parses slice(7).trim() — the two middlewares' parses are byte-for-byte identical. A whitespace-variant token parses to the SAME identity in both, so no divergence and no rotation vector exists. (The test-title contradiction the review also flagged is fixed.)

Everything from #456 stands

  • Session-cookie rotation bypass closed (apiKeyAuth-exact recognition; 130 rotating-Basic session requests → 429 at 121 on one bucket).
  • Pre-auth login bypass closed (130 rotating random-Bearer login attempts → 429 at 121 on one bucket, each still 401ing at the route; no-header logins share the bucket; non-login paths keep Bearer identity).
  • Deployment facts on record: no trust proxy anywhere (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, and fail-open untouched.

Identity suite 14 + behavior suite 9 — all green locally. Holding at the merge gate — exact head 21c8073a973f19e6a66ceea31ed2640374b2a7ff.

…ant-tolerant)

Three-layer remediation of the rate-limit identity bypass family:

1. Session-cookie rotation bypass (CLOSED): limiter identity recognizes
   EXACTLY apiKeyAuth's scheme and parse ('Bearer ' prefix,
   case-sensitive 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): Express's default non-strict,
   case-insensitive-to-route matching serves '/api/auth/login/',
   '/API/AUTH/LOGIN' to the same handler — the pre-auth check is a
   trailing-slash-and-case tolerant regex so no variant re-opens the
   mint. Regression drives all three variants; the bucket is the IP.

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-09T09:53:03.352574Z 21c8073 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: 2
AI judgment: APPROVE · Evidence: COMPLETE · Authority: advisory
Evidence complete · 3/3 changed files accounted for

Other findings (2)

  • [low] Hardcoded pre-auth path regex silently re-opens the bucket-mint bypass for any future public endpoint
  • [low] 'Exactly apiKeyAuth recognition' parity is asserted in comments but never pinned; suite itself hedges on the scheme regex

🔮 Devil's Advocate (2 turns): 2 upheld


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

// the same handler; an exact-equality list would re-open the mint on
// '/api/auth/login/'.
const isPreAuthPath = (path) =>
typeof path === "string" && /^\/api\/auth\/login\/?$/i.test(path);

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.

[LOW] Hardcoded pre-auth path regex silently re-opens the bucket-mint bypass for any future public endpoint

The fix correctly special-cases /api/auth/login, but the mechanism is a string allowlist in the limiter that must be kept in lockstep with auth.js's set of unguarded routes. The bypass exists on ANY endpoint where an unauthenticated caller can reach real handler work while sending an arbitrary Bearer value (register, refresh, password-reset request, logout, etc.). If a new public route is added to auth.js and someone forgets to extend this regex, per-token bucket minting silently re-opens with no failing test to catch it, because the identity suite only pins the login path. Suggested hardening: export the public/pre-auth path list (or a shared matcher) from the auth module and have both apiKeyAuth's skip logic and the limiter consume the same source of truth, plus an identity-suite test that iterates that shared list so new entries are automatically covered.

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();

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.

[LOW] 'Exactly apiKeyAuth recognition' parity is asserted in comments but never pinned; suite itself hedges on the scheme regex

The whole security argument rests on the limiter's startsWith("Bearer ") matching apiKeyAuth's scheme recognition exactly, yet no test drives a token through the REAL apiKeyAuth to confirm agreement on the malformed variants. Worse, the identity suite's own it.each comment is self-contradictory: "Bearer\tgap-token" // malformed: tab not matched by \\s+? (regex ^Bearer\\s+ — tab IS \\s; assert below) — acknowledging that IF auth.js uses /^Bearer\s+/, tab-separated headers WOULD authenticate while the limiter IP-keys them, making the 'exactly apiKeyAuth' invariant comment in rateLimiter.js false. The failure direction is safe (under-keying to the shared IP bucket, no bypass), which is why this is not a bug, but the invariant is load-bearing and should be pinned: add one end-to-end case per malformed variant (lowercase scheme, tab separator, empty token) through the real apiKeyAuth asserting both the auth outcome and the limiter bucket key agree.

@Alajmah

Alajmah commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by the v4 PR: the CodeQL new-alert on the limiter mount was traced to the startsWith-on-credential-header shape; recognition now expressed as a byte-identical slice comparison.

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