91 lines · 6.4 KB · TypeScriptRaw
/** * AVATAR SEAL 4 (coordinator, 27 Nov 2026): an assistant's token and the person's sealed-kind files, for EVERY kind a * device seals (`DEVICE_SEALED_KINDS`, generated from the list, so a kind added later is held to the same rule). * - No write of any kind: PUT, a parts upload, DELETE of a file or a folder: 403, the same whether the file is held. * - A read (GET, HEAD, a stream link, "which are missing") of a kind no sealed table opens: 403 sealed_kind, always. * - A read of a kind a sealed table opens (messages, people): only inside that table's window; outside one 403 * window_needed (inside one: window.test.ts "get_media on a person's face"). * - Every answer is the same for a file held and a file absent: no existence oracle. * The owner's device does all of it as before. Synthetic bytes only. */import { SELF } from "cloudflare:test";import { describe, expect, it } from "vitest";import { DEVICE_SEALED_KINDS, tableOfFileKind } from "../src/table_files.ts";import { DATABASE_ID, http, personalObject, PHOTOS_OPEN, tool } from "./personal.ts";const BYTES = new TextEncoder().encode("\xFF\xD8\xFF synthetic bytes of a person's file");/** A key of `kind` as its writers lay it, and its type: a book's or a song's is `<db>/<kind>/<sha>.<ext>`, any other a file in a folder. */function shaped(kind: string, sha: string, folder: string): { key: string; type: string } { if (kind === "books") return { key: `${DATABASE_ID}/books/${sha}.epub`, type: "application/epub+zip" }; if (kind === "music") return { key: `${DATABASE_ID}/music/${sha}.mp3`, type: "audio/mpeg" }; return { key: `${DATABASE_ID}/${kind}/${folder}/${sha}.jpg`, type: "image/jpeg" };}async function call(slug: string, method: string, key: string, bearer: string, query = "", body?: Uint8Array, type = "image/jpeg"): Promise<{ status: number; code: string | null }> { const res = await SELF.fetch(`https://example.test/b/${slug}/v1/media/${key}${query}`, { method, headers: { authorization: `Bearer ${bearer}`, ...(body ? { "content-type": type, "content-length": String(body.length) } : query === "?uploads" ? { "content-type": "application/json" } : {}) }, ...(body ? { body } : query === "?uploads" ? { body: JSON.stringify({ size: 6 * 1024 * 1024 }) } : {}), }); const text = method === "HEAD" ? "" : await res.text(); let code: string | null = null; try { code = (JSON.parse(text) as { code?: string }).code ?? null; } catch { code = null; } return { status: res.status, code };}describe("an assistant's token and every kind of the person's sealed files", () => { it("holds every kind in DEVICE_SEALED_KINDS to the rule, held or absent alike, and the owner's device to none of it", async () => { const slug = "sealed-kinds-token"; const { credential, owner } = await personalObject(slug); expect(DEVICE_SEALED_KINDS.length).toBeGreaterThanOrEqual(9); for (const kind of DEVICE_SEALED_KINDS) { const { key: held, type } = shaped(kind, "ab".repeat(32), "f-held"); const { key: absent } = shaped(kind, "cd".repeat(32), "f-absent"); expect((await call(slug, "PUT", held, credential, "", BYTES, type)).status, `${kind}: the device puts`).toBe(201); const readRefusal = tableOfFileKind(kind) ? { status: 403, code: "window_needed" } : { status: 403, code: "sealed_kind" }; for (const key of [held, absent]) { const at = `${kind} ${key === held ? "held" : "absent"}`; // Reads: the same refusal, held or absent. expect(await call(slug, "GET", key, owner), `${at} GET`).toEqual(readRefusal); expect((await call(slug, "HEAD", key, owner)).status, `${at} HEAD`).toBe(403); const stream = await http<{ code: string }>(slug, "POST", "/v1/media/stream", { key }, { authorization: `Bearer ${owner}` }); expect({ status: stream.status, code: stream.body.code }, `${at} stream`).toEqual(readRefusal); const missing = await http<{ code: string }>(slug, "POST", "/v1/media/missing", { keys: [key] }, { authorization: `Bearer ${owner}` }); expect([missing.status, missing.body.code], `${at} missing`).toEqual([403, "sealed_kind"]); // Writes: refused, held or absent. expect(await call(slug, "PUT", key, owner, "", BYTES, type), `${at} PUT`).toEqual({ status: 403, code: "sealed_kind" }); expect(await call(slug, "POST", key, owner, "?uploads"), `${at} parts`).toEqual({ status: 403, code: "sealed_kind" }); expect(await call(slug, "DELETE", key, owner), `${at} DELETE`).toEqual({ status: 403, code: "sealed_kind" }); } expect(await call(slug, "DELETE", `${DATABASE_ID}/${kind}/f-held/`, owner, "?prefix=1"), `${kind} folder DELETE`).toEqual({ status: 403, code: "sealed_kind" }); // Nothing the token tried moved anything: the device still finds the one file, and the absent one absent. expect((await call(slug, "GET", held, credential)).status, `${kind}: the device reads`).toBe(200); expect((await call(slug, "GET", absent, credential)).status, `${kind}: never planted`).toBe(404); } }, 600_000); it("draws nothing of a kind no window opens through get_media, at a size or whole", async () => { const slug = "sealed-kinds-draw"; const { credential, owner } = await personalObject(slug, PHOTOS_OPEN); const key = `${DATABASE_ID}/photos/${"ef".repeat(32)}.png`; // A real 32×24 PNG, held plain from before, named by a photo row. const png = Uint8Array.from(atob("iVBORw0KGgoAAAANSUhEUgAAACAAAAAYCAIAAAAUMWhjAAAAJElEQVR4nGM4EaBBU8QwasGoBaMWjFowasGoBaMWjFowNCwAAOj5wC7/fbJHAAAAAElFTkSuQmCC"), (c) => c.charCodeAt(0)); const put = await SELF.fetch(`https://example.test/b/${slug}/v1/media/${key}`, { method: "PUT", headers: { authorization: `Bearer ${credential}`, "content-type": "image/png", "content-length": String(png.length) }, body: png }); expect(put.status).toBe(201); const made = await http<{ made?: { id: string } }>(slug, "POST", "/v1/act", { act: "create", args: { table: "photo", data: { key, sha256: "ef".repeat(32), width: 32, height: 24 } } }, { authorization: `Bearer ${credential}` }); expect(made.status, JSON.stringify(made.body)).toBe(200); const id = made.body.made!.id; for (const args of [{ table: "photo", id }, { table: "photo", id, width: 16 }]) { const answer = await tool(slug, owner, "get_media", args); expect(answer.isError, JSON.stringify(args)).toBe(true); expect(answer.content[0]!.text).toMatch(/sealed files/); } });});