From 569d668141b3c76929d10ec5adfd4de40a2685a8 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 29 Sep 2026 02:04:34 +0200 Subject: [PATCH] TypeScript: refuse unknown store headers and extra record slots by name The store reader and the inspector share one header and record-slot rule; an unknown cultcache.store.* header no longer falls through to the legacy envelope decoder. Co-Authored-By: Claude Sonnet 5 --- .../cultcache-ts/src/cult-cache-inspector.ts | 9 ++-- .../single-file-messagepack-backing-store.ts | 6 +-- packages/cultcache-ts/src/store-format.ts | 33 ++++++++++++++ packages/cultcache-ts/test/cult-cache.test.ts | 45 +++++++++++++++++++ 4 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 packages/cultcache-ts/src/store-format.ts diff --git a/packages/cultcache-ts/src/cult-cache-inspector.ts b/packages/cultcache-ts/src/cult-cache-inspector.ts index 1301fe47..54e75700 100644 --- a/packages/cultcache-ts/src/cult-cache-inspector.ts +++ b/packages/cultcache-ts/src/cult-cache-inspector.ts @@ -1,5 +1,7 @@ import { decode } from "@msgpack/msgpack"; +import { STORE_FORMAT_VERSION, isV1Snapshot, requireV1RecordSlots } from "./store-format"; + export interface CultCacheInspection { filePath: string; fileSizeBytes: number; @@ -39,8 +41,6 @@ export interface InspectedRecord { payloadDecodeError?: string; } -const STORE_FORMAT_VERSION = "cultcache.store.v1"; - export function inspectCultCacheBytes( filePath: string, bytes: Uint8Array, @@ -197,6 +197,7 @@ function decodeV1Record(value: unknown, catalogBySchemaId: Map { return typeof value === "object" && value !== null; } diff --git a/packages/cultcache-ts/src/single-file-messagepack-backing-store.ts b/packages/cultcache-ts/src/single-file-messagepack-backing-store.ts index 4d2bb4f9..b7d9684c 100644 --- a/packages/cultcache-ts/src/single-file-messagepack-backing-store.ts +++ b/packages/cultcache-ts/src/single-file-messagepack-backing-store.ts @@ -4,6 +4,7 @@ import { dirname, resolve } from "node:path"; import { decode, encode } from "@msgpack/msgpack"; import { z } from "zod"; +import { STORE_FORMAT_VERSION, isV1Snapshot, requireV1RecordSlots } from "./store-format"; import type { CacheBackingStore, CultCacheEnvelope, @@ -12,8 +13,6 @@ import type { PushAllOptions, } from "./types"; -const STORE_FORMAT_VERSION = "cultcache.store.v1"; - const envelopeSchema = z.object({ key: z.string().min(1), type: z.string().min(1), @@ -285,7 +284,7 @@ function encodeCatalogMember(member: CultCacheSchemaCatalogMember): unknown[] { } function decodeSnapshot(decoded: unknown): DecodedSnapshot | undefined { - if (!Array.isArray(decoded) || decoded.length === 0 || decoded[0] !== STORE_FORMAT_VERSION) { + if (!isV1Snapshot(decoded)) { return undefined; } @@ -436,6 +435,7 @@ function decodeRecord(value: unknown): PersistedRecord { throw new Error("CultCache persisted records must be MessagePack arrays."); } + requireV1RecordSlots(value); const [key = "", schemaId = "", storedAt = "", payload = new Uint8Array()] = value; if (!isNonEmptyString(key) || !isNonEmptyString(schemaId) || !isNonEmptyString(storedAt)) { throw new Error("CultCache persisted records must declare key, schemaId, and storedAt."); diff --git a/packages/cultcache-ts/src/store-format.ts b/packages/cultcache-ts/src/store-format.ts new file mode 100644 index 00000000..02de60e7 --- /dev/null +++ b/packages/cultcache-ts/src/store-format.ts @@ -0,0 +1,33 @@ +export const STORE_FORMAT_VERSION = "cultcache.store.v1"; + +const STORE_FORMAT_PREFIX = "cultcache.store."; +const PERSISTED_RECORD_SLOTS = 4; + +/** + * True for a v1 snapshot, false for anything that is not a CultCache store header (the legacy + * envelope array). Any other `cultcache.store.*` header is refused by name. + */ +export function isV1Snapshot(decoded: unknown): decoded is unknown[] { + if (!Array.isArray(decoded) || typeof decoded[0] !== "string" || !decoded[0].startsWith(STORE_FORMAT_PREFIX)) { + return false; + } + + if (decoded[0] !== STORE_FORMAT_VERSION) { + throw new Error( + `CultCache store format "${decoded[0]}" is not readable; this runtime reads "${STORE_FORMAT_VERSION}" only. ` + + "The store needs a runtime that resolves document variants.", + ); + } + + return true; +} + +/** Refuses a persisted record with more slots than v1 defines, naming its key and schema id. */ +export function requireV1RecordSlots(record: unknown[]): void { + if (record.length > PERSISTED_RECORD_SLOTS) { + throw new Error( + `CultCache record "${String(record[0])}" (schema "${String(record[1])}") has ${record.length} slots; ` + + `this runtime reads ${PERSISTED_RECORD_SLOTS}. The store needs a runtime that resolves document variants.`, + ); + } +} diff --git a/packages/cultcache-ts/test/cult-cache.test.ts b/packages/cultcache-ts/test/cult-cache.test.ts index 5932b2f8..a743ba00 100644 --- a/packages/cultcache-ts/test/cult-cache.test.ts +++ b/packages/cultcache-ts/test/cult-cache.test.ts @@ -1413,3 +1413,48 @@ async function runJsonCommand( return JSON.parse(trimmed.split(/\r?\n/).at(-1) as string); } + +// Shared refusal vectors: tests/vectors/document-variants-c0, read by every runtime's tests. +const variantVectors = resolve(cultLibRoot, "tests", "vectors", "document-variants-c0"); +const itemSchemaId = "sha256:88d3fdf0a927acf3b163940d8f8c7fe62b3316542ce771a67ec8bc038f594788"; + +async function pullVector(name: string): Promise { + const dir = await mkdtemp(join(tmpdir(), "cultcache-vector-")); + const file = join(dir, "store.msgpack"); + await writeFile(file, await readFile(join(variantVectors, name))); + return new SingleFileMessagePackBackingStore(file).pullAll(); +} + +test("SingleFileMessagePackBackingStore refuses an unknown store header by name", async () => { + await assert.rejects(() => pullVector("unknown-header.msgpack"), /cultcache\.store\.v9/u); +}); + +test("SingleFileMessagePackBackingStore refuses an extra record slot naming the record", async () => { + await assert.rejects( + () => pullVector("extra-slot-full-payload.msgpack"), + (error: Error) => error.message.includes("item:anvil") && error.message.includes(itemSchemaId), + ); +}); + +test("SingleFileMessagePackBackingStore refuses a variant store by version or record", async () => { + await assert.rejects( + () => pullVector("variant-v2.msgpack"), + (error: Error) => error.message.includes("cultcache.store.v2") || error.message.includes("item:anvil-big"), + ); +}); + +test("CultCache inspector refuses the same vectors by name", async () => { + const inspect = async (name: string) => inspectCultCacheBytes(name, await readFile(join(variantVectors, name))); + await assert.rejects(() => inspect("unknown-header.msgpack"), /cultcache\.store\.v9/u); + await assert.rejects( + () => inspect("extra-slot-full-payload.msgpack"), + (error: Error) => error.message.includes("item:anvil") && error.message.includes(itemSchemaId), + ); +}); + +test("a v1 store written at the base commit still reads byte for byte", async () => { + const envelopes = await pullVector("v1-base.msgpack"); + assert.deepEqual(envelopes.map((entry) => [entry.key, entry.type]), [["alpha", "vectors.item"], ["beta", "vectors.item"]]); + assert.deepEqual([...envelopes[0]!.payload], [0x92, 0xa5, 0x61, 0x6c, 0x70, 0x68, 0x61, 0x01]); + assert.deepEqual([...envelopes[1]!.payload], [0x92, 0xa4, 0x62, 0x65, 0x74, 0x61, 0x02]); +});