ridge gates: read each committed plane's own bytes: WebP header dimensions against the manifest, bake id re-derived from the planes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Nibu 2026-11-14 09:25:49 +00:00
commit 7e99b4729d

View file

@ -1,5 +1,9 @@
// The committed baked set under public/ridge: what the baker wrote is what ships, within the budget the baker itself refuses on
// (export.py PAIR_BYTES_MAX, DECODE_BYTES_MAX). Reads with node fs; no decoder: plane sizes come from the manifest.
// (export.py PAIR_BYTES_MAX, DECODE_BYTES_MAX). Reads with node fs. The gate reads each plane's own bytes, not the manifest's claims about
// them: the WebP container is walked and its stated dimensions (VP8X canvas, VP8 / VP8L frame header) must equal the manifest's w and h,
// which the decode budget is computed from; and the bake id is re-derived from the plane bytes, as the baker derives it, so any
// corruption of a plane, same length or not, fails. No pixel decoder is in the tree; the baker's own check decodes pixels.
import { createHash } from 'node:crypto';
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
@ -41,6 +45,57 @@ export function sizeGate(m: WireManifest): string | undefined {
return undefined;
}
/**
* Why a buffer is not a well-formed WebP of `w` x `h`, or undefined. Checks the RIFF envelope (its size is the file's), that the chunks tile
* the file exactly, and the dimensions the container and the bitstream header each state.
*/
export function webpFault(b: Uint8Array, w: number, h: number): string | undefined {
const u32 = (o: number) => (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0;
const u24 = (o: number) => b[o] | (b[o + 1] << 8) | (b[o + 2] << 16);
const tag = (o: number) => String.fromCharCode(b[o], b[o + 1], b[o + 2], b[o + 3]);
if (b.length < 20 || tag(0) !== 'RIFF' || tag(8) !== 'WEBP') return 'not a RIFF WEBP';
if (u32(4) + 8 !== b.length) return `RIFF size ${u32(4)} does not fit a ${b.length} byte file`;
const dims: string[] = [];
let seen = 0;
let o = 12;
while (o < b.length) {
if (o + 8 > b.length) return 'a chunk header runs past the file';
const id = tag(o);
const len = u32(o + 4);
const body = o + 8;
if (body + len > b.length) return `chunk ${id} runs past the file`;
if (id === 'VP8X') dims.push(`${u24(body + 4) + 1}x${u24(body + 7) + 1}`);
if (id === 'VP8 ') {
if (b[body + 3] !== 0x9d || b[body + 4] !== 0x01 || b[body + 5] !== 0x2a)
return 'VP8 start code missing';
dims.push(
`${(b[body + 6] | (b[body + 7] << 8)) & 0x3fff}x${
(b[body + 8] | (b[body + 9] << 8)) & 0x3fff
}`
);
seen += 1;
}
if (id === 'VP8L') {
if (b[body] !== 0x2f) return 'VP8L signature missing';
const bits = u32(body + 1);
dims.push(`${(bits & 0x3fff) + 1}x${((bits >>> 14) & 0x3fff) + 1}`);
seen += 1;
}
o = body + len + (len & 1);
}
if (o !== b.length) return 'the chunks do not tile the file';
if (seen !== 1) return `${seen} bitstream chunks`;
const bad = dims.find((d) => d !== `${w}x${h}`);
return bad ? `states ${bad}, the manifest ${w}x${h}` : undefined;
}
/** The baker's bake id (export.py `bake_id`): first 12 hex of sha256 over the plane bytes, frames in FRAME_KEYS order, planes far, mid, near. */
export function bakeIdOf(planeBytes: (frame: string, plane: string) => Uint8Array): string {
const h = createHash('sha256');
FRAME_KEYS.forEach((k) => PLANE_NAMES.forEach((n) => h.update(planeBytes(k, n))));
return h.digest('hex').slice(0, 12);
}
const ROOT = join(process.cwd(), 'public', 'ridge');
const readJson = (p: string): unknown => JSON.parse(readFileSync(p, 'utf8'));
const index = parseIndex(readJson(join(ROOT, 'index.json')));
@ -67,6 +122,27 @@ describe('the committed ridge assets', () => {
});
});
it('decodes every plane as the WebP of the size the manifest states', () => {
raw.frames.flatMap(planes).forEach((p) => {
expect(
webpFault(readFileSync(join(ROOT, dir, p.file)), p.w, p.h),
p.file
).toBeUndefined();
});
});
it('holds the bytes its bake id was made from', () => {
const bytes = (frame: string, plane: string) =>
readFileSync(
join(
ROOT,
dir,
(raw.frames.find((f) => f.key === frame) as WireFrame).planes[plane].file
)
);
expect(bakeIdOf(bytes)).toBe(raw.bake_id);
});
it('keeps every adjacent pair of frames inside the fetch and decode budget', () => {
expect(sizeGate(raw)).toBeUndefined();
});
@ -97,6 +173,37 @@ describe('the committed ridge assets', () => {
});
});
describe('the plane gates refuse a plane that is not what the manifest says', () => {
const real = readFileSync(join(ROOT, Object.values(index.places)[0], 'noon-mid.webp'));
const manifest = readJson(
join(ROOT, Object.values(index.places)[0], 'manifest.json')
) as WireManifest;
const { w, h } = manifest.frames[2].planes.mid;
const id = (b: Uint8Array) => bakeIdOf(() => b);
it('accepts the real plane at its size', () => {
expect(webpFault(real, w, h)).toBeUndefined();
});
it("refuses a manifest that lies about the plane's size, and a file cut short or padded", () => {
expect(webpFault(real, w, h + 1)).toMatch(/states/);
expect(webpFault(real.subarray(0, real.length - 1), w, h)).toBeDefined();
expect(webpFault(Buffer.concat([real, Buffer.from([0])]), w, h)).toBeDefined();
});
it('refuses a frame header whose dimensions are changed, and bytes that are not a WebP', () => {
const vp8 = Buffer.from(real);
const at = vp8.indexOf('VP8 ') + 8 + 6;
vp8[at] ^= 0x01;
expect(webpFault(vp8, w, h)).toMatch(/states/);
expect(webpFault(Buffer.from('not a webp, but long enough to try'), w, h)).toMatch(/RIFF/);
});
it('re-derives a different bake id from a plane with the same length and 8 bytes overwritten', () => {
const bad = Buffer.from(real);
bad.fill(0xab, 100, 108);
expect(bad.length).toBe(real.length);
expect(id(bad)).not.toBe(id(real));
});
});
describe('sizeGate', () => {
const plane = (bytes: number): WirePlane => ({ file: 'x.webp', bytes, w: 1280, h: 600 });
const frame = (key: string, bytes: number): WireFrame => ({