Skip to content

[M2.2c2] Enforce shared authentication attempt limits - #45

Merged
samuelmbabhazi merged 1 commit into
mainfrom
feat/m2.2c2-auth-throttling
Nov 5, 2026
Merged

samuelmbabhazi merged 1 commit into
mainfrom
feat/m2.2c2-auth-throttling

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

M2.2c2, part of #21. Depends on #44; base feat/m2.2c1-identity-commands.

What changed and why

Add PostgreSQL-backed authentication attempt reservations with independent IP and subject quotas. Atomic upserts enforce progressive credential delays across API instances; rejected retries do not extend the block. Successful login clears only its subject bucket, while IP quotas remain effective against account cycling. Expired rows are removed in bounded, non-blocking batches and persisted keys are hashes.

The port returns quota/reset/retry metadata for the HTTP layer, which follows in M2.2c4. This PR does not yet expose account commands publicly.

Validation

  • Workspace lint, types and TypeScript tests pass.
  • Thirty PostgreSQL integration tests pass, including five limiter tests for concurrent workers, exact delay/window boundaries, 100 distinct-account attempts from one IP, email quotas and scope isolation.
  • SQL remains parameterized; database failures become opaque availability errors.

Security considerations

Delays reserve the next credential attempt before expensive hashing, so simultaneous requests cannot all pass before a failed-proof counter is updated. Session/login success cannot erase IP abuse history. Counters are security telemetry and carry no password, bearer token, email or IP plaintext. HTTP callers must normalize subjects and use trusted client-IP derivation.

Checklist

  • M2.2c2 acceptance checked; HTTP integration remains open
  • Lint, types and tests pass
  • Real concurrency and quota-boundary tests added
  • No shell invocation or plaintext secret persistence
  • Telemetry only; account commands remain audited
  • SPEC and threat model updated

@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.2c1-identity-commands branch from 8384abf to 426973c Compare November 5, 2026 12:14
@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/m2.2c1-identity-commands to main November 5, 2026 12:14
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.2c2-auth-throttling branch from bef50e9 to 2a43b7b Compare November 5, 2026 12:15
@samuelmbabhazi
samuelmbabhazi merged commit 30f8b5c into main Nov 5, 2026
9 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m2.2c2-auth-throttling branch November 5, 2026 12:15
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
## Task

M2.2c3a, part of #21. Depends on #45; base
`feat/m2.2c2-auth-throttling`.

## What changed and why

Enqueue identity emails atomically with account/credential/audit changes
using pg-boss and its upstream Prisma transaction adapter. Email
addresses, credentials and expiration are envelope-encrypted with
authenticated data tied to the job id. A separate worker retries
delivery up to five times with capped backoff, skips expired credentials
and sanitizes every persisted worker error.

Add the pg-boss lifecycle owner with bounded database connections and
graceful shutdown. The MIT-licensed dependency is pinned to 12.38.0 and
implements the queue already chosen in ADR 0004. Production SMTP wiring
follows in M2.2c3b; no endpoint is exposed by this PR.

## Validation

- Thirty-four real PostgreSQL integration tests pass.
- Four queue tests cover transaction rollback, ciphertext-only
persistence, restart delivery, transport failures and retries, opaque
persisted errors, expired credentials and tampered ciphertext.
- Workspace lint, types and builds pass; existing account, limiter,
repository, schema, audit and fleet integration suites pass.

## Security considerations

Queue and schema names are fixed. The upstream Prisma adapter executes
pg-boss SQL with separately bound values inside the current transaction;
request values never form SQL identifiers or query text. Only ciphertext
reaches queue tables. Worker exceptions omit transport responses and
validation details. Plaintext buffers are cleared after
encryption/decryption. SMTP transport is an injected port, exercised
separately in the next PR.

## Checklist

- [x] M2.2c3a acceptance checked; SMTP and HTTP flows remain open
- [x] Lint, types, builds and PostgreSQL integration tests pass
- [x] Restart, rollback, tampering and secret-disclosure tests added
- [x] No shell invocation or plaintext job persistence
- [x] Delivery enqueue joins command/audit transactions
- [x] Architecture and threat model updated
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…#97)

## Task

Review follow-up for #45 (M2.2c2). Refs #21

## What changed

- Throttling now has three axes per operation: client network, subject,
and subject from one client network (`source`).
- Credential proofs (login, verify, reset, MFA, token):
- The hard cap of 10 per 15 min moves from the subject to subject +
network.
- The subject alone keeps the progressive delay, now capped at 8 s, and
has no hard cap.
  - The subject + network axis keeps the delay up to 64 s.
- Signup and email delivery keep their per-subject cap of 5 per hour on
purpose, to prevent mailbox flooding.
- IPv6 clients are keyed by their /64 and IPv4-mapped addresses by their
IPv4, for both the network and the source axes (`clientNetwork`).
- `succeeded(subject, ip)` clears the account delay and that network's
account quota after a successful login.
- When admitted, the decision returned is the one with the fewest
remaining requests.
- SPEC and THREAT_MODEL updated.

## Why

Anyone could lock any known account out. About 10 failed logins every 15
minutes, from any address, kept the per-account bucket exhausted, so the
real owner got 429 indefinitely. A single IPv6 /64 also gave an attacker
effectively unlimited per-IP quota.

## How it was tested

- Unit tests: policy per axis (including the bounded global delay) and
`clientNetwork` (IPv4, mapped, compressed IPv6, zone ids,
non-addresses).
- Integration tests (`identity-throttle.integration.spec.ts`):
  - existing cases adjusted;
- an attacker exhausting their quota from one network does not stop the
owner from another network 8 s later;
  - rotating addresses inside one /64 shares one quota.
- `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

Trade-off: a distributed guesser is no longer hard-capped per account.
Each network gets at most 10 tries per 15 minutes per account and every
attempt on the account is spaced by up to 8 s, which still bounds online
guessing against Argon2id-hashed passwords. This follows OWASP guidance
to avoid hard lockouts.

## Checklist

- [x] Lint, TS tests pass locally
- [x] Tests added, including the lockout scenario
- [x] No new shell invocation and no new secret exposure path
- [x] Audit events unchanged
- [x] SPEC and THREAT_MODEL updated
- [ ] 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