93 lines · 6.8 KB · TypeScriptRaw
import { env, runInDurableObject } from "cloudflare:test";import { beforeAll, describe, expect, it, vi } from "vitest";import { Store } from "../src/store.ts";import * as vault from "../src/vault.ts";import { ADMIN, http, mint, personalObject, put } from "./personal.ts";/** * RULE (sealing plan §7 step 6): the refresh tokens the * Worker uses itself (Google Calendar, Spotify) are sealed under a Worker * secret named for what it is, SERVICE_TOKEN_KEY (t1), never VAULT_KEY (k1). * A one-time admin rekey moves the grants an earlier Worker sealed under k1, * and leaves every other secret (a saved password) exactly as it was. * Synthetic tokens only. */describe("service tokens under SERVICE_TOKEN_KEY", () => { const slug = "service-token"; let credential = ""; const google = "11111111-aaaa-4aaa-8aaa-000000000001"; const spotify = "11111111-aaaa-4aaa-8aaa-000000000002"; const password = "11111111-aaaa-4aaa-8aaa-000000000003"; const payload = (key: string) => JSON.stringify({ kind: "api_key", key }); let passwordSealed = ""; beforeAll(async () => { // The pass a revived account's alarm wakes never reaches Google or Spotify: every call out is refused here. vi.spyOn(globalThis, "fetch").mockImplementation(async () => new Response("{}", { status: 503 })); ({ credential } = await personalObject(slug)); passwordSealed = await vault.seal(env, password, payload("synthetic-password")); // As an earlier Worker kept them: every value under VAULT_KEY. await put(slug, [ { table: "secret", id: google, data: { name: "google.someone@example.com", service: "google", kind: "api_key", hint: "oken", sealed: await vault.seal(env, google, payload("synthetic-google-token")) } }, { table: "secret", id: spotify, data: { name: "spotify.someone", service: "spotify", kind: "api_key", hint: "oken", sealed: await vault.seal(env, spotify, payload("synthetic-spotify-token")) } }, { table: "secret", id: password, data: { name: "a site", service: "example.com", kind: "login", hint: "word", sealed: passwordSealed } }, ]); // A pass that ran between the deploy and the rekey: neither k1 grant opened, so both accounts stopped asking to connect again. const stopped = { code: "reconnect_required", message: "the grant is not in the vault any more: connect Google again", at: "2026-11-11T00:00:00.000Z" }; await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (_instance, state) => { const store = new Store(state.storage.sql, () => new Date()); store.setMeta("google_accounts", JSON.stringify([{ email: "someone@example.com", secret: google, connected_at: stopped.at, timezone: null, phase: "reconnect", sync_token: null, page_token: null, window: null, synced_at: null, last_pass_at: stopped.at, last_error: stopped, pulled: { created: 0, updated: 0, binned: 0 } }])); store.setMeta("spotify_account", JSON.stringify({ user_id: "someone", display_name: null, secret: spotify, connected_at: stopped.at, phase: "reconnect", step: null, cursors: {}, cycle_started_at: null, synced_at: "2026-11-10T00:00:00.000Z", last_pass_at: stopped.at, last_error: { ...stopped, message: "the grant is not in the vault any more: connect Spotify again" }, retry_at: null, shows: [] })); }); }, 60_000); const sealedOf = (id: string) => runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (_instance, state) => String(new Store(state.storage.sql, () => new Date()).get("secret", id)!.data.sealed)); it("keeps t1 and k1 apart: neither lock opens the other's value, nor another row's", async () => { const t1 = await vault.sealServiceToken(env, google, "x"); expect(t1).toMatch(/^t1:/); expect(await vault.openServiceToken(env, google, t1)).toBe("x"); expect(await vault.openServiceToken(env, spotify, t1)).toBeNull(); expect(await vault.open(env, google, t1)).toBeNull(); expect(await vault.openServiceToken(env, google, await vault.seal(env, google, "x"))).toBeNull(); expect(await vault.sealServiceToken({ ...env, SERVICE_TOKEN_KEY: undefined }, google, "x").catch((e: Error) => e.message)).toContain("SERVICE_TOKEN_KEY"); }); it("rekeys each Google and Spotify grant from k1 to t1 once, takes only the admin token, pulls again an account a pass stopped meanwhile, and never touches a saved password", async () => { const token = await mint(slug, credential, "assistant", ["owner"]); expect((await http(slug, "POST", "/v1/admin/service-tokens/rekey", undefined, { authorization: `Bearer ${token}` })).status).toBe(403); const first = await http(slug, "POST", "/v1/admin/service-tokens/rekey", undefined, ADMIN); expect(first.status, JSON.stringify(first.body)).toBe(200); expect(first.body).toEqual({ rekeyed: 2, already: 0, unopened: 0, revived: 2 }); const phases = await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (_instance, state) => { const store = new Store(state.storage.sql, () => new Date()); return [JSON.parse(store.meta("google_accounts")!)[0], JSON.parse(store.meta("spotify_account")!)].map((a: { phase: string }) => a.phase); }); // The woken pass may already have run (and been refused at the stubbed service), so only the stop is held to. expect(phases.every((p) => p !== "reconnect"), `both accounts pull again: ${phases}`).toBe(true); for (const [id, key] of [[google, "synthetic-google-token"], [spotify, "synthetic-spotify-token"]] as const) { const sealed = await sealedOf(id); expect(sealed).toMatch(/^t1:/); expect(await vault.openServiceToken(env, id, sealed)).toBe(payload(key)); } expect(await sealedOf(password), "a saved password stays as it was").toBe(passwordSealed); const again = await http(slug, "POST", "/v1/admin/service-tokens/rekey", undefined, ADMIN); expect(again.body).toEqual({ rekeyed: 0, already: 2, unopened: 0, revived: 0 }); expect(JSON.stringify([first.body, again.body])).not.toContain("synthetic"); });});/** RULE (sealing plan §7 step 6): the object no longer seals a plaintext it is handed, so an import that carries one is refused whole, and nothing of it is kept. */describe("k1 sealing on the admin import is removed", () => { it("refuses an import row carrying a plaintext to seal, by name", async () => { const slug = "service-token-import"; await personalObject(slug); const res = await http(slug, "POST", "/v1/admin/import", { rows: [{ table: "secret", id: "s-1", data: { name: "a site", kind: "login" }, seal: { column: "sealed", plaintext: "synthetic-plaintext" } }] }, ADMIN); expect(res.status).toBe(400); expect(res.body).toMatchObject({ code: "no_plaintext_seal" }); const exported = await http(slug, "GET", "/v1/admin/export", undefined, ADMIN); expect(JSON.stringify(exported.body)).not.toContain("synthetic-plaintext"); });});