fix(m2m,keys,identity): #110 SHOULD batch A — SA race, LastUsedAt throttle, M2M audits, default scopes, setup-confirm guard, durable HMAC replay #127

Merged
amiryahaya merged 1 commit from feat/110-should-batch-a into main 2026-11-09 05:50:05 +01:00
Owner

#110 SHOULD batch A (items 2/7/8/9-main already landed via #120 slices and PR #114). Seven fixes:

  • #1 JIT SA provisioning race: parallel first calls with the same sub hit the unique Did index → the loser catches the unique violation (provider-neutral IsUniqueConstraintViolation helper) and refetches the winner's row (suspended check preserved) instead of 500ing.
  • #3 ApiKey LastUsedAt: written only when stale (>1 min) — row-lock contention no longer scales with auth traffic.
  • #4 Production startup warning when M2m:Issuer is unset — issuer validation silently skips today (fail-open).
  • #5 M2M denials audited: m2m.auth.failed (both 401 legs, actor null + IP detail) and m2m.scope.denied (actor = principal, target = key/SA id, required_scope detail) — Category=security, matching the security.ratelimited pattern.
  • #6 default SA scopes: files:read activity:read via M2mAuthMiddleware.DefaultServiceAccountScopes — write must be granted deliberately. Scope refresh/admin surface deferred to the client phases.
  • #9b TotpSetupConfirm: corrupt TOTP material → 401 + security.totp_decrypt_failed audit (same contract as TotpVerify from PR #114).
  • #10 HMAC replay dual-layer: last accepted signature persists on ExtensionService (LastSignature/LastSignatureAt + migration) and is checked even with a cold in-process cache — replays no longer survive restarts within the ±5 min window. Limitation documented: only the latest signature per service survives a restart; burst coverage stays in-process.

Reviewed no-fix (per the audit's 're-rank' clause): #7-secondary TotpRecovery send-failure 503 (SMTP-infra path, not attacker-differentiable) and EmailRegister invite-gated 409 (reachable only with a VALID invitation whose email the holder already knows).

Tests: 7 new (throttle double-call, stale refresh, both audit rows, default-scope const, setup-confirm 401+audit, cold-cache replay rejection). Suite 952/0/39 (Release+Debug), coverage 87.74%. Batch B (test hygiene + CI composite action + Dockerfile) follows.

**#110 SHOULD batch A** (items 2/7/8/9-main already landed via #120 slices and PR #114). Seven fixes: - **#1** JIT SA provisioning race: parallel first calls with the same sub hit the unique Did index → the loser catches the unique violation (provider-neutral IsUniqueConstraintViolation helper) and refetches the winner's row (suspended check preserved) instead of 500ing. - **#3** ApiKey LastUsedAt: written only when stale (>1 min) — row-lock contention no longer scales with auth traffic. - **#4** Production startup warning when M2m:Issuer is unset — issuer validation silently skips today (fail-open). - **#5** M2M denials audited: `m2m.auth.failed` (both 401 legs, actor null + IP detail) and `m2m.scope.denied` (actor = principal, target = key/SA id, required_scope detail) — Category=security, matching the `security.ratelimited` pattern. - **#6** default SA scopes: `files:read activity:read` via `M2mAuthMiddleware.DefaultServiceAccountScopes` — write must be granted deliberately. Scope refresh/admin surface deferred to the client phases. - **#9b** TotpSetupConfirm: corrupt TOTP material → 401 + `security.totp_decrypt_failed` audit (same contract as TotpVerify from PR #114). - **#10** HMAC replay dual-layer: last accepted signature persists on ExtensionService (LastSignature/LastSignatureAt + migration) and is checked even with a cold in-process cache — replays no longer survive restarts within the ±5 min window. Limitation documented: only the latest signature per service survives a restart; burst coverage stays in-process. **Reviewed no-fix** (per the audit's 're-rank' clause): #7-secondary TotpRecovery send-failure 503 (SMTP-infra path, not attacker-differentiable) and EmailRegister invite-gated 409 (reachable only with a VALID invitation whose email the holder already knows). Tests: 7 new (throttle double-call, stale refresh, both audit rows, default-scope const, setup-confirm 401+audit, cold-cache replay rejection). Suite 952/0/39 (Release+Debug), coverage 87.74%. Batch B (test hygiene + CI composite action + Dockerfile) follows.
fix(m2m,keys,identity): #110 SHOULD batch A — SA race, LastUsedAt throttle, M2M audits, default scopes, setup-confirm guard, durable HMAC replay
All checks were successful
Security CI / security-scan (pull_request) Successful in 2m0s
API CI / build-and-test (pull_request) Successful in 11m22s
26fda2ff51
#110-1: parallel first JIT-provisioning calls with the same sub race on
the unique Did index — the loser now catches the unique violation and
refetches the winner's row (suspended check preserved) instead of 500ing.

#110-3: ApiKey LastUsedAt writes only when stale (>1 min) — an
unconditional UPDATE per authenticated call scaled row-lock contention
with traffic.

#110-4: Production startup warning when M2m:Issuer is unset — issuer
validation silently skips today (fail-open).

#110-5: M2M auth failures and insufficient_scope denials now leave
security audit rows (m2m.auth.failed / m2m.scope.denied) — machine
principals probing beyond their grant get telemetry.

#110-6: default service-account scope set drops files:write (now
files:read activity:read via M2mAuthMiddleware.DefaultServiceAccountScopes)
— write access must be granted deliberately. Scope freezing/refresh
deferred to the client phases with the admin surface.

#110-9b: TotpSetupConfirm degrades corrupt TOTP material to 401 +
security.totp_decrypt_failed audit — the same contract TotpVerify got in
PR #114; key rotation can no longer 500 the setup path.

#110-10: HMAC replay detection is now dual-layer — the in-process cache
covers hot duplicates; the last accepted signature persists on
ExtensionService (LastSignature/LastSignatureAt, migration included) and
is checked even with a cold cache, so replays no longer survive restarts
within the ±5 min window.

Reviewed no-fix (documented): #7-secondary TotpRecovery send-failure 503
(infra-error path, not attacker-differentiable) and EmailRegister
invite-gated 409 (only reachable with a VALID invitation whose email the
holder already knows).

7 new tests; suite 952/0/39 Release+Debug, coverage 87.74%.
Sign in to join this conversation.
No description provided.