From 056a13e28ee76ae8eda1d372e01d45d32a25dbc3 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sat, 21 Nov 2026 02:20:22 +0000 Subject: [PATCH] fix(media-use): read recipe freeze sources only as regular files (#8325) freezeRecipe read frame.md, STORYBOARD.md and BRIEF.md by path, which follows links. A received project could link one of them to a file outside the project, and the freeze copied that file's content into the project and user recipe folders. The freeze now reads each source once through readRegularFile, before any folder moves. A source that is a link or not a regular file stops the freeze with nothing written. A missing BRIEF.md still freezes without a brief skeleton. --- .../cli/src/media-use/lib/recipe-store.mjs | 45 ++++++++++++------- .../src/media-use/lib/recipe-store.test.mjs | 43 ++++++++++++++++++ skills-manifest.json | 2 +- skills/media-use/scripts/lib/recipe-store.mjs | 45 ++++++++++++------- 4 files changed, 102 insertions(+), 33 deletions(-) diff --git a/packages/cli/src/media-use/lib/recipe-store.mjs b/packages/cli/src/media-use/lib/recipe-store.mjs index 7d53fb66ca..dc96ce3e8a 100644 --- a/packages/cli/src/media-use/lib/recipe-store.mjs +++ b/packages/cli/src/media-use/lib/recipe-store.mjs @@ -173,12 +173,11 @@ export function skeletonizeStoryboard(source) { return out.join("\n").replace(/\n{3,}/g, "\n\n"); } -/** The run's workflow as BRIEF.md records it — the source of truth a freeze - * must not contradict. Undefined when no BRIEF.md (or no `workflow:`) exists. */ -function briefWorkflow(root) { - const brief = join(root, "BRIEF.md"); - if (!existsSync(brief)) return undefined; - const lines = readFileSync(brief, "utf8").split(/\r?\n/); +/** The run's workflow as BRIEF.md's text records it — the source of truth a freeze + * must not contradict. Undefined when there is no BRIEF.md (or no `workflow:`). */ +function briefWorkflow(brief) { + if (brief === undefined) return undefined; + const lines = brief.split(/\r?\n/); if (lines[0]?.trim() !== "---") return undefined; for (let i = 1; i < lines.length && lines[i].trim() !== "---"; i++) { const match = lines[i].match(/^workflow\s*:\s*(.+?)\s*$/); @@ -259,6 +258,19 @@ export function readRegularFile(path, noFollow = constants.O_NOFOLLOW ?? 0) { * Writes each named file into `dir` as a new regular file, replacing a link at that name instead * of following it, and removes a leftover brief skeleton the bundle does not have. */ +/** + * The content of the project's own `name`, or undefined when it is missing. Throws when it is a link + * or not a regular file, since a received project could link it to a file outside the project. + */ +function readProjectSource(root, name) { + const path = join(root, name); + const content = readRegularFile(path); + if (content === undefined && lstatSync(path, { throwIfNoEntry: false })) { + throw new Error(`${path} is a link or not a regular file; nothing was frozen`); + } + return content; +} + function writeRecipeFiles(dir, files) { mkdirSync(dir, { recursive: true }); for (const [name, content] of Object.entries(files)) replaceFile(join(dir, name), content); @@ -273,17 +285,19 @@ function writeRecipeFiles(dir, files) { export function freezeRecipe({ projectDir, name, workflow, blocks }) { const slug = slugifyRecipeName(name); const root = resolve(projectDir); - const fromBrief = briefWorkflow(root); + // Read before anything moves, so a source that is a link stops the freeze with nothing written. + const frame = readProjectSource(root, "frame.md"); + const storyboard = readProjectSource(root, "STORYBOARD.md")?.toString("utf8"); + const brief = readProjectSource(root, "BRIEF.md")?.toString("utf8"); + const fromBrief = briefWorkflow(brief); const fromFlag = workflow && String(workflow).trim() ? String(workflow).trim() : undefined; // BRIEF.md decides; the flag only covers projects briefed before it existed. const resolvedWorkflow = fromBrief ?? fromFlag; if (!resolvedWorkflow) { throw new Error("no workflow found — BRIEF.md names none and no --workflow was given"); } - const frameSpec = join(root, "frame.md"); - const storyboard = join(root, "STORYBOARD.md"); - if (!existsSync(frameSpec)) throw new Error("no frame.md to freeze — run the design step first"); - if (!existsSync(storyboard)) throw new Error("no STORYBOARD.md to freeze"); + if (frame === undefined) throw new Error("no frame.md to freeze — run the design step first"); + if (storyboard === undefined) throw new Error("no STORYBOARD.md to freeze"); refuseLinkedRecipeDir(root, slug); const dir = join(projectRecipesDir(root), slug); @@ -327,15 +341,14 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) { const files = { "recipe.json": `${JSON.stringify(recipe, null, 2)}\n`, - "frame.md": readFileSync(frameSpec), - "storyboard-skeleton.md": `${skeletonizeStoryboard(readFileSync(storyboard, "utf8")).trimEnd()}\n`, + "frame.md": frame, + "storyboard-skeleton.md": `${skeletonizeStoryboard(storyboard).trimEnd()}\n`, }; // Best-effort fourth artifact — projects briefed before BRIEF.md existed // (or by workflows that don't write one) freeze fine without it. - const brief = join(root, "BRIEF.md"); - const briefSkeleton = existsSync(brief); + const briefSkeleton = brief !== undefined; if (briefSkeleton) { - files["brief-skeleton.md"] = `${skeletonizeBrief(readFileSync(brief, "utf8")).trimEnd()}\n`; + files["brief-skeleton.md"] = `${skeletonizeBrief(brief).trimEnd()}\n`; } writeRecipeFiles(dir, files); diff --git a/packages/cli/src/media-use/lib/recipe-store.test.mjs b/packages/cli/src/media-use/lib/recipe-store.test.mjs index 45ef4dcd69..bfb02b97ad 100644 --- a/packages/cli/src/media-use/lib/recipe-store.test.mjs +++ b/packages/cli/src/media-use/lib/recipe-store.test.mjs @@ -563,6 +563,49 @@ for (const linked of [".media", ".media/recipes", ".media/recipes/promo"]) { }); } +/** Every file under `dir`, read as text; an empty list when `dir` is missing. */ +function textsUnder(dir) { + if (!existsSync(dir)) return []; + return readdirSync(dir, { recursive: true }) + .map((name) => join(dir, name)) + .filter((path) => lstatSync(path).isFile()) + .map((path) => readFileSync(path, "utf8")); +} + +for (const source of ["frame.md", "STORYBOARD.md", "BRIEF.md"]) { + test(`freeze refuses a ${source} that links outside the project, copying nothing`, () => { + withSandbox(({ project, root, home }) => { + freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" }); + const secret = join(root, "secret.txt"); + writeFileSync(secret, "---\nworkflow: product-launch-video\n---\nTOP-SECRET-TOKEN\n"); + rmSync(join(project, source), { force: true }); + symlinkSync(secret, join(project, source)); + + assert.throws( + () => + freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" }), + /is a link or not a regular file; nothing was frozen/, + ); + const recipes = [join(project, ".media/recipes"), join(home, ".media/recipes")]; + for (const text of recipes.flatMap(textsUnder)) assert.doesNotMatch(text, /TOP-SECRET/); + assert.deepEqual(readdirSync(join(project, ".media/recipes")), ["promo"]); + }); + }); +} + +test("freeze refuses a frame.md that is not a regular file", () => { + withSandbox(({ project, home }) => { + rmSync(join(project, "frame.md")); + mkdirSync(join(project, "frame.md")); + assert.throws( + () => freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" }), + /is a link or not a regular file; nothing was frozen/, + ); + assert.ok(!existsSync(join(project, ".media/recipes/promo"))); + assert.ok(!existsSync(join(home, ".media/recipes/promo"))); + }); +}); + test("use imports only regular recipe files from the user tier, never links or leftovers", () => { withSandbox(({ project, root, home }) => { freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" }); diff --git a/skills-manifest.json b/skills-manifest.json index 42d25ab935..9e213168e5 100644 --- a/skills-manifest.json +++ b/skills-manifest.json @@ -18,7 +18,7 @@ "files": 13 }, "media-use": { - "hash": "0cc7b566c82fc57f", + "hash": "cebaf2c0fa1d49ca", "files": 131 }, "remotion-to-hyperframes": { diff --git a/skills/media-use/scripts/lib/recipe-store.mjs b/skills/media-use/scripts/lib/recipe-store.mjs index 7d53fb66ca..dc96ce3e8a 100644 --- a/skills/media-use/scripts/lib/recipe-store.mjs +++ b/skills/media-use/scripts/lib/recipe-store.mjs @@ -173,12 +173,11 @@ export function skeletonizeStoryboard(source) { return out.join("\n").replace(/\n{3,}/g, "\n\n"); } -/** The run's workflow as BRIEF.md records it — the source of truth a freeze - * must not contradict. Undefined when no BRIEF.md (or no `workflow:`) exists. */ -function briefWorkflow(root) { - const brief = join(root, "BRIEF.md"); - if (!existsSync(brief)) return undefined; - const lines = readFileSync(brief, "utf8").split(/\r?\n/); +/** The run's workflow as BRIEF.md's text records it — the source of truth a freeze + * must not contradict. Undefined when there is no BRIEF.md (or no `workflow:`). */ +function briefWorkflow(brief) { + if (brief === undefined) return undefined; + const lines = brief.split(/\r?\n/); if (lines[0]?.trim() !== "---") return undefined; for (let i = 1; i < lines.length && lines[i].trim() !== "---"; i++) { const match = lines[i].match(/^workflow\s*:\s*(.+?)\s*$/); @@ -259,6 +258,19 @@ export function readRegularFile(path, noFollow = constants.O_NOFOLLOW ?? 0) { * Writes each named file into `dir` as a new regular file, replacing a link at that name instead * of following it, and removes a leftover brief skeleton the bundle does not have. */ +/** + * The content of the project's own `name`, or undefined when it is missing. Throws when it is a link + * or not a regular file, since a received project could link it to a file outside the project. + */ +function readProjectSource(root, name) { + const path = join(root, name); + const content = readRegularFile(path); + if (content === undefined && lstatSync(path, { throwIfNoEntry: false })) { + throw new Error(`${path} is a link or not a regular file; nothing was frozen`); + } + return content; +} + function writeRecipeFiles(dir, files) { mkdirSync(dir, { recursive: true }); for (const [name, content] of Object.entries(files)) replaceFile(join(dir, name), content); @@ -273,17 +285,19 @@ function writeRecipeFiles(dir, files) { export function freezeRecipe({ projectDir, name, workflow, blocks }) { const slug = slugifyRecipeName(name); const root = resolve(projectDir); - const fromBrief = briefWorkflow(root); + // Read before anything moves, so a source that is a link stops the freeze with nothing written. + const frame = readProjectSource(root, "frame.md"); + const storyboard = readProjectSource(root, "STORYBOARD.md")?.toString("utf8"); + const brief = readProjectSource(root, "BRIEF.md")?.toString("utf8"); + const fromBrief = briefWorkflow(brief); const fromFlag = workflow && String(workflow).trim() ? String(workflow).trim() : undefined; // BRIEF.md decides; the flag only covers projects briefed before it existed. const resolvedWorkflow = fromBrief ?? fromFlag; if (!resolvedWorkflow) { throw new Error("no workflow found — BRIEF.md names none and no --workflow was given"); } - const frameSpec = join(root, "frame.md"); - const storyboard = join(root, "STORYBOARD.md"); - if (!existsSync(frameSpec)) throw new Error("no frame.md to freeze — run the design step first"); - if (!existsSync(storyboard)) throw new Error("no STORYBOARD.md to freeze"); + if (frame === undefined) throw new Error("no frame.md to freeze — run the design step first"); + if (storyboard === undefined) throw new Error("no STORYBOARD.md to freeze"); refuseLinkedRecipeDir(root, slug); const dir = join(projectRecipesDir(root), slug); @@ -327,15 +341,14 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) { const files = { "recipe.json": `${JSON.stringify(recipe, null, 2)}\n`, - "frame.md": readFileSync(frameSpec), - "storyboard-skeleton.md": `${skeletonizeStoryboard(readFileSync(storyboard, "utf8")).trimEnd()}\n`, + "frame.md": frame, + "storyboard-skeleton.md": `${skeletonizeStoryboard(storyboard).trimEnd()}\n`, }; // Best-effort fourth artifact — projects briefed before BRIEF.md existed // (or by workflows that don't write one) freeze fine without it. - const brief = join(root, "BRIEF.md"); - const briefSkeleton = existsSync(brief); + const briefSkeleton = brief !== undefined; if (briefSkeleton) { - files["brief-skeleton.md"] = `${skeletonizeBrief(readFileSync(brief, "utf8")).trimEnd()}\n`; + files["brief-skeleton.md"] = `${skeletonizeBrief(brief).trimEnd()}\n`; } writeRecipeFiles(dir, files);