From a0aea85f3ab94e6fbd20be56db2174657931f293 Mon Sep 17 00:00:00 2001 From: Evelyn Osman Date: Thu, 12 Nov 2026 13:50:14 +0100 Subject: [PATCH 1/2] feat: mount the public incoming migration API Mount the incoming migration flow that earlier changes shipped dormant. Browser surfaces under /incoming let the saved user prepare, renew, cancel, recover, resume and activate an arrival and see its state. Each POST needs the verified session, same-origin and CSRF checks and, where the action is sensitive, a fresh email code. A repeat prepare by the same saved user returns the existing operation ID. Tool surfaces reuse standard XRPC. submitPlcOperation and createAccount are treated as incoming only when the Entryway-Arrival-Code header is present and are then never handled as hosted or managed movement. The arrival code selects the arrival and its saved user, and the method-bound source JWT is verified before the ownership lease is taken. createAccount returns tracked credentials only after the account is bound. refreshSession and activateAccount serve a tracked arrival credential and otherwise fall through to the ordinary routes. Pending arrivals are refused by registration, the account console, OAuth consent and signup, in addition to the existing status and migration checks. Activation by any consumer goes through the single guarded operation. Account storage gains bindPlacedAccount, which fails closed on a binding to another user. No schema change and no configuration key. The live public journey, the contracts group and the stock PDS callback have not been run for this change. This work was assisted by a machine --- .changeset/incoming-migration-public-api.md | 82 +++ docs/architecture.md | 16 +- docs/data-custody.md | 8 +- docs/incoming-migration.md | 154 ++++- docs/reuse-assessment.md | 8 +- docs/testing.md | 38 +- src/accounts/pending-arrival.test.mjs | 333 +++++++++ src/accounts/pending-arrival.ts | 76 +++ src/app-incoming.test.mjs | 161 +++++ src/app.mjs | 17 + src/compose-authentication.mjs | 2 + src/compose-incoming-arrival.mjs | 112 +++- src/compose-protocol.mjs | 4 + src/database/accounts.port.ts | 2 + src/database/drizzle/account-binding.test.mjs | 41 ++ src/database/drizzle/account-storage.ts | 21 + .../account-registration/create-account.ts | 7 + src/features/account-registration/page.mjs | 8 + .../operation-recovery-routes.test.mjs | 9 +- src/features/email-login/routes.test.mjs | 1 + .../external-migration/activate-arrival.ts | 19 +- .../external-migration/arrival-summary.ts | 39 ++ .../incoming-routes.test.mjs | 380 +++++++++++ .../external-migration/incoming-xrpc.test.mjs | 634 ++++++++++++++++++ src/features/external-migration/page.ts | 108 +++ .../external-migration/prepare-arrival.ts | 41 +- src/features/external-migration/routes.ts | 285 ++++++++ .../external-migration/tool-admission.ts | 207 ++++++ .../external-migration/xrpc-routes.ts | 153 +++++ .../authorization-routes.test.mjs | 1 + src/features/oauth-authorization/consent.mjs | 12 + .../oauth-authorization/consent.test.mjs | 35 + .../signup-consent-skip.test.mjs | 1 + src/http/account-console.mjs | 16 + src/logging/request-event.ts | 4 + .../browser/incoming-public-journey.spec.mjs | 422 ++++++++++++ .../incoming-arrival-composition.test.mjs | 4 + tests/local.sh | 13 +- tests/playwright.config.mjs | 45 +- .../helpers/incoming-arrival-fixture.mjs | 43 +- tests/support/helpers/incoming-http.mjs | 131 ++++ 41 files changed, 3615 insertions(+), 78 deletions(-) create mode 100644 .changeset/incoming-migration-public-api.md create mode 100644 src/accounts/pending-arrival.test.mjs create mode 100644 src/accounts/pending-arrival.ts create mode 100644 src/app-incoming.test.mjs create mode 100644 src/database/drizzle/account-binding.test.mjs create mode 100644 src/features/external-migration/arrival-summary.ts create mode 100644 src/features/external-migration/incoming-routes.test.mjs create mode 100644 src/features/external-migration/incoming-xrpc.test.mjs create mode 100644 src/features/external-migration/page.ts create mode 100644 src/features/external-migration/routes.ts create mode 100644 src/features/external-migration/tool-admission.ts create mode 100644 src/features/external-migration/xrpc-routes.ts create mode 100644 tests/browser/incoming-public-journey.spec.mjs create mode 100644 tests/support/helpers/incoming-http.mjs diff --git a/.changeset/incoming-migration-public-api.md b/.changeset/incoming-migration-public-api.md new file mode 100644 index 00000000..bb1e070d --- /dev/null +++ b/.changeset/incoming-migration-public-api.md @@ -0,0 +1,82 @@ +--- +"hypercerts-entryway": minor +--- + +Entryway now accepts an existing account from an external PDS. Earlier incoming +changes shipped dormant; this release mounts the whole flow, so the lifecycle, +tracked-credential and activation behavior they describe is now reachable. +It does not certify that an account's data transferred completely, and it adds +no schema change or configuration key. The routes are mounted on every +deployment (single-node SQLite, multi-node, and deployments with no migration +intent) with no configuration key or opt-out. No live public +journey, contracts group or stock-PDS callback proof has been run for this +change; it is covered by unit and in-process application tests only. + +What operators and users now see: + +- **Browser surfaces.** `GET /incoming` and `POST /incoming/prepare`, `/renew`, + `/cancel`, `/recover`, `/activate`, `/resume` and `/code`. Each needs the + verified browser session, the same-origin CSRF token and, except `/resume`, + `/code` and the read-only page, a fresh destination email code. The body is + strict JSON (or a form), the same-origin check runs before any budget is spent, and an unknown field is `InvalidRequest`. Another user's + operation and an unknown one are the same `AccountNotFound` 404. Responses are + `Cache-Control: no-store`. The arrival code is shown once and only its hash is + stored. +- **Tool surfaces (standard XRPC).** `com.atproto.identity.submitPlcOperation` + and `com.atproto.server.createAccount` are incoming only when the + `Entryway-Arrival-Code` header is present, and then are never handled as + hosted-account or managed movement. They also need a source service JWT bound + to that method, to Entryway's DID and to the arrival's DID. Without the header + those routes behave as before. `createAccount` returns the standard tracked + access and refresh credentials only after the account is bound; its supported + body is `handle` plus optional `did`, `email` and `inviteCode`. Other stock + fields, including `password`, are rejected with `InvalidRequest`, so an + unmodified stock tool that sends `password` does not work (pending owner + decision). `refreshSession` and `activateAccount` serve a tracked + arrival credential and otherwise fall through unchanged, after one arrival + lookup on every such request (pending owner decision). Once a migration is + finished, a later ordinary deactivation and reactivation use the ordinary route. +- **Proposed, awaiting owner decision:** the tool surfaces carry no cookie, so + the presented current arrival code selects the arrival and the user saved on + it. This is one bounded resolution that may change. +- **Lost operation ID.** A repeat `/incoming/prepare` by the same saved user for + the same DID, after a valid fresh email code, keeps its existing refusal and + adds that user's own `operationId` and `version`. Never an arrival code, and + never for another user's arrival. +- **Extra surfaces.** `/incoming/resume`, `/incoming/activate`, `/incoming/code` + and the `GET /incoming` status page are not in the accepted contract (pending + owner decision). +- **Lifecycle.** An arrival code lasts 24 hours and is renewable with a fresh + email code. A prepared arrival cannot be cancelled to release its + reservations: cancel returns `OperationRecoveryRequired`. Retrying a tool + request is the same body with the same credential until it expires or the + identity moves to the destination (after that, a `createAccount` retry gets 403 + `AccountMismatch` because the source key no longer verifies); later, the saved user recovers with + `/incoming/recover` (new credentials, fresh email code) or continues with + `/incoming/resume`. +- **Pending accounts.** Until activation a migrated account is paused. OAuth + authorization, account settings, signup, status, handle, + deletion and PLC operations refuse it (`MigrationPending` or an honest + page), and ordinary status changes cannot activate it. Browser email sign-in still + succeeds by design and then shows the pending page. By consumer class, + `assertPermitted` protects registration, `assertNoMigration` protects status, + handle, deletion and PLC changes, the `provisioning` status protects login, + security changes, admin and OAuth paths, and `isPending` protects the console, + consent and signup pages. A single enforced guard is follow-up work. Activation is one + operation for the saved user (fresh email code, `/incoming/activate`) and for + an authorized client (its tracked access credential on + `activateAccount`, including the stock PDS callback). The client decides when + to activate; Entryway checks identity, binding and backend state, not + repository or blob completeness, and does not stop later source writes. +- **Limits you should know.** Rotating or replacing credentials does not revoke + an access token already issued: the stock PDS accepts it until it expires + (five minutes). Request budgets reuse existing producers: 30 requests per IP + per ten minutes for each of the browser and tool groups (submit and create + share one tool bucket, so retries after `OperationRecoveryRequired` spend it), and five fresh-code + requests per ten minutes per purpose and user. New stable error codes in + failure logs: `ArrivalExpired`, `ArrivalProofRequired`, `OperationConflict`, + `OperationRecoveryRequired`. + +See the [incoming migration guide](https://github.com/hypercerts-org/hypercerts-entryway/blob/main/docs/incoming-migration.md) +for the accepted contract and the open decisions. Independent public +qualification of the whole journey has not been completed. diff --git a/docs/architecture.md b/docs/architecture.md index cbc3a2e1..29a99450 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -185,8 +185,20 @@ backend state and durable operation ownership. It does not certify a final sourc head or full inventory, or provide a global verification-to-activation write barrier. Concurrent/later source writes and missing blobs may not transfer. Early PLC cutover, create-active then pause, pending-account guards and isolation/drain recovery remain -required. This is design acceptance, not completion of credentials, mounted public -routes or stock-PDS public journey/recovery qualification. +required. The external-migration feature now owns the mounted incoming routes +(browser pages and strict JSON, and the tool-facing XRPC surfaces selected by the +`Entryway-Arrival-Code` header), registered explicitly through +`compose-incoming-arrival.mjs` and `app.mjs`. A pending arrival is refused by a mix of +existing checks (`assertNoMigration`, the `provisioning` status) and the explicit +predicate and action names in `accounts/pending-arrival.ts` (one enforcing caller; +a single enforced guard is follow-up work); one activation operation +serves the saved user, an authorized client and the stock PDS's forwarded +`activateAccount`. Tracked credentials are the existing legacy families, so a stock +PDS keeps accepting an already-issued access token until it expires (five minutes) +after recovery. Ordinary refresh and activate now do one arrival lookup first. The cookie-less tool resolution (arrival code selects the arrival +and its saved user) is proposed and awaits owner decision. This is not stock-PDS +public journey or recovery qualification, which has not been run; see +[incoming migration](incoming-migration.md#development-mounted-surfaces-and-pending-guard). The architecture checker resolves real imports, including TS/MJS aliases, dynamic imports and require. It rejects extra ports, raw SQL/SMTP/Better Auth access outside diff --git a/docs/data-custody.md b/docs/data-custody.md index 4335318d..931ac9d2 100644 --- a/docs/data-custody.md +++ b/docs/data-custody.md @@ -408,7 +408,13 @@ backend provisioning and deactivation, before transfer, so the tool can receive destination credentials. A bound migration-pending account is not active and cannot bypass guarded activation or ordinary-access restrictions. This differs from the current fixture's post-transfer binding and does not change existing -signup activation semantics. Implementation and public qualification remain open. +signup activation semantics. The routes are mounted (see +[mounted surfaces](incoming-migration.md#development-mounted-surfaces-and-pending-guard)); +public qualification remains open. Custody facts that hold in the mounted code: a +source service JWT proves control of the source key for one method and is never +hosted authority, a pending DID has no hosted account for it to act on, the saved +user comes from the arrival and its browser proof and never from email alone, and +the arrival code is shown once and stored only as a hash. The user accepted the [C4 client-managed contract](incoming-migration.md#c4-client-managed-migration-contract-accepted) on 2026-11-11, replacing the former Entryway final-source/inventory certification diff --git a/docs/incoming-migration.md b/docs/incoming-migration.md index 878f524c..432aa4d3 100644 --- a/docs/incoming-migration.md +++ b/docs/incoming-migration.md @@ -1,7 +1,8 @@ # How incoming account migration works **Status:** accepted design, updated November 11, 2026 for the client-managed -migration contract; implementation and public runtime qualification remain pending. +migration contract. The routes below are mounted; independent public runtime +qualification of the whole journey has not been completed. Incoming migration moves an existing account from an external PDS to a PDS behind Entryway. The account keeps its DID; the migration client transfers its records @@ -459,7 +460,8 @@ continueHandoff(request, operationId) continueCreation(request, operationId) ``` -These are internal callables, not mounted routes. Each loads the normalized verified +These are internal browser-bound callables; the browser routes that call them are +listed under [mounted surfaces](#development-mounted-surfaces-and-pending-guard). Each loads the normalized verified browser principal and checks origin/CSRF. Its saved user ID selects the arrival; `claim.resource` is the separate `arrival:` ownership resource, never a user ID. The same pending preparation ownership intent is reacquired and fenced before work. @@ -520,15 +522,16 @@ These associations do not mark or clear repository-allocation uncertainty. Binding the saved user and completing the invitation share one database transaction. A bound pending retry returns `{state: "pending", operationId, version}` without -remote effects. No public route, transfer credential or activation is added. Exhaustive +remote effects. Exhaustive crash/rehydration qualification, activation implementation and public migration qualification remain deferred. ### Development: tracked credentials and guarded activation `composeIncomingArrival` also exposes dormant credential and activation callables. -It now takes the application's `legacy` credential instance. No route, page, XRPC -handler, stock-callback consumer or pending guard is mounted; plan 012 injects these. +It takes the application's `legacy` credential instance. The mounted routes, XRPC +handlers, stock-callback consumer and pending guard that call these are described +under [mounted surfaces](#development-mounted-surfaces-and-pending-guard). Failures use stable codes from `DomainError` (`code`) or `HttpError` (`error`). ```text @@ -653,6 +656,147 @@ change. `ArrivalAuthority` gains `activatePlacedAccount`, `completeArrivalActiva performs the flip, `settleArrivalAttempt` also retires an attempt replaced by an approved safe retry, and `readArrivalFamilies` and `readArrivalFamily` are read-only. +### Development: mounted surfaces and pending guard + +**Status: implemented and unit-tested; no live public journey, no contracts group +and no stock-PDS callback proof has run.** Everything marked "pending owner +decision" below is this change's choice, not accepted contract. `src/app.mjs` +mounts the feature on every deployment (single-node SQLite, multi-node, and +deployments with no migration intent) with no configuration key or opt-out; this +is decided. Public qualification has not yet run, so the change is held as an +unmerged draft until the live journey and contracts pass. It is mounted through `createIncomingArrival` in +`src/compose-incoming-arrival.mjs` (production arrival storage over the account +authority, real browser authentication and CSRF) and registers +`src/features/external-migration/routes.ts` and `xrpc-routes.ts`. XRPC handlers are +registered before the ordinary protocol routes so an incoming request is selected +explicitly and never by fall-through. + +Browser surfaces, all under the issuer host. Each POST takes a strict body, rejects +an unknown field as `InvalidRequest` (400), checks origin and the CSRF `csrf` field +before any code is spent, and sets `Cache-Control: no-store`. A JSON request gets a +JSON response; a form post gets an HTML page. All spend the per-IP budget +`incoming-browser-ip` (30 per ten minutes, the existing producer's 30-per-IP limit) +only after the same-origin check, so a cross-origin post spends nothing. `GET +/incoming` has no budget, like the other GET pages. The `/incoming/resume`, +`/activate` and `/code` surfaces and the `GET /incoming` status page are not in the +C1 table (pending owner decision). + +Lost operation ID. If the SAME saved user repeats `/incoming/prepare` for the same DID +with a valid fresh email code and the repeat is refused with +`OperationRecoveryRequired`, and an unfinished (live, not completed or cancelled) +arrival of theirs exists, the refusal keeps its code and status and adds that arrival's `operationId` and +current `version` (JSON fields, or shown on the HTML page), so they can renew, +recover, activate or resume. This happens only after origin/CSRF, session, budget +and the fresh code have passed; the arrival is found by a read by DID, and owner scoping is +done by comparing the saved user and then reading with the owner-filtered +`readArrival`. It attaches to no other error (an ownership `OperationPending`, a +lease loss or a server failure), and if the lookup itself fails the original refusal +is what the caller sees. It returns no arrival code or other secret, changes nothing in storage, and is `no-store`. +Another user's arrival for that DID gets today's refusal with no ID. + +| Route | Body (plus `csrf`) | Proof and result | +| ------------------------- | --------------------------------------- | ------------------------------------------------------------------------------ | +| `GET /incoming` | optional `?operationId=` | Verified session; secret-free state of the saved user's own arrival | +| `POST /incoming/code` | none | Sends a sign-in code to the verified email on the `incoming-code` budget | +| `POST /incoming/prepare` | `did`, `handle`, `otp` | Fresh code; C1 preparation response, arrival code shown once | +| `POST /incoming/renew` | `operationId`, `expectedVersion`, `otp` | Fresh code; `{operationId, version, arrivalCode, expiresAt}` | +| `POST /incoming/cancel` | `operationId`, `expectedVersion`, `otp` | Fresh code; only the pre-intent seam cancels, else `OperationRecoveryRequired` | +| `POST /incoming/recover` | `operationId`, `expectedVersion`, `otp` | Fresh code; replacement tracked credentials, revoking earlier families | +| `POST /incoming/activate` | `operationId`, `expectedVersion`, `otp` | Fresh code; the one guarded activation, `{state, operationId, version}` | +| `POST /incoming/resume` | `operationId` | Saved user only; continues saved admitted steps, sends nothing new | + +Failures use the stable codes in C1: `InvalidRequest` 400 (shape, origin or CSRF), +`ArrivalProofRequired` 403, `AccountNotFound` 404 (unknown operation and another +user's operation, identical), `ArrivalExpired` and `OperationConflict` and +`OperationRecoveryRequired` 409, `RateLimitExceeded` 429. + +Tool surfaces (no cookie, no CSRF), on the standard XRPC paths: + +| Request | Incoming when | Needs | Success | +| ---------------------------------------------- | -------------------------------------------- | --------------------------------------------------------------------------------------------- | ------------------------------------ | +| `POST com.atproto.identity.submitPlcOperation` | `Entryway-Arrival-Code` header | Source JWT bound to that method, Entryway as audience, the arrival's DID; body `{operation}` | `{}` once canonical | +| `POST com.atproto.server.createAccount` | `Entryway-Arrival-Code` header | Source JWT bound to `createAccount`; body `handle` plus optional `did`, `email`, `inviteCode` | Tracked credentials, `active: false` | +| `POST com.atproto.server.refreshSession` | refresh token of a tracked unfinished family | The refresh credential | Rotated credentials | +| `POST com.atproto.server.activateAccount` | access token of a tracked arrival family | The tracked access credential (also the stock PDS's forwarded call) | `{}` | + +A tool must send exactly those fields. The strict subset rejects `password`, +`verificationCode`, `verificationPhone`, `recoveryKey` and `plcOp` with +`InvalidRequest`, and stock tools commonly send `password`, so an unmodified stock +tool does not work against this surface. Whether to accept or ignore them is a +pending owner decision. + +`refreshSession` and `activateAccount` now perform one arrival lookup before the +ordinary route, and fall through on `InvalidToken` or `ArrivalExpired`; that +fall-through design is a pending owner decision. A finished arrival's family is an +ordinary session: its committed activation result is returned only while the local +account is still `active`, and once the user deactivates it, `activateAccount` +reaches the ordinary route. With the header present these never fall through to hosted or managed movement, so +an invalid incoming credential is refused, not rerouted. Without it, and for any +token that is not a tracked arrival credential, the ordinary route runs unchanged; +a source service JWT is never offered to activation and cannot reach hosted +authority for a pending DID. The per-IP budget `incoming-tool-ip` (30 per ten +minutes) applies before admission. Submit and create share that one bucket, and +every retry, including one after `OperationRecoveryRequired`, spends from it, so a +tool that retries aggressively can exhaust it (`RateLimitExceeded`) for all callers +behind the same address. The method is fixed by the route, so an +unsupported method cannot reach admission. Identity-layer failures are +`InvalidPlcOperation` 400 or `IdentityConflict` 409. + +**Proposed, awaiting owner decision (not accepted contract).** Tool requests carry no +browser cookie, but admission and continuation resolve their owner from a browser +principal. The tool surfaces therefore select the arrival with the presented +current, unexpired arrival code, taking the saved user from the arrival and never +from an email or a request value; the DID is read from the credential's unverified +issuer only to look the code up, and admission still verifies the method-bound source +JWT. That verification (same verifier, method, audience, DID and request digest, no +replay state consumed) runs before any ownership lease is taken, and admission +verifies again and records replay. Before it, the code permits only a read; a leaked +code with an invalid credential cannot contend for the arrival, record, sign or +dispatch. Every failure to select an arrival, including an unparsable credential, +is the same `ArrivalExpired`. The replacement point is `select` in `tool-admission.ts`. + +Retry semantics that follow from stored admissions (pending owner decision whether +this is the intended contract). An exact retry is the same body +with the same credential, until it expires or the identity has moved to the +destination (the source key no longer verifies). A fresh credential for an +already-admitted body is `OperationConflict`. After the move, the tool cannot retry +`createAccount`: a retry after the destination move gets 403 `AccountMismatch` +because the source key no longer verifies. Recovery or resume is the path: a lost +first issuance is recovered by the saved user with `/incoming/recover`, and a +stalled continuation with `/incoming/resume`. + +Pending guard. A pending arrival is a `provisioning` account whose DID has an +unfinished saved arrival. The mechanism today is a mix, not a single enforced +action set. `src/accounts/pending-arrival.ts` defines the permitted and refused +action names and a predicate that reads saved arrival facts, but its +`assertPermitted` has one caller (registration's provisioning core), and the account +console, OAuth consent and signup form use its `isPending` to show an honest page. +Everything else relies on existing checks: `assertNoMigration` refuses status, +handle, deletion and PLC changes (`MigrationPending` 409), and ordinary XRPC +authentication, ordinary refresh and ordinary login refuse a `provisioning` account +(a refused refresh does not revoke the family). Browser email sign-in still +succeeds by design: it creates a session and then shows the pending page rather than +settings or a reactivation prompt. By consumer class: `assertPermitted` protects +registration; `assertNoMigration` protects status change, handle change, deletion +and PLC operations; the `provisioning` status check protects login, security +changes, admin paths and OAuth paths; `isPending` protects the console, consent and +signup pages. A single enforced guard is follow-up work; this is decided. + +Credential limits, stated plainly. Recovery revokes local families, but the stock PDS +accepts an access token already issued until it expires (five minutes). The browser +`recover` response contains credentials and is `no-store`. Nothing here deletes the +source account, and activation makes no claim about repository or blob completeness. + +Open for owner confirmation (pending owner decisions): + +- the cookie-less arrival-code resolution above; +- the exact-retry semantics (same body and same credential, until expiry or cutover); +- the refresh and activate fall-through design and its extra arrival lookup; +- the strict `createAccount` subset that rejects `password`; +- the extra browser surfaces: `/incoming/activate`, `/incoming/resume`, + `/incoming/code` and the `GET /incoming` status page; +- the per-IP budget keys `incoming-browser-ip` and `incoming-tool-ip`. + ## Contract qualification gates for implementers These are delivery contracts for implementers, not public APIs. The development diff --git a/docs/reuse-assessment.md b/docs/reuse-assessment.md index 3a87d46c..e404005d 100644 --- a/docs/reuse-assessment.md +++ b/docs/reuse-assessment.md @@ -94,9 +94,11 @@ Entryway-hosted inactive destination accepting a streamed import, and sequential same-DID reservation with placement isolation. That run is bounded by its own committed byte and phase budgets and does not establish finality, blob pagination or an activation barrier. C4 is now an accepted client-managed responsibility -contract, not a claim that those stronger guarantees were proved. C1–C5 are design contracts; existing -dormant producers cover preparation, admission and continuation only, -not mounted public APIs or complete readiness; see the +contract, not a claim that those stronger guarantees were proved. C1–C5 are design contracts; the +mounted incoming routes reuse the existing producers (arrival preparation, +admission, tracked credentials, guarded activation, challenge budgets, invitation +policy) and add no dependency or new service port. They are not complete readiness: +public journey qualification is separate; see the contract gates in [incoming migration](incoming-migration.md#contract-qualification-gates-for-implementers). diff --git a/docs/testing.md b/docs/testing.md index 3bdad0f9..d7c35d13 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -119,8 +119,8 @@ That run skips the live-gated tests; those skips are not qualification. Setting it as shown. Report live results only from an executed managed sandbox run. The offline cases cover the managed request boundary and phase budgets. They do not -prove any public behavior, and no Entryway endpoint or route implements this -migration: the contract test consumes stock PDS APIs, not Entryway handlers. +prove any public behavior: this contract test consumes stock PDS APIs, not Entryway +handlers. Entryway's own incoming routes are exercised by the public journey below. The preflight verifies the signed PLC operation against the source's observed rotation keys. The [contract gates](incoming-migration.md#contract-qualification-gates-for-implementers) @@ -129,9 +129,37 @@ accepted on 2026-11-11. The client owns transfer, checks its chosen transferred state and chooses when to request activation; Entryway does not certify a final source snapshot, exhaustive inventories or a global verification-to-activation write barrier. Counts, zero missing blobs and client intent do not replace source -ownership proof. Tracked credentials, guarded activation, public exposure and full -runtime qualification remain pending; dormant internal producers do not complete -those gates. +ownership proof. The incoming routes, tracked credentials and guarded activation are +mounted; full public runtime qualification is a separate gate that a passing unit +suite does not establish. + +### Public incoming journey + +```sh +./tests/local.sh incoming-public-journey +``` + +After `up`, this runs the one Playwright spec +`tests/browser/incoming-public-journey.spec.mjs` in the owned project's browser +container, selected by `INCOMING_PUBLIC_JOURNEY=1`; the default browser run does not +include it. It uses the standalone stock source PDS, a stock destination behind +Entryway, Mailpit and managed TLS, and reaches Entryway only through its public +browser pages and XRPC routes: a browser-verified destination email, an +arrival code, a source-signed handoff, creation returning tracked credentials, a +direct client transfer between the pinned endpoints, refresh, and activation +through the stock destination's `activateAccount`, which forwards to Entryway. It +seeds no authority rows and calls no private fixture. The client chooses what to +transfer and when to activate: the spec checks one record and one blob, not a +complete inventory, and later source writes are outside what it proves. List the +spec without a sandbox to verify discovery: + +```sh +INCOMING_PUBLIC_JOURNEY=1 npx playwright test --config tests/playwright.config.mjs --list +``` + +`fresh-group incoming-public-mechanisms incoming-public-journey contracts` runs the +same steps in one disposable project. A skipped, synthetic or negative-control +result is not a public qualification; report only an executed managed run. Contributors qualifying the dormant incoming-arrival composition run `./tests/local.sh incoming-arrival-live` after `up`. This creates fresh stock diff --git a/src/accounts/pending-arrival.test.mjs b/src/accounts/pending-arrival.test.mjs new file mode 100644 index 00000000..cd4a0659 --- /dev/null +++ b/src/accounts/pending-arrival.test.mjs @@ -0,0 +1,333 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import express from "express"; +import * as plc from "@did-plc/lib"; +import { createServiceJwt } from "@atproto/xrpc-server"; +import { + fixture, + admitProvisioning, + email, + handle, +} from "../../tests/support/helpers/incoming-arrival-fixture.mjs"; +import { query } from "../../tests/support/database-fixture.mjs"; +import { + createPendingArrivalGuard, + permittedWhilePending, + refusedWhilePending, +} from "../../dist/src/accounts/pending-arrival.js"; +import { createAccountPrimitives } from "../../dist/src/accounts/primitives.mjs"; +import { createStatusChange } from "../../dist/src/features/account-settings/change-status.mjs"; +import { createRegistration } from "../../dist/src/features/account-registration/create-account.js"; +import { createSignupForm } from "../../dist/src/features/account-registration/page.mjs"; +import { createCustodyInventoryStorage } from "../../dist/src/database/drizzle/migration-custody.js"; +import { createProtocolRouting } from "../../dist/src/http/protocol-authentication.mjs"; +import { createAccountConsole } from "../../dist/src/http/account-console.mjs"; +import { incomingCreationMethod } from "../../dist/src/features/external-migration/incoming-authorization.js"; + +const codeOf = (error) => error.code ?? error.error; +const hasCode = (code, status) => (error) => + codeOf(error) === code && (status === undefined || error.status === status); + +/** A bound, paused, tracked-credential arrival: the state every guard protects. */ +async function pending(t) { + const f = await fixture(t); + const prepared = await admitProvisioning(f); + const result = await f.composed.continueCreation( + f.request, + prepared.operationId, + ); + const session = await f.composed.issueArrivalSession( + f.request, + prepared.operationId, + result.version, + ); + return { f, prepared, session }; +} + +test("the action set is explicit, disjoint and deny-by-default", async () => { + const guard = createPendingArrivalGuard({ + hasPendingExternalMigration: async () => true, + }); + const row = { + did: "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa", + status: "provisioning", + }; + assert.equal( + permittedWhilePending.some((action) => + refusedWhilePending.includes(action), + ), + false, + ); + assert.deepEqual([...permittedWhilePending].sort(), [ + "arrival-activation", + "arrival-continuation", + "arrival-credential-recovery", + "arrival-credential-refresh", + "arrival-status", + ]); + for (const action of permittedWhilePending) + await guard.assertPermitted(row, action); + for (const action of refusedWhilePending) + await assert.rejects( + guard.assertPermitted(row, action), + hasCode("MigrationPending", 409), + action, + ); + // An action nobody reviewed is refused, not allowed. + await assert.rejects( + guard.assertPermitted(row, "some-new-ordinary-action"), + hasCode("MigrationPending", 409), + ); +}); + +test("the guard reads saved arrival facts, never the status alone", async () => { + const none = createPendingArrivalGuard({ + hasPendingExternalMigration: async () => false, + }); + const unfinishedSignup = { + did: "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa", + status: "provisioning", + }; + assert.equal(await none.isPending(unfinishedSignup), false); + await none.assertPermitted(unfinishedSignup, "registration"); + const some = createPendingArrivalGuard({ + hasPendingExternalMigration: async () => true, + }); + assert.equal( + await some.isPending({ ...unfinishedSignup, status: "active" }), + false, + ); + assert.equal(await some.isPending(null), false); + assert.equal(await some.isPending(unfinishedSignup), true); +}); + +test("ordinary setStatus cannot activate or deactivate a pending arrival", async (t) => { + const { f } = await pending(t); + const primitives = createAccountPrimitives({ + db: f.db, + config: f.config, + storage: f.storage, + ownership: f.ownership, + }); + let adminCalls = 0; + const setStatus = createStatusChange({ + db: f.db, + get: primitives.get, + save: primitives.save, + journal: primitives.journal, + serialized: primitives.serialized, + assertNoMigration: primitives.assertNoMigration, + admin: async () => { + adminCalls++; + }, + }); + for (const status of ["active", "deactivated"]) + await assert.rejects( + setStatus(f.did, status), + hasCode("MigrationPending", 409), + status, + ); + assert.equal(adminCalls, 0); + assert.equal(f.backendState.activations, 0); + assert.equal( + (await query(f.db, "SELECT status FROM accounts"))[0].status, + "provisioning", + ); + assert.equal((await f.arrivals.readArrivalAccess(f.did)).complete, false); + await assert.rejects( + primitives.assertNoMigration(f.did), + hasCode("MigrationPending", 409), + ); +}); + +test("after the one activation the same operations apply to an ordinary account", async (t) => { + const { f, session } = await pending(t); + await f.composed.activateArrivalWithCredential(session.accessJwt); + const primitives = createAccountPrimitives({ + db: f.db, + config: f.config, + storage: f.storage, + ownership: f.ownership, + }); + await primitives.assertNoMigration(f.did); +}); + +test("ordinary registration for a pending arrival's email is refused", async (t) => { + const { f } = await pending(t); + const primitives = createAccountPrimitives({ + db: f.db, + config: f.config, + storage: f.storage, + ownership: f.ownership, + }); + const registration = createRegistration({ + db: f.db, + custody: createCustodyInventoryStorage(f.db), + config: { ...f.config, plcRecoveryKeyDid: f.hot.did() }, + plcSigner: f.composed.accounts?.plcSigner ?? undefined, + plcClient: undefined, + storage: f.storage, + save: primitives.save, + claimHandle: primitives.claimHandle, + validateHandle: async () => {}, + journal: primitives.journal, + ownership: f.ownership, + }); + await assert.rejects( + registration.create({ + email, + handle, + pdsId: "pds1", + registrationIdentity: { + kind: "better-auth", + userId: f.principal.userId, + email, + }, + }), + hasCode("MigrationPending", 409), + ); + assert.equal( + (await query(f.db, "SELECT status FROM accounts"))[0].status, + "provisioning", + ); + assert.equal(f.backendState.activations, 0); +}); + +async function routing(f) { + const app = express(); + return createProtocolRouting({ + app, + db: f.db, + config: f.config, + accounts: { + get: (did) => f.storage.getByDid(did), + plcClient: { + getDocument: async () => + plc.formatDidDoc({ + did: f.did, + ...plc.normalizeOp(f.audit.at(-1).operation), + }), + }, + }, + accountAccess: {}, + oauth: {}, + legacy: f.legacy, + }); +} +const attempt = (authenticate, nsid, authorization, body = {}) => + authenticate( + { headers: { authorization }, body, res: { set() {} }, method: "POST" }, + nsid, + ); + +test("protocol authentication refuses a pending arrival's tracked credential and every service JWT", async (t) => { + const { f, session } = await pending(t); + const { authenticate } = await routing(f); + // The tracked access credential is not an ordinary account credential. + for (const nsid of [ + "com.atproto.server.getSession", + "com.atproto.server.activateAccount", + "com.atproto.server.deactivateAccount", + "com.atproto.identity.submitPlcOperation", + ]) + await assert.rejects( + attempt(authenticate, nsid, `Bearer ${session.accessJwt}`), + hasCode("AccountUnavailable", 403), + nsid, + ); + // A service JWT signed by the destination repository key verifies, but names + // no active hosted account: never managed movement or hosted authority. + const now = Math.floor(Date.now() / 1000); + for (const nsid of [ + incomingCreationMethod, + "com.atproto.identity.submitPlcOperation", + "com.atproto.server.deactivateAccount", + ]) { + const token = await createServiceJwt({ + iss: f.did, + aud: f.config.serviceDid, + lxm: nsid, + keypair: f.repository, + exp: now + 60, + }); + await assert.rejects( + attempt(authenticate, nsid, `Bearer ${token}`, { did: f.did }), + hasCode("AccountNotFound", 401), + nsid, + ); + } +}); + +test("ordinary refresh refuses a pending arrival's family without revoking it", async (t) => { + const { f, session } = await pending(t); + await assert.rejects( + f.legacy.refreshSession(`Bearer ${session.refreshJwt}`), + hasCode("AccountUnavailable", 403), + ); + // Still the tracked family's credential for the arrival's own refresh. + const refreshed = await f.composed.refreshArrivalSession(session.refreshJwt); + assert.equal(refreshed.active, false); +}); + +test("the account console shows a pending arrival an honest page, never settings", async (t) => { + const { f } = await pending(t); + const pages = []; + const console_ = createAccountConsole({ + app: express(), + accounts: { storage: f.storage }, + accountAccess: { + resolveBoundAccounts: async () => ({ + kind: "single", + account: await f.storage.getByDid(f.did), + }), + requireBoundAccount: async () => f.storage.getByDid(f.did), + }, + oauth: { + requireSession: async () => ({ ...f.principal, emailVerified: true }), + loadBrowser: async () => ({ deviceId: "d", csrf: "c" }), + checkCsrf() {}, + provider: { accountManager: { listDeviceAccounts: async () => [] } }, + }, + security: {}, + legacy: {}, + }); + const result = await console_.authenticated( + { method: "GET", query: {}, headers: {} }, + { + locals: {}, + set() {}, + status(code) { + pages.push(code); + return this; + }, + type() { + return this; + }, + send(html) { + pages.push(html); + }, + }, + ); + assert.equal(result, null); + assert.equal(pages[0], 409); + assert.match(pages[1], /unfinished incoming migration/); + assert.doesNotMatch(pages[1], /Reactivate|Deactivate|Delete/); +}); + +test("the signup form never offers to retry or complete a pending arrival", async (t) => { + const { f } = await pending(t); + const signupForm = createSignupForm({ + db: f.db, + config: f.config, + accounts: { + storage: f.storage, + ownership: f.ownership, + get: (did) => f.storage.getByDid(did), + pendingRegistration: async () => null, + }, + fields: () => "", + }); + const html = await signupForm({ authEmail: email }, { csrf: "c" }); + assert.match(html, /unfinished incoming migration/); + assert.doesNotMatch(html, /Retry account setup|Create account|
= new Set(permittedWhilePending); + +/** Honest wording shared by every browser and API refusal. */ +export const pendingArrivalMessage = + "This account is part of an unfinished incoming migration. It stays paused until the migration tool finishes and requests activation; it cannot be reactivated, changed or used to sign in to applications from here."; + +export function createPendingArrivalGuard( + storage: Pick, +) { + /** A pending arrival is an unfinished arrival with a placed, not-yet-active + * account. It is read from saved facts, never inferred from the account alone. */ + const isPending = async ( + row: Pick | null | undefined, + ): Promise => + row?.status === "provisioning" && + (await storage.hasPendingExternalMigration(row.did)); + return { + isPending, + /** Throws `MigrationPending` (409) unless the action is explicitly permitted. */ + async assertPermitted( + row: Pick | null | undefined, + action: PendingArrivalAction, + ): Promise { + if (permitted.has(action) || !(await isPending(row))) return; + throw new HttpError(409, "MigrationPending", pendingArrivalMessage); + }, + }; +} + +export type PendingArrivalGuard = ReturnType; diff --git a/src/app-incoming.test.mjs b/src/app-incoming.test.mjs new file mode 100644 index 00000000..19bf5cd6 --- /dev/null +++ b/src/app-incoming.test.mjs @@ -0,0 +1,161 @@ +import assert from "node:assert/strict"; +import { generateKeyPairSync, randomBytes } from "node:crypto"; +import { once } from "node:events"; +import http from "node:http"; +import test from "node:test"; +import { Secp256k1Keypair } from "@atproto/crypto"; +import { openTestDatabase } from "../tests/support/database-fixture.mjs"; +import { createApp } from "../dist/src/app.mjs"; +import { createMailFeature } from "../dist/src/mail/create-mail.js"; +import { createMailOutbox } from "../dist/src/database/drizzle/mail-outbox.js"; + +/** The real application assembly (registration order, composition, error + * handling) over an in-memory authority database. Nothing is stubbed except + * outbound mail delivery; no request here reaches a PDS or the directory. */ +async function application(t) { + const issuer = "https://entryway.example.test"; + const rotation = await Secp256k1Keypair.create({ exportable: true }); + const offline = await Secp256k1Keypair.create(); + const jwtJwk = { + ...generateKeyPairSync("ec", { namedCurve: "secp256k1" }).privateKey.export( + { format: "jwk" }, + ), + alg: "ES256K", + }; + const pds = (id) => ({ + id, + url: `https://${id}.example.test`, + internalUrl: `http://${id}:3000`, + did: `did:web:${id}.example.test`, + adminPassword: randomBytes(16).toString("hex"), + }); + const config = { + issuer, + serviceDid: "did:web:entryway.example.test", + clientUrl: "https://client.example.test", + handleDomains: [".example.test"], + plcUrl: "http://plc.invalid:2582", + jwtJwk, + plcRotationKeyHex: Buffer.from(await rotation.export()).toString("hex"), + plcRotationKeyDid: rotation.did(), + plcRecoveryKeyDid: offline.did(), + dpopSecret: randomBytes(32).toString("hex"), + betterAuthSecret: randomBytes(32).toString("hex"), + adminPassword: randomBytes(16).toString("hex"), + pds: [pds("pds1"), pds("pds2")], + }; + const db = await openTestDatabase(":memory:"); + t.after(() => db.close()); + const mail = createMailFeature({ + outbox: createMailOutbox(db), + transport: { send: async () => {} }, + }); + const { app } = await createApp({ config, db, mail }); + const server = app.listen(0, "127.0.0.1"); + t.after(() => new Promise((resolve) => server.close(resolve))); + await once(server, "listening"); + const call = (path, { method = "POST", headers = {}, json } = {}) => + new Promise((resolve, reject) => { + const request = http.request( + { + host: "127.0.0.1", + port: server.address().port, + path, + method, + headers: { + host: new URL(issuer).host, + ...(json === undefined + ? {} + : { "content-type": "application/json" }), + ...headers, + }, + }, + (response) => { + const chunks = []; + response.on("data", (chunk) => chunks.push(chunk)); + response.on("end", () => { + const text = Buffer.concat(chunks).toString("utf8"); + let body; + try { + body = JSON.parse(text); + } catch { + body = undefined; + } + resolve({ + status: response.statusCode, + headers: response.headers, + body, + }); + }); + }, + ); + request.on("error", reject); + request.end(json === undefined ? undefined : JSON.stringify(json)); + }); + return { call }; +} + +test("through the real application, requests without an incoming selector keep the ordinary routes", async (t) => { + const { call } = await application(t); + const create = await call("/xrpc/com.atproto.server.createAccount", { + json: {}, + }); + assert.equal(create.status, 400); + assert.notEqual(create.body.error, "ArrivalExpired"); + const submit = await call("/xrpc/com.atproto.identity.submitPlcOperation", { + json: { operation: {} }, + }); + assert.equal(submit.status, 401); + assert.equal(submit.body.error, "AuthRequired"); + const refresh = await call("/xrpc/com.atproto.server.refreshSession", { + headers: { authorization: "Bearer not-a-token" }, + }); + assert.equal(refresh.status, 400); + assert.equal(refresh.body.error, "InvalidToken"); + const activate = await call("/xrpc/com.atproto.server.activateAccount", { + headers: { authorization: "Bearer not-a-token" }, + }); + assert.equal(activate.status, 401); + assert.equal(activate.body.error, "InvalidToken"); + const anonymous = await call("/xrpc/com.atproto.server.activateAccount"); + assert.equal(anonymous.status, 401); + assert.equal(anonymous.body.error, "AuthRequired"); +}); + +test("through the real application, the arrival-code header reaches the incoming handlers and nothing else", async (t) => { + const { call } = await application(t); + const headers = { "entryway-arrival-code": "A".repeat(32) }; + const create = await call("/xrpc/com.atproto.server.createAccount", { + headers, + json: { handle: "someone.example.test" }, + }); + assert.equal(create.status, 409); + assert.equal(create.body.error, "ArrivalExpired"); + assert.equal(create.headers["cache-control"], "no-store"); + const submit = await call("/xrpc/com.atproto.identity.submitPlcOperation", { + headers, + json: { operation: {} }, + }); + assert.equal(submit.status, 409); + assert.equal(submit.body.error, "ArrivalExpired"); + // The header never makes an incoming request fall through to ordinary routes. + const shape = await call("/xrpc/com.atproto.server.createAccount", { + headers, + json: { email: "x@example.test", handle: "x.example.test", password: "p" }, + }); + assert.equal(shape.status, 400); + assert.equal(shape.body.error, "InvalidRequest"); +}); + +test("through the real application, the browser surfaces are registered and gated", async (t) => { + const { call } = await application(t); + const page = await call("/incoming", { method: "GET" }); + assert.equal(page.status, 303); + assert.equal(page.headers.location, "/login"); + const crossSite = await call("/incoming/prepare", { + headers: { origin: "https://evil.example.test" }, + json: { did: "x", handle: "y", otp: "1", csrf: "c" }, + }); + assert.equal(crossSite.status, 400); + assert.equal(crossSite.body.error, "InvalidRequest"); +}); diff --git a/src/app.mjs b/src/app.mjs index 853db6f6..53929582 100644 --- a/src/app.mjs +++ b/src/app.mjs @@ -12,6 +12,8 @@ import { createAccountSecurity } from "./compose-account-security.mjs"; import { createProtocolOperations } from "./compose-protocol-operations.mjs"; import { createAccountMigration } from "./features/pds-migration/move-between-pds.mjs"; import { requestFailureEvent } from "./logging/request-event.js"; +import { createIncomingArrival } from "./compose-incoming-arrival.mjs"; +import { mountIncomingRoutes } from "./features/external-migration/routes.js"; export async function createApp({ config, db, mail, lifecycle }) { const accounts = await createAccounts({ @@ -167,8 +169,23 @@ export async function createApp({ config, db, mail, lifecycle }) { }); app.use(express.json({ limit: "64kb" })); mountOperationRecoveryRoutes({ app, config, ownership: accounts.ownership }); + const incoming = await createIncomingArrival({ + db, + config, + accounts, + legacy, + oauth, + }); + mountIncomingRoutes({ + app, + issuer: config.issuer, + incoming, + oauth, + rateLimit: protocolOperations.rateLimit, + }); await mountXrpc({ app, + incoming, db, config, accounts, diff --git a/src/compose-authentication.mjs b/src/compose-authentication.mjs index f8268287..b44ac5e7 100644 --- a/src/compose-authentication.mjs +++ b/src/compose-authentication.mjs @@ -8,6 +8,7 @@ import { mountSignupRoutes } from "./features/account-registration/signup-routes import { createConsent } from "./features/oauth-authorization/consent.mjs"; import { mountAuthorizationRoutes } from "./features/oauth-authorization/authorization-routes.mjs"; import { authorizationRedirect } from "./features/oauth-authorization/redirect.mjs"; +import { createPendingArrivalGuard } from "./accounts/pending-arrival.js"; export async function createAuthentication({ app, @@ -53,6 +54,7 @@ export async function createAuthentication({ stores, flows, accountAccess, + pendingArrival: createPendingArrivalGuard(accounts.storage), clientRequestsSignupConsentSkip, }); const { startEmailCode } = mountEmailLogin({ diff --git a/src/compose-incoming-arrival.mjs b/src/compose-incoming-arrival.mjs index b078910c..9b80d868 100644 --- a/src/compose-incoming-arrival.mjs +++ b/src/compose-incoming-arrival.mjs @@ -13,6 +13,63 @@ import { createAuthenticatedArrival } from "./features/external-migration/submit import { readIncomingObservation } from "./plc/incoming-observation.js"; import { createArrivalCredentials } from "./features/external-migration/arrival-credentials.js"; import { createArrivalActivation } from "./features/external-migration/activate-arrival.js"; +import { createToolAdmission } from "./features/external-migration/tool-admission.js"; +import { summarizeArrival } from "./features/external-migration/arrival-summary.js"; +import { createArrivalStorage } from "./database/drizzle/migration-arrival.js"; + +/** Production arrival persistence over the application's account storage. The + * placed account, saved-user binding and the later `provisioning` -> `active` + * flip all go through the reviewed account authority, in the arrival's own + * transactions. */ +export function createIncomingArrivalStorage({ db, config, accounts }) { + const storage = accounts.storage; + return createArrivalStorage(db, config.pds, { + readAccount: (did) => storage.getByDid(did), + readBinding: (did) => storage.getVerifiedBinding(did), + reserveEmailClaim: (...args) => storage.reserveEmail(...args), + reserveHandleClaim: (...args) => storage.reserveHandle(...args), + recordSavedUserBinding: (did, userId) => + storage.bindPlacedAccount(did, userId), + activatePlacedAccount: (did) => storage.activatePlacedAccount(did), + insertPlacedAccount: (input) => + storage.insertAccount( + { + did: input.did, + email: input.email, + emailVerified: true, + handle: input.handle, + pdsId: input.pdsId, + pdsUrl: input.pdsUrl, + status: "provisioning", + createdAt: new Date().toISOString(), + }, + input.userId, + ), + }); +} + +/** The application's incoming-arrival feature: production storage, the real + * browser authentication/CSRF producers and the one composition below. */ +export async function createIncomingArrival({ + db, + config, + accounts, + legacy, + oauth, +}) { + return composeIncomingArrival({ + db, + arrivals: createIncomingArrivalStorage({ db, config, accounts }), + config, + accounts, + legacy, + authentication: oauth, + browserFlow: { + loadBrowser: (request) => oauth.loadBrowser(request, request.res), + checkCsrf: oauth.checkCsrf, + }, + }); +} export async function composeIncomingArrival({ db, @@ -98,6 +155,10 @@ export async function composeIncomingArrival({ rateLimit, plcClient: accounts.plcClient, }); + const verify = createIncomingAuthorization({ + serviceDid: config.serviceDid, + readDirectory: (did) => accounts.plcClient.getDocument(did), + }); const admission = createArrivalAdmission({ db, arrivals, invitation }); const authenticated = createAuthenticatedArrival({ arrivals, @@ -113,14 +174,11 @@ export async function composeIncomingArrival({ bindPending: admission.bindPending, backend: arrivalBackend, }), - verify: createIncomingAuthorization({ - serviceDid: config.serviceDid, - readDirectory: (did) => accounts.plcClient.getDocument(did), - }), + verify, client: accounts.plcClient, }); - // Dormant internal callables: browser authentication supplies the saved user, - // never claim.resource or a user ID from the request body. + // Browser callables: browser authentication supplies the saved user, never + // claim.resource or a user ID from the request body. const savedUser = async (request) => { const browser = await browserFlow.loadBrowser(request); browserFlow.checkCsrf(request, browser); @@ -147,6 +205,16 @@ export async function composeIncomingArrival({ loadBrowser: browserFlow.loadBrowser, rateLimit, }); + // Tool surfaces carry no cookie. PROPOSED, awaiting owner decision: the + // current arrival code selects the arrival and its saved user (see + // tool-admission.ts); browser surfaces below keep the browser-bound forms. + const toolAdmission = createToolAdmission({ + arrivals, + verify, + admit: authenticated.admit, + continueCreation: authenticated.continueCreation, + issueCreationSession: credentials.issueCreationSession, + }); const activation = createArrivalActivation({ arrivals, ownership: accounts.ownership, @@ -168,8 +236,36 @@ export async function composeIncomingArrival({ return { ...admission, prepareArrival, - // Tracked migration credentials and the one guarded activation. Dormant: - // plan 012 injects these into routes, XRPC and the stock callback. + submitIncomingHandoff: toolAdmission.submitHandoff, + createIncomingAccount: toolAdmission.createAccount, + /** Secret-free state for the saved user's own arrival; a read needs the + * verified session but no CSRF token. Another user's arrival is not found. */ + describeArrival: async (request, operationId) => { + const principal = await authentication.requireSession(request); + if (!principal?.emailVerified) + throw new DomainError( + "AccountMismatch", + 403, + "A verified saved browser owner is required", + ); + const aggregate = + typeof operationId === "string" && operationId.length <= 200 + ? await arrivals.readArrivalAggregate(operationId, principal.userId) + : null; + if (!aggregate) + throw new DomainError("AccountNotFound", 404, "Arrival not found"); + return summarizeArrival(aggregate); + }, + /** A fresh sign-in code for the saved user's own email, on the existing + * challenge budget. */ + sendArrivalCode: async (request) => { + const userId = await savedUser(request); + const principal = await authentication.requireSession(request); + await rateLimit(`incoming-code:${userId}`); + await authentication.sendSignInCode(principal.email); + return {}; + }, + // Tracked migration credentials and the one guarded activation. issueArrivalSession: async (request, operationId, expectedVersion) => credentials.issueArrivalSession( await savedUser(request), diff --git a/src/compose-protocol.mjs b/src/compose-protocol.mjs index 7babc197..f4a9bc01 100644 --- a/src/compose-protocol.mjs +++ b/src/compose-protocol.mjs @@ -11,9 +11,13 @@ import { mountAccountDeletionXrpc } from "./features/account-deletion/xrpc-route import { mountPlcXrpc } from "./plc/xrpc-routes.mjs"; import { mountOauthAuthorizationXrpc } from "./features/oauth-authorization/xrpc-routes.mjs"; import { mountMailXrpc } from "./mail/xrpc-routes.mjs"; +import { mountIncomingXrpc } from "./features/external-migration/xrpc-routes.js"; export async function mountXrpc(services) { const routing = await createProtocolRouting(services); + // Before the ordinary routes: incoming requests are selected explicitly by + // their arrival-code header or tracked credential; everything else falls through. + mountIncomingXrpc(services); mountHttpXrpc({ ...services, ...routing }); mountHandleChangeXrpc({ ...services, ...routing }); mountEmailLoginXrpc({ ...services, ...routing }); diff --git a/src/database/accounts.port.ts b/src/database/accounts.port.ts index 01bb9d83..97d44fd7 100644 --- a/src/database/accounts.port.ts +++ b/src/database/accounts.port.ts @@ -56,6 +56,8 @@ export interface AccountTransactor extends AccountReader { reserveExternalMigration(input: ExternalMigrationReservation): Promise; bindProvisionedArrival(input: BindProvisionedArrival): Promise; activateBoundArrival(input: ActivateBoundArrival): Promise; + /** Bind a placed incoming arrival account to its saved verified user. */ + bindPlacedAccount(did: string, userId: string): Promise; /** Incoming-arrival completion: moves the saved placed account from * `provisioning` to `active` through `saveAccount`'s reviewed invariants. The * arrival storage calls this inside its completion transaction. */ diff --git a/src/database/drizzle/account-binding.test.mjs b/src/database/drizzle/account-binding.test.mjs new file mode 100644 index 00000000..330a4884 --- /dev/null +++ b/src/database/drizzle/account-binding.test.mjs @@ -0,0 +1,41 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + openTestDatabase, + query, + verifiedUser, +} from "../../../tests/support/database-fixture.mjs"; +import { createAccountStorage } from "../../../dist/src/database/drizzle/account-storage.js"; + +const did = `did:plc:${"a".repeat(24)}`; +const hosts = [{ id: "pds1", url: "https://pds1.example.test" }]; + +test("a placed arrival binds once and fails closed for a different user", async () => { + const db = await openTestDatabase(":memory:"); + try { + const storage = createAccountStorage(db, hosts); + await verifiedUser(db, "user-a", "a@example.test"); + await verifiedUser(db, "user-b", "b@example.test"); + await storage.insertAccount({ + did, + email: "a@example.test", + handle: "a.example.test", + pdsId: "pds1", + pdsUrl: hosts[0].url, + status: "provisioning", + createdAt: new Date().toISOString(), + }); + await storage.bindPlacedAccount(did, "user-a"); + // An exact repeat changes nothing. + await storage.bindPlacedAccount(did, "user-a"); + await assert.rejects( + storage.bindPlacedAccount(did, "user-b"), + (error) => error.code === "IdentityConflict" && error.status === 409, + ); + const rows = await query(db, "SELECT * FROM account_bindings"); + assert.equal(rows.length, 1); + assert.equal(rows[0].user_id, "user-a"); + } finally { + await db.close(); + } +}); diff --git a/src/database/drizzle/account-storage.ts b/src/database/drizzle/account-storage.ts index 94e776eb..2dbea7c6 100644 --- a/src/database/drizzle/account-storage.ts +++ b/src/database/drizzle/account-storage.ts @@ -793,6 +793,27 @@ export function createAccountStorage( return row; }); }, + /** Saved-user binding for a placed incoming arrival account. Fails closed: + * an existing binding must already be this user's (an exact repeat changes + * nothing); a binding to a different user is a conflict and changes nothing. */ + async bindPlacedAccount(did, userId) { + await db.transact(async () => { + const existing = await one( + "account_bindings", + eq(t.account_bindings.did, did), + ); + if (existing) { + if (existing.user_id !== userId) + throw new DomainError( + "IdentityConflict", + 409, + "This account is already bound to a different verified user", + ); + return; + } + await db.insert("account_bindings", { did, user_id: userId }); + }); + }, async activatePlacedAccount(did) { await db.transact(async () => { const row = await getByDid(did); diff --git a/src/features/account-registration/create-account.ts b/src/features/account-registration/create-account.ts index c5ecc513..25c69a51 100644 --- a/src/features/account-registration/create-account.ts +++ b/src/features/account-registration/create-account.ts @@ -12,6 +12,7 @@ import { type RegistrationInput, } from "../../accounts/input-schema.js"; import { xrpc } from "../../pds/client.mjs"; +import { createPendingArrivalGuard } from "../../accounts/pending-arrival.js"; import type { AuthorityDatabase } from "../../database/connection.js"; import type { AccountRow } from "../../accounts/types.js"; @@ -486,6 +487,12 @@ export function createRegistration({ registrationIdentity, }: ProvisionInput): Promise => { let row = await registrationAccountForEmail(storage, email); + // An unfinished incoming arrival is a placed `provisioning` account for this + // email. Ordinary registration must never resume or activate it. + await createPendingArrivalGuard(storage).assertPermitted( + row, + "registration", + ); await assertEmailAvailable(email, row); if (row) { assertSameRegistration(row, { handle, pdsId, recoveryKey }); diff --git a/src/features/account-registration/page.mjs b/src/features/account-registration/page.mjs index 370aee9c..f590b742 100644 --- a/src/features/account-registration/page.mjs +++ b/src/features/account-registration/page.mjs @@ -1,5 +1,9 @@ import { escapeHtml } from "../../ui/html.mjs"; import { registrationAccountForEmail } from "./create-account.js"; +import { + createPendingArrivalGuard, + pendingArrivalMessage, +} from "../../accounts/pending-arrival.js"; const signupState = (recoveryRequired, retry) => { if (recoveryRequired) return { @@ -24,6 +28,10 @@ export function createSignupForm({ db, config, accounts, fields }) { const account = flow.authEmail ? await registrationAccountForEmail(accounts.storage, flow.authEmail) : null; + // An unfinished incoming arrival is not an unfinished signup: never offer + // to "retry" or complete it here. + if (await createPendingArrivalGuard(accounts.storage).isPending(account)) + return `

${escapeHtml(pendingArrivalMessage)}

Review your incoming migration

`; const attempt = flow.authEmail ? await accounts.ownership?.pendingExternal(`email:${flow.authEmail}`) : null; diff --git a/src/features/account-settings/operation-recovery-routes.test.mjs b/src/features/account-settings/operation-recovery-routes.test.mjs index eef9fd66..e59a04e1 100644 --- a/src/features/account-settings/operation-recovery-routes.test.mjs +++ b/src/features/account-settings/operation-recovery-routes.test.mjs @@ -102,7 +102,14 @@ test("operator recovery requires Entryway authorization and exact nonreplayable }; const form = createSignupForm({ db, - accounts: { ownership, storage: { listByEmail: async () => [account] } }, + accounts: { + ownership, + storage: { + listByEmail: async () => [account], + // An unfinished signup, not an incoming arrival. + hasPendingExternalMigration: async () => false, + }, + }, config: { pds: [{ id: "pds1", url: target }], handleDomains: [".example.test"], diff --git a/src/features/email-login/routes.test.mjs b/src/features/email-login/routes.test.mjs index ea7afd63..8f64a3de 100644 --- a/src/features/email-login/routes.test.mjs +++ b/src/features/email-login/routes.test.mjs @@ -441,6 +441,7 @@ async function verificationFixture( }, }; const { authenticated } = createConsent({ + pendingArrival: { isPending: async () => false }, db, provider, flows, diff --git a/src/features/external-migration/activate-arrival.ts b/src/features/external-migration/activate-arrival.ts index f1c6e441..a3532154 100644 --- a/src/features/external-migration/activate-arrival.ts +++ b/src/features/external-migration/activate-arrival.ts @@ -111,6 +111,14 @@ export function createArrivalActivation({ "This arrival is no longer actionable", ); + /** A finished arrival's committed result is only the answer to a lost final + * response while the local account is still active. Afterwards (for example the + * user deactivated it) the family is an ordinary session and the request is not + * an arrival's, so the ordinary route must handle it. */ + const assertStillActive = async (did: string) => { + if ((await accounts.getByDid(did))?.status !== "active") throw inactive(); + }; + const run = async ( claim: OperationClaim, initial: ArrivalAggregate, @@ -120,6 +128,7 @@ export function createArrivalActivation({ const read = async () => owner.read(saved.operationId, saved.userId); if (initial.progress.completedAt !== null) { // A lost response retries into the committed result; nothing is dispatched. + await assertStillActive(saved.did); await ownership.checkpoint("complete", null); return finished(initial); } @@ -366,7 +375,10 @@ export function createArrivalActivation({ // Both kinds of intent were just proved against this very arrival (the // credential's own unrevoked tracked family), so a finished arrival returns // its committed result with no backend write and no journal change. - if (initial.progress.completedAt !== null) return finished(initial); + if (initial.progress.completedAt !== null) { + await assertStillActive(initial.arrival.did); + return finished(initial); + } if (initial.arrival.cancelledAt !== null) throw inactive(); try { return await owner.resume(initial.arrival, (claim, current) => @@ -379,7 +391,10 @@ export function createArrivalActivation({ ) { // Another request completed activation and released the claim first. const current = await owner.read(operationId, userId); - if (current.progress.completedAt !== null) return finished(current); + if (current.progress.completedAt !== null) { + await assertStillActive(current.arrival.did); + return finished(current); + } } throw error; } diff --git a/src/features/external-migration/arrival-summary.ts b/src/features/external-migration/arrival-summary.ts new file mode 100644 index 00000000..787e426e --- /dev/null +++ b/src/features/external-migration/arrival-summary.ts @@ -0,0 +1,39 @@ +import { hasUnresolvedAttempt } from "./arrival-owner.js"; +import type { ArrivalAggregate } from "./arrival-types.js"; + +/** Secret-free facts the saved user may see about their own arrival. Never the + * arrival code, a credential, a signed operation or a signing key. */ +export interface ArrivalSummary { + readonly operationId: string; + readonly version: number; + readonly did: string; + readonly handle: string; + readonly state: + | "cancelled" + | "prepared" + | "handoff-published" + | "created" + | "paused-pending" + | "active"; + readonly codeExpiresAt: number; + readonly unresolvedAttempt: boolean; +} + +export function summarizeArrival(aggregate: ArrivalAggregate): ArrivalSummary { + const { arrival, progress } = aggregate; + let state: ArrivalSummary["state"] = "prepared"; + if (arrival.cancelledAt !== null) state = "cancelled"; + else if (progress.completedAt !== null) state = "active"; + else if (progress.boundAt !== null) state = "paused-pending"; + else if (progress.provisionedAt !== null) state = "created"; + else if (progress.canonicalHandoffCid !== null) state = "handoff-published"; + return { + operationId: arrival.operationId, + version: arrival.version, + did: arrival.did, + handle: arrival.handle, + state, + codeExpiresAt: arrival.codeExpiresAt, + unresolvedAttempt: hasUnresolvedAttempt(aggregate), + }; +} diff --git a/src/features/external-migration/incoming-routes.test.mjs b/src/features/external-migration/incoming-routes.test.mjs new file mode 100644 index 00000000..e0cf9418 --- /dev/null +++ b/src/features/external-migration/incoming-routes.test.mjs @@ -0,0 +1,380 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { fixture } from "../../../tests/support/helpers/incoming-arrival-fixture.mjs"; +import { incomingHttp } from "../../../tests/support/helpers/incoming-http.mjs"; +import { query } from "../../../tests/support/database-fixture.mjs"; +import { DomainError } from "../../../dist/src/accounts/errors.js"; + +const prepareBody = (f) => ({ did: f.did, handle: "destination.example.test" }); +const secretFree = (text, secrets) => { + for (const secret of secrets) assert.equal(text.includes(secret), false); +}; + +test("prepare is strict, proof-gated, no-store and returns the code once", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const body = { handle: "destination.example.test" }; + // Unknown fields, a missing DID or a missing proof field are the same stable 400. + for (const bad of [ + { ...prepareBody(f), email: "someone@example.test" }, + { ...prepareBody(f), userId: "destination-user" }, + body, + ]) { + const refused = await h.browser("/incoming/prepare", bad); + assert.equal(refused.status, 400); + assert.equal(refused.json.error, "InvalidRequest"); + } + // A body without the proof fields never reaches a code check. + const noCsrf = await h.call("/incoming/prepare", { + headers: { origin: f.config.issuer }, + json: { ...prepareBody(f), otp: "fresh-otp" }, + }); + assert.equal(noCsrf.status, 400); + assert.equal(f.verifications(), 0); + // Cross-origin and wrong-CSRF requests are refused before any code is checked. + const crossOrigin = await h.browser("/incoming/prepare", prepareBody(f), { + origin: "https://evil.example.test", + }); + assert.equal(crossOrigin.status, 400); + assert.equal(crossOrigin.json.error, "InvalidRequest"); + const wrongCsrf = await h.browser("/incoming/prepare", prepareBody(f), { + csrf: "forged", + }); + assert.equal(wrongCsrf.status, 400); + assert.equal(f.verifications(), 0); + const stale = await h.browser("/incoming/prepare", prepareBody(f), { + otp: "stale", + }); + assert.equal(stale.status, 403); + assert.equal(stale.json.error, "ArrivalProofRequired"); + assert.equal(f.reservations(), 0); + const ok = await h.browser("/incoming/prepare", prepareBody(f)); + assert.equal(ok.status, 200); + assert.equal(ok.headers["cache-control"], "no-store"); + assert.equal(typeof ok.json.arrivalCode, "string"); + assert.equal(typeof ok.json.operationId, "string"); + assert.equal(ok.json.version >= 1, true); + assert.equal(ok.json.entrywayDid, f.config.serviceDid); + // Only the hash is stored. + const stored = await query(f.db, "SELECT * FROM arrival_code"); + assert.equal(JSON.stringify(stored).includes(ok.json.arrivalCode), false); + // Replaying cannot reproduce the code: it is a stable recovery refusal. + const replay = await h.browser("/incoming/prepare", prepareBody(f)); + assert.equal(replay.status, 409); + assert.equal(replay.json.error, "OperationRecoveryRequired"); + secretFree(replay.text, [ok.json.arrivalCode]); +}); + +test("a form post renders the one-time page, no-store, and failures render stable pages", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const ok = await h.call("/incoming/prepare", { + headers: { origin: f.config.issuer }, + form: { + csrf: f.request.body.csrf, + otp: "fresh-otp", + ...prepareBody(f), + }, + }); + assert.equal(ok.status, 200); + assert.equal(ok.headers["cache-control"], "no-store"); + assert.match(ok.headers["content-type"], /text\/html/); + assert.match(ok.text, /shown once/); + const refused = await h.call("/incoming/prepare", { + headers: { origin: f.config.issuer }, + form: { csrf: f.request.body.csrf, otp: "stale", ...prepareBody(f) }, + }); + assert.equal(refused.status, 403); + assert.match(refused.text, /Verify the destination email again/); +}); + +test("renew is proof-gated, version-checked, no-store and returns a replacement code", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const prepared = (await h.browser("/incoming/prepare", prepareBody(f))).json; + const stale = await h.browser( + "/incoming/renew", + { operationId: prepared.operationId, expectedVersion: prepared.version }, + { otp: "stale" }, + ); + assert.equal(stale.status, 403); + assert.equal(stale.json.error, "ArrivalProofRequired"); + const wrongVersion = await h.browser("/incoming/renew", { + operationId: prepared.operationId, + expectedVersion: prepared.version + 5, + }); + assert.equal(wrongVersion.status, 409); + assert.equal(wrongVersion.json.error, "OperationConflict"); + const renewed = await h.browser("/incoming/renew", { + operationId: prepared.operationId, + expectedVersion: prepared.version, + }); + assert.equal(renewed.status, 200); + assert.equal(renewed.headers["cache-control"], "no-store"); + assert.notEqual(renewed.json.arrivalCode, prepared.arrivalCode); + assert.equal(renewed.json.version > prepared.version, true); + assert.deepEqual(Object.keys(renewed.json).sort(), [ + "arrivalCode", + "expiresAt", + "operationId", + "version", + ]); +}); + +test("a wrong owner and an unknown operation are the same 404 on every saved-user route", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const prepared = (await h.browser("/incoming/prepare", prepareBody(f))).json; + const results = []; + for (const [path, extra] of [ + ["/incoming/renew", { expectedVersion: prepared.version }], + ["/incoming/cancel", { expectedVersion: prepared.version }], + ["/incoming/recover", { expectedVersion: prepared.version }], + ["/incoming/activate", { expectedVersion: prepared.version }], + ["/incoming/resume", { otp: undefined }], + ]) { + const unknown = await h.browser(path, { + operationId: "no-such-operation", + ...extra, + }); + f.principal.userId = "someone-else"; + const stranger = await h.browser(path, { + operationId: prepared.operationId, + ...extra, + }); + f.principal.userId = "destination-user"; + assert.equal(unknown.status, 404, path); + assert.equal(unknown.json.error, "AccountNotFound", path); + assert.equal(stranger.status, 404, path); + assert.deepEqual(stranger.json, unknown.json, path); + results.push(path); + } + assert.equal(results.length, 5); + // The status page gives the same answer. + const unknownPage = await h.call("/incoming?operationId=nope", { + method: "GET", + }); + f.principal.userId = "someone-else"; + const strangerPage = await h.call( + `/incoming?operationId=${prepared.operationId}`, + { method: "GET" }, + ); + f.principal.userId = "destination-user"; + assert.equal(unknownPage.status, 404); + assert.equal(strangerPage.status, 404); + assert.equal(strangerPage.text, unknownPage.text); +}); + +test("cancel of a prepared arrival is the stable recovery refusal, never a release", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const prepared = (await h.browser("/incoming/prepare", prepareBody(f))).json; + const refused = await h.browser("/incoming/cancel", { + operationId: prepared.operationId, + expectedVersion: prepared.version, + }); + assert.equal(refused.status, 409); + assert.equal(refused.json.error, "OperationRecoveryRequired"); + const rows = await query(f.db, "SELECT cancelled_at FROM arrivals"); + assert.equal(rows[0].cancelled_at, null); +}); + +test("recover and activate refuse before binding and spend no backend effect", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const prepared = (await h.browser("/incoming/prepare", prepareBody(f))).json; + const input = { + operationId: prepared.operationId, + expectedVersion: prepared.version, + }; + for (const path of ["/incoming/recover", "/incoming/activate"]) { + const refused = await h.browser(path, input); + assert.equal(refused.status, 409, path); + assert.equal(refused.json.error, "OperationConflict", path); + } + assert.equal(f.backendState.activations, 0); + assert.equal(f.backendState.creates, 0); +}); + +test("budgets: the browser IP budget answers 429 with the existing code", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + let last; + for (let attempt = 0; attempt < 31; attempt++) + last = await h.browser("/incoming/prepare", { bad: true }); + assert.equal(last.status, 429); + assert.equal(last.json.error, "RateLimitExceeded"); +}); + +test("the status page shows only the saved user's facts and honest wording", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const prepared = (await h.browser("/incoming/prepare", prepareBody(f))).json; + const start = await h.call("/incoming", { method: "GET" }); + assert.equal(start.status, 200); + assert.equal(start.headers["cache-control"], "no-store"); + const detail = await h.call( + `/incoming?operationId=${encodeURIComponent(prepared.operationId)}`, + { method: "GET" }, + ); + assert.equal(detail.status, 200); + assert.match(detail.text, /Prepared/); + secretFree(detail.text, [prepared.arrivalCode, f.config.betterAuthSecret]); + // A non-string selector is a stable 400, not a lookup. + const array = await h.call("/incoming?operationId=a&operationId=b", { + method: "GET", + }); + assert.equal(array.status, 400); +}); + +test("code sends a fresh sign-in code only to the verified browser owner's own email, within budget", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const noCsrf = await h.call("/incoming/code", { + headers: { origin: f.config.issuer }, + json: { csrf: "forged" }, + }); + assert.equal(noCsrf.status, 400); + // An email or any other field is never accepted. + const extra = await h.call("/incoming/code", { + headers: { origin: f.config.issuer }, + json: { csrf: f.request.body.csrf, email: "someone@example.test" }, + }); + assert.equal(extra.status, 400); + assert.equal(f.sentCodes.length, 0); + const sent = []; + for (let attempt = 0; attempt < 6; attempt++) + sent.push( + await h.call("/incoming/code", { + headers: { origin: f.config.issuer }, + json: { csrf: f.request.body.csrf }, + }), + ); + assert.deepEqual( + sent.map((response) => response.status), + [200, 200, 200, 200, 200, 429], + ); + assert.equal(sent[0].headers["cache-control"], "no-store"); + assert.deepEqual(new Set(f.sentCodes), new Set([f.principal.email])); + assert.equal(sent[5].json.error, "RateLimitExceeded"); +}); + +test("a repeat prepare by the same saved user returns their own operation ID and nothing else", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const first = (await h.browser("/incoming/prepare", prepareBody(f))).json; + const snapshot = async () => + JSON.stringify([ + await query(f.db, "SELECT * FROM arrivals"), + await query(f.db, "SELECT * FROM arrival_code"), + await query(f.db, "SELECT * FROM arrival_reservations"), + ]); + const before = await snapshot(); + const reservations = f.reservations(); + const repeat = await h.browser("/incoming/prepare", prepareBody(f)); + // Same status and code as the existing refusal, with the ID added. + assert.equal(repeat.status, 409); + assert.equal(repeat.json.error, "OperationRecoveryRequired"); + assert.equal(repeat.headers["cache-control"], "no-store"); + assert.equal(repeat.json.operationId, first.operationId); + assert.equal(repeat.json.version, first.version); + assert.equal(repeat.text.includes(first.arrivalCode), false); + assert.equal(await snapshot(), before); + assert.equal(f.reservations(), reservations); + // The returned ID works for renew. + const renewed = await h.browser("/incoming/renew", { + operationId: repeat.json.operationId, + expectedVersion: repeat.json.version, + }); + assert.equal(renewed.status, 200); + assert.notEqual(renewed.json.arrivalCode, first.arrivalCode); + // The HTML page shows the ID honestly and still no code. + const form = await h.call("/incoming/prepare", { + headers: { origin: f.config.issuer }, + form: { + csrf: f.request.body.csrf, + otp: "fresh-otp", + ...prepareBody(f), + }, + }); + assert.equal(form.status, 409); + assert.match(form.text, /already have an incoming migration/); + assert.equal(form.text.includes(first.operationId), true); + assert.equal(form.text.includes(renewed.json.arrivalCode), false); +}); + +test("a repeat prepare gets no ID without the full proof, or for another user's arrival", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const first = (await h.browser("/incoming/prepare", prepareBody(f))).json; + const refusals = [ + await h.browser("/incoming/prepare", prepareBody(f), { otp: "stale" }), + await h.browser("/incoming/prepare", prepareBody(f), { csrf: "forged" }), + await h.browser("/incoming/prepare", prepareBody(f), { + origin: "https://evil.example.test", + }), + await h.call("/incoming/prepare", { + headers: { origin: f.config.issuer }, + json: { csrf: f.request.body.csrf, ...prepareBody(f) }, + }), + ]; + for (const refusal of refusals) { + assert.equal(refusal.json.operationId, undefined); + assert.equal(refusal.text.includes(first.operationId), false); + } + // Another verified user repeating for the same DID: today's refusal, no ID. + f.principal.userId = "someone-else"; + const stranger = await h.browser("/incoming/prepare", prepareBody(f)); + f.principal.userId = "destination-user"; + assert.equal(stranger.status >= 400 && stranger.status < 500, true); + assert.equal(stranger.json.operationId, undefined); + assert.equal(stranger.json.version, undefined); + assert.equal(stranger.text.includes(first.operationId), false); +}); + +test("the existing ID is attached only to the repeat-prepare refusal of an arrival that can still be acted on", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + const first = (await h.browser("/incoming/prepare", prepareBody(f))).json; + const repeat = () => h.browser("/incoming/prepare", prepareBody(f)); + // An unrelated refusal or a server failure never carries the ID. + const createArrival = f.arrivals.createArrival; + f.arrivals.createArrival = async () => { + throw new DomainError( + "OperationPending", + 409, + "Another operation is pending", + ); + }; + const pendingRefusal = await repeat(); + assert.equal(pendingRefusal.status, 409); + assert.equal(pendingRefusal.json.error, "OperationPending"); + assert.equal(pendingRefusal.json.operationId, undefined); + f.arrivals.createArrival = async () => { + throw new Error("boom"); + }; + const failure = await repeat(); + assert.equal(failure.status, 500); + assert.equal(failure.text.includes(first.operationId), false); + f.arrivals.createArrival = createArrival; + // If the lookup itself fails, the original refusal is still what the caller sees. + const read = f.arrivals.readArrivalAccess; + f.arrivals.readArrivalAccess = async () => { + throw new Error("lookup failed"); + }; + const lookupFailed = await repeat(); + f.arrivals.readArrivalAccess = read; + assert.equal(lookupFailed.status, 409); + assert.equal(lookupFailed.json.error, "OperationRecoveryRequired"); + assert.equal(lookupFailed.json.operationId, undefined); + // A cancelled arrival is not reported. + await query( + f.db, + "UPDATE arrivals SET cancelled_at = 1, active = 0, did_root = NULL", + [], + "run", + ); + const cancelled = await repeat(); + assert.equal(cancelled.json.operationId, undefined); + assert.equal(cancelled.text.includes(first.operationId), false); +}); diff --git a/src/features/external-migration/incoming-xrpc.test.mjs b/src/features/external-migration/incoming-xrpc.test.mjs new file mode 100644 index 00000000..12b503a9 --- /dev/null +++ b/src/features/external-migration/incoming-xrpc.test.mjs @@ -0,0 +1,634 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import * as plc from "@did-plc/lib"; +import { Secp256k1Keypair } from "@atproto/crypto"; +import { + fixture, + handle, +} from "../../../tests/support/helpers/incoming-arrival-fixture.mjs"; +import { incomingHttp } from "../../../tests/support/helpers/incoming-http.mjs"; +import { query } from "../../../tests/support/database-fixture.mjs"; +import { + incomingCreationMethod, + incomingHandoffMethod, +} from "../../../dist/src/features/external-migration/incoming-authorization.js"; +import { createInvites } from "../../../dist/src/features/account-registration/invites.mjs"; + +const submitPath = "/xrpc/com.atproto.identity.submitPlcOperation"; +const createPath = "/xrpc/com.atproto.server.createAccount"; +const refreshPath = "/xrpc/com.atproto.server.refreshSession"; +const activatePath = "/xrpc/com.atproto.server.activateAccount"; + +async function begin(t, options) { + const f = await fixture(t); + const h = await incomingHttp(t, f, options); + const prepared = await f.prepare(); + const operation = await plc.signOperation(prepared.handoff, f.source); + return { f, h, prepared, operation }; +} +const submit = async ({ f, h, prepared, operation }, overrides = {}) => + h.tool(submitPath, { + code: prepared.arrivalCode, + jwt: await f.credential(incomingHandoffMethod), + json: { operation }, + ...overrides, + }); +const invite = async (f) => { + const invites = await createInvites({ + db: f.db, + config: f.config, + accounts: { get: async () => null }, + }); + return (await invites.createInviteCode({ useCount: 1 })).code; +}; +const create = async ({ f, h, prepared }, json, overrides = {}) => + h.tool(createPath, { + code: prepared.arrivalCode, + jwt: await f.credential(incomingCreationMethod), + json, + ...overrides, + }); +const rows = (f, table) => query(f.db, `SELECT * FROM ${table}`); +const untouched = async (f) => { + assert.equal(f.publicationAttempts.length, 0); + assert.equal((await rows(f, "arrival_replay")).length, 0); + assert.equal(f.backendState.creates, 0); +}; + +/** Handoff published through the real route, then an invitation. */ +async function published(t, options) { + const s = await begin(t, options); + const ok = await submit(s); + assert.equal(ok.status, 200); + return s; +} +/** A creation accepted through the real route: tracked credentials issued. */ +async function created(t, options = {}) { + const s = await published(t, options); + const response = await create(s, { + handle, + inviteCode: await invite(s.f), + }); + assert.equal(response.status, 200, response.text); + return { ...s, session: response.json }; +} + +test("submitPlcOperation without the arrival-code header is not incoming", async (t) => { + const s = await begin(t); + const response = await submit(s, { code: undefined }); + assert.equal(response.status, 418); + assert.deepEqual(response.json, { ordinary: "identity.submitPlcOperation" }); + await untouched(s.f); +}); + +test("submitPlcOperation admits the source-signed handoff with no cookie and publishes only that operation", async (t) => { + const s = await begin(t); + // The exact retry is the same body with the same credential (its replay + // identity), until that credential expires. It is not a second publication. + const jwt = await s.f.credential(incomingHandoffMethod); + const response = await submit(s, { jwt }); + assert.equal(response.status, 200); + assert.deepEqual(response.json, {}); + assert.equal(response.headers["cache-control"], "no-store"); + assert.equal(s.f.publicationAttempts.length, 1); + assert.deepEqual(s.f.publicationAttempts[0], s.operation); + assert.equal((await rows(s.f, "arrival_replay")).length, 1); + const retry = await submit(s, { jwt }); + assert.equal(retry.status, 200); + assert.equal(s.f.publicationAttempts.length, 1); + assert.equal((await rows(s.f, "arrival_replay")).length, 1); +}); + +test("a fresh credential for an already admitted body is a conflict, never a second admission", async (t) => { + const s = await begin(t); + assert.equal((await submit(s)).status, 200); + const conflict = await submit(s); + assert.equal(conflict.status, 409); + assert.equal(conflict.json.error, "OperationConflict"); + assert.equal(s.f.publicationAttempts.length, 1); +}); + +test("submitPlcOperation refuses every wrong credential, code, DID, audience, method and shape without effect", async (t) => { + const s = await begin(t); + const other = `did:plc:${"b".repeat(24)}`; + const cases = [ + ["garbage code", { code: "A".repeat(32) }, 409, "ArrivalExpired"], + ["short code", { code: "x" }, 409, "ArrivalExpired"], + ["empty code", { code: "" }, 409, "ArrivalExpired"], + [ + "another DID's credential", + { jwt: await s.f.credential(incomingHandoffMethod, { iss: other }) }, + 409, + "ArrivalExpired", + ], + ["no credential", { jwt: undefined }, 409, "ArrivalExpired"], + ["malformed bearer", { jwt: "Bearer not-a-jwt" }, 409, "ArrivalExpired"], + [ + "wrong audience", + { + jwt: await s.f.credential(incomingHandoffMethod, { + aud: "did:web:other.example.test", + }), + }, + 403, + "AccountMismatch", + ], + [ + "creation-scoped credential", + { jwt: await s.f.credential(incomingCreationMethod) }, + 403, + "AccountMismatch", + ], + [ + "extra body field", + { json: { operation: s.operation, extra: true } }, + 400, + "InvalidRequest", + ], + [ + "operation not an object", + { json: { operation: "x" } }, + 400, + "InvalidRequest", + ], + ["empty body", { json: {} }, 400, "InvalidRequest"], + ]; + for (const [name, overrides, status, error] of cases) { + const response = await submit(s, overrides); + assert.equal(response.status, status, name); + assert.equal(response.json.error, error, name); + assert.notEqual(response.status, 418, name); + assert.equal(response.text.includes(s.prepared.arrivalCode), false, name); + } + await untouched(s.f); +}); + +test("a superseded code fails like an unknown one and the renewed code works", async (t) => { + const s = await begin(t); + const renewed = await s.h.browser("/incoming/renew", { + operationId: s.prepared.operationId, + expectedVersion: s.prepared.version, + }); + assert.equal(renewed.status, 200); + const old = await submit(s); + const unknown = await submit(s, { code: "B".repeat(32) }); + assert.equal(old.status, 409); + assert.equal(old.json.error, "ArrivalExpired"); + assert.deepEqual(old.json, unknown.json); + await untouched(s.f); + const fresh = await submit(s, { code: renewed.json.arrivalCode }); + assert.equal(fresh.status, 200); + assert.equal(s.f.publicationAttempts.length, 1); +}); + +test("an unsigned or wrongly signed handoff is refused before publication", async (t) => { + const s = await begin(t); + const stranger = await Secp256k1Keypair.create(); + const forged = await plc.signOperation(s.prepared.handoff, stranger); + const response = await submit(s, { json: { operation: forged } }); + assert.equal(response.status, 400); + assert.equal(response.json.error, "InvalidPlcOperation"); + await untouched(s.f); +}); + +test("createAccount without the header is not incoming", async (t) => { + const s = await published(t); + const response = await create(s, { handle }, { code: undefined }); + assert.equal(response.status, 418); + assert.equal(s.f.backendState.creates, 0); +}); + +test("createAccount returns tracked credentials only after the binding and family committed", async (t) => { + const { f, h, prepared, session } = await created(t); + void h; + assert.equal(session.did, f.did); + assert.equal(session.handle, handle); + assert.equal(session.active, false); + assert.equal(session.status, "deactivated"); + assert.equal(typeof session.accessJwt, "string"); + assert.equal(typeof session.refreshJwt, "string"); + assert.equal(f.backendState.creates, 1); + assert.equal(f.backendState.deactivated, true); + const account = (await rows(f, "accounts"))[0]; + assert.equal(account.status, "provisioning"); + assert.equal((await rows(f, "account_bindings")).length, 1); + const families = await f.arrivals.readArrivalFamilies(prepared.operationId); + assert.equal(families.length, 1); + assert.equal( + (await f.composed.authorizeArrivalAccess(session.accessJwt)).did, + f.did, + ); +}); + +test("a repeated createAccount cannot mint a second family; recovery is the saved-user path", async (t) => { + const s = await created(t); + const { f, h, prepared } = s; + const again = await create(s, { + handle, + inviteCode: "ignored-after-admission", + }); + assert.equal(again.status, 403); + assert.equal(again.json.error, "AccountMismatch"); + assert.equal(again.text.includes(s.session.accessJwt), false); + assert.equal( + (await f.arrivals.readArrivalFamilies(prepared.operationId)).length, + 1, + ); + assert.equal(f.backendState.creates, 1); + const recovered = await h.browser("/incoming/recover", { + operationId: prepared.operationId, + expectedVersion: ( + await f.arrivals.readArrivalAggregate( + prepared.operationId, + f.principal.userId, + ) + ).arrival.version, + }); + assert.equal(recovered.status, 200); + assert.equal(recovered.headers["cache-control"], "no-store"); + assert.notEqual(recovered.json.accessJwt, s.session.accessJwt); +}); + +test("invitation policy: required without a code is refused before any backend effect", async (t) => { + const s = await published(t); + assert.equal(s.f.config.inviteCodeRequired, true); + const response = await create(s, { handle }); + assert.equal(response.status, 400); + assert.equal(response.json.error, "InvalidInviteCode"); + assert.equal(s.f.backendState.creates, 0); +}); + +test("invitation policy: optional allows omission", async (t) => { + const s = await published(t); + s.f.config.inviteCodeRequired = false; + const response = await create(s, { handle }); + assert.equal(response.status, 200, response.text); + assert.equal(response.json.did, s.f.did); +}); + +test("invitation policy: a supplied invalid code is refused even when optional", async (t) => { + const s = await published(t); + s.f.config.inviteCodeRequired = false; + const response = await create(s, { handle, inviteCode: "no-such-code" }); + assert.equal(response.status, 400); + assert.equal(response.json.error, "InvalidInviteCode"); + assert.equal(s.f.backendState.creates, 0); +}); + +test("invitation policy: a supplied valid code is consumed once and no retry after cutover repeats it", async (t) => { + const s = await published(t); + const code = await invite(s.f); + const jwt = await s.f.credential(incomingCreationMethod); + const first = await create(s, { handle, inviteCode: code }, { jwt }); + assert.equal(first.status, 200); + // After cutover the source key no longer verifies, so even the exact retry + // cannot be admitted again: no second invitation, family or creation. The + // saved user recovers through /incoming/recover instead. + const retry = await create(s, { handle, inviteCode: code }, { jwt }); + assert.equal(retry.status, 403); + assert.equal(retry.json.error, "AccountMismatch"); + const used = await s.f.db.get("entryway:invites", code); + assert.equal(used.remaining, 0); + assert.equal(used.uses.length, 1); + assert.equal(s.f.backendState.creates, 1); +}); + +test("createAccount accepts only the supported strict subset", async (t) => { + const s = await published(t); + const inviteCode = await invite(s.f); + for (const body of [ + { handle, inviteCode, password: "correct horse battery" }, + { handle, inviteCode, plcOp: {} }, + { handle, inviteCode, verificationCode: "1" }, + { inviteCode }, + { handle: 7 }, + ]) { + const response = await create(s, body); + assert.equal(response.status, 400); + assert.equal(response.json.error, "InvalidRequest"); + } + // Valid shape but facts that are not the saved arrival's. + for (const body of [ + { handle: "someone.else.example.test", inviteCode }, + { handle, did: `did:plc:${"c".repeat(24)}`, inviteCode }, + { handle, email: "another@example.test", inviteCode }, + ]) { + const response = await create(s, body); + assert.equal(response.status, 400); + assert.equal(response.json.error, "InvalidAccount"); + } + assert.equal(s.f.backendState.creates, 0); +}); + +test("createAccount refuses wrong credentials without falling through to managed movement", async (t) => { + const s = await published(t); + const inviteCode = await invite(s.f); + const cases = [ + [{ code: "A".repeat(32) }, 409, "ArrivalExpired"], + [ + { jwt: await s.f.credential(incomingHandoffMethod) }, + 403, + "AccountMismatch", + ], + [ + { + jwt: await s.f.credential(incomingCreationMethod, { + aud: "did:web:other.example.test", + }), + }, + 403, + "AccountMismatch", + ], + [ + { + jwt: await s.f.credential(incomingCreationMethod, { + iss: `did:plc:${"b".repeat(24)}`, + }), + }, + 409, + "ArrivalExpired", + ], + [{ jwt: undefined }, 409, "ArrivalExpired"], + ]; + for (const [overrides, status, error] of cases) { + const response = await create(s, { handle, inviteCode }, overrides); + assert.equal(response.status, status); + assert.equal(response.json.error, error); + } + assert.equal(s.f.backendState.creates, 0); +}); + +test("createAccount before the handoff is canonical stays saved and creates nothing", async (t) => { + const s = await begin(t); + const response = await create(s, { handle, inviteCode: await invite(s.f) }); + assert.equal(response.status, 409); + assert.equal(response.json.error, "OperationRecoveryRequired"); + assert.equal(s.f.backendState.creates, 0); +}); + +test("refreshSession serves a tracked arrival family and leaves everything else to the ordinary route", async (t) => { + const { f, h, session } = await created(t); + const refreshed = await h.call(refreshPath, { + headers: { authorization: `Bearer ${session.refreshJwt}` }, + }); + assert.equal(refreshed.status, 200, refreshed.text); + assert.equal(refreshed.headers["cache-control"], "no-store"); + assert.notEqual(refreshed.json.refreshJwt, session.refreshJwt); + assert.equal(refreshed.json.active, false); + // Not an arrival family's credential: the ordinary route answers. + for (const authorization of [ + undefined, + "Bearer not-a-token", + `Bearer ${session.accessJwt}`, + ]) { + const ordinary = await h.call(refreshPath, { + headers: authorization ? { authorization } : {}, + }); + assert.equal(ordinary.status, 418); + } + // Replaying the rotated-out refresh credential is refused, not served. + const replay = await h.call(refreshPath, { + headers: { authorization: `Bearer ${session.refreshJwt}` }, + }); + assert.notEqual(replay.status, 200); + void f; +}); + +test("activateAccount activates a pending arrival with its tracked access credential, once", async (t) => { + const { f, h, session } = await created(t); + const response = await h.call(activatePath, { + headers: { authorization: `Bearer ${session.accessJwt}` }, + }); + assert.equal(response.status, 200, response.text); + assert.deepEqual(response.json, {}); + assert.equal(response.headers["cache-control"], "no-store"); + assert.equal(f.backendState.activations, 1); + assert.equal((await rows(f, "accounts"))[0].status, "active"); + // A lost response retries into the committed result with no second write. + const retry = await h.call(activatePath, { + headers: { authorization: `Bearer ${session.accessJwt}` }, + }); + assert.equal(retry.status, 200); + assert.equal(f.backendState.activations, 1); +}); + +test("activateAccount never activates for a source JWT, refresh token, replaced family or unknown bearer", async (t) => { + const { f, h, prepared, session } = await created(t); + const sourceJwt = await f.credential(incomingCreationMethod); + for (const authorization of [ + sourceJwt, + `Bearer ${session.refreshJwt}`, + "Bearer not-a-token", + "Basic Zm9vOmJhcg==", + ]) { + const response = await h.call(activatePath, { + headers: { authorization }, + }); + assert.equal(response.status, 418, authorization.slice(0, 12)); + } + assert.equal(f.backendState.activations, 0); + // A replaced family is refused with the ordinary expired-token code. + const recovered = await h.browser("/incoming/recover", { + operationId: prepared.operationId, + expectedVersion: ( + await f.arrivals.readArrivalAggregate( + prepared.operationId, + f.principal.userId, + ) + ).arrival.version, + }); + assert.equal(recovered.status, 200); + const stale = await h.call(activatePath, { + headers: { authorization: `Bearer ${session.accessJwt}` }, + }); + assert.equal(stale.status, 400); + assert.equal(stale.json.error, "ExpiredToken"); + assert.equal(f.backendState.activations, 0); + assert.equal((await rows(f, "accounts"))[0].status, "provisioning"); +}); + +test("browser activation needs a fresh code and the exact version, and activates through the same operation", async (t) => { + const { f, h, prepared } = await created(t); + const version = ( + await f.arrivals.readArrivalAggregate( + prepared.operationId, + f.principal.userId, + ) + ).arrival.version; + const input = { operationId: prepared.operationId, expectedVersion: version }; + const stale = await h.browser("/incoming/activate", input, { otp: "stale" }); + assert.equal(stale.status, 403); + assert.equal(stale.json.error, "ArrivalProofRequired"); + const wrong = await h.browser("/incoming/activate", { + ...input, + expectedVersion: version + 1, + }); + assert.equal(wrong.status, 409); + assert.equal(wrong.json.error, "OperationConflict"); + assert.equal(f.backendState.activations, 0); + const noCsrf = await h.browser("/incoming/activate", input, { + csrf: "forged", + }); + assert.equal(noCsrf.status, 400); + const ok = await h.browser("/incoming/activate", input); + assert.equal(ok.status, 200, ok.text); + assert.equal(ok.json.state, "active"); + assert.equal(f.backendState.activations, 1); + assert.equal((await rows(f, "accounts"))[0].status, "active"); +}); + +test("resume continues the saved steps for the saved user and sends nothing new", async (t) => { + const { f, h, prepared } = await created(t); + const effects = [f.backendState.creates, f.backendState.pauses]; + const response = await h.browser("/incoming/resume", { + operationId: prepared.operationId, + otp: undefined, + }); + assert.equal(response.status, 200, response.text); + assert.equal(response.json.state, "pending"); + assert.deepEqual([f.backendState.creates, f.backendState.pauses], effects); +}); + +test("recover refuses a stale version and a stale proof and leaves the issued family usable", async (t) => { + const { f, h, prepared, session } = await created(t); + const version = ( + await f.arrivals.readArrivalAggregate( + prepared.operationId, + f.principal.userId, + ) + ).arrival.version; + const stale = await h.browser("/incoming/recover", { + operationId: prepared.operationId, + expectedVersion: version - 1, + }); + assert.equal(stale.status, 409); + assert.equal(stale.json.error, "OperationConflict"); + const noProof = await h.browser( + "/incoming/recover", + { operationId: prepared.operationId, expectedVersion: version }, + { otp: "stale" }, + ); + assert.equal(noProof.status, 403); + assert.equal(noProof.json.error, "ArrivalProofRequired"); + assert.equal( + (await f.composed.authorizeArrivalAccess(session.accessJwt)).did, + f.did, + ); +}); + +test("only a legacy access token is ever offered to the activation operation", async (t) => { + const f = await fixture(t); + const offered = []; + const real = f.composed.activateArrivalWithCredential; + f.composed = { + ...f.composed, + activateArrivalWithCredential: (bearer) => { + offered.push(bearer); + return real(bearer); + }, + }; + const h = await incomingHttp(t, f); + const sourceJwt = await f.credential(incomingCreationMethod); + for (const authorization of [ + sourceJwt, + "Bearer not-a-token", + "Bearer a.b.c", + "Basic Zm9vOmJhcg==", + ]) + assert.equal( + (await h.call(activatePath, { headers: { authorization } })).status, + 418, + ); + assert.deepEqual(offered, []); +}); + +test("after completion the committed result answers only while the account is active; later reactivation is the ordinary route's", async (t) => { + const { f, h, session } = await created(t); + const bearer = { authorization: `Bearer ${session.accessJwt}` }; + assert.equal((await h.call(activatePath, { headers: bearer })).status, 200); + // Lost final response while still active: the committed result again. + assert.equal((await h.call(activatePath, { headers: bearer })).status, 200); + assert.equal(f.backendState.activations, 1); + // The user later deactivates through the ordinary route. + const row = await f.storage.getByDid(f.did); + await f.storage.saveAccount({ ...row, status: "deactivated" }); + const later = await h.call(activatePath, { headers: bearer }); + assert.equal(later.status, 418); + assert.deepEqual(later.json, { ordinary: "server.activateAccount" }); + assert.equal(f.backendState.activations, 1); + // The same tracked family's ordinary refresh is also not an arrival's. + const refreshed = await h.call(refreshPath, { + headers: { authorization: `Bearer ${session.refreshJwt}` }, + }); + assert.equal(refreshed.status, 418); +}); + +test("a cross-origin post spends no browser budget", async (t) => { + const f = await fixture(t); + const h = await incomingHttp(t, f); + for (let attempt = 0; attempt < 40; attempt++) { + const refused = await h.browser( + "/incoming/prepare", + { did: f.did, handle }, + { origin: "https://evil.example.test" }, + ); + assert.equal(refused.status, 400); + assert.equal(refused.json.error, "InvalidRequest"); + } + const ok = await h.browser("/incoming/prepare", { did: f.did, handle }); + assert.equal(ok.status, 200); +}); + +test("the tool request budget answers 429 with the existing code", async (t) => { + const s = await begin(t); + let last; + for (let attempt = 0; attempt < 31; attempt++) + last = await submit(s, { code: "A".repeat(32) }); + assert.equal(last.status, 429); + assert.equal(last.json.error, "RateLimitExceeded"); + // The budget is shared with a legitimate request from the same address. + const legitimate = await submit(s); + assert.equal(legitimate.status, 429); + await untouched(s.f); +}); + +test("a valid code with an invalid credential takes no lease and writes nothing", async (t) => { + const s = await begin(t); + let leases = 0; + const resume = s.f.ownership.resumePending; + s.f.ownership.resumePending = (...args) => { + leases++; + return resume(...args); + }; + const bad = [ + await s.f.credential(incomingHandoffMethod, { + aud: "did:web:other.example.test", + }), + await s.f.credential(incomingCreationMethod), + "Bearer a.b.c", + ]; + for (const jwt of bad) { + const response = await submit(s, { jwt }); + assert.equal(response.status === 403 || response.status === 409, true); + } + assert.equal(leases, 0); + await untouched(s.f); + // The legitimate request still takes the lease exactly as before. + assert.equal((await submit(s)).status, 200); + assert.equal(leases > 0, true); +}); + +test("a completed arrival is not reported on a repeat prepare", async (t) => { + const { f, h, prepared, session } = await created(t); + const activated = await h.call(activatePath, { + headers: { authorization: `Bearer ${session.accessJwt}` }, + }); + assert.equal(activated.status, 200); + const repeat = await h.browser("/incoming/prepare", { + did: f.did, + handle, + }); + assert.equal(repeat.status >= 400, true); + assert.equal(repeat.json.operationId, undefined); + assert.equal(repeat.text.includes(prepared.operationId), false); +}); diff --git a/src/features/external-migration/page.ts b/src/features/external-migration/page.ts new file mode 100644 index 00000000..396b505d --- /dev/null +++ b/src/features/external-migration/page.ts @@ -0,0 +1,108 @@ +import { escapeHtml as esc } from "../../ui/html.mjs"; +import { hidden } from "../../ui/forms.mjs"; +import type { ArrivalSummary } from "./arrival-summary.js"; + +const input = (name: string, label: string, attributes = "") => + ``; +const otpField = input( + "otp", + "Fresh email code", + 'type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="8"', +); +const post = ( + action: string, + csrf: string, + contents: string, + button: string, + secondary = false, +) => + `${hidden("csrf", csrf)}${contents}${esc(button)}`; +const operation = (summary: ArrivalSummary) => + hidden("operationId", summary.operationId) + + hidden("expectedVersion", String(summary.version)); + +const stateText: Record = { + prepared: + "Prepared. Your migration tool has not completed the identity handoff yet.", + "handoff-published": + "Your identity handoff was published. The destination account has not been created yet.", + created: + "The destination account exists but is not yet saved as paused and bound to you.", + "paused-pending": + "The destination account exists and is paused. It is not active: it cannot sign in to applications or be changed here until your migration tool finishes and activation is requested.", + active: "Migration complete. This account is active.", + cancelled: "This arrival was cancelled.", +}; + +/** The incoming-migration start page, or the saved arrival's status and the + * actions that are honest for its state. */ +export function renderIncomingPage( + csrf: string, + summary: ArrivalSummary | null, +): string { + if (!summary) + return `

Move an existing account to this service. You keep your DID. Your migration tool transfers your data and decides when to ask for activation; this service does not check that your data is complete.

+${post("/incoming/code", csrf, "", "Send me a fresh email code", true)} +

Prepare an arrival

+

Enter the new destination handle and the code from your email. You receive an arrival code once; give it to your migration tool.

+
${hidden("csrf", csrf)}${input("did", "Your DID", 'type="text" maxlength="64" autocomplete="off"')}${input("handle", "Destination handle", 'type="text" maxlength="253" autocomplete="off"')}${otpField}
+

Open a saved arrival

+
`; + const actions: string[] = []; + const resume = post( + "/incoming/resume", + csrf, + hidden("operationId", summary.operationId), + "Resume saved steps", + true, + ); + if (summary.state === "prepared" || summary.state === "handoff-published") + actions.push( + post( + "/incoming/renew", + csrf, + operation(summary) + otpField, + "Renew arrival code", + ), + resume, + ); + if (summary.state === "created") actions.push(resume); + if (summary.state === "paused-pending") + actions.push( + `

Replace the migration credentials if you lost them. Earlier credentials stop working, except an access token already issued, which the data server accepts until it expires (about five minutes).

${post("/incoming/recover", csrf, operation(summary) + otpField, "Replace migration credentials")}`, + `

Activate only after your migration tool has finished transferring and checking your data. This service does not check records or blobs, and later changes at the source may not be transferred.

${post("/incoming/activate", csrf, operation(summary) + otpField, "Activate this account")}`, + resume, + ); + const warning = summary.unresolvedAttempt + ? `

A saved server request is unresolved. Contact the service operator; retrying alone cannot settle it.

` + : ""; + return `
Operation
${esc(summary.operationId)}
DID
${esc(summary.did)}
Handle
${esc(summary.handle)}
Version
${summary.version}

${esc(stateText[summary.state])}

${warning}${actions.join("")}

Back

`; +} + +export const renderFailure = (message: string, recovery: boolean): string => + `

${esc(message)}

${ + recovery + ? "

Your saved migration is still pending. Contact the service operator; retrying or signing in again alone cannot settle an uncertain server request.

" + : "" + }

Back to incoming migration

`; + +/** Result pages. Secret values appear only on the page that issues them. */ +export const renderResult = ( + fields: Record, + secret: boolean, +): string => + `${secret ? "

Copy these now. They are shown once and are not stored in a readable form. Do not share them.

" : ""}
${Object.entries( + fields, + ) + .map( + ([name, value]) => + `
${esc(name)}
${esc(typeof value === "string" ? value : JSON.stringify(value))}
`, + ) + .join("")}

Back

`; + +/** The saved user's own arrival for this DID, after a repeated preparation. */ +export const renderExisting = (existing: { + operationId: string; + version: number; +}): string => + `

You already have an incoming migration for this DID. Its operation ID is ${esc(existing.operationId)} (version ${existing.version}). Your arrival code cannot be shown again; open it to renew the code or continue.

Open this migration

`; diff --git a/src/features/external-migration/prepare-arrival.ts b/src/features/external-migration/prepare-arrival.ts index 0380e78f..66cdfbe9 100644 --- a/src/features/external-migration/prepare-arrival.ts +++ b/src/features/external-migration/prepare-arrival.ts @@ -175,6 +175,45 @@ export function createIncomingPreparation({ throw new HttpError(400, "InvalidRequest", "A PLC DID is required"); ensureValidHandle(input.handle); const pds = choosePds(); + try { + return await prepareOwned(identity, input, pds); + } catch (error) { + // Only after the full proof sequence. A repeat by the SAME saved user for + // the same DID is told their own arrival's operation ID, so a lost + // preparation response can be continued. Another user's arrival is + // indistinguishable from today's refusal. Read-only: no code, no secret, + // no allocation, no version change. + if ( + error instanceof DomainError && + error.code === "OperationRecoveryRequired" + ) { + try { + const found = await arrivals.readArrivalAccess(input.did); + // `readArrival` is owner-scoped and returns only a live arrival, so a + // completed or cancelled one is never reported. + const saved = + found?.userId === identity.userId + ? await arrivals.readArrival(found.operationId, identity.userId) + : null; + if (saved) + Object.assign(error, { + existing: { + operationId: saved.operationId, + version: saved.version, + }, + }); + } catch { + // The original refusal is what the caller sees. + } + } + throw error; + } + }; + async function prepareOwned( + identity: { userId: string; email: string }, + input: { did: string; handle: string }, + pds: { id: string; url: string }, + ) { return ownership.run( `arrival:${input.did}`, { @@ -245,7 +284,7 @@ export function createIncomingPreparation({ }; }, ); - }; + } } /** One-time displayed arrival code. Only its hash and expiry are persisted. */ diff --git a/src/features/external-migration/routes.ts b/src/features/external-migration/routes.ts new file mode 100644 index 00000000..658cab41 --- /dev/null +++ b/src/features/external-migration/routes.ts @@ -0,0 +1,285 @@ +import express from "express"; +import type { Express, NextFunction, Request, Response } from "express"; +import { z } from "zod"; +import { HttpError } from "../../http/http-error.mjs"; +import { page } from "../../ui/html.mjs"; +import type { ArrivalSummary } from "./arrival-summary.js"; +import { + renderExisting, + renderFailure, + renderIncomingPage, + renderResult, +} from "./page.js"; + +/** The incoming-arrival operations the browser surfaces call. Each already + * performs origin/CSRF, the saved-user check and (where required) a fresh email + * code; this adapter only validates shapes and maps results and failures. */ +export interface IncomingBrowserOperations { + prepareArrival( + request: Request, + otp: string, + input: { did: string; handle: string }, + ): Promise>; + renewArrival( + request: Request, + otp: string, + input: { operationId: string; expectedVersion: number }, + ): Promise>; + cancelArrival( + request: Request, + otp: string, + input: { operationId: string; expectedVersion: number }, + ): Promise>; + recoverArrivalSession( + request: Request, + otp: string, + input: { operationId: string; expectedVersion: number }, + ): Promise>; + activateArrivalAsUser( + request: Request, + otp: string, + input: { operationId: string; expectedVersion: number }, + ): Promise>; + reconcileArrival( + request: Request, + operationId: string, + ): Promise>; + describeArrival( + request: Request, + operationId: unknown, + ): Promise; + sendArrivalCode(request: Request): Promise>; +} + +const proof = { csrf: z.string().min(1).max(200) }; +const code = z.string().min(1).max(32); +const operationId = z.string().min(1).max(200); +const saved = z.strictObject({ + ...proof, + otp: code, + operationId, + expectedVersion: z.number().int().min(1).max(Number.MAX_SAFE_INTEGER), +}); +const schemas = { + code: z.strictObject(proof), + prepare: z.strictObject({ + ...proof, + otp: code, + did: z.string().min(1).max(200), + handle: z.string().min(1).max(253), + }), + renew: saved, + cancel: saved, + recover: saved, + activate: saved, + resume: z.strictObject({ ...proof, operationId }), +}; + +const invalid = () => new HttpError(400, "InvalidRequest", "Invalid request"); + +/** Forms post strings; JSON bodies are held to their exact types. */ +function normalize(body: unknown, json: boolean): unknown { + if (json || !body || typeof body !== "object") return body; + const fields = { ...(body as Record) }; + const version = fields.expectedVersion; + if (typeof version === "string" && /^[0-9]{1,15}$/.test(version)) + fields.expectedVersion = Number(version); + return fields; +} + +/** CSRF failures arrive from the OAuth provider package with its own shape. */ +function stable(error: unknown): unknown { + if ( + error && + typeof error === "object" && + (error as { error?: unknown }).error === "invalid_request" + ) + return new HttpError( + 400, + "InvalidRequest", + "Invalid form origin or CSRF token", + ); + return error; +} + +export function mountIncomingRoutes({ + app, + issuer, + incoming, + oauth, + rateLimit, +}: { + app: Express; + issuer: string; + incoming: IncomingBrowserOperations; + oauth: { + loadBrowser( + request: Request, + response: Response, + ): Promise<{ csrf: string }>; + requireSession( + request: Request, + ): Promise<{ emailVerified: boolean } | null>; + }; + rateLimit: (key: string, limit?: number) => Promise; +}): void { + const form = express.urlencoded({ extended: false, limit: "16kb" }); + const origin = new URL(issuer).origin; + // A cross-site form must not be able to spend a victim IP's budget, so the + // same-origin check precedes it. CSRF is still verified by each operation. + const budget = async (request: Request) => { + if (request.headers.origin !== origin) + throw new HttpError( + 400, + "InvalidRequest", + "Invalid form origin or CSRF token", + ); + await rateLimit(`incoming-browser-ip:${request.ip}`, 30); + }; + const failure = ( + request: Request, + response: Response, + next: NextFunction, + error: unknown, + ) => { + const mapped = stable(error) as { + status?: number; + statusCode?: number; + error?: string; + code?: string; + message?: string; + existing?: { operationId: string; version: number }; + }; + const status = Number(mapped?.status ?? mapped?.statusCode ?? 500); + // The refusal's code and status are unchanged; the saved user's own + // operation ID is added only when preparation attached it. + if (mapped?.existing && status >= 400 && status < 500) { + response.set("Cache-Control", "no-store"); + const code = String(mapped.code ?? mapped.error); + if (request.is("application/json")) + return response.status(status).json({ + error: code, + message: mapped.message, + operationId: mapped.existing.operationId, + version: mapped.existing.version, + }); + return page( + response, + "Incoming migration", + renderFailure(mapped.message ?? "The request failed", false) + + renderExisting(mapped.existing), + status, + ); + } + if (request.is("application/json")) return next(mapped); + if (!(status >= 400 && status < 500)) return next(mapped); + const recovery = + (mapped.code ?? mapped.error) === "OperationRecoveryRequired"; + return page( + response, + recovery ? "Migration waiting for recovery" : "Incoming migration", + renderFailure(mapped.message ?? "The request failed", recovery), + status, + ); + }; + /** One POST: IP budget, exact shape, then the operation. Secret responses are + * never cacheable. Strings in forms and typed JSON are both accepted. */ + const post = ( + name: K, + run: ( + request: Request, + body: z.infer<(typeof schemas)[K]>, + ) => Promise>, + title: string, + secret: boolean, + ) => + app.post(`/incoming/${name}`, form, async (req, res, next) => { + try { + await budget(req); + const json = Boolean(req.is("application/json")); + const parsed = schemas[name].safeParse(normalize(req.body, json)); + if (!parsed.success) throw invalid(); + const result = await run(req, parsed.data as never); + res.set("Cache-Control", "no-store"); + if (json) return res.json(result); + return page(res, title, renderResult(result, secret)); + } catch (error) { + failure(req, res, next, error); + } + }); + post( + "prepare", + (req, body) => + incoming.prepareArrival(req, body.otp, { + did: body.did, + handle: body.handle, + }), + "Arrival prepared", + true, + ); + post( + "renew", + (req, body) => + incoming.renewArrival(req, body.otp, { + operationId: body.operationId, + expectedVersion: body.expectedVersion, + }), + "Arrival code renewed", + true, + ); + post( + "cancel", + (req, body) => + incoming.cancelArrival(req, body.otp, { + operationId: body.operationId, + expectedVersion: body.expectedVersion, + }), + "Arrival cancelled", + false, + ); + post( + "recover", + (req, body) => + incoming.recoverArrivalSession(req, body.otp, { + operationId: body.operationId, + expectedVersion: body.expectedVersion, + }), + "Migration credentials replaced", + true, + ); + post( + "activate", + (req, body) => + incoming.activateArrivalAsUser(req, body.otp, { + operationId: body.operationId, + expectedVersion: body.expectedVersion, + }), + "Account activated", + false, + ); + post( + "resume", + (req, body) => incoming.reconcileArrival(req, body.operationId), + "Saved steps resumed", + false, + ); + post("code", (req) => incoming.sendArrivalCode(req), "Code sent", false); + app.get("/incoming", async (req, res, next) => { + try { + const session = await oauth.requireSession(req); + if (!session?.emailVerified) return res.redirect(303, "/login"); + const browser = await oauth.loadBrowser(req, res); + const id = req.query.operationId; + if (id !== undefined && typeof id !== "string") throw invalid(); + const summary = + id === undefined ? null : await incoming.describeArrival(req, id); + return page( + res, + "Incoming migration", + renderIncomingPage(browser.csrf, summary), + ); + } catch (error) { + failure(req, res, next, error); + } + }); +} diff --git a/src/features/external-migration/tool-admission.ts b/src/features/external-migration/tool-admission.ts new file mode 100644 index 00000000..de9d7b3a --- /dev/null +++ b/src/features/external-migration/tool-admission.ts @@ -0,0 +1,207 @@ +import { HttpError } from "../../http/http-error.mjs"; +import type { ArrivalTransactor } from "../../database/migration-journal.port.js"; +import { DomainError } from "../../accounts/errors.js"; +import type { TrackedArrivalSession } from "./arrival-credentials.js"; +import { + incomingCreationMethod, + incomingHandoffMethod, + type IncomingMethod, + type VerifyIncomingAuthorization, +} from "./incoming-authorization.js"; +import { parseArrivalRequest } from "./arrival-request.js"; +import type { ArrivalRecord } from "./arrival-types.js"; +import { arrivalCodeHash } from "./prepare-arrival.js"; +import { PlcError } from "../../plc/errors.js"; + +/** Identity-layer failures carry no HTTP status. Map them to stable public + * codes; the message is the fixed public text, never a candidate or key. */ +async function stableIdentity(work: () => Promise): Promise { + try { + return await work(); + } catch (error) { + if (!(error instanceof PlcError)) throw error; + if (error.code === "InvalidPlcOperation") + throw new HttpError(400, "InvalidPlcOperation", error.message); + throw new HttpError( + 409, + "IdentityConflict", + "The canonical identity history does not allow this arrival", + ); + } +} + +/** + * Cookie-less admission for the migration tool. + * + * PROPOSED RESOLUTION, NOT AN ACCEPTED CONTRACT. C1 says the tool surfaces never + * accept or export browser cookies, but admission and continuation otherwise + * resolve their owner from a browser principal. Until the owner decides, this is + * the single place that selects an arrival for the XRPC surfaces: the presented + * current, unexpired arrival code selects the arrival and the user saved on it. + * The user is never taken from an email, a request value or the credential, and + * the method-bound source JWT is still verified by admission exactly as before. + * Replacing this resolution means replacing `select` below and nothing else. + * + * What the code alone permits before the source JWT verifies: reading the + * arrival row and contending for the arrival's ownership lease (a denial of + * service limited by the caller's request budget). It records nothing, signs + * nothing and dispatches nothing. Every failure to select an arrival, including + * an unparsable credential, is the same `ArrivalExpired`, so a caller without the + * code learns nothing about any DID, operation or user. + */ +const codeShape = /^[A-Za-z0-9_-]{16,128}$/; +const bearerShape = /^Bearer ([A-Za-z0-9._~+/-]+=*)$/; +const issuerShape = /^(did:plc:[a-z2-7]{24})(?:#atproto)?$/; + +const expired = () => + new HttpError(409, "ArrivalExpired", "This arrival code is no longer valid"); + +/** The DID a presented source credential claims, read WITHOUT verifying it. It + * only picks which arrival to look up; admission verifies the signature, the + * method, the audience and that this DID is the arrival's DID. */ +function claimedDid(presentation: unknown): string | null { + if (typeof presentation !== "string") return null; + const token = bearerShape.exec(presentation.trim())?.[1]; + const payload = token?.split(".")[1]; + if (!payload) return null; + try { + const claims: unknown = JSON.parse( + Buffer.from(payload, "base64url").toString("utf8"), + ); + const issuer = + claims && typeof claims === "object" + ? (claims as Record).iss + : undefined; + return typeof issuer === "string" + ? (issuerShape.exec(issuer)?.[1] ?? null) + : null; + } catch { + return null; + } +} + +export interface ToolAdmissionInput { + readonly arrivalCode: unknown; + readonly presentation: unknown; + readonly body: unknown; +} + +export function createToolAdmission({ + arrivals, + admit, + verify, + continueCreation, + issueCreationSession, +}: { + verify: VerifyIncomingAuthorization; + arrivals: Pick; + admit: (input: { + operationId: string; + userId: string; + method: IncomingMethod; + body: unknown; + presentation: unknown; + arrivalCode: string; + }) => Promise; + continueCreation: (input: { + operationId: string; + userId: string; + }) => Promise; + issueCreationSession: (input: { + did: string; + operationId: string; + arrivalCode: string; + }) => Promise; +}) { + /** The one selection point. Method first, then a current code. */ + const select = async ( + method: IncomingMethod, + input: ToolAdmissionInput, + ): Promise<{ record: ArrivalRecord; arrivalCode: string }> => { + if (method !== incomingHandoffMethod && method !== incomingCreationMethod) + throw new HttpError(400, "InvalidRequest", "Unsupported incoming method"); + const arrivalCode = input.arrivalCode; + const did = claimedDid(input.presentation); + if (typeof arrivalCode !== "string" || !codeShape.test(arrivalCode) || !did) + throw expired(); + const record = await arrivals.consumeArrivalCode({ + codeHash: arrivalCodeHash(arrivalCode), + did, + method, + }); + if (!record) throw expired(); + // The code only selects. The method-bound source credential is verified + // with the same verifier and request digest admission uses, BEFORE any + // ownership lease is taken, so a leaked code with a bad credential cannot + // contend for the arrival. Verification consumes no replay state; admission + // verifies again and records it. + const parsed = parseArrivalRequest(record, method, input.body); + await verify({ + presentation: input.presentation, + method, + operationId: record.operationId, + requestDigest: parsed.requestDigest, + expectedDid: record.did, + }); + return { record, arrivalCode }; + }; + return { + /** The standard identity submit: success is exactly the canonical handoff. */ + async submitHandoff( + input: ToolAdmissionInput, + ): Promise> { + const { record, arrivalCode } = await select( + incomingHandoffMethod, + input, + ); + const result = (await stableIdentity(() => + admit({ + operationId: record.operationId, + userId: record.userId, + method: incomingHandoffMethod, + body: input.body, + presentation: input.presentation, + arrivalCode, + }), + )) as { state?: string } | undefined; + // A lost or not-yet-canonical publication stays saved; the exact request + // is the retry. Never report publication that was not observed. + if (result?.state !== "published") + throw new DomainError( + "OperationRecoveryRequired", + 409, + "The handoff is saved but not yet observed as published; retry the same request", + ); + return {}; + }, + /** The standard account creation, returning tracked credentials only after + * the saved binding and the credential family committed together. */ + async createAccount( + input: ToolAdmissionInput, + ): Promise { + const { record, arrivalCode } = await select( + incomingCreationMethod, + input, + ); + return stableIdentity(async () => { + await admit({ + operationId: record.operationId, + userId: record.userId, + method: incomingCreationMethod, + body: input.body, + presentation: input.presentation, + arrivalCode, + }); + await continueCreation({ + operationId: record.operationId, + userId: record.userId, + }); + return issueCreationSession({ + did: record.did, + operationId: record.operationId, + arrivalCode, + }); + }); + }, + }; +} diff --git a/src/features/external-migration/xrpc-routes.ts b/src/features/external-migration/xrpc-routes.ts new file mode 100644 index 00000000..70345c19 --- /dev/null +++ b/src/features/external-migration/xrpc-routes.ts @@ -0,0 +1,153 @@ +import type { Express, NextFunction, Request, Response } from "express"; +import { decodeProtectedHeader } from "jose"; +import { z } from "zod"; +import { HttpError } from "../../http/http-error.mjs"; +import type { TrackedArrivalSession } from "./arrival-credentials.js"; + +/** Tool-facing surfaces. Authentication here is never a browser cookie. */ +export interface IncomingToolOperations { + submitIncomingHandoff(input: { + arrivalCode: unknown; + presentation: unknown; + body: unknown; + }): Promise>; + createIncomingAccount(input: { + arrivalCode: unknown; + presentation: unknown; + body: unknown; + }): Promise; + refreshArrivalSession(refresh: unknown): Promise; + activateArrivalWithCredential(bearer: unknown): Promise; +} + +export const arrivalCodeHeader = "entryway-arrival-code"; + +const submit = z.strictObject({ + operation: z.record(z.string(), z.unknown()), +}); +// Only the stock fields the incoming admission supports. Other stock optional +// fields are rejected, never silently dropped. +const create = z.strictObject({ + handle: z.string().min(1).max(253), + did: z.string().min(1).max(200).optional(), + email: z.string().min(1).max(320).optional(), + inviteCode: z.string().min(1).max(200).optional(), +}); + +const bearer = (request: Request): string | undefined => + /^Bearer (\S+)$/.exec(request.headers.authorization ?? "")?.[1]; + +/** Registered before the ordinary protocol routes. `createAccount` and + * `submitPlcOperation` are incoming only when the arrival-code header is + * present, and then they are never routed to hosted movement; every other + * request falls through unchanged. The two session routes serve a tracked + * arrival credential first and fall through for everything else. */ +export function mountIncomingXrpc({ + app, + incoming, + protocolOperations, +}: { + app: Express; + incoming: IncomingToolOperations; + protocolOperations: { rateLimit(key: string, limit?: number): Promise }; +}): void { + const handle = + ( + work: (request: Request) => Promise, + { onlyWithCode }: { onlyWithCode: boolean }, + ) => + async (request: Request, response: Response, next: NextFunction) => { + if (onlyWithCode && request.headers[arrivalCodeHeader] === undefined) + return next(); + try { + response.set("Cache-Control", "no-store"); + response.json((await work(request)) ?? {}); + } catch (error) { + next(error); + } + }; + const admitted = + ( + run: (input: { + arrivalCode: unknown; + presentation: unknown; + body: unknown; + }) => Promise, + schema: z.ZodType, + ) => + async (request: Request) => { + await protocolOperations.rateLimit(`incoming-tool-ip:${request.ip}`, 30); + const parsed = schema.safeParse(request.body); + if (!parsed.success) + throw new HttpError(400, "InvalidRequest", "Invalid request"); + return run({ + arrivalCode: request.headers[arrivalCodeHeader], + presentation: request.headers.authorization, + body: parsed.data, + }); + }; + app.post( + "/xrpc/com.atproto.identity.submitPlcOperation", + handle( + admitted((input) => incoming.submitIncomingHandoff(input), submit), + { + onlyWithCode: true, + }, + ), + ); + app.post( + "/xrpc/com.atproto.server.createAccount", + handle( + admitted((input) => incoming.createIncomingAccount(input), create), + { + onlyWithCode: true, + }, + ), + ); + // Tracked arrival credentials: only an unfinished, bound arrival's family is + // served here (InvalidToken or ArrivalExpired mean "not an unfinished + // arrival's"); anything else is the ordinary session, unchanged. + const notArrival = (error: unknown) => + ["InvalidToken", "ArrivalExpired"].includes( + String((error as { error?: unknown })?.error), + ); + app.post( + "/xrpc/com.atproto.server.refreshSession", + async (request, response, next) => { + const token = bearer(request); + if (!token) return next(); + try { + const session = await incoming.refreshArrivalSession(token); + response.set("Cache-Control", "no-store"); + return response.json(session); + } catch (error) { + return notArrival(error) ? next() : next(error); + } + }, + ); + // The stock PDS forwards its own activateAccount here with the caller's + // access credential, so this one route serves direct and PDS-forwarded calls. + // Only a legacy access token can be an arrival credential; a source service + // JWT, an OAuth token or anything else is never offered to activation. + app.post( + "/xrpc/com.atproto.server.activateAccount", + async (request, response, next) => { + const token = bearer(request); + let legacyAccess = false; + try { + legacyAccess = + token !== undefined && decodeProtectedHeader(token).typ === "at+jwt"; + } catch { + legacyAccess = false; + } + if (!token || !legacyAccess) return next(); + try { + await incoming.activateArrivalWithCredential(token); + response.set("Cache-Control", "no-store"); + return response.json({}); + } catch (error) { + return notArrival(error) ? next() : next(error); + } + }, + ); +} diff --git a/src/features/oauth-authorization/authorization-routes.test.mjs b/src/features/oauth-authorization/authorization-routes.test.mjs index fc094c61..97b4f1c5 100644 --- a/src/features/oauth-authorization/authorization-routes.test.mjs +++ b/src/features/oauth-authorization/authorization-routes.test.mjs @@ -190,6 +190,7 @@ async function fixture( }, }); const { authenticated } = createConsent({ + pendingArrival: { isPending: async () => false }, db, provider, stores: { account: async () => null }, diff --git a/src/features/oauth-authorization/consent.mjs b/src/features/oauth-authorization/consent.mjs index 6581a84e..3b090819 100644 --- a/src/features/oauth-authorization/consent.mjs +++ b/src/features/oauth-authorization/consent.mjs @@ -3,6 +3,7 @@ import { escapeHtml } from "../../ui/html.mjs"; import { hidden } from "../../ui/forms.mjs"; import { matchesAuthorizationHint } from "../../accounts/rules.js"; import { completeAuthorization } from "./complete-authorization.mjs"; +import { pendingArrivalMessage } from "../../accounts/pending-arrival.js"; export function createConsent({ db, config, @@ -10,6 +11,7 @@ export function createConsent({ stores, flows, accountAccess, + pendingArrival, clientRequestsSignupConsentSkip, }) { const { fields, save, readCurrentIntent, pageForFlow } = flows; @@ -111,6 +113,16 @@ export function createConsent({ 403, ); commitCookies(); + // Deny by default: a pending incoming arrival never authorizes an + // application and is never offered reactivation. + if (await pendingArrival.isPending(authorized.account)) + return pageForFlow( + res, + "Migration in progress", + `

${escapeHtml(pendingArrivalMessage)}

Review your incoming migration

`, + authorized.current, + 409, + ); if (authorized.account.status !== "active") return pageForFlow( res, diff --git a/src/features/oauth-authorization/consent.test.mjs b/src/features/oauth-authorization/consent.test.mjs index 11ccda76..f57a3bcf 100644 --- a/src/features/oauth-authorization/consent.test.mjs +++ b/src/features/oauth-authorization/consent.test.mjs @@ -6,6 +6,7 @@ function consentFor(status) { const pages = []; const linked = []; const { authenticated } = createConsent({ + pendingArrival: { isPending: async () => false }, db: { transact: (operation) => operation(), delete: async () => {} }, provider: { accountManager: { @@ -72,3 +73,37 @@ test("deactivated bound account keeps its session and reactivation path", async assert.equal(pages[0].statusCode, 403); assert.match(pages[0].body, /reactivate/); }); + +test("a pending incoming arrival is never offered reactivation and authorizes nothing", async () => { + const pages = []; + const { authenticated } = createConsent({ + // The real predicate is built from saved arrival facts; here a pending + // arrival is the `provisioning` row. + pendingArrival: { isPending: async (row) => row.status === "provisioning" }, + db: { transact: (operation) => operation(), delete: async () => {} }, + provider: { + accountManager: { upsertDeviceAccount: async () => {} }, + }, + stores: { account: async () => null }, + accountAccess: { + requireBoundAccount: async (_identity, did) => ({ + did, + status: "provisioning", + }), + }, + flows: { + fields: () => "", + save: async () => {}, + readCurrentIntent: async (flow) => ({ ...flow }), + pageForFlow: (_res, title, body, _flow, statusCode) => { + pages.push({ title, body, statusCode }); + }, + }, + }); + await sign(authenticated, "provisioning"); + assert.equal(pages.length, 1); + assert.equal(pages[0].statusCode, 409); + assert.equal(pages[0].title, "Migration in progress"); + assert.match(pages[0].body, /unfinished incoming migration/); + assert.doesNotMatch(pages[0].body, /reactivate this|Open account settings/); +}); diff --git a/src/features/oauth-authorization/signup-consent-skip.test.mjs b/src/features/oauth-authorization/signup-consent-skip.test.mjs index 05503a77..b1075c52 100644 --- a/src/features/oauth-authorization/signup-consent-skip.test.mjs +++ b/src/features/oauth-authorization/signup-consent-skip.test.mjs @@ -91,6 +91,7 @@ async function fixture( authEmail: account.email, }); const { authenticated } = createConsent({ + pendingArrival: { isPending: async () => false }, db, config, provider, diff --git a/src/http/account-console.mjs b/src/http/account-console.mjs index 834b8164..61451e3e 100644 --- a/src/http/account-console.mjs +++ b/src/http/account-console.mjs @@ -4,6 +4,10 @@ import { hidden } from "../ui/forms.mjs"; import { accountUrl, manageUrl } from "../ui/account-forms.mjs"; import { accountChooser } from "../ui/account-chooser.mjs"; import { HttpError } from "./http-error.mjs"; +import { + createPendingArrivalGuard, + pendingArrivalMessage, +} from "../accounts/pending-arrival.js"; import { noRecentLogin, retryableCodeFailures, @@ -40,6 +44,7 @@ export function createAccountConsole({ security, legacy, }) { + const pendingArrival = createPendingArrivalGuard(accounts.storage); const guarded = (handler) => async (req, res, next) => { try { await handler(req, res); @@ -108,6 +113,17 @@ export function createAccountConsole({ canSwitchAccount = resolution.kind === "selection-required"; } if (account.status === "deleted") return noAccount(res); + // Deny by default: an unfinished incoming arrival has no ordinary account + // settings, so nothing here may invite reactivation or any change. + if (await pendingArrival.isPending(account)) { + page( + res, + "Migration in progress", + `

${esc(pendingArrivalMessage)}

Review your incoming migration

`, + 409, + ); + return null; + } const deviceAccount = ( await oauth.provider.accountManager.listDeviceAccounts(browser.deviceId) ).find(({ account: device }) => device.did === account.did); diff --git a/src/logging/request-event.ts b/src/logging/request-event.ts index ceb29039..cb31a82f 100644 --- a/src/logging/request-event.ts +++ b/src/logging/request-event.ts @@ -12,6 +12,8 @@ const safeCodes = new Set([ "AccountMismatch", "AccountNotFound", "AccountUnavailable", + "ArrivalExpired", + "ArrivalProofRequired", "AuthRequired", "AuthenticationRequired", "AuthorityNotManaged", @@ -47,7 +49,9 @@ const safeCodes = new Set([ "MigrationUpstreamError", "MissingSnapshot", "NotFound", + "OperationConflict", "OperationPending", + "OperationRecoveryRequired", "OwnerBindingChanged", "RateLimitExceeded", "ReauthenticationRequired", diff --git a/tests/browser/incoming-public-journey.spec.mjs b/tests/browser/incoming-public-journey.spec.mjs new file mode 100644 index 00000000..d2211aac --- /dev/null +++ b/tests/browser/incoming-public-journey.spec.mjs @@ -0,0 +1,422 @@ +import { test, expect } from "@playwright/test"; +import assert from "node:assert/strict"; +import { randomUUID } from "node:crypto"; +import { readFileSync } from "node:fs"; +import { waitForMailpitCode } from "../support/helpers/mailpit.mjs"; +import { waitForStockPlcMessage } from "../support/helpers/stock-plc-mailpit.mjs"; + +// Public incoming-migration journey, run only by `./tests/local.sh +// incoming-public-journey` inside an owned sandbox. Entryway is reached only +// through its public browser pages and XRPC routes. The source is the standalone +// stock PDS, the destination is a stock PDS behind Entryway, mail is read from +// Mailpit, and nothing seeds authority rows or calls a private fixture. The +// client chooses what to transfer and when to activate; this does not certify +// completeness of every record or blob. +// Read in beforeAll so test discovery works without a sandbox. +let access; +let config; +const unique = randomUUID().replaceAll("-", "").slice(0, 16); +const destinationEmail = `public-arrival-${unique}@example.com`; +const sourceEmail = `public-source-${unique}@example.com`; +const handoffMethod = "com.atproto.identity.submitPlcOperation"; +const creationMethod = "com.atproto.server.createAccount"; +const pixel = Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", + "base64", +); +const run = {}; + +async function call(origin, method, { token, body, params, bytes, type } = {}) { + const url = new URL(`/xrpc/${method}`, origin); + for (const [key, value] of Object.entries(params ?? {})) + url.searchParams.set(key, value); + const post = + body !== undefined || + bytes !== undefined || + /(requestPlcOperationSignature|activateAccount|deactivateAccount)$/.test( + method, + ); + const response = await fetch(url, { + method: post ? "POST" : "GET", + headers: { + ...(token ? { authorization: `Bearer ${token}` } : {}), + ...(body === undefined ? {} : { "content-type": "application/json" }), + ...(bytes === undefined ? {} : { "content-type": type }), + }, + ...(body === undefined ? {} : { body: JSON.stringify(body) }), + ...(bytes === undefined ? {} : { body: bytes }), + redirect: "error", + signal: AbortSignal.timeout(30_000), + }); + const content = Buffer.from(await response.arrayBuffer()); + return { status: response.status, content, headers: response.headers }; +} +const json = ({ content }) => JSON.parse(content.toString("utf8") || "{}"); +// Status only: bodies of failed calls can echo credentials. +const ok = (response, what) => + expect(response.status, `${what} status`).toBeLessThan(300); + +// Stock service tokens last about a minute, so each is minted right before use. +const serviceAuth = async (lxm) => + json( + await call(access.sourceUrl, "com.atproto.server.getServiceAuth", { + token: run.source.accessJwt, + params: { aud: config.serviceDid, lxm }, + }), + ).token; + +test.describe.configure({ mode: "serial" }); +test.beforeAll(() => { + access = JSON.parse(readFileSync(process.env.INCOMING_ACCESS_PATH, "utf8")); + assert.equal(access.project, process.env.INCOMING_PROJECT); + config = JSON.parse(readFileSync(process.env.SERVICE_CONFIG_PATH, "utf8")); +}); + +test("a standalone source account has a record and a blob", async () => { + const description = json( + await call(access.sourceUrl, "com.atproto.server.describeServer"), + ); + run.sourceHandle = `s${unique}${description.availableUserDomains[0]}`; + run.destinationHandle = `d${unique}${config.handleDomains[0]}`; + expect(run.sourceHandle).not.toBe(run.destinationHandle); + const created = await call(access.sourceUrl, creationMethod, { + body: { + email: sourceEmail, + password: `Public-${randomUUID()}!`, + handle: run.sourceHandle, + }, + }); + ok(created, "source account"); + run.source = json(created); + const blob = await call(access.sourceUrl, "com.atproto.repo.uploadBlob", { + token: run.source.accessJwt, + bytes: pixel, + type: "image/png", + }); + ok(blob, "source blob"); + run.blob = json(blob).blob; + const record = await call(access.sourceUrl, "com.atproto.repo.putRecord", { + token: run.source.accessJwt, + body: { + repo: run.source.did, + collection: "app.bsky.actor.profile", + rkey: "self", + record: { + $type: "app.bsky.actor.profile", + displayName: "Public arrival journey", + avatar: run.blob, + }, + }, + }); + ok(record, "source record"); +}); + +test("the browser verifies the destination email and prepares the arrival", async ({ + page, +}) => { + const since = Date.now(); + await page.goto(`${config.issuer}/login`); + await page.getByLabel("Email address").first().fill(destinationEmail); + await page.getByRole("button", { name: "Send sign-in code" }).click(); + const login = await waitForMailpitCode({ + recipient: destinationEmail, + since, + baseUrl: access.mailpitUrl, + }); + await page.getByLabel("Sign-in code").fill(login.code); + await page.getByRole("button", { name: "Verify code" }).click(); + // A verified session exists even though no account does yet. + await page.goto(`${config.issuer}/incoming`); + await expect( + page.getByRole("heading", { name: "Incoming migration" }), + ).toBeVisible(); + // The service sends a fresh code to the verified email on request. + await page.waitForTimeout(6_000); + const asked = Date.now(); + await page + .getByRole("button", { name: "Send me a fresh email code" }) + .click(); + const fresh = await waitForMailpitCode({ + recipient: destinationEmail, + since: asked, + excludeIds: [login.messageId], + baseUrl: access.mailpitUrl, + }); + await page.goto(`${config.issuer}/incoming`); + await page.getByLabel("Your DID").fill(run.source.did); + await page.getByLabel("Destination handle").fill(run.destinationHandle); + await page.getByLabel("Fresh email code").fill(fresh.code); + const response = page.waitForResponse( + (candidate) => + candidate.url().endsWith("/incoming/prepare") && + candidate.request().method() === "POST", + ); + await page.getByRole("button", { name: "Prepare arrival" }).click(); + const prepared = await response; + expect(prepared.status()).toBe(200); + expect(prepared.headers()["cache-control"]).toBe("no-store"); + await expect(page.getByText("shown once")).toBeVisible(); + const fields = await page + .locator("dl.identity") + .evaluate((list) => + Object.fromEntries( + [...list.querySelectorAll("dt")].map((term) => [ + term.textContent, + term.nextElementSibling.textContent, + ]), + ), + ); + run.prepared = { + operationId: fields.operationId, + version: Number(fields.version), + arrivalCode: fields.arrivalCode, + handoff: JSON.parse(fields.handoff), + destinationEndpoint: fields.destinationEndpoint, + }; + // Booleans only: a failing assertion must never print the code. + expect(/^[A-Za-z0-9_-]{32}$/.test(run.prepared.arrivalCode)).toBe(true); + expect(run.prepared.destinationEndpoint).toBeTruthy(); + // No credential or code appears in any URL. + expect(page.url().includes(run.prepared.arrivalCode)).toBe(false); +}); + +test("the source signs the authority handoff and Entryway publishes only that", async () => { + // Stock service tokens last about a minute: the creation token is minted + // immediately before it is used, not here. + const since = Date.now(); + ok( + await call( + access.sourceUrl, + "com.atproto.identity.requestPlcOperationSignature", + { token: run.source.accessJwt }, + ), + "plc confirmation", + ); + const confirmation = await waitForStockPlcMessage({ + recipient: sourceEmail, + since, + excludeIds: [], + baseUrl: access.mailpitUrl, + }); + const signed = await call( + access.sourceUrl, + "com.atproto.identity.signPlcOperation", + { + token: run.source.accessJwt, + body: { + token: confirmation.token, + rotationKeys: run.prepared.handoff.rotationKeys, + alsoKnownAs: run.prepared.handoff.alsoKnownAs, + verificationMethods: run.prepared.handoff.verificationMethods, + services: run.prepared.handoff.services, + }, + }, + ); + ok(signed, "signed handoff"); + const operation = json(signed).operation; + // Wrong code, then wrong method, are refused before anything is published. + const wrongCode = await fetch( + new URL(`/xrpc/${handoffMethod}`, config.issuer), + { + method: "POST", + headers: { + authorization: `Bearer ${await serviceAuth(handoffMethod)}`, + "entryway-arrival-code": "A".repeat(32), + "content-type": "application/json", + }, + body: JSON.stringify({ operation }), + }, + ); + expect(wrongCode.status).toBe(409); + expect((await wrongCode.json()).error).toBe("ArrivalExpired"); + const wrongMethodJwt = await serviceAuth(creationMethod); + const wrongMethod = await fetch( + new URL(`/xrpc/${handoffMethod}`, config.issuer), + { + method: "POST", + headers: { + authorization: `Bearer ${wrongMethodJwt}`, + "entryway-arrival-code": run.prepared.arrivalCode, + "content-type": "application/json", + }, + body: JSON.stringify({ operation }), + }, + ); + expect(wrongMethod.status).toBe(403); + const submitted = await fetch( + new URL(`/xrpc/${handoffMethod}`, config.issuer), + { + method: "POST", + headers: { + authorization: `Bearer ${await serviceAuth(handoffMethod)}`, + "entryway-arrival-code": run.prepared.arrivalCode, + "content-type": "application/json", + }, + body: JSON.stringify({ operation }), + }, + ); + expect(submitted.status).toBe(200); + expect(submitted.headers.get("cache-control")).toBe("no-store"); +}); + +test("creation returns tracked credentials for a paused destination", async () => { + const headers = { + authorization: `Bearer ${await serviceAuth(creationMethod)}`, + "entryway-arrival-code": run.prepared.arrivalCode, + "content-type": "application/json", + }; + const body = { handle: run.destinationHandle }; + if (config.inviteCodeRequired) { + const invite = await fetch( + new URL("/xrpc/com.atproto.server.createInviteCode", config.issuer), + { + method: "POST", + headers: { + authorization: `Basic ${Buffer.from(`admin:${config.adminPassword}`).toString("base64")}`, + "content-type": "application/json", + }, + body: JSON.stringify({ useCount: 1 }), + }, + ); + expect(invite.status).toBe(200); + body.inviteCode = (await invite.json()).code; + } + const created = await fetch( + new URL(`/xrpc/${creationMethod}`, config.issuer), + { + method: "POST", + headers, + body: JSON.stringify(body), + }, + ); + expect(created.status).toBe(200); + expect(created.headers.get("cache-control")).toBe("no-store"); + run.destination = await created.json(); + expect(run.destination.did).toBe(run.source.did); + expect(run.destination.handle).toBe(run.destinationHandle); + expect(run.destination.active).toBe(false); + // A repeated creation cannot mint another family. + const repeated = await fetch( + new URL(`/xrpc/${creationMethod}`, config.issuer), + { + method: "POST", + headers, + body: JSON.stringify(body), + }, + ); + expect(repeated.status).toBeGreaterThanOrEqual(400); + // The paused destination refuses ordinary activation paths at Entryway. + const plain = await fetch( + new URL("/xrpc/com.atproto.server.getSession", config.issuer), + { headers: { authorization: `Bearer ${run.destination.accessJwt}` } }, + ); + expect(plain.status).toBeGreaterThanOrEqual(400); +}); + +test("the client transfers its chosen state directly between the pinned endpoints", async () => { + const exported = await call(access.sourceUrl, "com.atproto.sync.getRepo", { + params: { did: run.source.did }, + }); + ok(exported, "source export"); + const imported = await call( + run.prepared.destinationEndpoint, + "com.atproto.repo.importRepo", + { + token: run.destination.accessJwt, + bytes: exported.content, + type: "application/vnd.ipld.car", + }, + ); + ok(imported, "destination import"); + const blobs = json( + await call(access.sourceUrl, "com.atproto.sync.listBlobs", { + params: { did: run.source.did }, + }), + ).cids; + for (const cid of blobs) { + const blob = await call(access.sourceUrl, "com.atproto.sync.getBlob", { + params: { did: run.source.did, cid }, + }); + ok(blob, "source blob download"); + ok( + await call( + run.prepared.destinationEndpoint, + "com.atproto.repo.uploadBlob", + { + token: run.destination.accessJwt, + bytes: blob.content, + type: blob.headers.get("content-type") ?? "application/octet-stream", + }, + ), + "destination blob upload", + ); + } + const status = json( + await call( + run.prepared.destinationEndpoint, + "com.atproto.server.checkAccountStatus", + { token: run.destination.accessJwt }, + ), + ); + expect(status.activated).toBe(false); + expect(status.repoCommit).toBeTruthy(); +}); + +test("the tracked credential refreshes, then activates through the stock destination callback", async () => { + const refreshed = await fetch( + new URL("/xrpc/com.atproto.server.refreshSession", config.issuer), + { + method: "POST", + headers: { authorization: `Bearer ${run.destination.refreshJwt}` }, + }, + ); + expect(refreshed.status).toBe(200); + run.destination = await refreshed.json(); + const activated = await call( + run.prepared.destinationEndpoint, + "com.atproto.server.activateAccount", + { token: run.destination.accessJwt }, + ); + ok(activated, "stock activateAccount"); + const status = json( + await call( + run.prepared.destinationEndpoint, + "com.atproto.server.checkAccountStatus", + { token: run.destination.accessJwt }, + ), + ); + expect(status.activated).toBe(true); +}); + +test("the activated account serves the transferred state and the client deactivates the source", async () => { + const record = json( + await call(run.prepared.destinationEndpoint, "com.atproto.repo.getRecord", { + params: { + repo: run.source.did, + collection: "app.bsky.actor.profile", + rkey: "self", + }, + }), + ); + expect(record.value.displayName).toBe("Public arrival journey"); + const blob = await call( + run.prepared.destinationEndpoint, + "com.atproto.sync.getBlob", + { params: { did: run.source.did, cid: run.blob.ref.$link } }, + ); + ok(blob, "destination blob"); + expect(blob.content.equals(pixel)).toBe(true); + const directory = await fetch(new URL(`/${run.source.did}`, access.plcUrl)); + const document = await directory.json(); + expect( + document.service.find((service) => service.id === "#atproto_pds") + .serviceEndpoint, + ).toBe(run.prepared.destinationEndpoint); + ok( + await call(access.sourceUrl, "com.atproto.server.deactivateAccount", { + token: run.source.accessJwt, + body: {}, + }), + "source deactivation", + ); +}); diff --git a/tests/contracts/incoming-arrival-composition.test.mjs b/tests/contracts/incoming-arrival-composition.test.mjs index 6d7c36e1..f1c16909 100644 --- a/tests/contracts/incoming-arrival-composition.test.mjs +++ b/tests/contracts/incoming-arrival-composition.test.mjs @@ -77,6 +77,10 @@ for (const mode of ["modern", "legacy"]) { "refreshArrivalSession", "recoverArrivalSession", "authorizeArrivalAccess", + "submitIncomingHandoff", + "createIncomingAccount", + "describeArrival", + "sendArrivalCode", "activateArrivalAsUser", "activateArrivalWithCredential", ].sort(), diff --git a/tests/local.sh b/tests/local.sh index ce93e4e8..cf86c0c8 100755 --- a/tests/local.sh +++ b/tests/local.sh @@ -15,7 +15,7 @@ command=${1:-help} mkdir -p "$artifacts" # Phase identity comes from the controller, never from a command or request body. case "$command" in - fresh|fresh-group|prepare|up|contracts|database-contracts|database-dialects|resilience-profiles|database-profile|browser|incoming-public-mechanisms|incoming-arrival-live|migration|migration-resume|resilience|reverify|interop-profile|interop-acceptance|plc-recovery|process-crash|managed-recovery|authority-drills|all|down|status|help) ;; + fresh|fresh-group|prepare|up|contracts|database-contracts|database-dialects|resilience-profiles|database-profile|browser|incoming-public-mechanisms|incoming-public-journey|incoming-arrival-live|migration|migration-resume|resilience|reverify|interop-profile|interop-acceptance|plc-recovery|process-crash|managed-recovery|authority-drills|all|down|status|help) ;; *) echo 'code=UNKNOWN_PHASE'; exit 2 ;; esac printf 'phase=%s status=started\n' "$command" @@ -108,7 +108,7 @@ case "$command" in (( $# > 0 )) || { echo 'fresh-group needs at least one step' >&2; exit 2; } for step in "$@"; do case "$step" in - browser|contracts|database-contracts|migration|interop-profile|interop-acceptance|plc-recovery|process-crash|managed-recovery|authority-drills|resilience) ;; + browser|contracts|incoming-public-mechanisms|incoming-public-journey|database-contracts|migration|interop-profile|interop-acceptance|plc-recovery|process-crash|managed-recovery|authority-drills|resilience) ;; *) echo "Unknown fresh-group step: $step" >&2; exit 2 ;; esac done @@ -215,6 +215,13 @@ case "$command" in compose run --rm --no-deps --volume "$sandbox/incoming-access.json:/run/incoming-access.json:ro" -e INCOMING_ACCESS_PATH=/run/incoming-access.json -e "INCOMING_PROJECT=$project" -e INCOMING_PUBLIC_MECHANISMS=1 -e INCOMING_RECOVERY=1 -e INCOMING_DISPATCHER_EXIT_REFERENCE=owned-dispatcher-container-exited-0-and-removed -e INCOMING_UPSTREAM_DRAIN_REFERENCE=owned-pds1-pds2-stopped-then-healthy browser node --test --test-name-pattern='stock applied' tests/contracts/incoming-arrival-live.test.mjs fi ;; + incoming-public-journey) + require_state + prepare_incoming_access + # The public browser/API journey: one Playwright spec, selected explicitly + # because it needs this project's access projection and mail capture. + compose run --rm --no-deps --volume "$sandbox/incoming-access.json:/run/incoming-access.json:ro" -e INCOMING_ACCESS_PATH=/run/incoming-access.json -e "INCOMING_PROJECT=$project" -e INCOMING_PUBLIC_JOURNEY=1 browser npx --no-install playwright test --config tests/playwright.config.mjs 2>&1 | tee "$artifacts/incoming-public-journey.log" | python3 "$root/scripts/ci-summary.py" + ;; migration) require_state exec 9> "$artifacts/migration.lock" @@ -293,7 +300,7 @@ case "$command" in deno task sandbox status ;; *) - echo 'Usage: ./tests/local.sh fresh|fresh-group |prepare|up|browser|incoming-public-mechanisms|contracts|database-contracts|database-profile|resilience-profiles|migration|migration-resume|reverify|interop-profile|interop-acceptance|plc-recovery|process-crash|managed-recovery|authority-drills|resilience|all|status|down' + echo 'Usage: ./tests/local.sh fresh|fresh-group |prepare|up|browser|incoming-public-mechanisms|incoming-public-journey|contracts|database-contracts|database-profile|resilience-profiles|migration|migration-resume|reverify|interop-profile|interop-acceptance|plc-recovery|process-crash|managed-recovery|authority-drills|resilience|all|status|down' echo 'all requires a fresh project fixture; down retains state and volumes. No implicit reset.' ;; esac diff --git a/tests/playwright.config.mjs b/tests/playwright.config.mjs index 1a93e214..32ae636f 100644 --- a/tests/playwright.config.mjs +++ b/tests/playwright.config.mjs @@ -1,26 +1,35 @@ import { defineConfig } from "@playwright/test"; const publicRun = process.env.PUBLIC_BROWSER_TESTS === "true"; -const artifactPrefix = publicRun ? "public-browser" : "browser"; +// The incoming public journey needs an owned sandbox's access projection, so +// only `./tests/local.sh incoming-public-journey` selects it. +const incomingJourney = process.env.INCOMING_PUBLIC_JOURNEY === "1"; +const artifactPrefix = incomingJourney + ? "incoming-public-journey" + : publicRun + ? "public-browser" + : "browser"; export default defineConfig({ testDir: "./browser", - testMatch: publicRun - ? "public-browser.spec.mjs" - : [ - "browser.spec.mjs", - "account-console.spec.mjs", - "account-confirm.spec.mjs", - "account-console-multi-binding.spec.mjs", - "migration-console.spec.mjs", - "experience-browser.spec.mjs", - "browser-oauth.spec.mjs", - "oauth-lifecycle.spec.mjs", - "account-chooser.spec.mjs", - "oauth-remembered-device.spec.mjs", - "oauth-signup-consent-skip.spec.mjs", - "oauth-sign-in-hints.spec.mjs", - "shared-email-lookups.spec.mjs", - ], + testMatch: incomingJourney + ? "incoming-public-journey.spec.mjs" + : publicRun + ? "public-browser.spec.mjs" + : [ + "browser.spec.mjs", + "account-console.spec.mjs", + "account-confirm.spec.mjs", + "account-console-multi-binding.spec.mjs", + "migration-console.spec.mjs", + "experience-browser.spec.mjs", + "browser-oauth.spec.mjs", + "oauth-lifecycle.spec.mjs", + "account-chooser.spec.mjs", + "oauth-remembered-device.spec.mjs", + "oauth-signup-consent-skip.spec.mjs", + "oauth-sign-in-hints.spec.mjs", + "shared-email-lookups.spec.mjs", + ], fullyParallel: false, workers: 1, retries: 0, diff --git a/tests/support/helpers/incoming-arrival-fixture.mjs b/tests/support/helpers/incoming-arrival-fixture.mjs index 551f7f6b..6f13b186 100644 --- a/tests/support/helpers/incoming-arrival-fixture.mjs +++ b/tests/support/helpers/incoming-arrival-fixture.mjs @@ -13,12 +13,14 @@ import { Secp256k1Keypair } from "@atproto/crypto"; import { cidForCbor } from "@atproto/common"; import * as plc from "@did-plc/lib"; import { createLegacy } from "../../../dist/src/oauth/legacy-credentials.mjs"; -import { composeIncomingArrival } from "../../../dist/src/compose-incoming-arrival.mjs"; +import { + composeIncomingArrival, + createIncomingArrivalStorage, +} from "../../../dist/src/compose-incoming-arrival.mjs"; import { createBrowserFlow } from "../../../dist/src/http/browser-flow.mjs"; import { createOperationOwnership } from "../../../dist/src/accounts/operation-ownership.js"; import { createOperationOwnershipStore } from "../../../dist/src/database/drizzle/operation-ownership.js"; import { createAccountStorage } from "../../../dist/src/database/drizzle/account-storage.js"; -import { createArrivalStorage } from "../../../dist/src/database/drizzle/migration-arrival.js"; import { openTestDatabase, verifiedUser, query } from "../database-fixture.mjs"; export const endpoint = "https://destination.example.test"; @@ -107,34 +109,12 @@ export async function fixture(t, mode = "modern", mutation = "", fault = "") { }, ]; const storage = createAccountStorage(db, hosts); + // The application's own arrival persistence, never a fixture copy. const arrivalStorage = () => - createArrivalStorage(db, hosts, { - readAccount: (value) => storage.getByDid(value), - readBinding: (value) => storage.getVerifiedBinding(value), - reserveEmailClaim: (...args) => storage.reserveEmail(...args), - reserveHandleClaim: (...args) => storage.reserveHandle(...args), - recordSavedUserBinding: (subject, userId) => - db.insert( - "account_bindings", - { did: subject, user_id: userId }, - { ignoreConflict: true }, - ), - activatePlacedAccount: (subject) => - storage.activatePlacedAccount(subject), - insertPlacedAccount: (input) => - storage.insertAccount( - { - did: input.did, - email: input.email, - emailVerified: true, - handle: input.handle, - pdsId: input.pdsId, - pdsUrl: input.pdsUrl, - status: "provisioning", - createdAt: new Date().toISOString(), - }, - input.userId, - ), + createIncomingArrivalStorage({ + db, + config: { pds: hosts }, + accounts: { storage }, }); const arrivals = arrivalStorage(); if (fault.startsWith("after-commit")) @@ -151,8 +131,12 @@ export async function fixture(t, mode = "modern", mutation = "", fault = "") { authenticatedAt: new Date(), }; let verifications = 0; + const sentCodes = []; const authentication = { requireSession: async () => principal, + sendSignInCode: async (address) => { + sentCodes.push(address); + }, verifySignInCode: async ({ email: destinationEmail, otp }) => { verifications++; assert.equal(destinationEmail, email); @@ -460,6 +444,7 @@ export async function fixture(t, mode = "modern", mutation = "", fault = "") { repository, composed, reservations: () => reservations, + sentCodes, verifications: () => verifications, prepare: (otp = "fresh-otp") => composed.prepareArrival(request, otp, { did, handle }), diff --git a/tests/support/helpers/incoming-http.mjs b/tests/support/helpers/incoming-http.mjs new file mode 100644 index 00000000..e1f6961a --- /dev/null +++ b/tests/support/helpers/incoming-http.mjs @@ -0,0 +1,131 @@ +import { once } from "node:events"; +import http from "node:http"; +import express from "express"; +import { requestFailureEvent } from "../../../dist/src/logging/request-event.js"; +import { createProtocolChallenges } from "../../../dist/src/accounts/challenges.mjs"; +import { mountIncomingRoutes } from "../../../dist/src/features/external-migration/routes.js"; +import { mountIncomingXrpc } from "../../../dist/src/features/external-migration/xrpc-routes.js"; + +/** The real incoming adapters over a fixture's real composition, behind the + * application's JSON parser and failure mapping. Ordinary protocol routes are + * stand-ins that answer 418, so a request that falls through is observable. + * Requests use node:http because the fixture replaces globalThis.fetch. */ +export async function incomingHttp(t, f, { ordinary = true } = {}) { + const app = express(); + app.use(express.json({ limit: "64kb" })); + const { rateLimit } = await createProtocolChallenges({ + db: f.db, + config: f.config, + accounts: f.composed.accounts ?? {}, + }); + mountIncomingRoutes({ + app, + issuer: f.config.issuer, + incoming: f.composed, + oauth: { + loadBrowser: async () => ({ csrf: f.request.body.csrf }), + requireSession: async () => f.principal, + }, + rateLimit, + }); + mountIncomingXrpc({ + app, + incoming: f.composed, + protocolOperations: { rateLimit }, + }); + if (ordinary) + for (const name of [ + "server.createAccount", + "identity.submitPlcOperation", + "server.refreshSession", + "server.activateAccount", + ]) + app.post(`/xrpc/com.atproto.${name}`, (_req, res) => + res.status(418).json({ ordinary: name }), + ); + app.use((_req, res) => res.status(404).json({ error: "NotFound" })); + // eslint-disable-next-line no-unused-vars -- Express requires four-argument error middleware. + app.use((error, _req, res, _next) => { + const status = Number(error.status ?? error.statusCode ?? 500); + const failure = requestFailureEvent(status, error, "test"); + res.status(failure.status).json({ + error: failure.code, + message: status < 500 ? error.message : "failed", + }); + }); + const server = app.listen(0, "127.0.0.1"); + t.after(() => new Promise((resolve) => server.close(resolve))); + await once(server, "listening"); + const port = server.address().port; + const call = async ( + path, + { method = "POST", headers = {}, json, form } = {}, + ) => { + const body = + json !== undefined + ? JSON.stringify(json) + : form !== undefined + ? new URLSearchParams(form).toString() + : undefined; + const response = await new Promise((resolve, reject) => { + const request = http.request( + { + host: "127.0.0.1", + port, + path, + method, + headers: { + ...(json !== undefined + ? { "content-type": "application/json" } + : {}), + ...(form !== undefined + ? { "content-type": "application/x-www-form-urlencoded" } + : {}), + ...headers, + }, + }, + (incoming) => { + const chunks = []; + incoming.on("data", (chunk) => chunks.push(chunk)); + incoming.on("end", () => + resolve({ + status: incoming.statusCode, + headers: incoming.headers, + text: Buffer.concat(chunks).toString("utf8"), + }), + ); + }, + ); + request.on("error", reject); + request.end(body); + }); + let parsed; + try { + parsed = JSON.parse(response.text); + } catch { + parsed = undefined; + } + return { ...response, json: parsed }; + }; + /** A browser request: same-origin header, CSRF token and fresh email code. */ + const browser = ( + path, + fields = {}, + { otp = "fresh-otp", csrf, origin } = {}, + ) => + call(path, { + headers: { origin: origin ?? f.config.issuer }, + json: { csrf: csrf ?? f.request.body.csrf, otp, ...fields }, + }); + /** A tool request: bearer source JWT plus the arrival-code header, no cookie. */ + const tool = (path, { code, jwt, json, headers = {} }) => + call(path, { + headers: { + ...(jwt ? { authorization: jwt } : {}), + ...(code === undefined ? {} : { "entryway-arrival-code": code }), + ...headers, + }, + json, + }); + return { call, browser, tool }; +} From c7f65b7f356bd07c0e3ce075e2d2d49de9b9242b Mon Sep 17 00:00:00 2001 From: Evelyn Osman Date: Thu, 12 Nov 2026 15:01:45 +0100 Subject: [PATCH 2/2] fix: leave ordinary sessions to the ordinary activation and refresh routes The incoming activateAccount and refreshSession handlers run before the ordinary routes and must answer only for a tracked arrival credential. They ran the strict token check first, so an ordinary account whose access token had expired or whose session family was revoked got the incoming 400 ExpiredToken where the ordinary route answers 401. An expired ordinary refresh token had the same flaw. A live run against a stock PDS found it: delegated activation of a deactivated hosted account returned 400. The handlers now verify the token's signature, type, issuer and audience while tolerating expiry, look up whether the session family is tracked to an arrival, and fall through for anything untracked without writing anything. The strict checks still run before any incoming answer. A finished arrival's family is an ordinary session, so once the session store finds it expired or revoked it also falls through. A tracked family of an unfinished arrival keeps the incoming refusal. Also update the stale password step in the public-mechanisms test: the form moved to the password page. This work was assisted by a machine --- docs/incoming-migration.md | 12 +- src/app-incoming.test.mjs | 53 ++++++- .../external-migration/arrival-credentials.ts | 28 +++- .../external-migration/incoming-xrpc.test.mjs | 136 ++++++++++++++++++ src/oauth/legacy-credentials.mjs | 7 + tests/contracts/incoming-credentials.test.mjs | 2 +- .../incoming-public-mechanisms.test.mjs | 1 + 7 files changed, 232 insertions(+), 7 deletions(-) diff --git a/docs/incoming-migration.md b/docs/incoming-migration.md index 432aa4d3..e22a767d 100644 --- a/docs/incoming-migration.md +++ b/docs/incoming-migration.md @@ -727,8 +727,16 @@ pending owner decision. `refreshSession` and `activateAccount` now perform one arrival lookup before the ordinary route, and fall through on `InvalidToken` or `ArrivalExpired`; that -fall-through design is a pending owner decision. A finished arrival's family is an -ordinary session: its committed activation result is returned only while the local +fall-through design is a pending owner decision. The lookup happens only after the +credential's signature is verified (expiry tolerated), so a genuine credential is +judged "tracked or not" before its expiry or revocation is judged. An ordinary +credential that is expired, revoked, replayed or untracked always reaches the +ordinary route, which alone decides its answer and any replay handling; the arrival +path writes nothing for it. While its arrival is unfinished, a tracked family that +is revoked or expired keeps the incoming `ExpiredToken` refusal, since the ordinary +route does not serve a pending account. A finished arrival's family is an ordinary +session: once it is expired or revoked, `activateAccount` falls through and the +ordinary route refuses it. Its committed activation result is returned only while the local account is still `active`, and once the user deactivates it, `activateAccount` reaches the ordinary route. With the header present these never fall through to hosted or managed movement, so an invalid incoming credential is refused, not rerouted. Without it, and for any diff --git a/src/app-incoming.test.mjs b/src/app-incoming.test.mjs index 19bf5cd6..5acff8cc 100644 --- a/src/app-incoming.test.mjs +++ b/src/app-incoming.test.mjs @@ -3,6 +3,7 @@ import { generateKeyPairSync, randomBytes } from "node:crypto"; import { once } from "node:events"; import http from "node:http"; import test from "node:test"; +import { SignJWT, importJWK } from "jose"; import { Secp256k1Keypair } from "@atproto/crypto"; import { openTestDatabase } from "../tests/support/database-fixture.mjs"; import { createApp } from "../dist/src/app.mjs"; @@ -92,7 +93,20 @@ async function application(t) { request.on("error", reject); request.end(json === undefined ? undefined : JSON.stringify(json)); }); - return { call }; + const sign = async (typ, claims, issuedAt = Math.floor(Date.now() / 1000)) => + new SignJWT({ + scope: "com.atproto.access", + sid: randomBytes(32).toString("base64url"), + ...claims, + }) + .setProtectedHeader({ alg: "ES256K", typ }) + .setSubject("did:plc:ordinaryaccount") + .setIssuer(issuer) + .setJti(randomBytes(32).toString("base64url")) + .setIssuedAt(issuedAt) + .setExpirationTime(issuedAt + (typ === "at+jwt" ? 300 : 3600)) + .sign(await importJWK(jwtJwk, "ES256K")); + return { call, config, sign }; } test("through the real application, requests without an incoming selector keep the ordinary routes", async (t) => { @@ -159,3 +173,40 @@ test("through the real application, the browser surfaces are registered and gate assert.equal(crossSite.status, 400); assert.equal(crossSite.body.error, "InvalidRequest"); }); + +test("through the real application, a genuine but untracked expired or revoked credential gets the ordinary route's answer", async (t) => { + const { call, config, sign } = await application(t); + const stale = Math.floor(Date.now() / 1000) - 3600; + const access = { + aud: config.pds[0].did, + }; + const refresh = { aud: config.serviceDid, scope: "com.atproto.refresh" }; + const ordinary = {}; + for (const [name, path, jwt] of [ + ["expired access", "activateAccount", await sign("at+jwt", access, stale)], + ["unknown-family access", "activateAccount", await sign("at+jwt", access)], + [ + "expired refresh", + "refreshSession", + await sign("refresh+jwt", refresh, stale), + ], + [ + "unknown-family refresh", + "refreshSession", + await sign("refresh+jwt", refresh), + ], + ]) { + const response = await call(`/xrpc/com.atproto.server.${path}`, { + headers: { authorization: `Bearer ${jwt}` }, + }); + ordinary[name] = [response.status, response.body.error]; + } + // Statuses are the ordinary handlers' own, observed without the incoming + // handlers: activation answers 401 (never the incoming 400 ExpiredToken). + assert.deepEqual(ordinary, { + "expired access": [401, "InvalidToken"], + "unknown-family access": [401, "InvalidToken"], + "expired refresh": [400, "ExpiredToken"], + "unknown-family refresh": [400, "InvalidToken"], + }); +}); diff --git a/src/features/external-migration/arrival-credentials.ts b/src/features/external-migration/arrival-credentials.ts index 3a17ba31..1ee4b0ce 100644 --- a/src/features/external-migration/arrival-credentials.ts +++ b/src/features/external-migration/arrival-credentials.ts @@ -46,6 +46,10 @@ export interface ArrivalFamilyCredentials { }>; persist(minted: { familyId: string }): Promise; revoke(familyIds: readonly string[], reason: string): Promise; + readFamily( + input: unknown, + kind: "access" | "refresh", + ): Promise<{ familyId: string; did: string }>; readAccess( input: unknown, ): Promise<{ familyId: string; did: string; pdsDid: string }>; @@ -256,8 +260,20 @@ export function createArrivalCredentials({ const identify = async ( bearer: unknown, ): Promise => { - const access = await signer.readAccess(bearer); - const tracked = await arrivals.readArrivalFamily(access.familyId); + // A credential is only an arrival's once it is signature-verified and its + // family is tracked. Expired or revoked state is judged after that, so an + // ordinary credential in any condition is "not an arrival's" and falls + // through to the ordinary route. + const claimed = await signer.readFamily(bearer, "access"); + const tracked = await arrivals.readArrivalFamily(claimed.familyId); + if (!tracked) throw invalidToken(); + // A finished arrival's family is an ordinary session. Once the session + // store finds it expired or revoked, it is the ordinary route's to refuse. + const finished = + (await arrivals.readArrivalAccess(claimed.did))?.complete === true; + const access = await signer.readAccess(bearer).catch((error: unknown) => { + throw finished && error instanceof HttpError ? invalidToken() : error; + }); const projection = await arrivals.readArrivalAccess(access.did); if ( !tracked || @@ -357,11 +373,17 @@ export function createArrivalCredentials({ async refreshArrivalSession( refreshCredential: unknown, ): Promise { - const { familyId, did } = await signer.readRefresh(refreshCredential); + // Signature-verified, expiry-tolerant: only to decide whether the family + // is tracked. Nothing is rotated or revoked for an untracked family. + const { familyId, did } = await signer.readFamily( + refreshCredential, + "refresh", + ); const tracked = await arrivals.readArrivalFamily(familyId); const access = await arrivals.readArrivalAccess(did); if (!tracked || !access || access.operationId !== tracked.operationId) throw invalidToken(); + await signer.readRefresh(refreshCredential); if (tracked.revokedAt !== null) throw expiredToken(); const initial = await owner.read(access.operationId, access.userId); assertBoundPending(initial); diff --git a/src/features/external-migration/incoming-xrpc.test.mjs b/src/features/external-migration/incoming-xrpc.test.mjs index 12b503a9..52fbcf17 100644 --- a/src/features/external-migration/incoming-xrpc.test.mjs +++ b/src/features/external-migration/incoming-xrpc.test.mjs @@ -563,6 +563,38 @@ test("after completion the committed result answers only while the account is ac assert.equal(refreshed.status, 418); }); +test("after completion an expired or revoked session of the migrated family is the ordinary route's to refuse", async (t) => { + const { f, h, session } = await created(t); + const bearer = { authorization: `Bearer ${session.accessJwt}` }; + assert.equal((await h.call(activatePath, { headers: bearer })).status, 200); + const ordinary = { ordinary: "server.activateAccount" }; + // Ordinary deactivation revokes the session family in the legacy store only. + const { sid } = JSON.parse( + Buffer.from(session.accessJwt.split(".")[1], "base64url").toString(), + ); + await f.legacy.arrivalSessions.revoke([sid], "test"); + const revoked = await h.call(activatePath, { headers: bearer }); + assert.equal(revoked.status, 418); + assert.deepEqual(revoked.json, ordinary); + t.mock.timers.enable({ apis: ["Date"], now: Date.now() }); + t.mock.timers.tick(6 * 60 * 1000); + const expired = await h.call(activatePath, { headers: bearer }); + assert.equal(expired.status, 418); + assert.deepEqual(expired.json, ordinary); + assert.equal(f.backendState.activations, 1); +}); + +test("after completion an expired session of the migrated family alone falls through", async (t) => { + const { f, h, session } = await created(t); + const bearer = { authorization: `Bearer ${session.accessJwt}` }; + assert.equal((await h.call(activatePath, { headers: bearer })).status, 200); + t.mock.timers.enable({ apis: ["Date"], now: Date.now() }); + t.mock.timers.tick(6 * 60 * 1000); + const expired = await h.call(activatePath, { headers: bearer }); + assert.equal(expired.status, 418); + assert.equal(f.backendState.activations, 1); +}); + test("a cross-origin post spends no browser budget", async (t) => { const f = await fixture(t); const h = await incomingHttp(t, f); @@ -632,3 +664,107 @@ test("a completed arrival is not reported on a repeat prepare", async (t) => { assert.equal(repeat.json.operationId, undefined); assert.equal(repeat.text.includes(prepared.operationId), false); }); + +/** A family in the shared legacy store that no arrival tracks: what an + * ordinary hosted account's session looks like to the incoming handlers. */ +async function ordinaryFamily(f) { + const minted = await f.legacy.arrivalSessions.prepare({ + did: f.did, + pdsId: f.config.pds[0].id, + }); + await f.legacy.arrivalSessions.persist(minted); + return minted; +} +const familyRow = (f, id) => f.db.get("legacy:sessions", id); +const refreshRow = async (f, token) => + f.db.get( + "legacy:refresh", + JSON.parse(Buffer.from(token.split(".")[1], "base64url")).jti, + ); + +test("activateAccount leaves an ordinary credential to the ordinary route in any condition", async (t) => { + const { f, h } = await created(t); + const ordinary = await ordinaryFamily(f); + const call = (jwt) => + h.call(activatePath, { headers: { authorization: `Bearer ${jwt}` } }); + assert.equal((await call(ordinary.tokens.accessJwt)).status, 418); + // Revoked family: the live regression answered 400 instead of the ordinary 401. + await f.legacy.arrivalSessions.revoke([ordinary.familyId], "test"); + assert.deepEqual((await call(ordinary.tokens.accessJwt)).json, { + ordinary: "server.activateAccount", + }); + // Expired access token of a live family. + const live = await ordinaryFamily(f); + t.mock.timers.enable({ apis: ["Date"], now: Date.now() }); + t.mock.timers.tick(6 * 60 * 1000); + assert.deepEqual((await call(live.tokens.accessJwt)).json, { + ordinary: "server.activateAccount", + }); + assert.equal(f.backendState.activations, 0); +}); + +test("activateAccount still refuses a tracked arrival credential whose family is expired", async (t) => { + const { f, h, session } = await created(t); + t.mock.timers.enable({ apis: ["Date"], now: Date.now() }); + t.mock.timers.tick(6 * 60 * 1000); + const response = await h.call(activatePath, { + headers: { authorization: `Bearer ${session.accessJwt}` }, + }); + assert.equal(response.status, 400); + assert.equal(response.json.error, "ExpiredToken"); + assert.equal(f.backendState.activations, 0); +}); + +test("refreshSession leaves ordinary revoked, expired and replayed refresh tokens untouched for the ordinary route", async (t) => { + const { f, h } = await created(t); + const call = (jwt) => + h.call(refreshPath, { headers: { authorization: `Bearer ${jwt}` } }); + const ordinaryAnswer = { ordinary: "server.refreshSession" }; + // Replayed: the arrival path performs no replay revocation or rotation. + const replayed = await ordinaryFamily(f); + await f.legacy.arrivalSessions.refresh(replayed.tokens.refreshJwt, { + account: async (family) => ({ did: family.did }), + details: async () => ({}), + }); + const before = await familyRow(f, replayed.familyId); + assert.deepEqual( + (await call(replayed.tokens.refreshJwt)).json, + ordinaryAnswer, + ); + assert.deepEqual(await familyRow(f, replayed.familyId), before); + assert.equal( + (await refreshRow(f, replayed.tokens.refreshJwt)).usedAt !== undefined, + true, + ); + // Revoked. + const revoked = await ordinaryFamily(f); + await f.legacy.arrivalSessions.revoke([revoked.familyId], "test"); + const revokedRow = await familyRow(f, revoked.familyId); + assert.deepEqual( + (await call(revoked.tokens.refreshJwt)).json, + ordinaryAnswer, + ); + assert.deepEqual(await familyRow(f, revoked.familyId), revokedRow); + // Expired refresh token (family lifetime elapsed). + const expired = await ordinaryFamily(f); + const unused = await refreshRow(f, expired.tokens.refreshJwt); + t.mock.timers.enable({ apis: ["Date"], now: Date.now() }); + t.mock.timers.tick(91 * 24 * 60 * 60 * 1000); + assert.deepEqual( + (await call(expired.tokens.refreshJwt)).json, + ordinaryAnswer, + ); + assert.deepEqual(await refreshRow(f, expired.tokens.refreshJwt), unused); +}); + +test("refreshSession still refuses an expired tracked refresh credential with the expired-token code", async (t) => { + const { h, session, f } = await created(t); + t.mock.timers.enable({ apis: ["Date"], now: Date.now() }); + t.mock.timers.tick(91 * 24 * 60 * 60 * 1000); + const response = await h.call(refreshPath, { + headers: { authorization: `Bearer ${session.refreshJwt}` }, + }); + assert.equal(response.status, 400); + assert.equal(response.json.error, "ExpiredToken"); + void f; +}); diff --git a/src/oauth/legacy-credentials.mjs b/src/oauth/legacy-credentials.mjs index 151d6b79..9c05f23a 100644 --- a/src/oauth/legacy-credentials.mjs +++ b/src/oauth/legacy-credentials.mjs @@ -663,6 +663,13 @@ export async function createLegacy({ db, config, accounts }) { for (const id of familyIds) await invalidateFamily(await get("sessions", id), reason); }, + /** Signature, type, issuer and audience only, tolerating expiry: names the + * family a genuine credential claims, so the caller can tell whether it is + * tracked before any strict check. It authorizes nothing and writes nothing. */ + async readFamily(input, kind) { + const payload = await parseToken(input, kind, { allowExpired: true }); + return { familyId: payload.sid, did: payload.sub }; + }, /** Stateless token checks plus a live family, without any account-status * rule. Exact subject, PDS audience, type and main-access scope are required; * app-password scopes are never an arrival credential. */ diff --git a/tests/contracts/incoming-credentials.test.mjs b/tests/contracts/incoming-credentials.test.mjs index 98e1ff86..657f155a 100644 --- a/tests/contracts/incoming-credentials.test.mjs +++ b/tests/contracts/incoming-credentials.test.mjs @@ -428,7 +428,7 @@ test("access authorization requires exact subject, audience, type, scope and a t payload: { scope: "com.atproto.access", sid: "u".repeat(43) }, }, "at+jwt", - "ExpiredToken", + "InvalidToken", ], ]) await assert.rejects( diff --git a/tests/contracts/incoming-public-mechanisms.test.mjs b/tests/contracts/incoming-public-mechanisms.test.mjs index dcb2678c..5f99b4c9 100644 --- a/tests/contracts/incoming-public-mechanisms.test.mjs +++ b/tests/contracts/incoming-public-mechanisms.test.mjs @@ -934,6 +934,7 @@ test( const page = await browser.newPage(); const identity = await createAccount(page, "destination-probe"); const password = `Public qualification ${randomUUID()}!`; + await page.locator('a[href^="/account/manage/password?"]').click(); const form = page.locator('form[action="/account/password-set"]'); await form.getByLabel("New account password").fill(password); await form.getByRole("button").click();