fix(identity/sharing): B-6 auth-consistency batch (#37) — OTP atomicity, share expiry, TOTP replay+lockout, OIDC exact-match, recovery uniformity #79

Merged
amiryahaya merged 1 commit from fix/b6-auth-consistency into main 2026-11-07 04:44:20 +01:00
Owner

Closes #37 (baseline-review auth-consistency batch; items 6/7 — Logins unique index, Redis HMAC replay — already landed previously)

# Fix Before → After
1 OTP atomicity — new IOtpStore.ConsumeAttemptAsync (InMemory lock-gated; Redis delete-first, true INCR/Lua → M2) 10/10 concurrent correct codes mint sessions (proven by barrier test) → exactly 1
2 Expired shares — GetShare checks ExpiresAt beside RevokedAt (in-memory, SQLite-compat) expired share served the wrapped key → 404
3 TOTP replay + lockout — new TotpSecurityGuard: consumed codes dead 90s; 5 consecutive failures → 15 min lock (even valid codes rejected, uniform 401) + auth.totp.* audit events code replayable ±1 window forever; no lockout
4 OIDC redirect exact-match — StartsWith allowlist → exact failsulit://auth/callback.evil.com passed → 400
5 Recovery enumeration — unknown/non-TOTP accounts answer the identical 200 body (200 ms pad); recovery-verify unknown = same 401 as bad code 404 vs 200 oracle

Also: TotpVerify's failed-login audit row used Guid.Empty ActorId (the #74 FK-poison class) — now the real user id.

Verification

TDD: RED ×7 deterministic (OtpAtomicity proven with a barrier store — all 10 racers read the same pre-consume entry). EmailAuthFlowTests.TotpRecovery_UnknownEmail_Returns404 updated to the new anti-enumeration contract (uniform 200) — behavior change, test updated not deleted.

Suite 869 / 0 failed / 39 skipped (Release + Debug) · coverage gate 84.92% ≥ 80

Documented residual

Redis OTP consume is best-effort (IDistributedCache has no CAS) — true atomic INCR/Lua lands with the M2 Redis-hardening work, pairing with RedisSessionStore.

Closes #37 (baseline-review auth-consistency batch; items 6/7 — Logins unique index, Redis HMAC replay — already landed previously) | # | Fix | Before → After | |---|---|---| | 1 | **OTP atomicity** — new `IOtpStore.ConsumeAttemptAsync` (InMemory lock-gated; Redis delete-first, true INCR/Lua → M2) | 10/10 concurrent correct codes mint sessions (proven by barrier test) → **exactly 1** | | 2 | **Expired shares** — GetShare checks `ExpiresAt` beside `RevokedAt` (in-memory, SQLite-compat) | expired share served the wrapped key → **404** | | 3 | **TOTP replay + lockout** — new `TotpSecurityGuard`: consumed codes dead 90s; 5 consecutive failures → 15 min lock (even valid codes rejected, uniform 401) + `auth.totp.*` audit events | code replayable ±1 window forever; no lockout | | 4 | **OIDC redirect exact-match** — StartsWith allowlist → exact | `failsulit://auth/callback.evil.com` passed → **400** | | 5 | **Recovery enumeration** — unknown/non-TOTP accounts answer the identical 200 body (200 ms pad); recovery-verify unknown = same 401 as bad code | 404 vs 200 oracle | Also: TotpVerify's failed-login audit row used `Guid.Empty` ActorId (the #74 FK-poison class) — now the real user id. ## Verification TDD: **RED ×7 deterministic** (OtpAtomicity proven with a barrier store — all 10 racers read the same pre-consume entry). `EmailAuthFlowTests.TotpRecovery_UnknownEmail_Returns404` updated to the new anti-enumeration contract (uniform 200) — behavior change, test updated not deleted. **Suite 869 / 0 failed / 39 skipped (Release + Debug) · coverage gate 84.92% ≥ 80** ## Documented residual Redis OTP consume is best-effort (IDistributedCache has no CAS) — true atomic INCR/Lua lands with the M2 Redis-hardening work, pairing with RedisSessionStore.
1. OTP attempt-limit made atomic: IOtpStore.ConsumeAttemptAsync
   (InMemory = lock-gated verify-and-consume; Redis = delete-first
   best-effort, true INCR/Lua deferred to M2 Redis hardening). The old
   read-check-write let 10/10 concurrent correct codes succeed under a
   deterministic barrier test — now exactly 1.

2. Expired shares no longer serve wrapped keys: GetShare checks
   ExpiresAt (in-memory, SQLite-compat) alongside RevokedAt.

3. TOTP replay + lockout (new TotpSecurityGuard): consumed codes are
   dead for their 90s window; 5 consecutive failures lock the account
   15 min — even genuinely valid codes rejected while locked, uniform
   401 surface, auth.totp.locked_rejected / replay_rejected / locked
   audit events. Per-process state documented (multi-instance = M2).

4. OIDC redirect_uri allowlist is EXACT-match (was StartsWith —
   'failsulit://auth/callback.evil.com' slipped through; both lookalike
   cases now 400, exact URI still accepted).

5. TOTP recovery enumeration closed: unknown + non-TOTP accounts answer
   the identical 200 body as known ones (200ms pad, recovery-share
   pattern); recovery-verify unknown account returns the same 401 as a
   bad code. EmailAuthFlowTests' 404-contract test updated to the new
   anti-enumeration contract.

Also: TotpVerify's failed-login audit used Guid.Empty ActorId (the #74
FK-poison class) — now logs the real user id.

TDD: 7 tests first — RED x7 deterministic (OtpAtomicity proven with a
barrier store: all 10 racers read the same pre-consume entry).
Suite 869 / 0 failed / 39 skipped (Release + Debug); gate 84.92% >= 80.
Sign in to join this conversation.
No description provided.