Skip to content

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

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

al-maisan wants to merge 3 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

  • Each admitted proof attempt runs in one transaction that first locks (SELECT … FOR UPDATE) every unconsumed request carrying the submitted reset-secret hash, then looks up the code, checks the budget, compares the secret and charges a failure. A correct proof can only complete a request with its own secret, so it queues behind in-flight guesses on that secret and always sees their charges.
  • The code-selected request is refused as expired, or with a generic 429 when its budget is spent, before its secret is compared — an exhausted request stays closed even to the correct code.
  • Any failed guess (wrong code, or a correct code with another request's secret) charges every active request carrying the submitted secret; when all of them are spent the answer is the generic 429. A random secret cannot burn anyone else's budget.
  • Expired requests are never charged. A secret whose requests have all expired has nothing durable to charge, so its failures still spend the process-local budget.
  • A correct code with the matching secret never charges; valid repeated V2 options checks remain free.
  • User and platform resets share one pure decision function (decide_proof_attempt), so the two flows cannot drift; budget outcomes are an enum (ResetProofAttempt), not overloaded DB errors.

Email cap semantics

  • Creation locks the account row, counts every request row created in the rolling hour (consumed or not — every row means one sent email) and inserts in one transaction — the same user-row serialization the credential-mutation flows 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 handler stays read-only; all charging lives in the shared proof verifier.
  • No plaintext codes, secrets, or emails in logs or public errors.
  • The process-local limiters remain as cheap first-line guardrails; the database is now the authority.
  • Lock order cannot deadlock with destructive reset: proof attempts lock only reset rows (in id order), never the user row.
  • Migration is timestamped and reversible (up.sql/down.sql, new columns default 0, two new indexes).
  • docs/development-contracts.md describes the new limits.

Validation

At head, in the pinned OpenSecret Nix shell:

  • cargo fmt --all -- --check, cargo clippy --locked --all-targets -- -D warnings: clean
  • Unit suite: 1190 passed, 0 failed (includes unit tests for the shared proof decision)
  • .agents/skills/validate-opensecret/scripts/disposable_db_tests.sh --redo-latest: migration redo OK; all 36 AEAD/DB, 2 OAuth DB, 6 callback, 21 compaction and 43 recovery tests pass
  • DB coverage includes: budget shared across two app states on one database; 20 concurrent wrong guesses examine and charge exactly the budget; a correct proof waits for an in-flight guess holding the secret's row locks and then sees the spent budget; failures for an all-expired secret stay throttled; platform expiry and wrong-secret charging match user resets; 12 concurrent initiations never exceed the email cap; rolling-window boundary
  • just lane os-rust, just lane os-flake-check: pass
  • Not run locally: os-sdk-e2e, live TINFOIL provider contract, encrypted smoke script

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
- Decide each user and platform reset proof in one transaction that locks
  the submitted secret's unconsumed requests, so concurrent guesses cannot
  read a stale budget before a correct proof is accepted.
- Share one pure decision (`decide_proof_attempt`) between user and
  platform resets: a correct code with another request's secret is charged
  on both, and budget outcomes are an enum instead of overloaded errors.
- Keep failed user proofs for an all-expired secret on the process-local
  limiter, and report a correct platform code for an expired request as
  expired instead of charging a sibling.
- Fix the migration comment, the email-cap log line and the
  password-reset contract docs; tighten the concurrency test and add
  lock, expiry-throttle and platform charge tests.

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