Skip to content

fix(auth): consume account rate-limit only after signature verification (CD-04) - #4066

Merged
ety001 merged 1 commit into
nextfrom
fix/cd-04-login-account-bucket-after-verify
Oct 19, 2026
Merged

ety001 merged 1 commit into
nextfrom
fix/cd-04-login-account-bucket-after-verify

Conversation

@ety001

@ety001 ety001 commented Oct 19, 2026

Copy link
Copy Markdown
Member

Problem (audit finding CD-04, MEDIUM)

POST /api/auth/login consumed the per-account rate-limit bucket
(auth:login:acct, 10/min) keyed by the submitted username before
the chain account lookup, challenge validation, and posting-key signature
verification. Both the IP and account buckets are 10 req / 60 s, so an
attacker with their own valid challenge + CSRF token (both obtainable via
GET /api/auth/challenge) could submit well-formed requests with invalid
signatures naming the victim's username, exhaust the victim's account
bucket, and make the victim's real login attempts fail with 429 — a
targeted login lockout / DoS.

Check order — before

  1. IP rate limit (auth:login:ip)
  2. Body size cap (64 KB)
  3. Required-field validation
  4. CSRF double-submit gate
  5. Account rate limit keyed by submitted username ← consumed victim quota pre-verification
  6. getAccount RPC
  7. Posting authority checks (key_auths shape, eligible posting keys)
  8. Challenge matches session
  9. Signed data JSON parse + field match
  10. Timestamp freshness (±5 min)
  11. Signature verification
  12. Success: mint session / revoke old session / cookies / overseer

Check order — after

  1. IP rate limit (auth:login:ip) — unchanged, still the first check
  2. Body size cap (64 KB)
  3. Required-field validation
  4. CSRF double-submit gate
  5. getAccount RPC
  6. Posting authority checks
  7. Challenge matches session
  8. Signed data JSON parse + field match
  9. Timestamp freshness (±5 min)
  10. Signature verification
  11. Account rate limit keyed by username — only for VERIFIED logins ← moved
  12. Success: mint session / revoke old session / cookies / overseer

Design decision

Option (a) from the audit guidance: keep the IP check where it is and move
the per-account bucket consumption to after successful signature
verification
. The enforced property is: a request that cannot be
attributed to the account holder never consumes that account's quota
.
Once the posting-key signature verifies, the request is attributable, so
consuming the account bucket there is sound.

Why not a composite (IP, username) failure bucket (option b): forging a
signature is cryptographically infeasible, so per-account damping of
unverified attempts buys nothing the per-IP bucket does not already
provide; option (a) keeps a single account bucket and a smaller diff. The
account dimension still serves its purpose for attributable traffic — it
stops one verified credential being hammered (session churn) from
rotating IPs. RATE_LIMITS.authLoginAccount (key auth:login:acct,
10/min) is unchanged; only the point of consumption moved.

Residual risks (documented in code + docs):

  • An attacker can still burn their own per-IP quota (10/min/IP) and
    force a per-attempt getAccount RPC for well-formed CSRF-valid requests —
    that is exactly what the IP bucket exists to cap.
  • NAT/CGNAT users behind one shared egress IP collectively share the IP
    bucket (pre-existing fixed-window property, unchanged).
  • Redis remains optional; without it the limiter is a no-op (unchanged).

Test evidence

__tests__/api/auth/login.test.ts (27 tests, all passing) — new describe
block "account-bucket attribution (audit CD-04)" with an in-memory
fixed-window counting limiter standing in for the Redis Lua script:

  • Attack simulation: 15 well-formed, CSRF-valid, invalid-signature
    requests naming the victim across 3 rotating IPs → all 401, the victim's
    account bucket is never incremented, and the victim's subsequent valid
    login from her own IP returns 200 (account bucket count == 1).
  • Post-verification per-account limit: 10 verified logins from
    rotating IPs succeed; the 11th is denied 429 + Retry-After: 60 and
    mints no session.
  • Unverified paths never touch the account quota: malformed body
    (400), missing CSRF header (403), challenge mismatch (400), unknown
    account (404), non-posting key (401), invalid signature (401) — zero
    account-bucket increments.
  • Per-IP limit still enforced upfront: unchanged 429 + Retry-After
    test when the IP bucket denies.
  • Updated ordering test asserts the account check runs after
    verifySignature (via invocationCallOrder).

Full suite: pnpm test → 85 files / 1066 tests passed. pnpm lint →
no new warnings or errors (pre-existing baseline: 7 warnings + 1
pre-existing error in __tests__/proxy.test.ts:383, untouched).

Docs

docs/CONFIGURATION.md rate-limit table now reads "IP (all attempts) and
account (verified logins only)" plus a paragraph explaining the
attribution rule and residual risk; lib/cache/rate-limit.ts registry
comments updated to match.

…on (CD-04)

The login route consumed the per-account bucket keyed by the submitted
username before the chain lookup and posting-key signature verification,
so an attacker with their own valid challenge + CSRF token could submit
invalid signatures naming a victim and 429-lock the victim's real logins
(targeted login DoS, audit CD-04). The account bucket now runs after the
signature verifies: unverified attempts only burn the sender's per-IP
bucket, while the account dimension keeps rate-limiting verified logins
(session churn from rotating IPs). Residual risk: an attacker can still
exhaust their own IP quota and trigger per-attempt RPC work, which is
exactly what the IP bucket is for.
@ety001
ety001 merged commit 8b95af2 into next Oct 19, 2026
0 of 2 checks passed
@ety001
ety001 deleted the fix/cd-04-login-account-bucket-after-verify branch October 19, 2026 15:40
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