Skip to content

[M2.2c1] Add audited account and session flows - #44

Merged
samuelmbabhazi merged 1 commit into
mainfrom
feat/m2.2c1-identity-commands
Nov 5, 2026
Merged

samuelmbabhazi merged 1 commit into
mainfrom
feat/m2.2c1-identity-commands

Conversation

@samuelmbabhazi

Copy link
Copy Markdown
Collaborator

Task

M2.2c1, part of #21. Depends on #43; base feat/m2.2b3-identity-store.

What changed and why

Add account and session commands behind application ports. Signup preserves existing credentials, verification/reset credentials are single-use, login rotates the presented session, and password reset invalidates all older sessions and reset links. Password proof is checked again against the user's security version under a row lock to prevent a concurrent reset from authorizing a stale login.

Every command mutation publishes an identity event in the same database transaction. Session authentication verifies account status, security version, idle/absolute expiry and a session-bound CSRF hash using a timing-safe comparison. SMTP delivery, request limits, public HTTP endpoints and MFA are intentionally separate remaining plan items; these commands are not exposed over HTTP in this PR.

Validation

  • Twenty-five real PostgreSQL integration tests pass, including eight complete command-flow tests.
  • Coverage includes mailbox verification, duplicate signup, expired/wrong-purpose links, concurrent reset attempts, session rotation, CSRF binding, logout idempotency, reset-versus-login races, generic credential rejection and injected audit failures.
  • Workspace lint, types, TypeScript tests and builds pass.

Security considerations

Credential values remain wrapped until hashing, storage or the mail port. Password hashing and mail delivery occur outside database transactions. A mail delivery failure leaves an unverified account eligible for the explicit resend flow; it grants no session. Request throttling and production delivery must land before exposing the commands publicly. No new shell invocation or agent action is introduced.

Checklist

  • M2.2c1 acceptance checked; M2.2c remains open
  • Lint, types, tests and builds pass
  • Concurrency, replay, expiry and audit-failure tests added
  • No new secret logging or shell invocation
  • State changes audited atomically
  • Remaining HTTP, delivery, throttling and MFA work explicit in PLAN

@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.2b3-identity-store branch 3 times, most recently from 2a98306 to 6432448 Compare November 5, 2026 12:13
@samuelmbabhazi
samuelmbabhazi changed the base branch from feat/m2.2b3-identity-store to main November 5, 2026 12:13
@samuelmbabhazi
samuelmbabhazi force-pushed the feat/m2.2c1-identity-commands branch from 8384abf to 426973c Compare November 5, 2026 12:14
@samuelmbabhazi
samuelmbabhazi merged commit 3e2db5c into main Nov 5, 2026
9 checks passed
@samuelmbabhazi
samuelmbabhazi deleted the feat/m2.2c1-identity-commands branch November 5, 2026 12:14
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
## 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

- [x] M2.2c2 acceptance checked; HTTP integration remains open
- [x] Lint, types and tests pass
- [x] Real concurrency and quota-boundary tests added
- [x] No shell invocation or plaintext secret persistence
- [x] Telemetry only; account commands remain audited
- [x] SPEC and threat model updated
samuelmbabhazi added a commit that referenced this pull request Nov 5, 2026
…#101)

## Task

Review follow-up for #44 (M2.2c1). Refs #21

## What changed

**API**
- `POST /auth/verify` takes `{ token, password }` (`VerifyEmailDto`).
Verification checks the password against the stored hash, with Argon2id
work outside any lock or transaction. Under the user lock it then
rechecks that the hash did not change in the meantime, before consuming
the link and activating the account.
- A wrong password does not consume the verification link.
- Password reset (request and confirm) accepts pending accounts as well
as active ones. A reset on a pending account activates it
(`identity.email_verified` then `identity.password_reset`), invalidates
outstanding verification links, and advances the security version as
before.
- `openapi.json` and the api-client regenerated.

**Web**
- The verification page asks for "Password you chose when signing up"
(`autocomplete=current-password`) and links to password reset for people
who did not create the account or forgot the password.

**Docs and tests**
- SPEC 8.1 and a new THREAT_MODEL row (S4) describe the defence.
- New integration test for the full attack: an attacker registers the
owner's address with their own password. The owner cannot activate that
password with the link, claims the account through reset, and afterwards
the attacker's password and verification link are both rejected.
- New integration test: a wrong password does not consume the link.
- Existing unit, integration and browser flows now send the password
with the code.

## Why

Account pre-hijacking:
1. An attacker registers `[email protected]` with their own password.
The account stays pending and the verification link goes to the victim.
2. A later signup by the victim was silently ignored, and reset refused
pending accounts.
3. The victim clicked the link, which activated the attacker's password.
4. The attacker could then log in, enroll MFA and create API tokens.
Enrolling MFA would lock the owner out even after a reset.

## How it was tested

- `nx run-many -t lint typecheck test build -p web api` in a clean
worktree: 12 tasks pass.
- `pnpm gen:openapi` and `pnpm gen:api-client`: the committed output is
the regenerated output.
- Integration and browser 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

- Contract change on `POST /auth/verify`: the password is now required.
Only the bundled web UI calls this endpoint, and it is updated in this
PR.
- The verify endpoint keeps its `verify` attempt limit (per network and
account after #97). No new enumeration signal: every failure returns the
same 401.

## Checklist

- [x] Lint, typecheck, tests and build pass locally
- [x] Malicious-scenario integration test added
- [x] No new shell invocation and no new secret exposure path
- [x] Audit events emitted for the new activation path
- [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