Skip to content

[M2.2c4c1] Persist encrypted atomic authentication retries - #50

Merged
samuelmbabhazi merged 2 commits into
mainfrom
feat/m2.2c4c1-private-idempotency
Nov 5, 2026
Merged

samuelmbabhazi merged 2 commits into
mainfrom
feat/m2.2c4c1-private-idempotency

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Authentication retries now have a durable encrypted request/response store. A transaction-scoped PostgreSQL advisory lock serializes each route/key across API instances; the command, its audit and the cached response commit or roll back together. Reusing a key for another request returns a conflict.

Keys are lowercase UUIDv4/v7 values; rows contain only a purpose-bound key hash and envelope ciphertext, with a five-minute lifetime enforced by SQL. Both request credentials and response cookies are encrypted, avoiding a low-cost persisted password-verification hash. Comparisons use fixed-length constant-time hashes in memory. Expired rows are cleaned in batches of at most 100.

Validation: API lint/typecheck and all 45 real PostgreSQL/TLS SMTP integration tests pass. Four new tests cover concurrent instances, route/request binding, exact expiry, encrypted storage, command/audit rollback on write failure, corruption and invalid keys. HTTP integration follows in M2.2c4c2.

Security: this explicitly documented five-minute encrypted snapshot is the exception to hash-only long-lived credential persistence; a replay result is not an authentication authority. Unknown storage/crypto errors are opaque. No external transport runs in the transaction and no new business mutation bypasses audit. Stacked on #49; no merge requested.

  • M2.2c4c1 criteria met and plan updated
  • Schema migration tested on real PostgreSQL
  • Concurrency and failure recovery covered
  • No plaintext credentials, new shell invocation or generated-code edits

@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.2c4c1-private-idempotency branch from 6abbba2 to f26387f Compare November 4, 2026 11:13
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.2c4c1-private-idempotency branch from f26387f to aca3238 Compare November 4, 2026 11:36
@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/m2.2c4b-auth-http to main November 5, 2026 12:16
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.2c4c1-private-idempotency branch from aca3238 to 5825b90 Compare November 5, 2026 12:30
@samuelmbabhazi
samuelmbabhazi merged commit 48bc99b into main Nov 5, 2026
9 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m2.2c4c1-private-idempotency branch November 5, 2026 12:31
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
Every identity POST now accepts an optional Idempotency-Key. Identical
retries return the encrypted committed result, including the original
session cookies, without creating another session, email job or audit
event. The response indicates Idempotency-Replayed; changed bodies or
credential contexts conflict.

Logout validates its session/CSRF inside the first execution so a retry
can acknowledge the already committed revocation. Cached logout
responses require the exact original session and CSRF. Cached login
responses revalidate the session and return 409 without cookies after
revocation/reset/expiry. Origin checks and persistent rate limits still
run before replay. Failed retry persistence removes Set-Cookie and rolls
back the command.

Validation: all 47 PostgreSQL/TLS SMTP integration tests pass, including
concurrent HTTP signup retries, cookie replay, logout retries, changed
credentials, revoked grants and forced storage failure. API lint,
typecheck, unit tests and production build pass. OpenAPI/client
regenerated. The SMTP acceptance fixture now selects by
recipient/purpose and accepts every base64url code, including a leading
hyphen.

Security: no plaintext request/response snapshots; retry state shares
the audited command transaction. No new external I/O occurs inside it;
bounded password work uses the existing transaction deadline and
capacity limits. Physical cleanup is explicitly documented separately
from the five-minute replay window. Stacked on #50; no merge requested.

- [x] M2.2c4c2 and account/session HTTP acceptance complete
- [x] Concurrency, rollback and credential-revocation tests pass
- [x] Plan/spec and generated contract updated
- [x] No new shell invocation or secret exposure path
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…ion pool (#95)

## Task

Review follow-up for #50 and #51 (M2.2c4c1 and M2.2c4c2). Refs #21

## What changed

- New `BoundedConcurrency` helper: at most N operations run at once, up
to M more wait without holding anything, and callers beyond that get 503
immediately.
- `PrivateIdentityReplay.execute` runs its transaction through a limit
of 3 active and 32 queued.

## Why

With an `Idempotency-Key`, register, login and reset run inside the
replay transaction, which holds a pooled connection while the request
waits for Argon2id (PasswordHasher allows 2 running and 16 waiting). An
anonymous client sending about 20 concurrent keyed logins could hold up
to 18 connections, while the `pg` pool behind `PrismaPg` defaults to 10.
Every other request, including session checks, then waited for a
connection and failed with 503: unauthenticated callers could stall the
whole API.

Keeping the command, its audit row and the encrypted retry result in one
transaction is what makes retries safe, so the transaction stays.
Instead, at most three replay transactions may hold connections, leaving
at least seven for everything else. Waiting replays hold no connection.

## How it was tested

- Unit tests for `BoundedConcurrency`: peak concurrency never exceeds
the limit, queued work runs when a slot frees, a full queue fails fast
with 503, and failures release their slot.
- `nx run-many -t lint typecheck test -p api` in a clean worktree: all
pass.
- Integration tests could not run locally (no Docker in the sandbox) and
GitHub Actions is blocked by the account spending limit. CI must pass
before merge.

## Security considerations

Removes an unauthenticated denial-of-service path. A flood of keyed
requests now receives 503 from the replay gate instead of exhausting the
database pool. Per-IP and per-subject throttles still apply before this
gate.

## Checklist

- [x] Lint, TS tests pass locally
- [x] Tests added
- [x] No new shell invocation and no new secret exposure path
- [x] Audit events unchanged
- [ ] CI green (blocked by billing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant