Skip to content

OpenSecret: Add database-backed password-reset guess and email limits - #1684

Open
al-maisan wants to merge 2 commits into
MaplePrivacyLabs:masterfrom
al-maisan:1486-durable-reset-limits
Open

al-maisan wants to merge 2 commits into
MaplePrivacyLabs:masterfrom
al-maisan:1486-durable-reset-limits

Conversation

@al-maisan

@al-maisan al-maisan commented Nov 25, 2026 •

Copy link
Copy Markdown

Closes #1486.

What

Password-reset email codes had no durable guess budget, and reset initiation had no per-account email cap that survives restarts or holds across enclave processes: the process-local limiters in web/rate_limit.rs reset on restart and each replica kept its own allowance.

Both limits now live in PostgreSQL, coordinated through the reset request rows themselves (failed_attempts column + rolling-hour row count), for the legacy user confirm route, the Transport V2 options/completion routes through the shared proof verifier, and the platform (developer) routes.

Guess budget semantics

  • A wrong code charges the durable budget of the unconsumed requests carrying the submitted reset-secret hash — a random secret cannot burn anyone else's budget, and guessing the code stays distinct from guessing the secret.
  • A correct code with the matching secret never charges; valid repeated V2 options checks remain free.
  • The budget is checked before the code is examined, so an exhausted request stays closed even to the correct code — same fail-closed boundary as the process-local limiter today.

Email cap semantics

  • Creation counts the account's request rows inside a rolling hour (every row means one sent email), under the user-row lock — the same serialization the credential-mutation flows already use — so concurrent processes cannot exceed the cap together.
  • Past the cap: no request row, no email, and the ordinary generic initiation response, byte-identical across unknown, OAuth-only, throttled and valid accounts.
  • The window is rolling: rows older than an hour stop counting even while unconsumed.

Invariants kept

  • The V2 options route stays read-only; nothing in the routes mutates reset state — all charging lives in the shared proof verifier.
  • No plaintext codes, secrets, or emails in logs or public errors; the charge guard logs nothing secret-derived.
  • The process-local limiters remain as cheap first-line guardrails; the database is now the authority.
  • Migration is timestamped and reversible (up.sql/down.sql, new columns default 0, two new indexes).

Validation

Disposable dockerized Postgres 17, migrated with diesel migration run (up and redo verified):

  • cargo fmt --all -- --check: clean
  • cargo clippy --locked --all-targets -- -D warnings: clean
  • Unit suite: 1185 passed, 0 failed
  • AEAD_TAMPER_TEST_DATABASE_URL ignored DB suites (--ignored --test-threads=1): all password-reset, recovery-route, recovery-db, tamper and oauth tests pass, including the existing password_reset_limits contracts
  • New coverage: budget shared across separate app states (two processes, one database), 20 concurrent wrong guesses over separate pooled connections never exceed the counter, 12 concurrent initiations never exceed the email cap, and the rolling-window boundary frees budget only for rows older than the hour
  • db_compaction_* suites pass against the opensecret_agent_test_*-named database they require
  • Not exercised locally: live TINFOIL provider contract and the encrypted smoke script (need live credentials/stack)

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

al-maisan added a commit to al-maisan/Maple that referenced this pull request Nov 25, 2026
…9s and expiry charges

Review fixes on PR MaplePrivacyLabs#1684:

- The guess budget is now checked on the request the submitted code
  selects, not on the newest request sharing the secret: a spent
  request stays closed even to its correct code, and a same-secret
  sibling can neither reopen it nor be shielded by it. Wrong-code
  guesses still charge the shared per-(account, secret) budget, per
  the MaplePrivacyLabs#1486 charging spec, with a regression covering several open
  requests sharing one secret.
- Only a genuinely spent budget maps to the rate-limited response on
  both the user and platform paths; any other database failure
  propagates as an infrastructure error instead of a 24-hour 429.
- Expired user requests are never charged: expiry returns before the
  charge, the guarded UPDATE filters to active rows, and a secret whose
  requests have all expired reports the failure without charging (new
  NoActivePasswordResetRequest distinction). Expiry coverage added.
- The concurrent-initiation test now asserts the exact cap and that
  every initiation call itself succeeded, so a no-op path cannot pass.
…cyLabs#1486)

Password-reset email codes previously had no durable guess budget, and
reset initiation had no per-account email cap that survives restarts or
holds across enclave processes: the process-local limiters in
web/rate_limit.rs reset on restart and each replica kept its own
allowance.

Both limits now live in PostgreSQL, coordinated through the reset
request rows themselves:

- Guess budget: a failed_attempts counter on every reset request
  (user and platform). A wrong code charges the durable budget of the
  unconsumed requests carrying the submitted reset-secret hash, so a
  random secret cannot burn anyone else's budget; a correct code with
  the matching secret never charges. The budget is checked before the
  code is examined, so an exhausted request stays closed even to the
  correct code, mirroring the process-local limiter.

- Email cap: creation counts the account's request rows inside a
  rolling hour under the user-row lock (the same serialization the
  credential-mutation flows use), and past the cap creates no request
  row and sends no email. Every response stays byte-identical to the
  ordinary generic initiation answer, preserving account-enumeration
  protections for unknown, OAuth-only, throttled and valid accounts.

Both apply to legacy user confirmation, Transport V2 options and
completion through the shared proof verifier, and platform/developer
reset initiation and verification. The process-local limiters remain
as cheap first-line guardrails.

Coverage adds cross-process sharing across separate app states,
concurrent wrong guesses and concurrent initiation over separate
pooled connections, and the rolling-window boundary.
…9s and expiry charges

Review fixes on PR MaplePrivacyLabs#1684:

- The guess budget is now checked on the request the submitted code
  selects, not on the newest request sharing the secret: a spent
  request stays closed even to its correct code, and a same-secret
  sibling can neither reopen it nor be shielded by it. Wrong-code
  guesses still charge the shared per-(account, secret) budget, per
  the MaplePrivacyLabs#1486 charging spec, with a regression covering several open
  requests sharing one secret.
- Only a genuinely spent budget maps to the rate-limited response on
  both the user and platform paths; any other database failure
  propagates as an infrastructure error instead of a 24-hour 429.
- Expired user requests are never charged: expiry returns before the
  charge, the guarded UPDATE filters to active rows, and a secret whose
  requests have all expired reports the failure without charging (new
  NoActivePasswordResetRequest distinction). Expiry coverage added.
- The concurrent-initiation test now asserts the exact cap and that
  every initiation call itself succeeded, so a no-op path cannot pass.
@al-maisan
al-maisan force-pushed the 1486-durable-reset-limits branch from f650af0 to 7e6d03f Compare November 25, 2026 08:22
@al-maisan

al-maisan commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

@AnthonyRonning Hi Anthony — took #1486 as an excuse to dig into the OpenSecret backend.

Both reset limits (per-request guess budget, rolling-hour email cap) are now durable in Postgres instead of process-local. The charge is a guarded UPDATE scoped to the reset-secret hash, so a random secret can't burn someone else's budget; the email cap serializes through the user row the same way the credential-mutation flows already do. Every existing route contract keeps its exact responses, and there's cross-process + concurrent-connection coverage (commits signed).

Happy to take #1483 next if this approach looks good.

@al-maisan al-maisan changed the title Add database-backed password-reset guess and email limits OpenSecret: Add database-backed password-reset guess and email limits Nov 25, 2026

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add database-backed limits for password-reset guesses and emails

1 participant