chadtmiller.com/monolith
An atproto PDS
0
0
// The account lifecycle (spec/LIFECYCLE.md): accounts deactivated, taken
// down and deleted, records and blobs taken down, and what each state does
// to every method, on both sides, with both firehoses compared. A run of its
// own, `--lifecycle`: its steps are changes of state, each followed by the
// methods of the spec's tables for the account it changed.
import http from 'node:http'
import { isDeepStrictEqual } from 'node:util'
import { cborDecode, cborEncode, cidForCbor } from '@atproto/common'
import { readCarWithRoot } from '@atproto/repo'
import { CRASH_EVERY, LIFECYCLE_STEPS } from '../lib/args.mjs'
import { ADMIN_PASSWORD } from '../lib/env.mjs'
import { checkMail } from '../lib/mail.mjs'
import { begin, end, now } from '../lib/observe.mjs'
import { plcLog } from '../lib/plc.mjs'
import { cand, net, plcProxy, plcUrl, ref, server, wait, waitReady } from '../lib/processes.mjs'
import { below, chance, pick, rand } from '../lib/random.mjs'
import { count, differ, run } from '../lib/report.mjs'
import { accounts, addAccount, didOf } from '../lib/state.mjs'
import { normalize, strip } from '../lib/xrpc.mjs'
import { createAccount, freshFront } from './accounts.mjs'
import { subscribe } from './firehose.mjs'
import { Browser, wwwError } from '../lib/oauth.mjs'
import { services } from '../lib/services.mjs'
import { issuer } from '../lib/server.mjs'
import { approved, codeOf, exchange as oauthExchange, handleOf, loadPage, login as oauthLogin, token as tokenOn } from './oauth.mjs'
import { clientLogin, clientRequest, exchangeAndKeep, silent, standing } from './oauth-clients.mjs'
import { addPasskey, passkeyRefused, passkeysAble, whoOf } from './passkeys.mjs'
import { b64u } from '../lib/webauthn.mjs'
const S = 'com.atproto.server.'
const A = 'com.atproto.admin.'
const R = 'com.atproto.repo.'
const Y = 'com.atproto.sync.'
const I = 'com.atproto.identity.'
/// The collection the run's records are in.
const C = 'com.example.life'
/// A DID no server here holds.
const NOBODY = 'did:plc:aaaaaaaaaaaaaaaaaaaaaaaa'
/// A JWT no server here gave out.
const FORGED = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4In0.c2ln'
const basic = (user, password) => `Basic ${Buffer.from(`${user}:${password}`).toString('base64')}`
const ADMIN = basic('admin', ADMIN_PASSWORD)
const bearer = (t) => `Bearer ${t}`
const repoRef = (did) => ({ $type: `${A}defs#repoRef`, did })
const strongRef = (uri) => ({ $type: `${R}strongRef`, uri, cid: 'bafyreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvske7mr3hpm' })
const DATETIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/
const APP_PASSWORD = /^[a-z2-7]{4}-[a-z2-7]{4}-[a-z2-7]{4}-[a-z2-7]{4}$/
const TID = /\/[234567a-z]{13}$/
const sides = () => [ref, cand]
let serial = 0
const next = () => ++serial
// ---- Requests -------------------------------------------------------------
/// A call to one side: `auth` is the whole `Authorization` header. A
/// non-JSON answer that succeeds, a CAR file or a blob, is `{status, bytes}`.
async function send(side, nsid, { params, body, auth, method, type } = {}) {
const url = new URL(`${side.url}/xrpc/${nsid}`)
for (const [k, v] of Object.entries(params ?? {})) for (const x of [v].flat()) if (x !== undefined) url.searchParams.append(k, String(x))
const headers = {}
if (auth) headers.authorization = auth
let payload
if (body instanceof Uint8Array) {
if (type !== null) headers['content-type'] = type ?? 'application/octet-stream'
payload = body
} else if (body !== undefined) {
headers['content-type'] = 'application/json'
payload = JSON.stringify(body)
}
try {
const r = await fetch(url, { method: method ?? (body === undefined ? 'GET' : 'POST'), headers, body: payload })
const ct = r.headers.get('content-type') ?? ''
if (r.ok && !ct.startsWith('application/json')) {
const bytes = new Uint8Array(await r.arrayBuffer())
return bytes.length ? { status: r.status, bytes } : { status: r.status, body: null }
}
const text = await r.text()
let parsed = null
try {
parsed = text ? JSON.parse(text) : null
} catch {
parsed = { unparsed: text.slice(0, 200) }
}
return { status: r.status, body: parsed }
} catch (err) {
return { status: 0, body: { error: `request failed: ${err.message}` } }
}
}
/// Keys whose values differ between any two servers, by method, taken out
/// once held to what they must be.
const VOLATILE = {
[`${Y}getLatestCommit`]: ['cid', 'rev'],
[`${Y}getRepoStatus`]: ['rev'],
[`${Y}listRepos`]: ['head', 'rev'],
[`${S}checkAccountStatus`]: ['repoCommit', 'repoRev'],
[`${S}createAppPassword`]: ['password', 'createdAt'],
[`${S}listAppPasswords`]: ['createdAt'],
[`${S}getServiceAuth`]: ['token'],
}
/// An answer as compared: `normalize`'s, a CAR file or blob by its status,
/// the volatile keys taken out, and a takedown's `ref` the side made, a
/// datetime, named (the harness sends none of its own).
function view(side, res, nsid, extra) {
if (res.bytes) return { status: res.status, body: '<bytes>' }
const n = normalize(side, res, nsid, extra)
const drop = new Set(VOLATILE[nsid] ?? [])
const walk = (v, key) => {
if (Array.isArray(v)) return v.map((x) => walk(x))
if (v && typeof v === 'object') return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, drop.has(k) ? `<${k}>` : walk(x, k)]))
if (key === 'ref' && typeof v === 'string' && DATETIME.test(v)) return '<time>'
return v
}
return walk(n)
}
/// A request as the report shows it: the reference's, with DIDs named and
/// tokens shortened.
function shownReq(request) {
const r = typeof request === 'function' ? request(ref) : request
const auth = typeof r.auth === 'string' ? r.auth.replace(/^(\S+) (eyJ\S+)$/, '$1 <jwt>') : r.auth
const body = r.body instanceof Uint8Array ? `<${r.body.length} bytes>` : r.body
return JSON.parse(strip(ref, JSON.stringify({ ...r, auth, body })))
}
/// The same request to both sides, compared. `request` is a function of the
/// side, or the same for both. Answers both raw answers.
async function same(kind, nsid, request) {
const on = (side) => (typeof request === 'function' ? request(side) : request)
const [a, b] = await Promise.all(sides().map((side) => send(side, nsid, on(side))))
const [na, nb] = [view(ref, a, nsid), view(cand, b, nsid)]
if (!isDeepStrictEqual(na, nb)) await differ(`${nsid} (${kind})`, shownReq(request), na, nb)
count(`lifecycle: ${kind}`, a.status < 400)
return [a, b]
}
/// What fails a check held to both sides: the reference is held to it too.
const fail = (side, what, request, want, got) => differ(`the ${side.name}'s ${what}`, request, want, got, side === ref ? 'the reference fails the check it is held to' : undefined)
/// GET /.well-known/atproto-did on `side` with `host` as its Host; one to
/// the server is noted, with its trace (spec/OPERATIONS.md).
function wellKnown(side, host) {
const u = new URL(side.url)
const headers = { host }
const rec = side === cand ? begin('GET', `${side.url}/.well-known/atproto-did`, headers) : null
return new Promise((ok) => {
const req = http.request({ host: u.hostname, port: u.port, path: '/.well-known/atproto-did', headers }, (res) => {
let body = ''
res.setEncoding('utf8')
res.on('data', (c) => (body += c))
res.on('end', () => {
if (rec) end(rec, res.statusCode, body)
ok({ status: res.statusCode, body })
})
})
req.on('error', (err) => {
if (rec) rec.ended = now()
ok({ status: 0, body: err.message })
})
req.end()
})
}
// ---- Accounts --------------------------------------------------------------
/// Each account's lifecycle, by side: `app` and `priv`, an app password's
/// session and a privileged one's, `{access, refresh}`; `spare`, a session
/// made with its password to refresh; `td`, a session made with
/// `allowTakendown`; `mailed`, the last `delete_account` token mailed to it.
const life = new Map()
const lifeOf = (acct) => {
if (!life.has(acct)) life.set(acct, { reference: {}, server: {}, appName: null, records: new Map() })
return life.get(acct)
}
const own = (acct) => (side) => bearer(acct.sides[side.name].token)
/// The account's state on the reference, which the server must match:
/// `{deleted}`, or `{deactivated, takendown}`.
async function stateOf(acct) {
const r = await send(ref, `${A}getSubjectStatus`, { params: { did: didOf(ref, acct) }, auth: ADMIN })
if (r.status !== 200) return { deleted: true, deactivated: false, takendown: false, active: false }
const deactivated = !!r.body.deactivated?.applied
const takendown = !!r.body.takedown?.applied
return { deleted: false, deactivated, takendown, active: !deactivated && !takendown }
}
/// A session on both sides by `createSession` with `body` (a function of
/// the side), compared; answers each side's `{access, refresh}`, or null.
async function login(kind, body) {
const [a, b] = await same(kind, `${S}createSession`, (side) => ({ body: body(side) }))
if (a.status !== 200 || b.status !== 200) return null
return { reference: { access: a.body.accessJwt, refresh: a.body.refreshJwt }, server: { access: b.body.accessJwt, refresh: b.body.refreshJwt } }
}
/// A record `rkey` of the account on both sides, from `value` (a function of
/// the side, for blob refs), compared. Answers the reference's answer.
async function put(acct, rkey, value) {
const [a] = await same(`putRecord ${rkey}`, `${R}putRecord`, (side) => ({ auth: own(acct)(side), body: { repo: didOf(side, acct), collection: C, rkey, record: { $type: C, ...value(side) } } }))
if (a.status === 200) lifeOf(acct).records.set(rkey, a.body.cid)
return a
}
/// Upload `bytes` as the account on both sides, compared; answers each
/// side's blob ref, or null.
async function upload(acct, bytes) {
const [a, b] = await same('uploadBlob', `${R}uploadBlob`, (side) => ({ auth: own(acct)(side), body: bytes, type: 'text/plain' }))
return a.status === 200 && b.status === 200 ? { reference: a.body.blob, server: b.body.blob } : null
}
/// What every account starts with here: a record `a`, a blob referenced by
/// record `b`, an app password and a privileged one, each with a session,
/// and a spare session.
async function prepare(acct) {
const l = lifeOf(acct)
await put(acct, 'a', () => ({ text: `a of ${acct.n}` }))
const blob = await upload(acct, Buffer.from(`lifecycle blob of ${acct.n}`))
l.blob = blob.reference.ref.$link
await put(acct, 'b', (side) => ({ file: blob[side.name] }))
for (const [key, privileged] of [['app', false], ['priv', true]]) {
const name = `${key}-${next()}`
const [a, b] = await same('createAppPassword', `${S}createAppPassword`, (side) => ({ auth: own(acct)(side), body: { name, privileged } }))
for (const [side, res] of [[ref, a], [cand, b]]) if (!APP_PASSWORD.test(res.body?.password)) await fail(side, 'app password', { name }, 'four groups of four of a-z and 2-7', res.body)
const pw = { reference: a.body.password, server: b.body.password }
if (key === 'app') l.appPassword = pw
const s = await login(`createSession with an app password`, (side) => ({ identifier: didOf(side, acct), password: pw[side.name] }))
for (const side of sides()) l[side.name][key] = s[side.name]
}
const spare = await login('createSession', (side) => ({ identifier: acct.handle, password: acct.password }))
for (const side of sides()) l[side.name].spare = spare[side.name]
await oauthSession(acct, 'a new account')
await approveDevice(acct)
await lifePasskey(acct)
}
/// The account's passkey, on the server alone, added afresh: none while it
/// is taken down or deleted, as `_passkeyAdd` then refuses it
/// (spec/PASSKEYS.md, "The account's states").
async function lifePasskey(acct) {
const l = lifeOf(acct)
l.passkey = null
if (!passkeysAble()) return
const st = await stateOf(acct)
if (st.takendown || st.deleted) return
l.who = whoOf(acct)
l.passkey = await addPasskey(l.who)
}
// ---- OAuth -----------------------------------------------------------------------
/// The client each account's OAuth session is of: its scope allows
/// deactivating the account (spec/OAUTH-SCOPES.md).
const OAUTH_CLIENT = { scope: 'atproto transition:generic transition:email account:status?action=manage', redirects: ['http://127.0.0.1/callback'] }
/// A new OAuth session of the account, through checks/oauth.mjs's flow,
/// compared at each step: kept as its `oauth`, or none if the reference
/// gave none, as to a taken-down account at sign-in and a deactivated one
/// at consent.
async function oauthSession(acct, tag) {
const s = await oauthLogin(acct, { client: OAUTH_CLIENT, reject: false, narrow: false, whole: true })
lifeOf(acct).oauth = s
count(`lifecycle: OAuth sign-in, ${tag}`, Boolean(s))
}
/// A call to one side with the account's OAuth access token and a proof.
function sendOAuth(s, side, nsid, { params, body, method } = {}) {
const url = new URL(`${issuer(side)}/xrpc/${nsid}`)
for (const [k, v] of Object.entries(params ?? {})) url.searchParams.append(k, String(v))
const headers = body === undefined ? {} : { 'content-type': 'application/json' }
return s.dpop[side.name].send(url.href, { method: method ?? (body === undefined ? 'GET' : 'POST'), headers, body: body === undefined ? undefined : JSON.stringify(body), accessToken: s.tokens[side.name].access })
}
/// The same call to both sides with the account's OAuth session, compared
/// as `same` compares, and the error of `WWW-Authenticate`.
async function sameOAuth(kind, acct, nsid, request = () => ({})) {
const s = lifeOf(acct).oauth
if (!s) return []
const [a, b] = await Promise.all(sides().map((side) => sendOAuth(s, side, nsid, request(side))))
const show = (side, r) => ({ ...view(side, r, nsid), www: wwwError(r.headers?.['www-authenticate']) })
const [na, nb] = [show(ref, a), show(cand, b)]
if (!isDeepStrictEqual(na, nb)) await differ(`${nsid} (${kind}, with an OAuth token)`, shownReq({ ...request(ref), auth: 'DPoP <its OAuth access token>' }), na, nb)
count(`lifecycle: ${kind} with an OAuth token`, a.status < 400)
return [a, b]
}
/// The six sync reads of the account's repository with its OAuth session,
/// some or all, compared by status, error and the error of
/// `WWW-Authenticate`: a CAR file or a blob is not compared.
async function oauthSyncReads(acct, tag, some) {
const s = lifeOf(acct).oauth
if (!s) return
for (const [name, nsid, params] of some(SYNC_READS(acct), 3)) {
const [a, b] = await Promise.all(sides().map((side) => sendOAuth(s, side, nsid, { params: params(side) })))
const [na, nb] = [a, b].map((r) => ({ status: r.status, error: r.status >= 400 ? (r.body?.error ?? null) : null, www: wwwError(r.headers?.['www-authenticate']) }))
if (!isDeepStrictEqual(na, nb)) await differ(`${nsid} (${name}, ${tag}, with an OAuth token)`, { params: JSON.parse(strip(ref, JSON.stringify(params(ref)))), auth: 'DPoP <its OAuth access token>' }, na, nb)
count(`lifecycle: ${name}, ${tag}, with an OAuth token`, a.status < 400)
}
}
/// The account's OAuth session refreshed on both sides, compared: refused
/// once the session ended, when it is dropped.
async function refreshOAuth(acct, tag) {
const l = lifeOf(acct)
const s = l.oauth
if (!s) return
const body = (side) => new URLSearchParams({ grant_type: 'refresh_token', client_id: s.clientId, refresh_token: s.tokens[side.name].refresh }).toString()
const [a, b] = await Promise.all(sides().map((side) => s.dpop[side.name].send(`${issuer(side)}/oauth/token`, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: body(side) })))
const [na, nb] = [a, b].map((r) => ({ status: r.status, error: r.body?.error ?? null, scope: r.body?.scope ?? null }))
if (!isDeepStrictEqual(na, nb)) await differ(`OAuth refresh, ${tag}`, { refresh: `<its OAuth refresh token>` }, na, nb)
count(`lifecycle: OAuth refresh, ${tag}`, a.status === 200)
if (a.status === 200) {
s.tokens = { reference: { access: a.body.access_token, refresh: a.body.refresh_token }, server: { access: b.body.access_token, refresh: b.body.refresh_token } }
} else l.oauth = null
}
/// The account signed in on a device of its own, remembered, and the run's
/// confidential client approved there (spec/OAUTH-CLIENTS.md, "Silent
/// sign-in"), for silent sign-in.
async function approveDevice(acct) {
const l = lifeOf(acct)
l.device ??= { reference: new Browser(), server: new Browser() }
await clientLogin(acct, (await standing()).conf, { browsers: l.device, remember: true, kind: 'confidential client, for silent sign-in' })
}
/// A request of the confidential client the account approved on its
/// device, `login_hint` its handle, `prompt=none` or none, loaded there and
/// compared: a taken-down account is not looked at, nor is a deactivated
/// one hinted (spec/LIFECYCLE.md, spec/OAUTH-CLIENTS.md). A code given at
/// once is exchanged; without one, an active account approves the client
/// there again, now and then.
async function silentSignIn(acct, st, tag) {
const l = lifeOf(acct)
if (!l.device || st.deleted || !handleOf(acct)) return
const prompt = pick(['none', 'none', undefined])
const req = await clientRequest(acct, (await standing()).conf, { prompt, hint: () => handleOf(acct), scope: 'atproto', kind: `PAR (silent sign-in, ${tag})` })
if (!req) return
const page = await loadPage(req, l.device, `silent sign-in, prompt ${prompt ?? 'absent'}, ${tag}`)
const code = silent(page.reference) ? codeOf(page.reference, req.mode).code : undefined
if (code) await exchangeAndKeep(acct, req, { reference: code, server: codeOf(page.server, req.mode).code }, `token: code of a silent sign-in, ${tag}`)
else if (st.active && chance(0.5)) await approveDevice(acct)
}
/// A request approved for the account on both sides, its code not yet
/// exchanged, kept as its `pending` to exchange after the step's change.
/// `ends` is set by a takedown or its lifting, which end it whatever state
/// the account is left in; a deactivation leaves it deactivated, which the
/// exchange sees (spec/LIFECYCLE.md).
async function pendingApproval(acct) {
const l = lifeOf(acct)
l.pending = await approved(acct)
l.ends = false
}
/// The account's pending approval exchanged after the change: compared
/// where neither side should take it but the reference refuses it, or where
/// both should; held to spec/LIFECYCLE.md on the server alone, `400
/// invalid_grant`, where the change ended it or the account is taken down
/// or deactivated and the reference takes it.
async function exchangePending(acct, tag) {
const l = lifeOf(acct)
const p = l.pending
l.pending = null
if (!p) return
const st = await stateOf(acct)
const kind = `token: a code approved before ${tag}`
if (!(l.ends || !st.active)) return oauthExchange(acct, p.req, p.codes, { kind })
const body = (side) => ({ grant_type: 'authorization_code', client_id: p.req.clientId, redirect_uri: p.req.redirect, code: p.codes[side.name], code_verifier: p.req.pk.verifier })
const [a, b] = await Promise.all(sides().map((side) => tokenOn(side, p.req.dpop[side.name], body(side))))
const [va, vb] = [a, b].map((r) => ({ status: r.status, error: r.body?.error ?? null }))
const want = { status: 400, error: 'invalid_grant' }
if (va.status !== 200 && !isDeepStrictEqual(va, vb)) await differ(`OAuth (${kind})`, { token: 'its code' }, va, vb)
else if (!isDeepStrictEqual(vb, want)) await fail(cand, `OAuth ${kind} (spec/LIFECYCLE.md: the reference takes it)`, { token: 'its code', endedBy: l.ends ? tag : null, account: st }, want, vb)
count(`lifecycle: ${kind}`, false)
}
/// A request held to spec/LIFECYCLE.md on the server alone, where the
/// reference departs: its status and error must be `want`'s
/// (spec/LIFECYCLE.md, "What the reference does that the server does not
/// copy").
async function heldAlone(kind, nsid, request, want) {
const b = await send(cand, nsid, request(cand))
const got = { status: b.status, error: b.body?.error ?? null }
if (!isDeepStrictEqual(got, want)) await fail(cand, `${nsid} (${kind}; spec/LIFECYCLE.md: the reference serves it)`, shownReq(request), want, got)
count(`lifecycle: ${kind}, on the server alone`, b.status < 400)
}
/// A new account, prepared.
async function newAccount() {
const acct = await createAccount({ withPassword: true })
await prepare(acct)
return acct
}
// ---- The firehose ------------------------------------------------------------
/// Both sides' events, from the start, by side name, and how many of each
/// the harness has compared.
const live = { reference: [], server: [] }
const seen = { reference: 0, server: 0 }
/// How many `#sync` events of each DID each side has sent.
const syncsSeen = { reference: new Map(), server: new Map() }
/// Each DID's last `#commit` on each side's stream, `{cid, rev}`.
const lastCommit = { reference: new Map(), server: new Map() }
let liveSub = {}
const kindOf = (e) => (e.header.op === -1 ? `error ${e.body.error}` : e.header.t)
/// An event as compared: its type, its account named alike on both sides,
/// and what the type carries that both sides must agree on.
function line(side, e) {
const b = e.body
const t = kindOf(e)
const who = strip(side, String(b.repo ?? b.did))
if (t === '#account') return `${t} ${who} active=${b.active} status=${b.status ?? '-'}`
if (t === '#identity') return `${t} ${who} handle=${b.handle}`
if (t === '#commit') return `${t} ${who} ${(b.ops ?? []).map((op) => `${op.action} ${op.path.replace(TID, '/<tid>')} ${op.cid?.toString() ?? '-'}`).join(' | ')}`
return `${t} ${who}`
}
/// Wait until neither side has sent anything new for 400 ms and both have
/// sent as many events since `from`, or 6 seconds: an event may reach a
/// subscriber a while after the call that made it answered.
async function settle(events, from) {
let last = ''
let quiet = 0
for (let t = Date.now(); Date.now() - t < 6000; await wait(25)) {
const n = sides().map((side) => events[side.name].length - from[side.name])
const now = n.join(' ')
if (now !== last) {
last = now
quiet = 0
} else quiet += 25
if (quiet >= 400 && n[0] === n[1]) return
}
}
/// A `#sync` event: its blocks a CAR file whose root is the account's
/// latest commit as the stream has it, `last`, of the event's rev and DID.
async function checkSync(side, e, last, request) {
const b = e.body
try {
const car = await readCarWithRoot(b.blocks)
const commit = cborDecode(car.blocks.get(car.root))
if (commit.did !== b.did || commit.rev !== b.rev || car.root.toString() !== last?.cid || b.rev !== last?.rev) await fail(side, '#sync', request, `the account's latest commit, ${last?.cid} of rev ${last?.rev}`, { root: car.root.toString(), rev: b.rev })
} catch (err) {
await fail(side, '#sync', request, 'blocks that are a CAR file holding the commit', err.message)
}
}
/// The events each side put on its firehose since the last check: the same,
/// in order, every `seq` after the one before, and each `#sync` of the
/// account's latest commit (spec/LIFECYCLE.md, "The firehose").
async function checkEvents(what) {
await settle(live, seen)
const fresh = {}
for (const side of sides()) {
const all = live[side.name]
fresh[side.name] = all.slice(seen[side.name])
for (let i = Math.max(1, seen[side.name]); i < all.length; i++) if (!(all[i].body.seq > all[i - 1].body.seq)) await fail(side, 'firehose', { after: what }, `a seq after ${all[i - 1].body.seq}`, all[i].body.seq)
seen[side.name] = all.length
// An account's first #sync is its creation's (spec/ACCOUNTS.md,
// "createAccount"); each after is an activation's, of its latest commit.
for (const e of fresh[side.name]) {
const did = e.body.repo ?? e.body.did
if (e.header.t === '#commit') lastCommit[side.name].set(did, { cid: e.body.commit?.toString(), rev: e.body.rev })
if (e.header.t !== '#sync') continue
const syncs = (syncsSeen[side.name].get(did) ?? 0) + 1
syncsSeen[side.name].set(did, syncs)
if (syncs > 1) await checkSync(side, e, lastCommit[side.name].get(did), { after: what })
}
}
const [a, b] = sides().map((side) => fresh[side.name].map((e) => line(side, e)))
if (!isDeepStrictEqual(a, b)) await differ('the firehose', { after: what }, a, b, 'the events since the last check, in order')
count('lifecycle: firehose check', true)
}
/// A new subscription from cursor 0 on each side: the same events, in
/// order, every `seq` after the one before, and of a deleted account only
/// its deletion (spec/LIFECYCLE.md, "The firehose").
async function checkReplay(what) {
const got = { reference: [], server: [] }
const subs = sides().map((side) => subscribe(side, 0, got[side.name]))
await settle(got, { reference: 0, server: 0 })
for (const s of subs) s.ws.close()
const deleted = new Set()
for (const acct of accounts) if ((await stateOf(acct)).deleted) deleted.add(`<did:${acct.n}>`)
for (const side of sides()) {
const es = got[side.name]
for (let i = 1; i < es.length; i++) if (!(es[i].body.seq > es[i - 1].body.seq)) await fail(side, 'firehose replay', { cursor: 0, after: what }, `a seq after ${es[i - 1].body.seq}`, es[i].body.seq)
const kept = es.map((e) => line(side, e)).find((l) => deleted.has(l.split(' ')[1]) && !/^#account \S+ active=false status=deleted$/.test(l))
if (kept) await fail(side, 'firehose replay', { cursor: 0, after: what }, 'no event of a deleted account but its deletion (spec/LIFECYCLE.md)', kept)
}
const [a, b] = sides().map((side) => got[side.name].map((e) => line(side, e)))
if (!isDeepStrictEqual(a, b)) await differ('subscribeRepos from cursor 0', { after: what }, a, b, 'the events replayed, in order')
count('lifecycle: firehose replay', true)
}
// ---- What each state does ------------------------------------------------------
/// The six sync reads of the account's repository.
const SYNC_READS = (acct) => [
['getLatestCommit', `${Y}getLatestCommit`, (side) => ({ did: didOf(side, acct) })],
['getRepo', `${Y}getRepo`, (side) => ({ did: didOf(side, acct) })],
['getBlocks', `${Y}getBlocks`, (side) => ({ did: didOf(side, acct), cids: lifeOf(acct).records.get('a') })],
['sync getRecord', `${Y}getRecord`, (side) => ({ did: didOf(side, acct), collection: C, rkey: 'a' })],
['getBlob', `${Y}getBlob`, (side) => ({ did: didOf(side, acct), cid: lifeOf(acct).blob })],
['listBlobs', `${Y}listBlobs`, (side) => ({ did: didOf(side, acct) })],
]
/// The `Authorization` headers the sync reads are sent with: by name, a
/// function of the side, or undefined for none.
function syncAuths(acct, other) {
const l = lifeOf(acct)
const auths = [
['no token', () => undefined],
['its token', own(acct)],
['its app password session', (side) => bearer(l[side.name].app.access)],
['the admin', () => ADMIN],
['a wrong Basic', () => basic('admin', 'wrong')],
['a refresh token', (side) => bearer(acct.sides[side.name].refresh)],
['a forged token', () => bearer(FORGED)],
['a DPoP header', () => 'DPoP abc'],
['its token as DPoP', (side) => `DPoP ${acct.sides[side.name].token}`],
['a forged token as DPoP', () => `DPoP ${FORGED}`],
]
if (other) auths.push(['another account\'s token', own(other)])
if (l.reference.td) auths.push(['its takendown session', (side) => bearer(l[side.name].td.access)])
return auths
}
/// Every method of the tables for the account, compared (spec/LIFECYCLE.md,
/// "What each state does to every method"). `full` sends every variant;
/// otherwise some.
async function matrix(acct, full) {
const st = await stateOf(acct)
const l = lifeOf(acct)
const alive = accounts.filter((a) => a !== acct && !(life.get(a)?.gone))
const other = alive.length ? pick(alive) : null
const some = (xs, n) => (full ? xs : xs.filter(() => chance(n / xs.length)))
const did = (side) => didOf(side, acct)
const tag = st.deleted ? 'deleted' : st.takendown ? 'taken down' : st.deactivated ? 'deactivated' : 'active'
// Its repository, read by anyone (spec/LIFECYCLE.md, "Its repository,
// read by anyone").
for (const repo of [did, () => acct.handle]) await same(`describeRepo, ${tag}`, `${R}describeRepo`, (side) => ({ params: { repo: repo(side) } }))
await same(`describeRepo by the admin, ${tag}`, `${R}describeRepo`, (side) => ({ params: { repo: did(side) }, auth: ADMIN }))
await same(`listRecords, ${tag}`, `${R}listRecords`, (side) => ({ params: { repo: did(side), collection: C } }))
if (st.active) await same(`getRecord, ${tag}`, `${R}getRecord`, (side) => ({ params: { repo: did(side), collection: C, rkey: 'a' } }))
for (const [name, nsid, params] of SYNC_READS(acct)) for (const [who, auth] of some(syncAuths(acct, other), 2)) await same(`${name} with ${who}, ${tag}`, nsid, (side) => ({ params: params(side), auth: auth(side) }))
// Of a DID no account has: the token's errors come before RepoNotFound.
for (const [name, nsid, params] of some(SYNC_READS(acct), 2)) for (const [who, auth] of some(syncAuths(acct, other), 2)) await same(`${name} of a DID no account has, with ${who}`, nsid, (side) => ({ params: { ...params(side), did: NOBODY }, auth: auth(side) }))
for (const auth of [undefined, bearer(FORGED)]) await same(`getRepoStatus, ${tag}`, `${Y}getRepoStatus`, (side) => ({ params: { did: did(side) }, auth }))
await checkLatest(acct, st, 'getRepoStatus', async (side) => (await send(side, `${Y}getRepoStatus`, { params: { did: did(side) } })).body?.rev, 'rev')
await same(`listRepos, ${tag}`, `${Y}listRepos`, { params: { limit: 1000 } })
// Its handle, which resolves to nothing (spec/LIFECYCLE.md, "Its handle").
await same(`resolveHandle, ${tag}`, `${I}resolveHandle`, { params: { handle: acct.handle } })
const wk = await Promise.all(sides().map((side) => wellKnown(side, acct.handle)))
const [wa, wb] = wk.map((r, i) => ({ status: r.status, body: r.status === 200 ? strip(sides()[i], r.body) : null }))
if (!isDeepStrictEqual(wa, wb)) await differ(`/.well-known/atproto-did, ${tag}`, { host: acct.handle }, wa, wb)
// Its own calls (spec/LIFECYCLE.md, "Its own calls").
await same(`getSession with no token, ${tag}`, `${S}getSession`, {})
await same(`getSession with its token, ${tag}`, `${S}getSession`, (side) => ({ auth: own(acct)(side) }))
await same(`getSession with an app password's session, ${tag}`, `${S}getSession`, (side) => ({ auth: bearer(l[side.name].app.access) }))
await refreshSpare(acct, st, tag)
await same(`deleteSession, ${tag}`, `${S}deleteSession`, (side) => ({ auth: bearer(acct.sides[side.name].refresh), method: 'POST' }))
const logins = [
['by handle', (side) => ({ identifier: acct.handle, password: acct.password })],
['by email', () => ({ identifier: acct.email.toUpperCase(), password: acct.password })],
['by DID with allowTakendown', (side) => ({ identifier: did(side), password: acct.password, allowTakendown: true })],
['with an app password', (side) => ({ identifier: acct.handle, password: l.appPassword[side.name] })],
['with an app password and allowTakendown', (side) => ({ identifier: did(side), password: l.appPassword[side.name], allowTakendown: true })],
['with a wrong password', () => ({ identifier: acct.handle, password: 'not-it' })],
]
for (const [how, body] of some(logins, 2)) {
const s = await login(`createSession ${how}, ${tag}`, body)
if (s && st.takendown && body(ref).allowTakendown) await takendownSession(acct, s, `createSession ${how}`)
}
const name = `life-${next()}`
const [ca, cb] = await same(`createAppPassword, ${tag}`, `${S}createAppPassword`, (side) => ({ auth: own(acct)(side), body: { name } }))
for (const [side, res] of [[ref, ca], [cand, cb]]) if (res.status === 200 && !APP_PASSWORD.test(res.body.password)) await fail(side, 'app password', { name }, 'four groups of four of a-z and 2-7', res.body)
// Taken down or deleted, it is refused before its input (spec/LIFECYCLE.md).
await same(`createAppPassword with bad input, ${tag}`, `${S}createAppPassword`, (side) => ({ auth: own(acct)(side), body: { name: 5 } }))
await same(`updateHandle with bad input, ${tag}`, `${I}updateHandle`, (side) => ({ auth: own(acct)(side), body: { handle: 5 } }))
if (st.deleted) {
// The reference keeps a deleted account's app passwords; the server
// keeps nothing of it (spec/LIFECYCLE.md).
const b = await send(cand, `${S}listAppPasswords`, { auth: own(acct)(cand) })
if (b.status !== 200 || !isDeepStrictEqual(b.body, { passwords: [] })) await differ(`${S}listAppPasswords of a deleted account`, { token: `<token of did:${acct.n}>` }, { status: 200, body: { passwords: [] } }, b)
count('lifecycle: listAppPasswords, deleted', true)
} else await same(`listAppPasswords, ${tag}`, `${S}listAppPasswords`, (side) => ({ auth: own(acct)(side) }))
await same(`revokeAppPassword, ${tag}`, `${S}revokeAppPassword`, (side) => ({ auth: own(acct)(side), body: { name: 'never-made' } }))
// The account's OAuth routes, on the server alone: the reference serves
// none (spec/LIFECYCLE.md, "Its own calls"; spec/OAUTH-ACCOUNT.md).
for (const [route, key] of [['_oauthSessions', 'sessions'], ['_oauthDevices', 'devices']]) {
const b = await send(cand, route, { auth: own(acct)(cand) })
const list = b.body?.[key]
const fine = b.status === 200 && Array.isArray(list) && Object.keys(b.body).length === 1 && list.every((x) => typeof x?.id === 'string' && /^[0-9a-f]+$/.test(x.id)) && (!st.deleted || list.length === 0)
if (!fine) await differ(`${route}, ${tag}`, { token: `<token of did:${acct.n}>` }, { status: 200, body: { [key]: st.deleted ? [] : 'its list, as for an active account, each id lower case hex' } }, b, 'spec/LIFECYCLE.md, "Its own calls"')
count(`lifecycle: ${route}`, fine)
}
for (const route of ['_oauthRevokeSession', '_oauthRevokeDevice']) {
const b = await send(cand, route, { auth: own(acct)(cand), body: { id: '0123456789abcdef' } })
if (b.status !== 200 || !isDeepStrictEqual(b.body, {})) await differ(`${route}, ${tag}`, { token: `<token of did:${acct.n}>` }, { status: 200, body: {} }, b, 'spec/LIFECYCLE.md, "Its own calls"')
count(`lifecycle: ${route}`, b.status === 200)
}
// The account's passkey routes, on the server alone: the reference has
// no passkeys (spec/LIFECYCLE.md, "Its own calls"; spec/PASSKEYS.md, "The
// account's states"). Its passkey is listed while it is not deleted, and
// once it is, signs in no more.
if (passkeysAble()) {
const auth = own(acct)(cand)
const b = await send(cand, '_passkeyList', { auth })
const list = b.body?.passkeys
const mine = l.passkey ? b64u(l.passkey.id) : null
const fine = b.status === 200 && Array.isArray(list) && Object.keys(b.body).length === 1 && (st.deleted ? list.length === 0 : !mine || list.some((x) => x?.id === mine))
if (!fine) await differ(`_passkeyList, ${tag}`, { token: `<token of did:${acct.n}>` }, { status: 200, body: { passkeys: st.deleted ? [] : mine ? `its passkeys, ${mine.slice(0, 12)}… among them` : 'its passkeys' } }, b, 'spec/LIFECYCLE.md, "Its own calls"')
count('lifecycle: _passkeyList', fine)
const add = await send(cand, '_passkeyAdd', { auth, body: { name: `lifecycle ${next()}` } })
const want = st.deleted ? { status: 403, error: 'AccountNotFound' } : st.takendown ? { status: 401, error: 'AccountTakedown' } : { status: 200, url: true }
const got = add.status === 200 ? { status: 200, url: typeof add.body?.url === 'string' && typeof add.body?.expiresAt === 'string' } : { status: add.status, error: add.body?.error ?? null }
if (!isDeepStrictEqual(got, want)) await differ(`_passkeyAdd, ${tag}`, { token: `<token of did:${acct.n}>` }, want, { status: add.status, body: add.body }, 'spec/LIFECYCLE.md, "Its own calls"')
count('lifecycle: _passkeyAdd', add.status === 200)
// Taken down or deleted, it is refused before its input
// (spec/PASSKEYS.md, "Requests").
const bad = await send(cand, '_passkeyAdd', { auth, body: { name: 5 } })
const badWant = want.status === 200 ? { status: 400, error: 'InvalidRequest' } : want
if (!isDeepStrictEqual({ status: bad.status, error: bad.body?.error ?? null }, badWant)) await differ(`_passkeyAdd with bad input, ${tag}`, { token: `<token of did:${acct.n}>`, body: { name: 5 } }, badWant, bad, 'spec/PASSKEYS.md, "Requests"')
const rm = await send(cand, '_passkeyRemove', { auth, body: { id: b64u(Buffer.from(`no passkey ${next()}`)) } })
if (rm.status !== 200 || !isDeepStrictEqual(rm.body, {})) await differ(`_passkeyRemove, ${tag}`, { token: `<token of did:${acct.n}>` }, { status: 200, body: {} }, rm, 'spec/LIFECYCLE.md, "Its own calls"')
count('lifecycle: _passkeyRemove', rm.status === 200)
if (st.deleted && l.passkey) await passkeyRefused(l.who, l.passkey, 'a passkey of a deleted account', 'spec/PASSKEYS.md, "The account\'s states"')
}
// A service token and a call forwarded to the AppView (spec/PROXY.md,
// "getServiceAuth", "Which requests are forwarded"), which a taken-down
// account is refused, though the reference serves it (spec/LIFECYCLE.md,
// "What the reference does that the server does not copy").
const serviceCalls = [
['getServiceAuth', `${S}getServiceAuth`, () => ({ params: { aud: services.appview.did, lxm: 'app.bsky.feed.getPosts' } })],
['a forwarded call', 'app.bsky.feed.getPosts', (side) => ({ params: { uris: `at://${did(side)}/app.bsky.feed.post/x` } })],
]
for (const [who, auth] of [['its token', own(acct)], ['an app password\'s session', (side) => bearer(l[side.name].app.access)]]) {
for (const [name, nsid, req] of serviceCalls) {
const request = (side) => ({ ...req(side), auth: auth(side) })
if (st.takendown) await heldAlone(`${name} with ${who}, ${tag}`, nsid, request, { status: 401, error: 'AccountTakedown' })
else await same(`${name} with ${who}, ${tag}`, nsid, request)
}
}
// Its OAuth session, which a takedown or its lifting, a deactivation with
// an OAuth token and a deletion end; a new one is refused to a taken-down
// account at sign-in and a deactivated one at consent.
if (l.oauth) {
await sameOAuth(`getSession, ${tag}`, acct, `${S}getSession`)
await sameOAuth(`checkAccountStatus, ${tag}`, acct, `${S}checkAccountStatus`)
for (const [name, nsid, req] of serviceCalls) await sameOAuth(`${name}, ${tag}`, acct, nsid, req)
await oauthSyncReads(acct, tag, some)
const ok = next()
await sameOAuth(`createRecord, ${tag}`, acct, `${R}createRecord`, (side) => ({ body: { repo: did(side), collection: C, rkey: `o${ok}`, record: { $type: C } } }))
await sameOAuth(`activateAccount, ${tag}`, acct, `${S}activateAccount`, () => ({ method: 'POST' }))
if (full || chance(0.5)) await refreshOAuth(acct, tag)
}
if (!st.deleted && !l.oauth && (full || chance(0.3))) await oauthSession(acct, tag)
await silentSignIn(acct, st, tag)
// Writes: its own, to another's repository, and another's to its.
const key = `w${next()}`
const pn = next()
const writes = [
['createRecord', `${R}createRecord`, (side) => ({ repo: did(side), collection: C, rkey: key, record: { $type: C, text: key } })],
['putRecord', `${R}putRecord`, (side) => ({ repo: did(side), collection: C, rkey: 'p', record: { $type: C, n: pn } })],
['deleteRecord', `${R}deleteRecord`, (side) => ({ repo: did(side), collection: C, rkey: 'never-made' })],
['applyWrites', `${R}applyWrites`, (side) => ({ repo: did(side), writes: [] })],
]
for (const [name, nsid, body] of some(writes, 2)) await same(`${name}, ${tag}`, nsid, (side) => ({ auth: own(acct)(side), body: body(side) }))
await same(`createRecord with bad input, ${tag}`, `${R}createRecord`, (side) => ({ auth: own(acct)(side), body: { repo: did(side), collection: 'nope', record: {} } }))
const [k1, k2, k3, up] = [next(), next(), next(), next()]
await same(`createRecord with an app password's session, ${tag}`, `${R}createRecord`, (side) => ({ auth: bearer(l[side.name].app.access), body: { repo: did(side), collection: C, rkey: `w${k1}`, record: { $type: C } } }))
if (other) {
await same(`createRecord to another's repository, ${tag}`, `${R}createRecord`, (side) => ({ auth: own(acct)(side), body: { repo: didOf(side, other), collection: C, rkey: `x${k2}`, record: { $type: C } } }))
await same(`createRecord by another account, ${tag}`, `${R}createRecord`, (side) => ({ auth: own(other)(side), body: { repo: did(side), collection: C, rkey: `x${k3}`, record: { $type: C } } }))
}
await same(`uploadBlob, ${tag}`, `${R}uploadBlob`, (side) => ({ auth: own(acct)(side), body: Buffer.from(`lifecycle upload ${up}`), type: 'text/plain' }))
// The account's state is checked before anything of the body
// (spec/LIFECYCLE.md).
await same(`uploadBlob with no Content-Type, ${tag}`, `${R}uploadBlob`, (side) => ({ auth: own(acct)(side), body: Buffer.from('no type'), type: null }))
if (chance(full ? 0.3 : 0.1)) await same(`uploadBlob of 5 MiB and a byte, ${tag}`, `${R}uploadBlob`, (side) => ({ auth: own(acct)(side), body: Buffer.alloc(5242881, 0x61), type: 'text/plain' }))
await same(`updateHandle to its own, ${tag}`, `${I}updateHandle`, (side) => ({ auth: own(acct)(side), body: { handle: acct.handle } }))
await same(`getRecommendedDidCredentials, ${tag}`, `${I}getRecommendedDidCredentials`, (side) => ({ auth: own(acct)(side) }))
// checkAccountStatus, its commit held to the latest (spec/LIFECYCLE.md,
// "checkAccountStatus").
for (const [who, auth] of [['its token', own(acct)], ['an app password\'s session', (side) => bearer(l[side.name].app.access)]]) {
await same(`checkAccountStatus with ${who}, ${tag}`, `${S}checkAccountStatus`, (side) => ({ auth: auth(side) }))
}
await checkLatest(acct, st, 'checkAccountStatus', async (side) => {
const r = await send(side, `${S}checkAccountStatus`, { auth: own(acct)(side) })
return r.status === 200 ? `${r.body.repoCommit} ${r.body.repoRev}` : undefined
}, 'both')
if (full || chance(0.3)) {
// Asked for, in its forms, and its email (spec/LIFECYCLE.md,
// "requestAccountDelete").
await same(`requestAccountDelete, ${tag}`, `${S}requestAccountDelete`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
await same(`requestAccountDelete with a body, ${tag}`, `${S}requestAccountDelete`, (side) => ({ auth: own(acct)(side), body: {} }))
await same(`requestAccountDelete with no token, ${tag}`, `${S}requestAccountDelete`, { method: 'POST' })
await same(`requestAccountDelete with an app password's session, ${tag}`, `${S}requestAccountDelete`, (side) => ({ auth: bearer(l[side.name].priv.access), method: 'POST' }))
await noteMail(acct, `requestAccountDelete, ${tag}`)
}
// The email methods, invite codes, preferences and push registration: a
// taken-down or deleted account is refused after the token, before the
// input (spec/LIFECYCLE.md).
const ownCalls = [
['requestEmailConfirmation', `${S}requestEmailConfirmation`, { method: 'POST' }],
['requestEmailConfirmation with a body', `${S}requestEmailConfirmation`, { body: {} }],
['confirmEmail with a wrong token', `${S}confirmEmail`, { body: { email: acct.email, token: 'AAAAA-BBBBB' } }],
['confirmEmail with bad input', `${S}confirmEmail`, { body: { email: 5 } }],
['requestEmailUpdate', `${S}requestEmailUpdate`, { method: 'POST' }],
['updateEmail with a wrong token', `${S}updateEmail`, { body: { email: acct.email, token: 'AAAAA-BBBBB' } }],
['updateEmail with bad input', `${S}updateEmail`, { body: { email: 5 } }],
['getAccountInviteCodes', `${S}getAccountInviteCodes`, {}],
['getAccountInviteCodes with a bad parameter', `${S}getAccountInviteCodes`, { params: { includeUsed: 'nope' } }],
['getPreferences', 'app.bsky.actor.getPreferences', {}],
['putPreferences', 'app.bsky.actor.putPreferences', { body: { preferences: [] } }],
['putPreferences with bad input', 'app.bsky.actor.putPreferences', { body: { preferences: 5 } }],
]
for (const [name, nsid, req] of some(ownCalls, 6)) await same(`${name}, ${tag}`, nsid, (side) => ({ ...req, auth: own(acct)(side) }))
await same(`getPreferences with an app password's session, ${tag}`, 'app.bsky.actor.getPreferences', (side) => ({ auth: bearer(l[side.name].app.access) }))
await noteMail(acct, `the email methods, ${tag}`)
if (full || chance(0.3)) {
await same(`requestPasswordReset by its email, ${tag}`, `${S}requestPasswordReset`, { body: { email: acct.email } })
await noteMail(acct, `requestPasswordReset, ${tag}`)
}
// Push registration makes a service token, which a taken-down account is
// refused, though the reference makes it (spec/LIFECYCLE.md).
const push = { serviceDid: services.appview.did, token: `push-${acct.n}`, platform: 'web', appId: 'xyz.blueskyweb.app' }
for (const method of ['registerPush', 'unregisterPush']) {
if (!full && chance(0.5)) continue
const request = (side) => ({ auth: own(acct)(side), body: push })
if (st.takendown) await heldAlone(`${method}, ${tag}`, `app.bsky.notification.${method}`, request, { status: 401, error: 'AccountTakedown' })
else await same(`${method}, ${tag}`, `app.bsky.notification.${method}`, request)
}
if (st.deleted) {
await same(`deactivateAccount, ${tag}`, `${S}deactivateAccount`, (side) => ({ auth: own(acct)(side), body: {} }))
await same(`activateAccount, ${tag}`, `${S}activateAccount`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
}
// Its handle and email, taken until it is deleted (spec/LIFECYCLE.md,
// "States").
if (!st.deleted) {
await same(`createAccount with its handle, ${tag}`, `${S}createAccount`, { body: { handle: acct.handle, email: `${freshFront()}@example.com`, password: 'pw' } })
await same(`createAccount with its email, ${tag}`, `${S}createAccount`, { body: { handle: `${freshFront()}.test`, email: acct.email, password: 'pw' } })
if (other) await same(`updateHandle to its handle, ${tag}`, `${I}updateHandle`, (side) => ({ auth: own(other)(side), body: { handle: acct.handle } }))
}
// A takendown session's tokens.
if (l.reference.td) await tdCalls(acct, tag, some)
}
/// The account's latest commit, read by the admin, must be what `read` (a
/// function of the side) says, where it answers.
async function checkLatest(acct, st, what, read, which) {
if (st.deleted) return
for (const side of sides()) {
const got = await read(side)
if (got === undefined) continue
const latest = (await send(side, `${Y}getLatestCommit`, { params: { did: didOf(side, acct) }, auth: ADMIN })).body
const want = which === 'rev' ? latest?.rev : `${latest?.cid} ${latest?.rev}`
if (got !== want) await fail(side, what, { did: `<did:${acct.n}>` }, `the latest commit, ${want}`, got)
}
}
/// refreshSession with the account's spare session, which a takedown ends:
/// a new one is made with its password when it can be.
async function refreshSpare(acct, st, tag) {
const l = lifeOf(acct)
const [a, b] = await same(`refreshSession, ${tag}`, `${S}refreshSession`, (side) => ({ auth: bearer(l[side.name].spare.refresh), method: 'POST' }))
if (a.status === 200 && b.status === 200) {
l.reference.spare = { access: a.body.accessJwt, refresh: a.body.refreshJwt }
l.server.spare = { access: b.body.accessJwt, refresh: b.body.refreshJwt }
} else if (a.body?.error === 'ExpiredToken' && !st.deleted && !st.takendown) {
const s = await login(`createSession for a new spare session, ${tag}`, () => ({ identifier: acct.handle, password: acct.password }))
if (s) for (const side of sides()) l[side.name].spare = s[side.name]
}
}
/// A JWT's header and payload, or null.
function decode(token) {
try {
const [h, p] = token.split('.').slice(0, 2).map((x) => JSON.parse(Buffer.from(x, 'base64url').toString('utf8')))
return { header: h, payload: p }
} catch {
return null
}
}
/// A session made with `allowTakendown` for a taken-down account: its
/// tokens' claims as spec/SESSIONS.md's table says, of scope
/// `com.atproto.takendown`; kept to use (spec/LIFECYCLE.md, "Sessions of a
/// taken-down account").
async function takendownSession(acct, s, what) {
for (const side of sides()) {
const did = didOf(side, acct)
for (const [field, typ, scope, life] of [['access', 'at+jwt', 'com.atproto.takendown', 7200], ['refresh', 'refresh+jwt', 'com.atproto.refresh', 7776000]]) {
const jwt = decode(s[side.name][field])
const p = jwt?.payload ?? {}
// As in sessions.mjs: the reference may give a second more, the server not.
const life2 = side === ref && p.exp - p.iat === life + 1 ? life : p.exp - p.iat
const got = { typ: jwt?.header?.typ, scope: p.scope, sub: p.sub === did ? '<its DID>' : p.sub, aud: p.aud, life: life2, jti: field === 'refresh' ? typeof p.jti : undefined }
const want = { typ, scope, sub: '<its DID>', aud: 'did:web:localhost', life, jti: field === 'refresh' ? 'string' : undefined }
if (!isDeepStrictEqual(got, want)) await fail(side, `${field} token of a takendown session`, { what }, want, got)
}
}
const l = lifeOf(acct)
for (const side of sides()) l[side.name].td = s[side.name]
}
/// A takendown session's tokens on what they may and may not do.
async function tdCalls(acct, tag, some) {
const l = lifeOf(acct)
const td = (side) => bearer(l[side.name].td.access)
const calls = [
['getSession', `${S}getSession`, {}],
['checkAccountStatus', `${S}checkAccountStatus`, {}],
['listAppPasswords', `${S}listAppPasswords`, {}],
['getRecommendedDidCredentials', `${I}getRecommendedDidCredentials`, {}],
['activateAccount', `${S}activateAccount`, { method: 'POST' }],
['requestAccountDelete', `${S}requestAccountDelete`, { method: 'POST' }],
['createAppPassword', `${S}createAppPassword`, { body: { name: `td-${next()}` } }],
['requestEmailConfirmation', `${S}requestEmailConfirmation`, { method: 'POST' }],
['confirmEmail', `${S}confirmEmail`, { body: { email: acct.email, token: 'AAAAA-BBBBB' } }],
['requestEmailUpdate', `${S}requestEmailUpdate`, { method: 'POST' }],
['updateEmail', `${S}updateEmail`, { body: { email: acct.email, token: 'AAAAA-BBBBB' } }],
['getAccountInviteCodes', `${S}getAccountInviteCodes`, {}],
['getPreferences', 'app.bsky.actor.getPreferences', {}],
['putPreferences', 'app.bsky.actor.putPreferences', { body: { preferences: [] } }],
['registerPush', 'app.bsky.notification.registerPush', { body: { serviceDid: services.appview.did, token: `push-${acct.n}`, platform: 'web', appId: 'xyz.blueskyweb.app' } }],
]
for (const [name, nsid, req] of some(calls, 3)) await same(`${name} with a takendown session, ${tag}`, nsid, (side) => ({ ...req, auth: td(side) }))
const key = `t${next()}`
await same(`createRecord with a takendown session, ${tag}`, `${R}createRecord`, (side) => ({ auth: td(side), body: { repo: didOf(side, acct), collection: C, rkey: key, record: { $type: C } } }))
await same(`refreshSession with a takendown session, ${tag}`, `${S}refreshSession`, (side) => ({ auth: bearer(l[side.name].td.refresh), method: 'POST' }))
await noteMail(acct, `requestAccountDelete with a takendown session, ${tag}`)
}
/// The emails sent since the last, compared; a `deleteAccount` one is kept
/// as the account's token on each side, and a `resetPassword` one as its
/// `reset`.
async function noteMail(acct, what) {
const sent = await checkMail(what, { account: `<did:${acct.n}>` })
for (const side of sides()) {
const mine = sent[side.name].filter((x) => x.to === acct.email.toLowerCase())
const m = mine.filter((x) => x.kind === 'deleteAccount').at(-1)
if (m) lifeOf(acct)[side.name].mailed = m.token
const r = mine.filter((x) => x.kind === 'resetPassword').at(-1)
if (r) lifeOf(acct)[side.name].reset = r.token
}
}
// ---- Changes ---------------------------------------------------------------------
/// deactivateAccount, by the account, in one of its forms
/// (spec/LIFECYCLE.md, "deactivateAccount").
async function deactivate(acct) {
const l = lifeOf(acct)
const deleteAfter = pick(['2030-01-01T00:00:00.000Z', '2000-01-01T00:00:00Z'])
const forms = [
['', (side) => ({ auth: own(acct)(side), body: {} })],
[' with deleteAfter', (side) => ({ auth: own(acct)(side), body: { deleteAfter } })],
[' with a bad deleteAfter', (side) => ({ auth: own(acct)(side), body: { deleteAfter: 'tomorrow' } })],
[' with no body', (side) => ({ auth: own(acct)(side), method: 'POST' })],
[' with no token', () => ({ body: {} })],
[' with an app password\'s session', (side) => ({ auth: bearer(l[side.name].app.access), body: {} })],
[' with a privileged app password\'s session', (side) => ({ auth: bearer(l[side.name].priv.access), body: {} })],
]
if (l.reference.td) forms.push([' with a takendown session', (side) => ({ auth: bearer(l[side.name].td.access), body: {} })])
// With an OAuth token it ends every OAuth session of the account.
if (l.oauth && chance(0.2)) return sameOAuth('deactivateAccount', acct, `${S}deactivateAccount`, () => ({ body: {} }))
const [how, req] = chance(0.5) ? forms[below(2)] : pick(forms)
await same(`deactivateAccount${how}`, `${S}deactivateAccount`, req)
}
/// activateAccount, by the account, in one of its forms (spec/LIFECYCLE.md,
/// "activateAccount").
async function activate(acct) {
const l = lifeOf(acct)
const forms = [
['', (side) => ({ auth: own(acct)(side), method: 'POST' })],
[' with a body', (side) => ({ auth: own(acct)(side), body: {} })],
[' with no token', () => ({ method: 'POST' })],
[' with an app password\'s session', (side) => ({ auth: bearer(l[side.name].app.access), method: 'POST' })],
]
const [how, req] = chance(0.6) ? forms[0] : pick(forms)
await same(`activateAccount${how}`, `${S}activateAccount`, req)
}
const updateStatus = (kind, body, auth = () => ADMIN) => same(kind, `${A}updateSubjectStatus`, (side) => ({ auth: auth(side), body: body(side) }))
const getStatus = (kind, params, auth = () => ADMIN) => same(kind, `${A}getSubjectStatus`, (side) => ({ auth: auth(side), params: params(side) }))
/// The admin takes the account down, with a `ref` or without; sometimes
/// after it asked for a deletion token, which must still delete it
/// (spec/LIFECYCLE.md, "updateSubjectStatus", "getSubjectStatus").
async function takedown(acct) {
if (chance(0.3)) {
await same('requestAccountDelete before a takedown', `${S}requestAccountDelete`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
await noteMail(acct, 'requestAccountDelete before a takedown')
}
const ref_ = chance(0.5) ? { ref: `case-${next()}` } : {}
const [t] = await updateStatus('updateSubjectStatus takedown', (side) => ({ subject: repoRef(didOf(side, acct)), takedown: { applied: true, ...ref_ } }))
if (t.status === 200) lifeOf(acct).ends = true
const [a, b] = await getStatus('getSubjectStatus of an account', (side) => ({ did: didOf(side, acct) }))
// A takedown without a `ref` has the time it was made as one: the
// harness sends no datetime of its own.
if (!ref_.ref) for (const [side, res] of [[ref, a], [cand, b]]) {
const made = Date.parse(res.body?.takedown?.ref)
if (!(Math.abs(made - Date.now()) < 15_000)) await fail(side, 'takedown ref', { did: `<did:${acct.n}>` }, 'the time it was taken down, as a datetime', res.body?.takedown)
}
}
/// The admin lifts the account's takedown (spec/LIFECYCLE.md,
/// "updateSubjectStatus").
async function lift(acct) {
const extra = chance(0.3) ? { ref: 'x' } : {}
const [t] = await updateStatus('updateSubjectStatus lifting a takedown', (side) => ({ subject: repoRef(didOf(side, acct)), takedown: { applied: false, ...extra } }))
if (t.status === 200) lifeOf(acct).ends = true
await getStatus('getSubjectStatus of an account', (side) => ({ did: didOf(side, acct) }))
}
/// The admin deactivates or activates the account (spec/LIFECYCLE.md,
/// "updateSubjectStatus").
async function adminDeactivation(acct) {
const applied = chance(0.5)
await updateStatus(`updateSubjectStatus ${applied ? 'deactivating' : 'activating'}`, (side) => ({ subject: repoRef(didOf(side, acct)), deactivated: { applied } }))
await getStatus('getSubjectStatus of an account', (side) => ({ did: didOf(side, acct) }))
}
/// The admin's other forms: both attributes, the pair it refuses, neither,
/// a DID no account has, subjects and input it refuses, and
/// getSubjectStatus without what it needs (spec/LIFECYCLE.md,
/// "updateSubjectStatus", "getSubjectStatus").
async function adminOdd(acct) {
const did = (side) => didOf(side, acct)
const coin = chance(0.5)
const gone = accounts.find((a) => life.get(a)?.gone)
const forms = [
['both attributes', (side) => ({ subject: repoRef(did(side)), takedown: { applied: coin }, deactivated: { applied: true } })],
['both lifted', (side) => ({ subject: repoRef(did(side)), takedown: { applied: false }, deactivated: { applied: false } })],
['a takedown with activation', (side) => ({ subject: repoRef(did(side)), takedown: { applied: true }, deactivated: { applied: false } })],
['neither attribute', (side) => ({ subject: repoRef(did(side)) })],
['a DID no account has', () => ({ subject: repoRef(NOBODY), takedown: { applied: coin } })],
['a DID no account has, neither attribute', () => ({ subject: repoRef(NOBODY) })],
['a DID no account has, deactivating', () => ({ subject: repoRef(NOBODY), deactivated: { applied: true } })],
['a DID no account has, activating', () => ({ subject: repoRef(NOBODY), deactivated: { applied: false } })],
['a DID no account has, a lifted takedown and deactivating', () => ({ subject: repoRef(NOBODY), takedown: { applied: false }, deactivated: { applied: true } })],
...(gone ? [
['a deleted account\'s DID, deactivating', (side) => ({ subject: repoRef(didOf(side, gone)), deactivated: { applied: true } })],
['a deleted account\'s DID, activating', (side) => ({ subject: repoRef(didOf(side, gone)), deactivated: { applied: false } })],
] : []),
['a subject of another type', (side) => ({ subject: { $type: 'com.example.subject', did: did(side) }, takedown: { applied: true } })],
['a subject without $type', (side) => ({ subject: { did: did(side) }, takedown: { applied: true } })],
['a handle as the DID', () => ({ subject: repoRef(acct.handle), takedown: { applied: true } })],
['no subject', () => ({ takedown: { applied: true } })],
['a takedown without applied', (side) => ({ subject: repoRef(did(side)), takedown: {} })],
]
const [how, body] = pick(forms)
await updateStatus(`updateSubjectStatus with ${how}`, body)
const gets = [
['by DID', (side) => ({ did: did(side) })],
['of a DID no account has', () => ({ did: NOBODY })],
['with nothing', () => ({})],
['of a blob without a DID', () => ({ blob: lifeOf(acct).blob })],
]
const [what, params] = pick(gets)
await getStatus(`getSubjectStatus ${what}`, params)
}
/// Subjects that name no record or account to change: a record's `uri` of a
/// DID alone or a collection alone, a DID no account has, a subject of
/// another `$type` without `takedown`, and `deactivated` of each; then
/// getSubjectStatus of such `uri`s and of one by handle. Each form, every
/// time (spec/LIFECYCLE.md, "updateSubjectStatus", "getSubjectStatus").
async function oddSubjects(acct) {
const did = (side) => didOf(side, acct)
const coin = chance(0.5)
const forms = [
['a record subject of its DID alone', (side) => ({ subject: strongRef(`at://${did(side)}`), takedown: { applied: coin } })],
['a record subject of a collection alone', (side) => ({ subject: strongRef(`at://${did(side)}/${C}`), takedown: { applied: coin } })],
['a record subject of its DID alone, deactivating', (side) => ({ subject: strongRef(`at://${did(side)}`), deactivated: { applied: coin } })],
['a record subject of a DID no account has alone', () => ({ subject: strongRef(`at://${NOBODY}`), takedown: { applied: coin } })],
['a record of a DID no account has, deactivating', () => ({ subject: strongRef(`at://${NOBODY}/${C}/t`), deactivated: { applied: coin } })],
['a blob of a DID no account has, deactivating', () => ({ subject: { $type: `${A}defs#repoBlobRef`, did: NOBODY, cid: lifeOf(acct).blob }, deactivated: { applied: coin } })],
['a subject of another type, deactivating', (side) => ({ subject: { $type: 'com.example.subject', did: did(side) }, deactivated: { applied: coin } })],
['a subject of another type, neither attribute', (side) => ({ subject: { $type: 'com.example.subject', did: did(side) } })],
['a subject of another type, a lifted takedown and deactivating', (side) => ({ subject: { $type: 'com.example.subject', did: did(side) }, takedown: { applied: false }, deactivated: { applied: coin } })],
]
for (const [how, body] of forms) await updateStatus(`updateSubjectStatus with ${how}`, body)
const gets = [
['of its DID alone as a uri', (side) => ({ uri: `at://${did(side)}` })],
['of a collection alone', (side) => ({ uri: `at://${did(side)}/${C}` })],
['of a record by handle', () => ({ uri: `at://${acct.handle}/${C}/a` })],
['of a record by a handle no account has', () => ({ uri: `at://nobody-${next()}.test/${C}/a` })],
]
for (const [what, params] of gets) await getStatus(`getSubjectStatus ${what}`, params)
await getStatus('getSubjectStatus of an account', (side) => ({ did: did(side) }))
}
/// The admin methods with auth that is not the admin's (spec/LIFECYCLE.md,
/// "The admin").
async function adminAuth(acct) {
const l = lifeOf(acct)
const forms = [
['none', () => undefined],
['a wrong password', () => basic('admin', 'wrong')],
['another user', () => basic('root', ADMIN_PASSWORD)],
['no colon', () => `Basic ${Buffer.from(ADMIN_PASSWORD).toString('base64')}`],
['a Basic value that does not decode', () => 'Basic !!!'],
['basic in lower case', () => ADMIN.replace('Basic', 'basic')],
['DPoP', () => 'DPoP abc'],
['a session\'s access token', own(acct)],
['a refresh token', (side) => bearer(l[side.name].spare.refresh)],
['a forged token', () => bearer(FORGED)],
['three words', () => 'Basic a b'],
]
const [how, auth] = pick(forms)
await getStatus(`getSubjectStatus with ${how}`, (side) => ({ did: didOf(side, acct) }), auth)
await updateStatus(`updateSubjectStatus with ${how}`, (side) => ({ subject: repoRef(didOf(side, acct)), takedown: { applied: false } }), auth)
if (chance(0.3)) await updateStatus(`updateSubjectStatus with ${how} and bad input`, () => ({ nope: 1 }), auth)
}
/// A record of the account taken down or lifted, read every way, and
/// sometimes updated, deleted or made again (spec/LIFECYCLE.md, "Records
/// and blobs taken down").
async function recordTakedown(acct) {
const st = await stateOf(acct)
const l = lifeOf(acct)
const did = (side) => didOf(side, acct)
const uri = (side, rkey = 't') => `at://${did(side)}/${C}/${rkey}`
if (st.active && (!l.records.has('t') || chance(0.3))) {
const text = `t ${next()}`
await put(acct, 't', () => ({ text }))
}
const cid = l.records.get('t') ?? l.records.get('a')
const r = rand()
const takedown_ = { applied: chance(0.6), ...(chance(0.5) ? { ref: `rec-${next()}` } : {}) }
if (r < 0.7) await updateStatus('updateSubjectStatus of a record', (side) => ({ subject: { $type: 'com.atproto.repo.strongRef', uri: uri(side), cid }, takedown: takedown_ }))
else if (r < 0.75) await updateStatus('updateSubjectStatus of a record it does not hold', (side) => ({ subject: { $type: 'com.atproto.repo.strongRef', uri: uri(side, 'never-made'), cid }, takedown: { applied: true } }))
else if (r < 0.8) await updateStatus('updateSubjectStatus of a record by handle', () => ({ subject: { $type: 'com.atproto.repo.strongRef', uri: `at://${acct.handle}/${C}/t`, cid }, takedown: { applied: true } }))
else if (r < 0.85) await updateStatus('updateSubjectStatus of a record of a DID no account has', () => ({ subject: { $type: 'com.atproto.repo.strongRef', uri: `at://${NOBODY}/${C}/t`, cid }, takedown: { applied: true } }))
else if (r < 0.9) await updateStatus('updateSubjectStatus deactivating a record', (side) => ({ subject: { $type: 'com.atproto.repo.strongRef', uri: uri(side), cid }, deactivated: { applied: true } }))
else if (st.active) {
const w = rand()
const text = `t ${next()}`
if (w < 0.4) await put(acct, 't', () => ({ text }))
else if (w < 0.8) {
await same('deleteRecord of a record taken down or not', `${R}deleteRecord`, (side) => ({ auth: own(acct)(side), body: { repo: did(side), collection: C, rkey: 't' } }))
l.records.delete('t')
} else await put(acct, 't', () => ({ text: 'the same again' }))
}
await getStatus('getSubjectStatus of a record', (side) => ({ uri: uri(side) }))
if (chance(0.2)) await getStatus('getSubjectStatus of a record it does not hold', (side) => ({ uri: uri(side, 'never-made') }))
if (chance(0.2)) await getStatus('getSubjectStatus of a record of a DID no account has', () => ({ uri: `at://${NOBODY}/${C}/t` }))
if (chance(0.2)) await getStatus('getSubjectStatus of a record and a DID', (side) => ({ uri: uri(side), did: did(side) }))
if (st.active) for (const [who, auth] of [['no token', () => undefined], ['its token', own(acct)]]) await same(`getRecord of a record taken down or not, with ${who}`, `${R}getRecord`, (side) => ({ params: { repo: did(side), collection: C, rkey: 't' }, auth: auth(side) }))
await same('listRecords with a record taken down or not', `${R}listRecords`, (side) => ({ params: { repo: did(side), collection: C }, auth: ADMIN }))
await same('sync getRecord of a record taken down or not', `${Y}getRecord`, (side) => ({ params: { did: did(side), collection: C, rkey: 't' }, auth: ADMIN }))
await same('describeRepo with a record taken down or not', `${R}describeRepo`, (side) => ({ params: { repo: did(side) } }))
}
/// A second blob of the account, referenced by record `c`, taken down or
/// lifted, read every way, referenced again and uploaded again
/// (spec/LIFECYCLE.md, "Records and blobs taken down").
async function blobTakedown(acct) {
const st = await stateOf(acct)
const l = lifeOf(acct)
const did = (side) => didOf(side, acct)
if (st.active && (!l.blob2 || chance(0.2))) {
const bytes = Buffer.from(`lifecycle second blob of ${acct.n} ${next()}`)
const b = await upload(acct, bytes)
if (b) {
l.blob2 = { cid: b.reference.ref.$link, bytes, refs: b }
await put(acct, 'c', (side) => ({ file: b[side.name] }))
}
}
if (!l.blob2) return
const cid = l.blob2.cid
const r = rand()
const takedown_ = { applied: chance(0.6), ...(chance(0.5) ? { ref: `blob-${next()}` } : {}) }
if (r < 0.75) await updateStatus('updateSubjectStatus of a blob', (side) => ({ subject: { $type: `${A}defs#repoBlobRef`, did: did(side), cid }, takedown: takedown_ }))
else if (r < 0.82) await updateStatus('updateSubjectStatus of a blob it does not hold', (side) => ({ subject: { $type: `${A}defs#repoBlobRef`, did: did(side), cid: 'bafkreickmc7x2s6b4scxithx5diimbjeoux4uhheemy347cdt7jdaq7rke' }, takedown: { applied: true } }))
else if (r < 0.88) await updateStatus('updateSubjectStatus of a blob of a DID no account has', () => ({ subject: { $type: `${A}defs#repoBlobRef`, did: NOBODY, cid }, takedown: { applied: true } }))
else if (r < 0.92) await updateStatus('updateSubjectStatus deactivating a blob', (side) => ({ subject: { $type: `${A}defs#repoBlobRef`, did: did(side), cid }, deactivated: { applied: true } }))
else if (st.active) {
// The record referencing it deleted: the blob, and its takedown, go.
await same('deleteRecord of the blob\'s record', `${R}deleteRecord`, (side) => ({ auth: own(acct)(side), body: { repo: did(side), collection: C, rkey: 'c' } }))
l.records.delete('c')
}
await getStatus('getSubjectStatus of a blob', (side) => ({ did: did(side), blob: cid }))
if (chance(0.2)) await getStatus('getSubjectStatus of a blob of a DID no account has', () => ({ did: NOBODY, blob: cid }))
if (chance(0.2)) await getStatus('getSubjectStatus of a blob, a record and a DID', (side) => ({ did: did(side), blob: cid, uri: `at://${did(side)}/${C}/a` }))
for (const [who, auth] of [['no token', () => undefined], ['its token', own(acct)], ['the admin', () => ADMIN]]) await same(`getBlob of a blob taken down or not, with ${who}`, `${Y}getBlob`, (side) => ({ params: { did: did(side), cid }, auth: auth(side) }))
await same('listBlobs with a blob taken down or not', `${Y}listBlobs`, (side) => ({ params: { did: did(side) }, auth: ADMIN }))
const key = `d${next()}`
if (st.active && chance(0.5)) await same('createRecord referencing a blob taken down or not', `${R}createRecord`, (side) => ({ auth: own(acct)(side), body: { repo: did(side), collection: C, rkey: key, record: { $type: C, file: l.blob2.refs[side.name] } } }))
if (!st.takendown && !st.deleted && chance(0.5)) await same('uploadBlob of a blob taken down or not', `${R}uploadBlob`, (side) => ({ auth: own(acct)(side), body: l.blob2.bytes, type: 'text/plain' }))
}
/// deleteAccount refused, each of its checks in turn (spec/LIFECYCLE.md,
/// "deleteAccount").
async function deleteErrors(acct) {
const l = lifeOf(acct)
const did = (side) => didOf(side, acct)
const token = (side) => l[side.name].mailed ?? 'AAAAA-BBBBB'
const forms = [
['a DID that does not parse', () => ({ did: 'nope', password: acct.password, token: 'x' })],
['no token', (side) => ({ did: did(side), password: acct.password })],
['a password of 513', (side) => ({ did: did(side), password: 'x'.repeat(513), token: token(side) })],
['a password of 513 and a DID no account has', () => ({ did: NOBODY, password: 'x'.repeat(513), token: 'x' })],
['a password of 512', (side) => ({ did: did(side), password: 'x'.repeat(512), token: token(side) })],
['a DID no account has', () => ({ did: NOBODY, password: acct.password, token: 'AAAAA-BBBBB' })],
['a wrong password', (side) => ({ did: did(side), password: 'not-it', token: token(side) })],
['an app password', (side) => ({ did: did(side), password: l.appPassword[side.name], token: token(side) })],
['a token never mailed', (side) => ({ did: did(side), password: acct.password, token: 'AAAAA-BBBBB' })],
['an empty token', (side) => ({ did: did(side), password: acct.password, token: '' })],
['its token with a space', (side) => ({ did: did(side), password: acct.password, token: ` ${token(side)}` })],
['a field it does not name and a DID no account has', () => ({ did: NOBODY, password: 'x', token: 'x', extra: 1 })],
]
const [how, body] = pick(forms)
const auth = chance(0.2) ? bearer(FORGED) : undefined
await same(`deleteAccount with ${how}`, `${S}deleteAccount`, (side) => ({ body: body(side), auth }))
}
/// The account deleted, with its token, after the ways its token is wrong;
/// then everything about it read again, and the firehose replayed
/// (spec/LIFECYCLE.md, "deleteAccount").
async function deletion(acct) {
const l = lifeOf(acct)
const did = (side) => didOf(side, acct)
const st = await stateOf(acct)
if (!st.takendown || !l.reference.mailed) {
await same('requestAccountDelete', `${S}requestAccountDelete`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
await noteMail(acct, 'requestAccountDelete')
}
// A password reset asked for before the deletion: its token is gone with
// the account.
if (chance(0.5)) {
await same('requestPasswordReset before a deletion', `${S}requestPasswordReset`, { body: { email: acct.email } })
await noteMail(acct, 'requestPasswordReset before a deletion')
}
if (!l.reference.mailed || !l.server.mailed) return
if (chance(0.4)) {
const old = { reference: l.reference.mailed, server: l.server.mailed }
await same('requestAccountDelete again', `${S}requestAccountDelete`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
await noteMail(acct, 'requestAccountDelete again')
await same('deleteAccount with the token it replaced', `${S}deleteAccount`, (side) => ({ body: { did: did(side), password: acct.password, token: old[side.name] } }))
}
const alive = accounts.filter((a) => a !== acct && !(life.get(a)?.gone))
const other = pick(alive)
const otherState = await stateOf(other)
if (chance(0.4) && !otherState.takendown) {
await same('requestAccountDelete of another account', `${S}requestAccountDelete`, (side) => ({ auth: own(other)(side), method: 'POST' }))
await noteMail(other, 'requestAccountDelete of another account')
if (lifeOf(other).reference.mailed) await same('deleteAccount with another account\'s token', `${S}deleteAccount`, (side) => ({ body: { did: did(side), password: acct.password, token: lifeOf(other)[side.name].mailed } }))
}
if (chance(0.3)) await same('deleteAccount with the other side\'s token', `${S}deleteAccount`, (side) => ({ body: { did: did(side), password: acct.password, token: l[side === ref ? 'server' : 'reference'].mailed } }))
await same('deleteAccount with its token and a wrong password', `${S}deleteAccount`, (side) => ({ body: { did: did(side), password: 'not-it', token: l[side.name].mailed } }))
await checkEvents('deleteAccount refused')
const lower = chance(0.3)
await same(`deleteAccount${lower ? ' with its token in lower case' : ''}`, `${S}deleteAccount`, (side) => ({ body: { did: did(side), password: acct.password, token: lower ? l[side.name].mailed.toLowerCase() : l[side.name].mailed } }))
await same('deleteAccount again', `${S}deleteAccount`, (side) => ({ body: { did: did(side), password: acct.password, token: l[side.name].mailed } }))
l.gone = true
acct.deleted = true
await checkEvents('deleteAccount')
await matrix(acct, true)
await checkEvents(`what a deleted account answers`)
await checkReplay('deleteAccount')
// Its activation: the body first, then its DID document as validDid reads
// it, and only then its key, which is gone (spec/LIFECYCLE.md).
await same('activateAccount with a body, deleted', `${S}activateAccount`, (side) => ({ auth: own(acct)(side), body: {} }))
if (chance(0.5)) {
await moveDid(acct, () => 'https://elsewhere.example.com')
await same('activateAccount of a deleted account with its DID elsewhere', `${S}activateAccount`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
await updateStatus('updateSubjectStatus activating a deleted account with its DID elsewhere', (side) => ({ subject: repoRef(did(side)), deactivated: { applied: false } }))
await moveDid(acct, (side) => side.publicUrl)
}
if (l.reference.reset && l.server.reset) await same('resetPassword with a token mailed before the deletion', `${S}resetPassword`, (side) => ({ body: { token: l[side.name].reset, password: 'a-new-password' } }))
// Its handle and email are free.
if (chance(0.25)) {
const [a, b] = await same('updateHandle to a deleted account\'s handle', `${I}updateHandle`, (side) => ({ auth: own(other)(side), body: { handle: acct.handle } }))
if (a.status === 200 && b.status === 200) {
other.handle = acct.handle
for (const side of sides()) other.handles[side.name].push(acct.handle)
// The reference keeps the document it resolved for an hour
// (spec/IDENTITY.md, "describeRepo's didDoc and handleIsCorrect"):
// have it resolve the new one, as identity.mjs does.
await net.pds.ctx.idResolver.did.resolve(didOf(ref, other), true)
}
} else if (chance(0.6)) await sameHandleAccount(acct)
}
/// A new account made with a deleted account's handle and email
/// (spec/LIFECYCLE.md, "States").
async function sameHandleAccount(gone) {
const body = { handle: gone.handle, email: gone.email, password: `pw-${freshFront()}` }
const nsid = `${S}createAccount`
const [a, b] = await Promise.all(sides().map((side) => send(side, nsid, { body })))
const name = `<did:${accounts.length + 1}>`
const [na, nb] = [view(ref, a, nsid, [[a.body?.did, name]]), view(cand, b, nsid, [[b.body?.did, name]])]
if (!isDeepStrictEqual(na, nb)) await differ(`${nsid} with a deleted account's handle and email`, body, na, nb)
count('lifecycle: createAccount with a deleted account\'s handle and email', a.status === 200)
if (a.status !== 200 || b.status !== 200) return
const acct = addAccount({ handle: a.body.handle, email: body.email, password: body.password }, { reference: { did: a.body.did, token: a.body.accessJwt, refresh: a.body.refreshJwt }, server: { did: b.body.did, token: b.body.accessJwt, refresh: b.body.refreshJwt } })
await checkEvents('createAccount with a deleted account\'s handle')
await prepare(acct)
}
/// The account's DID moved by an operation each side signs with its
/// rotation key, its `atproto_pds` endpoint `to` (a function of the side).
async function moveDid(acct, to) {
for (const side of sides()) {
const did = didOf(side, acct)
const last = (await plcLog(did)).body.at(-1)
const { sig: _, ...fields } = last
const op = { ...fields, services: { ...fields.services, atproto_pds: { type: 'AtprotoPersonalDataServer', endpoint: to(side) } }, prev: (await cidForCbor(last)).toString() }
const signed = { ...op, sig: Buffer.from(await side.plcSigner.sign(cborEncode(op))).toString('base64url') }
const r = await fetch(`${plcUrl}/${did}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(signed) })
if (!r.ok) await differ('an operation of the harness\'s own', { did: `<did:${acct.n}>` }, 'the directory takes it', { status: r.status, body: await r.text() }, 'the harness fails its own step')
}
await net.pds.ctx.idResolver.did.resolve(didOf(ref, acct), true)
}
/// The account's DID moved to another server, then back:
/// checkAccountStatus's `validDid`, and activation refused while it is
/// away. Or, now and then, the PLC directory down for both sides instead
/// (spec/LIFECYCLE.md, "checkAccountStatus", "activateAccount").
async function validDid(acct) {
const st = await stateOf(acct)
if (st.deleted) return
if (chance(0.4)) return directoryDown(acct, st)
await moveDid(acct, () => 'https://elsewhere.example.com')
await same('checkAccountStatus with its DID elsewhere', `${S}checkAccountStatus`, (side) => ({ auth: own(acct)(side) }))
await same('deactivateAccount with its DID elsewhere', `${S}deactivateAccount`, (side) => ({ auth: own(acct)(side), body: {} }))
await same('activateAccount with its DID elsewhere', `${S}activateAccount`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
// The takedown's lift stays, with its #account, though the activation is
// refused (spec/LIFECYCLE.md).
const both = chance(0.5)
await updateStatus(`updateSubjectStatus activating${both ? ' and lifting a takedown' : ''} with its DID elsewhere`, (side) => ({ subject: repoRef(didOf(side, acct)), ...(both ? { takedown: { applied: false } } : {}), deactivated: { applied: false } }))
await getStatus('getSubjectStatus with its DID elsewhere', (side) => ({ did: didOf(side, acct) }))
await checkEvents('activation with its DID elsewhere')
await moveDid(acct, (side) => side.publicUrl)
await same('checkAccountStatus with its DID back', `${S}checkAccountStatus`, (side) => ({ auth: own(acct)(side) }))
if (!st.deactivated) await same('activateAccount with its DID back', `${S}activateAccount`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
}
/// The PLC directory down for both sides, closing each connection or
/// answering `500`: the server's requests to it go through the harness's
/// proxy, and the reference's PLC client is pointed at the proxy while it
/// is down. `validDid` is then `false`, and an activation `502
/// UpstreamFailure`, a takedown lifted in the same call staying lifted
/// (spec/LIFECYCLE.md).
async function directoryDown(acct, st) {
const mode = pick(['refuse', 'error'])
const how = mode === 'refuse' ? 'unreachable' : 'failing'
const client = net.pds.ctx.plcClient
const was = client.url
client.url = plcProxy.url
plcProxy.down = mode
try {
await same(`checkAccountStatus with the directory ${how}`, `${S}checkAccountStatus`, (side) => ({ auth: own(acct)(side) }))
if (chance(0.3)) await same(`deactivateAccount with the directory ${how}`, `${S}deactivateAccount`, (side) => ({ auth: own(acct)(side), body: {} }))
await same(`activateAccount with the directory ${how}`, `${S}activateAccount`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
const both = st.takendown && chance(0.5)
await updateStatus(`updateSubjectStatus activating${both ? ' and lifting a takedown' : ''} with the directory ${how}`, (side) => ({ subject: repoRef(didOf(side, acct)), ...(both ? { takedown: { applied: false } } : {}), deactivated: { applied: false } }))
if (chance(0.3)) await updateStatus(`updateSubjectStatus activating a DID no account has with the directory ${how}`, () => ({ subject: repoRef(NOBODY), deactivated: { applied: false } }))
} finally {
plcProxy.down = null
client.url = was
}
await getStatus(`getSubjectStatus after the directory was ${how}`, (side) => ({ did: didOf(side, acct) }))
}
/// A password reset of the account, in whatever state it is, with its own
/// password again: the server ends its OAuth sessions and signs it out of
/// its devices where the reference does not (spec/EMAIL.md, "resetPassword",
/// "What the reference does that the server does not copy"), so both sides'
/// are dropped after: its OAuth session, its device for silent sign-in, and
/// the browsers checks/oauth.mjs signs it in with. Its passkey is removed,
/// on the server alone, and a new one added (spec/PASSKEYS.md, "The
/// account's states").
async function passwordReset(acct) {
const l = lifeOf(acct)
for (const side of sides()) l[side.name].reset = null
await same('requestPasswordReset', `${S}requestPasswordReset`, { body: { email: acct.email } })
await noteMail(acct, 'requestPasswordReset')
if (!l.reference.reset || !l.server.reset) return
const [a, b] = await same('resetPassword', `${S}resetPassword`, (side) => ({ body: { token: l[side.name].reset, password: acct.password } }))
for (const side of sides()) l[side.name].reset = null
l.oauth = null
l.device = null
acct.oauth = undefined
if (a.status !== 200 || b.status !== 200 || !l.passkey) return
const gone = b64u(l.passkey.id)
const listed = await send(cand, '_passkeyList', { auth: own(acct)(cand) })
if (!Array.isArray(listed.body?.passkeys) || listed.body.passkeys.length) await differ('_passkeyList after resetPassword', { token: `<token of did:${acct.n}>`, removed: gone }, { status: 200, body: { passkeys: [] } }, listed, 'spec/PASSKEYS.md, "The account\'s states"')
await lifePasskey(acct)
}
/// A takendown session made, its tokens checked and used (spec/LIFECYCLE.md,
/// "Sessions of a taken-down account").
async function takendownLogin(acct) {
const st = await stateOf(acct)
if (!st.takendown) return takedown(acct)
const byHandle = chance(0.5)
const s = await login('createSession with allowTakendown', (side) => ({ identifier: byHandle ? acct.handle : didOf(side, acct), password: acct.password, allowTakendown: true }))
if (s) await takendownSession(acct, s, 'createSession with allowTakendown')
await tdCalls(acct, 'taken down', (xs) => xs)
for (const [name, nsid, params] of SYNC_READS(acct)) await same(`${name} with a takendown session`, nsid, (side) => ({ params: params(side), auth: bearer(lifeOf(acct)[side.name].td.access) }))
}
// ---- Crashes -------------------------------------------------------------------------
/// The server killed, sometimes with a change on its way, and started
/// again: every account's state, what it answers, and the firehose, against
/// the reference, which was not killed (spec/LIFECYCLE.md, "Durability").
async function crash() {
const alive = accounts.filter((a) => !(life.get(a)?.gone))
const acct = pick(alive)
const st = await stateOf(acct)
let inFlight = null
const r = rand()
if (r < 0.25 && st.active) inFlight = { what: 'deactivateAccount', nsid: `${S}deactivateAccount`, req: (side) => ({ auth: own(acct)(side), body: {} }), landed: (s) => s.deactivated }
else if (r < 0.5 && !st.takendown) inFlight = { what: 'a takedown', nsid: `${A}updateSubjectStatus`, req: (side) => ({ auth: ADMIN, body: { subject: repoRef(didOf(side, acct)), takedown: { applied: true, ref: 'crash' } } }), landed: (s) => s.takendown }
else if (r < 0.65 && alive.length > 3 && !st.takendown) {
await same('requestAccountDelete before a crash', `${S}requestAccountDelete`, (side) => ({ auth: own(acct)(side), method: 'POST' }))
await noteMail(acct, 'requestAccountDelete before a crash')
const l = lifeOf(acct)
inFlight = { what: 'deleteAccount', nsid: `${S}deleteAccount`, req: (side) => ({ body: { did: didOf(side, acct), password: acct.password, token: l[side.name].mailed } }), landed: (s) => s.deleted, after: () => (l.gone = acct.deleted = true) }
}
let sent = null
if (inFlight) {
await send(ref, inFlight.nsid, inFlight.req(ref))
sent = send(cand, inFlight.nsid, inFlight.req(cand))
await wait(below(8))
}
await server.kill()
server.start()
await waitReady(server, cand.url)
const last = live.server.findLast((e) => e.body.seq !== undefined)?.body.seq ?? 0
liveSub.server = subscribe(cand, last, live.server)
if (inFlight) {
await sent
const got = await send(cand, `${A}getSubjectStatus`, { params: { did: didOf(cand, acct) }, auth: ADMIN })
const now = got.status === 200 ? { deleted: false, deactivated: !!got.body.deactivated?.applied, takendown: !!got.body.takedown?.applied } : { deleted: true }
// Not landed: it is made again, which must then land.
if (!inFlight.landed(now)) await send(cand, inFlight.nsid, inFlight.req(cand))
inFlight.after?.()
count(`lifecycle: crash with ${inFlight.what} on its way`, true)
} else count('lifecycle: crash', true)
await checkEvents(`a restart${inFlight ? ` with ${inFlight.what} on its way` : ''}`)
for (const a of accounts) await getStatus('getSubjectStatus after a restart', (side) => ({ did: didOf(side, a) }))
for (const a of accounts) await matrix(a, false)
await checkEvents('what the accounts answer after a restart')
await checkReplay('a restart')
}
// ---- The run ---------------------------------------------------------------------------
const CHANGES = [
[deactivate, 8], [activate, 8], [takedown, 5], [lift, 5], [adminDeactivation, 4], [adminOdd, 4], [adminAuth, 2],
[recordTakedown, 5], [blobTakedown, 5], [deleteErrors, 3], [deletion, 4], [validDid, 3], [takendownLogin, 3], [passwordReset, 2], [oddSubjects, 2],
]
/// The changes a request approved before may be exchanged after: those
/// whose ending of it the harness knows (spec/LIFECYCLE.md, "OAuth
/// sessions"), by the account's state after or by `ends`.
const APPROVED_BEFORE = new Set([deactivate, activate, takedown, lift, adminDeactivation, adminAuth, recordTakedown, blobTakedown, deleteErrors, deletion])
/// The lifecycle run (spec/LIFECYCLE.md): both firehoses from the start,
/// accounts prepared, then each step a change to an account, its events,
/// and every method for it.
export async function lifecycleRun() {
for (const side of sides()) liveSub[side.name] = subscribe(side, 0, live[side.name])
for (let i = 0; i < 5; i++) await newAccount()
await checkEvents('the first accounts')
for (const acct of accounts) await matrix(acct, true)
await checkEvents('what the first accounts answer')
const total = CHANGES.reduce((n, [, w]) => n + w, 0)
for (let step = 1; step <= LIFECYCLE_STEPS; step++) {
run.step = step
const alive = accounts.filter((a) => !(life.get(a)?.gone))
if (alive.length < 4) await newAccount()
const acct = pick(accounts.filter((a) => !(life.get(a)?.gone)))
let r = below(total)
let change = CHANGES[0][0]
for (const [fn, w] of CHANGES) if ((r -= w) < 0) {
change = fn
break
}
if (change === deletion && alive.length <= 3) change = deleteErrors
if (APPROVED_BEFORE.has(change) && chance(0.4) && (await stateOf(acct)).active) await pendingApproval(acct)
await change(acct)
await exchangePending(acct, change.name)
await checkEvents(change.name)
if (!lifeOf(acct).gone) await matrix(acct, true)
if (chance(0.3)) await matrix(pick(accounts), false)
await checkEvents(`what the accounts answer after ${change.name}`)
if (CRASH_EVERY && step % CRASH_EVERY === 0) await crash()
if (step % 10 === 0) console.error(`${step} lifecycle steps agree`)
}
await checkReplay('the end')
}