68 lines · 4.2 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import blind from "../../../crates/cotton-conformance/vectors/seal/blind.json";import keys from "../../../crates/cotton-conformance/vectors/seal/keys.json";import row from "../../../crates/cotton-conformance/vectors/seal/row.json";import { bodyAad, bucket, blindKey, dekAad, exactToken, normalise, openRow, queryTokens, SealError, tableKey, textTokens, words } from "../src/seal.ts";const hex = (h: string): Uint8Array => Uint8Array.from(h.match(/../g)!.map((b) => parseInt(b, 16)));const toHex = (b: Uint8Array): string => [...b].map((x) => x.toString(16).padStart(2, "0")).join("");async function refusedAs(p: Promise<unknown>): Promise<string> { try { await p; } catch (error) { expect(error).toBeInstanceOf(SealError); return (error as SealError).code; } throw new Error("it opened");}/** RULE (sealing plan §2, §4): the object's twin reads the Rust core's vectors exactly, and opens nothing it should not. */describe("the TypeScript seal twin against the Rust vectors", () => { it("derives every table key and blind key", async () => { const dk = hex(keys.dk_hex); for (const t of keys.table_keys) expect(toHex(await tableKey(dk, keys.database_id, t.table, t.v)), `${t.table} v${t.v}`).toBe(t.tk_hex); for (const b of keys.blind_keys) expect(toHex(await blindKey(dk, keys.database_id, b.table)), b.table).toBe(b.bk_hex); }); it("opens the stored row to the row it sealed, without the blobs, and pads as the core does", async () => { expect(bodyAad(row.database_id, row.table, row.id)).toBe(row.body_aad); expect(dekAad(row.database_id, row.table, row.id, row.seal_v)).toBe(row.dek_aad); for (const [len, padded] of row.buckets) expect(bucket(len!), `bucket(${len})`).toBe(padded); expect(bucket(new TextEncoder().encode(row.sealed_plaintext).length)).toBe(row.padded_len); const tk = await tableKey(hex(row.dk_hex), row.database_id, row.table, row.seal_v); expect(await openRow(tk, row.database_id, row.table, row.id, row.stored)).toEqual(row.row); }); it("refuses another row, database, table or version, a tampered blob, a missing key and a missing column, each by name", async () => { const dk = hex(row.dk_hex); for (const r of row.refusals) { const tk = await tableKey(dk, r.database_id, r.table, r.seal_v); expect(await refusedAs(openRow(tk, r.database_id, r.table, r.id, { ...row.stored, seal_v: r.seal_v })), r.what).toBe("seal_does_not_open"); } const tk = await tableKey(dk, row.database_id, row.table, row.seal_v); const body = row.stored.sealed_enc; const flipped = body.slice(0, -2) + (body.endsWith("A") ? "B" : "A") + body.slice(-1); expect(await refusedAs(openRow(tk, row.database_id, row.table, row.id, { ...row.stored, sealed_enc: flipped }))).toBe("seal_does_not_open"); expect(await refusedAs(openRow(null, row.database_id, row.table, row.id, row.stored))).toBe("seal_key_missing"); const { dek_enc: _gone, ...noDek } = row.stored; expect(await refusedAs(openRow(tk, row.database_id, row.table, row.id, noDek))).toBe("seal_column_missing"); expect(await refusedAs(openRow(tk, row.database_id, row.table, row.id, { ...row.stored, seal_v: undefined }))).toBe("seal_column_missing"); // A clear column the blob also holds is a clash, never one silently over the other. expect(await refusedAs(openRow(tk, row.database_id, row.table, row.id, { ...row.stored, body: "clear" }))).toBe("seal_column_clash"); expect(await refusedAs(openRow(tk, row.database_id, "a|b", row.id, row.stored))).toBe("seal_bad_input"); }); it("normalises, cuts words and makes the row, query and exact tokens", async () => { const bk = hex(blind.bk_hex); for (const t of blind.texts) { expect(normalise(t.text), t.text).toBe(t.normalised); expect(words(t.text), t.text).toEqual(t.words); expect([...(await textTokens(bk, t.column, t.text))].sort(), t.text).toEqual([...t.tokens].sort()); } for (const q of blind.queries) expect(await queryTokens(bk, q.column, q.query), q.query).toEqual(q.tokens); for (const x of blind.exact) expect(await exactToken(bk, x.column, x.value), x.value).toBe(x.token); expect(await exactToken(bk, "external_id", " ")).toBeNull(); });});