feat(identity,m2m): auth-guard state into rows + advisory invite locks (#120 slice 3, ADR-011) #125

Merged
amiryahaya merged 1 commit from feat/120-slice3-locks-guard-budget into main 2026-11-08 18:53:59 +01:00
Owner

#120 slice 3 / ADR-011: the last per-process auth state moves into the database — every distributed-state gap the audits flagged ('per-process → M2 hardening') is now closed structurally.

TotpSecurityGuard (B-6 #37 semantics preserved exactly):

  • consumed codes → totp_consumed_codes — SHA-256 of accountId:code (no raw 6-digit codes at rest, ApiKey.SecretHash discipline); the 90-second replay window is enforced at QUERY time, so stale rows are sweep hygiene
  • failure/lockout → totp_failure_states (count, locked_until): 5 consecutive failures → 15-min lock, original lock kept while locked, reset on success, expiry clears the row
  • DI singleton → scoped (the state IS the database — a singleton would hide it behind process memory again)
  • consequences for free: lockout/replay state now survives restarts and is shared across instances

M2mAuthMiddleware failure budget (SDK-03):

  • static ConcurrentDictionary → auth_failure_events rows (written only on FAILURE — never success); fixed 1-minute window, 30 failures → 429; leak-free (the old entries-never-swept TODO from #110 dies here) and shared across instances

InvitationAcceptanceService (#90, now multi-instance):

  • per-invite ConcurrentDictionary<SemaphoreSlim> → pg_advisory_xact_lock(hashtextextended(invite_id)) INSIDE the accept transaction — releases with the tx, nothing to leak. Npgsql-only: SQLite (the test harness) has no advisory locks and serializes writes on its single connection; the atomic Pending-claim remains the authoritative guard in every database (the #90 invariant tests stay green)

Sweep extended to all four transient tables (sessions, challenges, consumed codes, auth failures) with precomputed DateTimeOffset cutoffs — TimeSpan-typed parameters don't survive the SQLite DateTimeOffset-TEXT conversion (same lesson as the slice-2 sweep).

Tests: 7 new (M2Slice3Tests): failures-persist-as-rows-and-reset-on-success (cross-scope), lockout expiry clears, replay-window-by-query + hashed-at-rest + 64-hex invariants, guard-scoped-not-singleton DI assertion, budget fixed-window exhaustion + per-IP isolation, sweep coverage, HTTP 31st-failure → 429 too_many_auth_failures. B-6 lockout/replay + #90/#107 invite invariants green as regressions.

Suite 945/0/39 (Release+Debug), coverage gate 87.46%. Third slice of #120; slice 4 (compose/health/package leftovers) remains.

**#120 slice 3 / ADR-011:** the last per-process auth state moves into the database — every distributed-state gap the audits flagged ('per-process → M2 hardening') is now closed structurally. **TotpSecurityGuard** (B-6 #37 semantics preserved exactly): - consumed codes → **totp_consumed_codes** — SHA-256 of accountId:code (no raw 6-digit codes at rest, ApiKey.SecretHash discipline); the 90-second replay window is enforced at QUERY time, so stale rows are sweep hygiene - failure/lockout → **totp_failure_states** (count, locked_until): 5 consecutive failures → 15-min lock, original lock kept while locked, reset on success, expiry clears the row - DI singleton → **scoped** (the state IS the database — a singleton would hide it behind process memory again) - consequences for free: lockout/replay state now survives restarts and is shared across instances **M2mAuthMiddleware failure budget** (SDK-03): - static ConcurrentDictionary → **auth_failure_events** rows (written only on FAILURE — never success); fixed 1-minute window, 30 failures → 429; leak-free (the old entries-never-swept TODO from #110 dies here) and shared across instances **InvitationAcceptanceService** (#90, now multi-instance): - per-invite ConcurrentDictionary\<SemaphoreSlim\> → **pg_advisory_xact_lock(hashtextextended(invite_id))** INSIDE the accept transaction — releases with the tx, nothing to leak. Npgsql-only: SQLite (the test harness) has no advisory locks and serializes writes on its single connection; the atomic Pending-claim remains the authoritative guard in every database (the #90 invariant tests stay green) **Sweep** extended to all four transient tables (sessions, challenges, consumed codes, auth failures) with precomputed DateTimeOffset cutoffs — TimeSpan-typed parameters don't survive the SQLite DateTimeOffset-TEXT conversion (same lesson as the slice-2 sweep). **Tests:** 7 new (M2Slice3Tests): failures-persist-as-rows-and-reset-on-success (cross-scope), lockout expiry clears, replay-window-by-query + hashed-at-rest + 64-hex invariants, guard-scoped-not-singleton DI assertion, budget fixed-window exhaustion + per-IP isolation, sweep coverage, HTTP 31st-failure → 429 too_many_auth_failures. B-6 lockout/replay + #90/#107 invite invariants green as regressions. Suite 945/0/39 (Release+Debug), coverage gate 87.46%. Third slice of #120; slice 4 (compose/health/package leftovers) remains.
feat(identity,m2m): auth-guard state into rows + advisory invite locks (#120 slice 3, ADR-011)
All checks were successful
Security CI / security-scan (pull_request) Successful in 2m46s
API CI / build-and-test (pull_request) Successful in 3m43s
668db3cc09
The last per-process auth state moves into the database:

TotpSecurityGuard (B-6 #37 semantics preserved exactly):
- consumed codes → totp_consumed_codes (SHA-256 of accountId:code — no raw
  6-digit codes at rest), 90s replay window enforced at QUERY time
- failure/lockout state → totp_failure_states (count, locked_until),
  5 failures → 15 min lock, original lock kept, reset on success, expiry
  clears the row
- DI singleton → scoped (the state IS the database; a singleton would hide
  it behind process memory again)
- multi-instance + restart-surviving for free

M2mAuthMiddleware failure budget (SDK-03):
- static ConcurrentDictionary → auth_failure_events rows (written only on
  FAILURE); fixed 1-min window, 30 → 429; leak-free (swept) and shared
  across instances

InvitationAcceptanceService (#90, now multi-instance):
- per-invite ConcurrentDictionary semaphores → pg_advisory_xact_lock
  inside the accept transaction (releases with the tx, nothing to leak);
  Npgsql-only — SQLite has no advisory locks and serializes on its single
  connection; the atomic Pending-claim remains authoritative everywhere

SessionCleanupService sweep extended: sessions, challenges, consumed
codes, auth-failure events — with precomputed DateTimeOffset cutoffs
(TimeSpan parameters don't survive the SQLite DateTimeOffset-TEXT
conversion; same lesson as the slice-2 sweep).

7 new tests + B-6 lockout/replay and #90/#107 invite invariants green.
Suite 945/0/39 Release+Debug, coverage 87.46%.
Sign in to join this conversation.
No description provided.