Skip to content

Add a generic request limiter and limit password resets - #1546

Merged
AnthonyRonning merged 1 commit into
masterfrom
feat/generic-rate-limiter
Nov 10, 2026
Merged

AnthonyRonning merged 1 commit into
masterfrom
feat/generic-rate-limiter

Conversation

@AnthonyRonning

@AnthonyRonning AnthonyRonning commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What

Generalizes the inference rate limiter into one in-memory limiter that any inner route can use, and applies it to password reset for user and platform accounts.

Under Transport V2 the edge sees one encrypted endpoint and the backend has no trusted client address, so request limits have to run on the inner routes, after the request is decrypted. The existing limiter did that, but only for authenticated inference: one policy, keyed by the signed-in account.

The limiter (src/web/rate_limit.rs)

  • One Limiter type: admissions per rolling window, an optional fixed cooldown, a key capacity. Live keys are never evicted.
  • Each limit is a field of RateLimits with its policy written in Rust.
  • The key is the account a request acts on:
    • authenticated routes mount the enforce_user layer, which charges the verified caller;
    • unauthenticated routes have no caller identity, so the handler charges the account the request names, once resolved, with Limiter::check.
  • A rejection is an ordinary response, so Transport V2 seals it. The generic one is a 429 with Retry-After and x-opensecret-error-code: rate_limit_exceeded.
  • Keys are only ever identities the server resolved. A bucket per caller-chosen string would let one caller fill the table.

Inference admission is unchanged: same policy, same INFERENCE_RATE_LIMIT_* settings, same OpenAI-shaped error.

Password reset limits

The same two limits for /password-reset/* and /platform/password-reset/*:

Limit Policy Past the limit
Reset requests 5 per targeted account per rolling hour The usual generic answer; no request is created and no email is sent
Confirmation attempts 5 per reset request for its 24-hour life 429, even with the right code; the owner starts a new request

An attempt is charged to the request its secret belongs to. A submission whose secret matches no request of that account cannot complete anything and is refused without being counted, so only the party that started a request can spend its attempts.

Limits of this version

  • State is per process: it resets on restart and each replica has its own allowance. This is the in-memory first step discussed in Add database-backed limits for password-reset guesses and emails #1486, not the database-backed version.
  • Any caller can use up an account's reset requests for the hour. That delays the owner's reset email; it never changes a credential.
  • Every confirmation attempt is charged, including a correct one. That is safe here because a correct confirmation consumes its request. Recovery codes and seed-preserving password reset #1470 adds a route that checks the same proof without consuming it; that route must not spend this budget on a correct proof, so whichever of the two PRs lands second needs that adjustment and a rebase of confirm_password_reset.

Not in this PR

Login attempt limits, a longer reset code, and cancelling an account's other live codes after a platform reset.

Verification

  • cargo fmt --check, cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --locked --all-features: 1139 passed, 0 failed, 61 ignored
  • Disposable-database helper: 27 AEAD/database tests (4 new, covering both account kinds through the inner routers), 2 OAuth, 8 callback, 21 compaction; none skipped
  • Transport V2 gateway test covers both 429 shapes inside the encrypted response
  • Not run: SDK end-to-end, client apps, a live enclave

Part of #1486.

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Request limits had one implementation, tied to authenticated inference:
one policy, keyed by the signed-in account. Routes with no signed-in
caller could not use it, and under Transport V2 the edge cannot limit
them either, because it sees one encrypted endpoint and the backend has
no trusted client address.

web/rate_limit.rs replaces the inference limiter with one Limiter type.
Each limit is a field of RateLimits with its policy in Rust: admissions
per rolling window, an optional fixed cooldown and a key capacity. The
key is the account a request acts on:

- an authenticated route mounts the enforce_user layer, which charges
  the verified caller;
- an unauthenticated route's handler charges the account the request
  names, once it has resolved it, with Limiter::check.

Limits run on the inner routes, so a rejection is an ordinary response
and Transport V2 seals it. The generic one is a 429 with Retry-After
and x-opensecret-error-code: rate_limit_exceeded.

Inference admission is unchanged: same policy, environment settings and
OpenAI-shaped error.

Password reset, for user and platform accounts alike:

- 5 reset requests per targeted account per rolling hour. Past that the
  route gives its usual generic answer but creates no request and sends
  no email.
- 5 confirmation attempts per reset request for its 24-hour life. An
  attempt is charged to the request its secret belongs to; a secret
  that matches no request is refused uncounted, so only the party that
  started a request can spend its attempts.

State is per process, so this is a guardrail, not a global quota.

Part of #1486.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@AnthonyRonning
AnthonyRonning merged commit c499ceb into master Nov 10, 2026
23 checks passed
@AnthonyRonning
AnthonyRonning deleted the feat/generic-rate-limiter branch November 10, 2026 09:46
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