Skip to content

fix(api): limit failed password logins per email - #3759

Open
Aryainguz wants to merge 3 commits into
ClickHouse:mainfrom
Aryainguz:aryainguz/login-rate-limit
Open

Aryainguz wants to merge 3 commits into
ClickHouse:mainfrom
Aryainguz:aryainguz/login-rate-limit

Conversation

@Aryainguz

@Aryainguz Aryainguz commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Addresses the second half of #2775. POST /login/password had no rate limit, so password guessing was unlimited. #2781 fixed the first half.

  • Failed logins are limited to 10 per email every 15 minutes. Only rejected credentials count, not successful logins or server errors (e.g. the database being down).
  • Once limited, the login page shows "Too many failed login attempts…". JSON clients (the CLI) get a 429.
  • The limit is keyed on the email, not the IP. Behind the app's /api proxy, req.ip comes from X-Forwarded-For, which the client can set.

Known trade-offs

  • Lockout: anyone who knows an email can keep that account locked out of password login. Letting the right password through would let guessing continue, and there is no trusted client IP behind the /api proxy to key on. Restarting the API clears the counters.
  • Password spraying: each email gets its own 10 tries.
  • Per instance: the store is in memory, like the /api/v2 and /mcp limiters.
  • Hard-coded limits: like the MCP limiter. @pulpdrew happy to make them configurable.

Screenshots or video

Before After
image image

/login?err=tooManyAttempts

How to reproduce

Starting point: yarn dev. Set API to the API URL it prints. Any email works, no user needed.

On main:

  1. for i in $(seq 11); do curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' -X POST $API/login/password -d 'email=a@example.com&password=wrong'; done
  2. All 11 attempts redirect to /login?err=authFail.

On this branch:

  1. Run the same loop. Attempts 1–10 redirect to /login?err=authFail, and the 11th to /login?err=tooManyAttempts.
  2. make dev-int FILE=login.int runs the new integration tests.

References

POST /login/password accepted unlimited password attempts. Failed attempts
are now limited to 10 per email every 15 minutes. Successful logins don't
count. The browser form is redirected to /login?err=tooManyAttempts and JSON
clients get a 429.

Addresses the second half of ClickHouse#2775.
@Aryainguz
Aryainguz requested a review from a team as a code owner November 21, 2026 23:24
@changeset-bot

changeset-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1b165bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@hyperdx/api Patch
@hyperdx/app Patch
@hyperdx/otel-collector Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Nov 21, 2026

Copy link
Copy Markdown

@Aryainguz is attempting to deploy a commit to the HyperDX Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

PR Review

If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:

  • 🔴 P0/P1 in code this PR adds or changes: fix before asking for review.
  • 🟡 P2 in code this PR changes: your call. Fix if small, otherwise reply.
  • 🔵 P3 is collapsed at the bottom: nobody needs to act on it before merge.
  • Anything about surrounding code, or asking you to widen the change (hoist a helper,
    dedupe with another file, fix other call sites): never fix here, whatever the
    priority. Reply with one sentence; if it is P0, say so plainly so a human sees it.

One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.

1 finding(s): 🔴 1 P0/P1 · 🟡 0 P2 · 🔵 0 P3

Already reported on an earlier push — no new comments. 1 unchanged from an earlier push (already inline above).


Priority comes from a separate grading pass over the finished review. P3 is folded here instead of posted inline. No finding blocks merge automatically; a maintainer decides.

@Aryainguz

Aryainguz commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

PR Review

If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:

  • 🔴 P0/P1 in code this PR adds or changes: fix before asking for review.
  • 🟡 P2 in code this PR changes: your call. Fix if small, otherwise reply.
  • 🔵 P3 is collapsed at the bottom: nobody needs to act on it before merge.
  • Anything about surrounding code, or asking you to widen the change (hoist a helper,
    dedupe with another file, fix other call sites): never fix here, whatever the
    priority. Reply with one sentence; if it is P0, say so plainly so a human sees it.

One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.

3 finding(s): 🔴 1 P0/P1 · 🟡 2 P2 · 🔵 0 P3

None could be anchored to changed lines; they are listed below.

Findings outside the changed lines

  • 🔴 P1 packages/api/src/routers/api/root.ts:151 — Anyone who knows a user's email can lock that user out of password login, with no IP or credential factor → The limiter key is only the normalized email, and once a key goes over the limit the handler refuses the correct password too (the test at login.int.test.ts:54 checks this). That means an unauthenticated client can send 10 wrong passwords every 15 minutes and keep any account locked out indefinitely, including the team admin's. An admin in a password-only setup then has no way back in. Suggested fix: keep the hard block for (email, client IP), and use a separate, much higher per-email limit (or a delay) as the backstop against distributed guessing. Alternatively, make the limits configurable as the PR description offers, so operators can tune this. At minimum, document the lockout trade-off.
  • 🟡 P2 packages/api/src/routers/api/root.ts:157 — The CLI shows the raw code 'tooManyAttempts' to the user instead of a readable message → packages/cli/src/api/client.ts:163 builds its message from json.message || json.error, so the user sees Login failed (HTTP 429): tooManyAttempts. Add a message field to the 429 body, e.g. { error: 'tooManyAttempts', message: 'Too many failed login attempts. Please wait a few minutes and try again.' }.
  • 🟡 P2 packages/api/src/routers/api/__tests__/login.int.test.ts:103 — The JSON 429 test only passes if an earlier test already blocked MOCK_USER.email → This test sends one wrong password and expects a 429. That only works because the test at line 54 has already used up MOCK_USER.email's limit. Run on its own, or after the tests are reordered, the request gets a 303 authFail and the test fails. Make it self-contained: use its own email, send LOGIN_MAX_FAILED_ATTEMPTS failed JSON attempts first, then assert that the next one gets the 429 and JSON body.

Priority comes from a separate grading pass over the finished review. P3 is folded here instead of posted inline. No finding blocks merge automatically; a maintainer decides.

P1 here is the trade-off.

An (email, IP) key can be bypassed here: req.ip comes from X-Forwarded-For, which the client sets, and the default docker-compose.yml publishes the API port, so the app proxy can be skipped too. The test "limits an email sent in the query string" sends a new X-Forwarded-For on every attempt; the email key still blocks it, but an IP key wouldn't. A higher per-email backstop would allow the same lockout, just after more attempts, while giving more guesses. A temporary lockout seemed better than unlimited guessing.

Better options would be a trusted client IP (proxy settings plus not exposing the API port), or a CAPTCHA or email unlock link. @pulpdrew happy to make the limits configurable or take on any of these as a follow-up, let me know what you think.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Deep Review

Scope: POST /login/password gains a per-email rate limit (10 failed attempts / 15 min) via express-rate-limit, with a new inline error middleware that marks rejected credentials so only they count. CLI/JSON clients get a 429; browsers redirect to /login?err=tooManyAttempts (new AuthPage message). Base ef81fe6d.

The middleware ordering and skipSuccessfulRequests/requestWasSuccessful timing were traced end-to-end and are correct: rejected credentials are counted, successful logins and server errors (DB down) are not, and the 10-then-block off-by-one matches the tests. Both prior P2 comments (CLI readable message, self-contained JSON test) are fully addressed on this branch.

🔴 P0/P1 — must fix

  • packages/api/src/routers/api/root.ts:45 — The limiter keys solely on the normalized email and blocks even the correct password once over the threshold, so any unauthenticated party who knows a target email can send 10 wrong passwords per 15-minute window and keep that account — including an admin in a password-only deployment — locked out of password login indefinitely.
    • Fix: Exempt requests that present valid credentials from the block and scope the counter to a trustworthy client identifier in addition to email, or substitute a non-lockout control such as backoff/CAPTCHA; if shipping as-is, a maintainer must consciously accept the lockout trade-off and the mitigation can be a tracked follow-up.
    • security, adversarial, previous-comments, correctness
🔵 P3 nitpicks (4)
  • packages/api/src/routers/api/root.ts:38 — The default in-memory store is per-process, so behind N load-balanced API replicas the effective limit is 10×N failed attempts per window and round-robin routing spreads guesses across buckets, weakening the stated 10/15-min guarantee; the explanatory comment does not mention this.
    • Fix: Note the per-instance semantics in the comment, or back the limiter with a shared store (e.g. Redis) if cluster-wide enforcement is required.
    • adversarial, reliability, security, correctness
  • packages/api/src/routers/api/root.ts:134 — loginRejected is set from the generic err.name === 'AuthenticationError', which passport also raises for passwordAuthNotAllowed and missing credentials, so a user on a password-disabled team (or repeatedly submitting an empty form) stops seeing their accurate error and gets tooManyAttempts after 10 tries.
    • Fix: Count only genuine credential rejections, e.g. gate on the specific session message rather than the generic AuthenticationError name.
  • packages/api/src/routers/api/root.ts:54 — The user-facing copy Too many failed login attempts… is duplicated in the API 429 body and AuthPage.tsx, so the two can drift independently.
    • Fix: Accept as low-risk cross-package duplication or centralize the login error strings.
  • packages/api/src/routers/api/root.ts:39 — LOGIN_MAX_FAILED_ATTEMPTS and the inline 15 * 60 * 1000 window are hard-coded while the external-api limiter reads its max from config.
    • Fix: Source the max and window from config/env and name the window constant for consistency and operability.

Reviewers (7): correctness, security, adversarial, testing, reliability, maintainability, previous-comments.

Testing gaps:

  • The blocks after too many failed attempts test shares MOCK_USER.email with the preceding success test, so its failed-count=0 precondition is supplied implicitly rather than being self-contained (login.int.test.ts:37).
  • No test renders the new AuthPage tooManyAttempts message branch (AuthPage.tsx:236).
  • Most failure-path loops assert res.headers.location but not res.status (303), so a 302-vs-303 redirect regression would go largely undetected.
  • No coverage for window reset after 15 min, multi-instance/shared-store behavior, or concurrent (non-sequential) attempts racing the increment/skip path.

Coverage notes: A P2 memory-pressure concern (one store entry per distinct attacker-supplied email key; root.ts:45) was suppressed below the confidence gate — it matches the existing v2/mcp limiter pattern and is not a regression. Password spraying (one password tried once across many emails) is an inherent limitation of per-email keying, noted as residual risk.

No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond

Comment thread packages/api/src/routers/api/root.ts Resolved
Comment thread packages/api/src/routers/api/root.ts Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant