Skip to content

Art and animation, written as code. Illustrations, loops, interactive web art, launch-videos and scored films in dozens of styles, identical on every render.

Decode verified bytes with forced FLAC demuxers

Alex Greenshpunfe88af3b8458
diff --git a/skills/anidoodle/engine/tools/sounds-unit.mjs b/skills/anidoodle/engine/tools/sounds-unit.mjs
index ab7b446..54e6b0c 100644
--- a/skills/anidoodle/engine/tools/sounds-unit.mjs
+++ b/skills/anidoodle/engine/tools/sounds-unit.mjs
@@ -5,7 +5,7 @@ import { build } from "esbuild";
import { strict as assert } from "node:assert";
import { execFileSync, spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
-import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
+import { mkdirSync, readFileSync, writeFileSync, symlinkSync, chmodSync } from "node:fs";
import { resolve, join } from "node:path";
import { loadBanks } from "./sounds.mjs";
@@ -46,6 +46,23 @@ zone.sha256 = originalRoom.sha256; manifest.rooms = {}; writeFileSync(join(root,
check('inherited constructor never becomes a sample-bank lookup', () => {
M.clearBanks(); loadBanks(M, root, ['constructor']); assert.equal(M.bankFor('constructor'), undefined);
});
+check('symlink outside the pack is rejected before decode', () => {
+ const outside = join(root, '..', 'outside-loader.flac'); writeFileSync(outside, bytes);
+ const linked = join(root, 'escaped.flac'); symlinkSync(outside, linked);
+ zone.file = 'escaped.flac'; writeFileSync(join(root, 'manifest.json'), JSON.stringify(manifest));
+ try { assert.throws(() => loadBanks(M, root, ['piano']), /zone escapes pack/); } finally { zone.file = 'large.flac'; writeFileSync(join(root, 'manifest.json'), JSON.stringify(manifest)); }
+});
+check('forced FLAC demuxers decode the exact hashed bytes even after the path changes', () => {
+ const wrappers = join(root, 'decoder-wrappers'); mkdirSync(wrappers, { recursive: true });
+ const oldPath = process.env.PATH;
+ for (const command of ['ffprobe', 'ffmpeg']) {
+ const real = execFileSync('which', [command], { encoding: 'utf8' }).trim();
+ const shim = `#!/usr/bin/env node\nimport {readFileSync,writeFileSync} from 'node:fs';\nimport {execFileSync} from 'node:child_process';\nimport {createHash} from 'node:crypto';\nconst args=process.argv.slice(2), input=readFileSync(0);\nif(args[args.indexOf('-f')+1]!=='flac'||!args.includes('pipe:0'))throw Error('decoder must force FLAC on stdin');\nif(createHash('sha256').update(input).digest('hex')!==${JSON.stringify(zone.sha256)})throw Error('decoder did not receive hashed bytes');\n${command === 'ffprobe' ? `writeFileSync(${JSON.stringify(file)},'#EXTM3U\\nhttps://invalid.example/external.flac\\n');` : ''}\nprocess.stdout.write(execFileSync(${JSON.stringify(real)},args,{input,maxBuffer:1<<28}));\n`;
+ const path = join(wrappers, command); writeFileSync(path, shim); chmodSync(path, 0o755);
+ }
+ try { process.env.PATH = wrappers + ':' + oldPath; M.clearBanks(); loadBanks(M, root, ['piano']); assert.equal(M.bankFor('piano').zones[0].channels[0].length, frames); }
+ finally { process.env.PATH = oldPath; writeFileSync(file, bytes); }
+});
const isolatedHome = join(root, 'default-home'), install = join(isolatedHome, '.anidoodle', 'sounds'); mkdirSync(install, { recursive: true });
writeFileSync(join(install, 'large.flac'), bytes);
writeFileSync(join(install, 'manifest.json'), JSON.stringify({ ...manifest, rooms: {}, instruments: { piano: { ...entry, range: [21, 108] } } }));
diff --git a/skills/anidoodle/engine/tools/sounds.mjs b/skills/anidoodle/engine/tools/sounds.mjs
index dbd472c..947a615 100644
--- a/skills/anidoodle/engine/tools/sounds.mjs
+++ b/skills/anidoodle/engine/tools/sounds.mjs
@@ -10,10 +10,10 @@ const decodeFlac = (root, id, zone) => {
const file = realpathSync(resolve(root, zone.file)); if (!file.startsWith(root + sep)) throw new Error(`${id}: zone escapes pack: ${zone.file}`);
const bytes = readFileSync(file), sha = createHash("sha256").update(bytes).digest("hex");
if (typeof zone.sha256 !== "string" || sha !== zone.sha256.toLowerCase()) throw new Error(`${id}: sha256 mismatch: ${zone.file}`);
- // ffprobe stops after the headers; feeding a whole FLAC on stdin can hit EPIPE on its early exit.
- const info = JSON.parse(execFileSync("ffprobe", ["-v", "error", "-show_streams", "-of", "json", file], { encoding: "utf8", maxBuffer: 1 << 20 })).streams;
+ // Count frames so ffprobe consumes the complete verified buffer rather than closing stdin early.
+ const info = JSON.parse(execFileSync("ffprobe", ["-v", "error", "-f", "flac", "-i", "pipe:0", "-count_frames", "-show_streams", "-of", "json"], { input: bytes, encoding: "utf8", maxBuffer: 1 << 20 })).streams;
if (info.length !== 1 || info[0].codec_name !== "flac" || Number(info[0].sample_rate) !== 48000 || Number(info[0].bits_per_raw_sample) !== 24 || info[0].channels !== zone.channels || ![1, 2].includes(zone.channels)) throw new Error(`${id}: expected 48 kHz 24-bit FLAC with ${zone.channels} channels: ${zone.file}`);
- const pcm = execFileSync("ffmpeg", ["-v", "error", "-i", file, "-vn", "-f", "f32le", "-acodec", "pcm_f32le", "-ar", "48000", "pipe:1"], { maxBuffer: 1 << 28 });
+ const pcm = execFileSync("ffmpeg", ["-v", "error", "-f", "flac", "-i", "pipe:0", "-vn", "-f", "f32le", "-acodec", "pcm_f32le", "-ar", "48000", "pipe:1"], { input: bytes, maxBuffer: 1 << 28 });
if (!Number.isInteger(zone.frames) || zone.frames < 1 || pcm.length !== zone.frames * zone.channels * 4) throw new Error(`${id}: frame count mismatch: ${zone.file}`);
const channels = Array.from({ length: zone.channels }, () => new Float32Array(zone.frames));
for (let i = 0; i < zone.frames; i++) for (let c = 0; c < zone.channels; c++) { const v = pcm.readFloatLE((i * zone.channels + c) * 4); if (!Number.isFinite(v)) throw new Error(`${id}: non-finite PCM: ${zone.file}`); channels[c][i] = v; }