Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/PROJECT_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

**Stage:** Phases 1–4 are complete. `@pegma/identity@0.1.0` is published and
live in pegma.dev with `@pegma/authorization-identity@0.1.2`. The public API
remains unstable.
remains unstable. `0.1.1` is a documentation-only patch that closes the
operator-guidance findings from the 2026-07-28 security scan; it changes no
runtime behavior and no public API.

The audited `0.0.0` name-reservation bootstrap is published and deliberately
unadvertised. Version `0.1.0` contains the reviewed full passwordless
Expand Down
29 changes: 29 additions & 0 deletions docs/RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
# Release notes

## 0.1.1

`0.1.1` closes the operator-guidance findings from the 2026-07-28 repository
security scan. It changes no runtime behavior and no public API; the packed
`dist/` output is identical to `0.1.0`. The release exists so consumers
receive the corrected package README.

### Security documentation

- `claimsFor` and `repairUserByEmail` are now named as privileged operations
alongside `provisionVerifiedUser`. None of the three may be reachable from
unauthenticated traffic or from a user-supplied `principalId` or email.
- The account-creation flow is documented as also authenticating existing
accounts: `finishAccountCreation` returns an existing principal's claims,
so the creation endpoint needs sign-in-grade rate limiting, session policy,
and anomaly detection.
- Email change now carries explicit host obligations: authenticate the
endpoint for the named principal, and require a fresh passkey assertion
before `beginEmailChange`. The old-address notification is a detective
control that arrives only after the change commits.
- `docs/THREAT_MODEL.md` records two acceptances explicitly: authenticator
attestation is not verified (`attestationType: "none"`), and email-change
freshness is a host obligation rather than a component invariant.

Three scan findings were reviewed and disputed as non-findings, with the
reasoning kept in `docs/securityscan.md`: plaintext deliverable addresses at
rest, deterministic HMAC-derived codes, and the credential-ID timing
distinction in `finishPasskeyAuthentication`.

## 0.1.0

`0.1.0` is the first advertised stable release of `@pegma/identity`. It
Expand Down
14 changes: 14 additions & 0 deletions docs/THREAT_MODEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ Security invariants:
the reservation; an active generation must cross-check the independently
stored passkey mirror's owner, credential identity, generation, public key,
transports, label, creation time, and counter.
- Authenticator attestation is deliberately not verified. Registration uses
`attestationType: "none"`, so no authenticator-model or AAGUID policy is
expressed and software passkeys are accepted. This is the correct default
for consumer passkeys; a host that must assume hardware-bound keys cannot
obtain that assurance from this component.
- Challenges are unpredictable, hashed at rest, short-lived, single-use,
attempt-bounded, and version-conditionally swept through storage-core's
authoritative bounded collection scan. The adapter returns at most the
Expand Down Expand Up @@ -119,6 +124,15 @@ Security invariants:
- Email change is a repairable cross-collection saga. Old-address notification
is atomically enqueued with completed operation state only after User and
new-index agreement.
- Email change carries no freshness requirement inside the component: it
verifies only that the named principal is active and email-verified, and
mailbox control of the new address completes it. Authenticating the
change endpoint for that principal and requiring a fresh passkey assertion
before `beginEmailChange` are host obligations, recorded in the package
README. The old-address notification is a detective control that arrives
only after the change commits, so a host that skips step-up leaves a
hijacked session able to redirect email sign-in and recovery, bounded by
the victim's retained passkeys.
- Mail delivery is at least once. Provider idempotency keys fence ambiguous
sends; authenticated callbacks are generation-fenced; dead-letter and
terminal-unknown outcomes remain operator-visible until acknowledged.
Expand Down
34 changes: 26 additions & 8 deletions docs/securityscan.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,65 @@ Findings are appended below in the order they were discovered during the scan.

---

*(Scan complete — 7 findings, none Critical/High/Medium. Summary at bottom.)*
_(Scan complete — 7 findings, none Critical/High/Medium. Summary at bottom.)_

### F-01 — Privileged claim-issuing primitives on the public API surface

- **Severity:** Low (hardening / trust-boundary documentation)
- **Status:** Mitigated by documentation — `packages/identity/README.md:153-170` explicitly labels `provisionVerifiedUser` "an explicitly privileged provisioning operation" and warns "Do not expose `provisionVerifiedUser` directly as public signup"; `claimsFor` is shown inside the same privileged block. Residual: the warning covers provisioning but does not separately call out `claimsFor`/`repairUserByEmail`; a one-line addition would close the gap.
- **Status:** Resolved 2026-07-29 — the "Verified provisioning" section of `packages/identity/README.md` now names all three methods. At scan time it labelled `provisionVerifiedUser` "an explicitly privileged provisioning operation" and warned "Do not expose `provisionVerifiedUser` directly as public signup", but did not separately call out `claimsFor`/`repairUserByEmail`; that residual is closed.
- **Evidence:** `createIdentity()` exposes `provisionVerifiedUser`, `repairUserByEmail`, and `claimsFor` directly on the frozen public object (`packages/identity/src/index.ts:389-393`). `claimsFor` (`packages/identity/src/users.ts:456-463`) returns `VerifiedIdentityClaims` for **any** principalId that resolves to an active verified user, with no proof of possession. `provisionVerifiedUser` (`packages/identity/src/users.ts:334-382`) creates an already-verified user without any email-code ceremony.
- **Exploitability:** Not exploitable inside the component — these exist so the verified email-code repair path and host bootstrap can run. Risk is realized only if a host wires them to an unauthenticated route; then arbitrary verified-account creation / claim minting for any known principalId.
- **Recommendation:** Document in the package README that these three methods must never be reachable from unauthenticated traffic; consider a naming or options-level gate in a future API revision.
- ✅ Resolved 2026-07-29 — `packages/identity/README.md` now names `claimsFor` and `repairUserByEmail` as privileged alongside `provisionVerifiedUser` and forbids reaching any of the three from unauthenticated traffic or user-supplied identifiers.

### F-02 — Email change requires no fresh authentication; old address notified only after the fact

- **Severity:** Low
- **Status:** Open — design tradeoff, partially mitigated
- **Status:** Resolved 2026-07-29 by host guidance — the underlying design tradeoff stands (the component owns no session, so it cannot observe assertion freshness); the obligation now sits explicitly with the host in `packages/identity/README.md` and `docs/THREAT_MODEL.md`.
- **Evidence:** `begin("email_change", ...)` (`packages/identity/src/email-codes.ts:302-322`) requires only that the principal is active + email-verified. Proof of control of the **new** inbox (`finishEmailChange`) completes the change; the old address receives `old-address-notification` only after `effectState: "complete"` (`packages/identity/src/email-codes.ts:604-619`, `803-870`).
- **Exploitability:** An attacker holding a hijacked session (sessions are out of component scope) can redirect email-code sign-in and recovery to an inbox they control. Bounded by: passkeys remain bound to the principal (victim retains passkey sign-in and can revert), and the old-address notification is a detective control.
- **Recommendation:** Host guidance to require a fresh passkey assertion before `beginEmailChange`; confirm coverage in `docs/THREAT_MODEL.md`.
- ✅ Resolved 2026-07-29 — README now requires hosts to authenticate the change endpoint for the named principal and to demand a fresh passkey assertion before `beginEmailChange`; the freshness gap and its bound are recorded as an invariant in `docs/THREAT_MODEL.md`. No code change: the component owns no session, so freshness is not observable here.

### F-03 — WebAuthn attestation is not verified

- **Severity:** Informational
- **Status:** Accepted design (confirm in threat model)
- **Status:** Accepted design — confirmed in `docs/THREAT_MODEL.md` on 2026-07-29
- **Evidence:** `attestationType: "none"` in `beginRegistration` (`packages/identity/src/passkeys.ts:785`). No authenticator-model/AAGUID policy is possible; software passkeys are permitted.
- **Exploitability:** N/A for typical consumer passkey deployments; relevant only if a host assumes hardware-bound keys.
- ✅ Resolved 2026-07-29 — the acceptance is now an explicit invariant in `docs/THREAT_MODEL.md`, stating that attestation is not verified, that software passkeys are accepted, and that hosts needing hardware-bound keys cannot get that assurance here.

### F-04 — Plaintext PII (email addresses) at rest

- **Severity:** Informational
- **Status:** Accepted design
- **Evidence:** Email addresses are stored unhashed in `UserRecord.email`, `EmailIndexRecord.email`, `EmailCodeOperationRecord.targetEmail`/`oldEmail` (`packages/identity/src/records.ts:18-54`, `packages/identity/src/email-operation-records.ts:26-56`). Required so the Mail worker can deliver; hashes own lookup/uniqueness. At-rest protection is delegated to the storage layer.
- ⚠️ Disputed 2026-07-29 — not a valid finding: a deliverable address is not a secret and cannot be hashed. `prepareMail` renders to `operation.targetEmail`/`oldEmail` and the Mail job's `recipientRef`, so a recoverable address is a functional requirement of asynchronous delivery, not an oversight. Lookup and uniqueness already run on domain-separated digests, the threat model treats email as attacker-knowable contact data (`docs/THREAT_MODEL.md:60`), and at-rest confidentiality sits with the storage adapter and operator (boundary 4). No defect and no residual action in this component.

### F-05 — Email codes are deterministic HMAC outputs, not random values

- **Severity:** Informational
- **Status:** Accepted design — key-management dependency
- **Evidence:** `createHmacEmailCodeProtector` (`packages/identity/src/crypto.ts:80-162`) derives the code as `HMAC(secret, handleHash ‖ block) mod 10^8`. The stored verifier is separately domain-separated (`email-codes.ts:331-343`). Code secrecy rests entirely on the host-owned HMAC secret; compromise of the secret exposes all in-TTL codes. Minimum 32-byte secret enforced (`crypto.ts:83-92`); secret zeroed after key import (`crypto.ts:102`).
- **Positive observations (no action):** unbiased rejection sampling (`crypto.ts:104-127`), constant-time verifier compare (`crypto.ts:63-72`, `156-159`), 8-digit space bounded by `maxAttempts ≤ 10` + TTL ≤ 15 min (decoder invariants `email-operation-records.ts:245-247`) + dual rate limiters (`email-codes.ts:480-484`).
- ⚠️ Disputed 2026-07-29 — not a valid finding: "deterministic" describes the derivation, not the guessability. The HMAC input is the digest of a fresh per-operation handle from `newId()` (`crypto.randomUUID()` by default, 122 bits), so each code is a PRF output over a random input and is uniformly distributed and unpredictable to anyone without the key — the same guarantee a CSPRNG code would give. Determinism is required so an asynchronous Mail worker can render a code that was never stored. The key-compromise dependency is inherent to any keyed scheme and is already an explicit invariant (`docs/THREAT_MODEL.md:111-118`) with a documented rotation procedure in the package README. No defect and no residual action.

### F-06 — `finishAccountCreation` silently doubles as email sign-in for existing accounts

- **Severity:** Informational
- **Status:** Accepted design — host-policy dependency
- **Status:** Accepted design — host-policy dependency, documented in `packages/identity/README.md` on 2026-07-29
- **Evidence:** When a code is verified for an email that already belongs to an active principal, `repair()` returns the existing user (`packages/identity/src/email-codes.ts:689-710`) and `finishAccountCreation` issues `VerifiedIdentityClaims` for that principal (`email-codes.ts:758-774`). The account-creation endpoint is therefore also a full sign-in endpoint.
- **Exploitability:** None beyond the intended mailbox-control-equals-sign-in model, but a host that treats "signup" as lower-risk than "login" (weaker rate limiting, no session policy, no anomaly detection on the creation route) would under-protect a sign-in path.
- **Recommendation:** State explicitly in the package README that the creation flow authenticates existing accounts and must carry the same endpoint policy as sign-in.
- ✅ Resolved 2026-07-29 — README now states that `finishAccountCreation` returns an existing principal's claims and that the creation endpoint must carry the same rate limiting, session policy, and anomaly detection as `finishEmailSignIn`.

### F-07 — Timing side-channel distinguishes registered credential IDs

- **Severity:** Low
- **Status:** Accepted per threat model (credential IDs are not secrets)
- **Evidence:** `finishAuthentication` returns via `verificationFailed(claim)` before `verifyAuthenticationResponse` when the credential ID is unknown (`packages/identity/src/passkeys.ts:936-962`); a registered ID proceeds through signature verification, a measurably more expensive path.
- **Exploitability:** An attacker who already possesses a candidate credential ID can confirm its registration remotely. Credential IDs are random, non-enumerable authenticator outputs; the threat model (`docs/THREAT_MODEL.md:60`) explicitly treats them as non-secret. Residual value is negligible.
- ⚠️ Disputed 2026-07-29 — not a valid finding: the oracle only confirms a value the attacker already holds, and it discloses neither a principal nor an account's existence, so it is below the Low bar in this repository's severity calibration. Each probe additionally costs one `beginPasskeyAuthentication` challenge — claimed and released per attempt (`passkeys.ts:952-962`, `745-755`) — under the authentication limiter. Equalizing the path would mean running a synthetic signature verification against a fabricated public key on every unknown credential, adding attacker-triggerable CPU cost and a new failure mode to protect a value the threat model declares non-secret (`docs/THREAT_MODEL.md:60`). Behavior is unchanged deliberately.

---

Expand Down Expand Up @@ -93,6 +100,17 @@ Findings are appended below in the order they were discovered during the scan.

### Suggested follow-ups (non-blocking)

1. README: one line stating `claimsFor`/`repairUserByEmail` are privileged alongside `provisionVerifiedUser` (F-01).
2. README: one line stating the account-creation flow authenticates existing accounts (F-06).
3. Threat model: one line recording that attestation is not verified (`attestationType: "none"`, F-03) so the acceptance is explicit.
1. README: one line stating `claimsFor`/`repairUserByEmail` are privileged alongside `provisionVerifiedUser` (F-01). — done 2026-07-29.
2. README: one line stating the account-creation flow authenticates existing accounts (F-06). — done 2026-07-29.
3. Threat model: one line recording that attestation is not verified (`attestationType: "none"`, F-03) so the acceptance is explicit. — done 2026-07-29.

## Disposition review

**Reviewed:** 2026-07-29

Every finding was re-read against the implicated code before disposition.

- **Resolved:** F-01, F-02, F-03, F-06 — operator-guidance gaps closed in `packages/identity/README.md` and `docs/THREAT_MODEL.md`. All three suggested follow-ups above are complete.
- **Disputed:** F-04, F-05, F-07 — accurate observations that do not describe a weakness in this component; the reasoning is recorded on each item.

No runtime source changed, so the public API and behavior of `@pegma/identity` are unchanged. The `0.1.1` patch release exists to ship the corrected package README to consumers.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "identity",
"version": "0.1.0",
"version": "0.1.1",
"private": true,
"description": "Passkeys-first, passwordless first-party identity for Pegma hosts.",
"license": "MIT",
Expand Down
24 changes: 24 additions & 0 deletions packages/identity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ fallback, and recovery do not disclose whether an address exists. Unknown
fallback/recovery requests commit durable suppression records with the same
request-path transaction shape; they can never become valid later.

The creation flow also authenticates existing accounts. When the verified
address already belongs to an active principal, `finishAccountCreation`
returns that principal's claims instead of failing, because mailbox control
is the same proof either way. Treat the creation endpoint as a sign-in
endpoint: give it the same rate limiting, session policy, and anomaly
detection as `finishEmailSignIn`, never a weaker "signup" policy.

`beginEmailChange` and `finishEmailChange` are authenticated management
operations. The host must prove the caller owns the named `principalId`
before calling either; the library only checks that the principal is active
and email-verified. Because mailbox control of the _new_ address completes
the change, and the old address is notified only after the change commits,
require a fresh passkey assertion — not merely a live session — immediately
before `beginEmailChange`. Without that step-up, a stolen session can
redirect email sign-in and recovery to an attacker-controlled inbox; the
victim's passkeys stay bound to the principal, so passkey sign-in and a
reverting change remain available.

`emailCodeSecret` must contain at least 32 random bytes loaded from stable,
host-managed secret storage on every process restart; never generate it during
process startup. Pass a plain `Uint8Array`. Node callers that decode with
Expand Down Expand Up @@ -168,6 +186,12 @@ const claims = await identity.claimsFor(user.principalId);
Do not expose `provisionVerifiedUser` directly as public signup. Normal
account creation verifies an email code through `beginAccountCreation` and
`finishAccountCreation`.
`claimsFor` and `repairUserByEmail` are privileged in exactly the same sense:
`claimsFor` mints verified claims for any active verified `PrincipalId`
without proof of possession, and `repairUserByEmail` completes a reservation
for any known address. None of these three methods may ever be reachable from
unauthenticated traffic, or from a request whose `principalId` or email is
taken from user input rather than from an already-authenticated context.
The reserve/prepare/commit/activate protocol is durable across crashes;
`repairUserByEmail` completes an interrupted operation. Concurrent equivalent
emails converge on the one principal that won the structural reservation.
Expand Down
2 changes: 1 addition & 1 deletion packages/identity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pegma/identity",
"version": "0.1.0",
"version": "0.1.1",
"description": "Passkeys-first, passwordless first-party identity for Pegma hosts.",
"license": "MIT",
"type": "module",
Expand Down
Loading