/** * AVATAR SEAL 2 (5), coordinator 27 Nov 2026: an old build shown a sealed face draws a blank, not a crash. * * cd apps/business && pnpm -s exec tsx scripts/__tests__/upgrade-face.ts [] # default 41f5d29eca * * A fresh personal object under `wrangler dev` holds an invented face sealed as a new build seals it (f1 under the * person table's files root, `sealF1`, of a data key made here). The core at the OLD sha (the UPGRADE PROOF harness, * scripts/upgrade-proof/old-core.sh) connects and asks for the face as a picture is drawn, twice * (`upgrade_old_side_is_shown_a_sealed_face`). Asserted: the old core runs to the end (no panic), and never hands the * picture's bytes or an image: a refusal, or a file that starts as an f1 file, which no image decoder opens. The * shells draw a face that does not decode as the person's initials: Android's ChatPictures.load wraps its decode in * runCatching (unchanged since 41f5d29eca); iOS's ChatPicturesAhead guards CGImageSourceCreateWithURL and its * thumbnail, nil for it. Invented data only. */ import { createHash } from "node:crypto"; import { spawnSync, type ChildProcess } from "node:child_process"; import { mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { APP, check, startDevWorker, stop } from "./dev-worker.ts"; import { WS } from "./fixture.ts"; const REPO = resolve(APP, "../.."); // The Worker's own f1 sealing and file keys, loaded at run time: they are written against the Workers types, which the // scripts' typecheck does not carry (a static import pulled src/arrival.ts into it). type Sealing = { sealF1(fk: Uint8Array, prefix: Uint8Array, key: string, plain: Uint8Array): Promise; personFileKey(root: Uint8Array, key: string): Promise; tableFileRoot(dk: Uint8Array, databaseId: string, table: string): Promise; }; const from = (file: string): string => new URL(`../../src/${file}`, import.meta.url).href; const { sealF1 } = (await import(from("arrival_file.ts"))) as Pick; const { personFileKey, tableFileRoot } = (await import(from("table_files.ts"))) as Pick; const sha = process.argv[2] ?? "41f5d29eca"; const PICTURE = process.env.COTTON_E2E_PICTURE ?? ""; const tmp = mkdtempSync(join(tmpdir(), "upgrade-face-")); let wrangler: ChildProcess | null = null; try { check(PICTURE.length > 0, "COTTON_E2E_PICTURE names an invented picture"); const built = spawnSync(join(REPO, "scripts/upgrade-proof/old-core.sh"), [sha], { encoding: "utf8" }); check(built.status === 0, `the core at ${sha} builds\n${String(built.stderr).slice(-3000)}`); const oldCore = String(built.stdout).trim().split("\n").pop()!; const { base, credential } = await startDevWorker(tmp, WS, (child) => { wrangler = child; }); const picture = new Uint8Array(readFileSync(PICTURE)); const digest = createHash("sha256").update(picture).digest("hex"); const key = `${WS}/people/e2e-person/${digest}.jpg`; const dk = crypto.getRandomValues(new Uint8Array(32)); const parts = await sealF1(await personFileKey(await tableFileRoot(dk, WS, "person"), key), crypto.getRandomValues(new Uint8Array(7)), key, picture); const sealed = new Uint8Array(parts.reduce((n, p) => n + p.length, 0)); parts.reduce((o, p) => (sealed.set(p, o), o + p.length), 0); const put = await fetch(`${base}/v1/media/${key}`, { method: "PUT", headers: { authorization: `Bearer ${credential}`, "content-type": "application/vnd.cotton.f1", "content-length": String(sealed.length) }, body: sealed }); check(put.status === 201, `the sealed face is stored (${put.status})`); const container = join(tmp, "phone"); mkdirSync(container); const env = { ...process.env, COTTON_E2E_BASE: base, COTTON_E2E_CREDENTIAL: credential, COTTON_E2E_DATABASE: WS, UPGRADE_CONTAINER: container, UPGRADE_FACE_KEY: key }; const old = spawnSync(oldCore, ["phone::personal_object::upgrade_writer::upgrade_old_side_is_shown_a_sealed_face", "--exact", "--ignored", "--nocapture", "--test-threads=1"], { cwd: tmp, encoding: "utf8", env }); const out = `${String(old.stdout)}\n${String(old.stderr)}`; check(old.status === 0 && !out.includes("panicked at"), `the old core at ${sha} runs to the end, no panic\n${out.slice(-3000)}`); const said = /UPGRADE-FACE (\{.*\})/.exec(out); check(said !== null, "the old core says what it was handed"); const { asked } = JSON.parse(said![1]!) as { asked: { answer: string; bytes?: number; head?: string; code?: string }[] }; const IMAGE_HEADS = ["ffd8ff", "89504e47", "47494638", "52494646"]; for (const [i, one] of asked.entries()) { console.log(` old core, ask ${i + 1}: ${JSON.stringify(one)}`); if (one.answer === "path") { check(one.bytes !== picture.length || one.head !== Buffer.from(picture.slice(0, 4)).toString("hex"), `ask ${i + 1}: never the picture's bytes`); check(!IMAGE_HEADS.some((h) => (one.head ?? "").startsWith(h.slice(0, 6))), `ask ${i + 1}: not an image (starts ${one.head})`); check(one.head === Buffer.from("CTF1").toString("hex"), `ask ${i + 1}: the f1 file as the object holds it, which no decoder opens (starts ${one.head})`); } else { check(one.answer === "refused", `ask ${i + 1}: refused in words`); } } console.log(`upgrade face @${sha}: ok, the old core is handed no image and runs on (draws the initials)`); } catch (error) { console.error((error as Error).message); process.exitCode = 1; } finally { stop(wrangler); rmSync(tmp, { recursive: true, force: true }); }