From e383abf475b6ebd5072d6aa1cb380bc7d984c629 Mon Sep 17 00:00:00 2001 From: WaterrrForever Date: Thu, 19 Nov 2026 07:26:36 +0000 Subject: [PATCH] fix(media-use): re-freezing a recipe archives only under an integer version (#8201) * fix(media-use): re-freezing a recipe archives only under an integer version freezeRecipe archived the previous folder at `${dir}@v${previous.version}`, taking version straight from the old recipe.json and removing that path recursively before renaming the folder onto it. recipe.json is committed with the project, so a received project could set version to a path such as "1/../../../../victim", ship a promo@v1 folder for it to walk through, and the next freeze of that name deleted a folder outside the project and moved the recipe folder into its place. Only a positive integer version is trusted now; anything else archives as v1, the same fallback the version bump already used. Both media-use copies of recipe-store.mjs carry the change. Co-Authored-By: Claude Opus 5.5 * fix(media-use): never replace an existing recipe archive on re-freeze A malformed recipe.json version fell back to slot 1, so re-freezing removed a real promo@v1 archive before renaming the current folder into it. The archive slot is now the claimed version only when that archive does not exist yet; otherwise it is the first number above every archive on disk. Nothing is removed any more. The version is checked against Number.MAX_SAFE_INTEGER before either folder moves, so a freeze never writes a version the next freeze would reject. Refs SECOPS-2882 Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../cli/src/media-use/lib/recipe-store.mjs | 31 +++++- .../src/media-use/lib/recipe-store.test.mjs | 100 +++++++++++++++++- skills-manifest.json | 2 +- skills/media-use/scripts/lib/recipe-store.mjs | 31 +++++- 4 files changed, 154 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/media-use/lib/recipe-store.mjs b/packages/cli/src/media-use/lib/recipe-store.mjs index 9e1ac57487..6a40c3db82 100644 --- a/packages/cli/src/media-use/lib/recipe-store.mjs +++ b/packages/cli/src/media-use/lib/recipe-store.mjs @@ -196,6 +196,18 @@ function readRecipeJson(dir) { } } +/** The highest `@v` archive number in `recipesDir`, 0 when there is none. */ +function highestArchive(recipesDir, slug) { + const prefix = `${slug}@v`; + let highest = 0; + for (const entry of readdirSync(recipesDir)) { + if (!entry.startsWith(prefix)) continue; + const number = Number(entry.slice(prefix.length)); + if (Number.isSafeInteger(number) && number > highest) highest = number; + } + return highest; +} + function prefValue(prefs, key) { return prefs[key]?.value; } @@ -224,10 +236,21 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) { let version = 1; const previous = existsSync(dir) ? readRecipeJson(dir) : null; if (previous) { - version = (Number.isInteger(previous.version) ? previous.version : 1) + 1; - const archive = `${dir}@v${previous.version ?? 1}`; - rmSync(archive, { recursive: true, force: true }); - renameSync(dir, archive); + // recipe.json travels with the project, so its version is untrusted text: only a positive + // integer may name the archive, and an archive already on disk is never replaced. A + // malformed version, or one whose archive exists, moves to the first slot above them all. + const claimed = + Number.isSafeInteger(previous.version) && previous.version > 0 ? previous.version : 0; + const slot = + claimed && !existsSync(`${dir}@v${claimed}`) + ? claimed + : Math.max(claimed, highestArchive(projectRecipesDir(root), slug)) + 1; + version = slot + 1; + // Checked before either folder moves, and keeps the next freeze's version valid too. + if (!Number.isSafeInteger(version)) { + throw new Error(`recipe "${slug}" has no version number left to freeze into`); + } + renameSync(dir, `${dir}@v${slot}`); } mkdirSync(dir, { recursive: true }); 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 e33fa78fa5..8d78e327b8 100644 --- a/packages/cli/src/media-use/lib/recipe-store.test.mjs +++ b/packages/cli/src/media-use/lib/recipe-store.test.mjs @@ -1,6 +1,14 @@ import { strict as assert } from "node:assert"; import { test } from "node:test"; -import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { + existsSync, + mkdirSync, + mkdtempSync, + readdirSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { @@ -318,6 +326,96 @@ test("re-freezing bumps the version and archives the old folder", () => { }); }); +test("re-freezing never builds the archive path from a non-integer recipe.json version", () => { + for (const version of ["1/../../../../victim", "/../../../../victim", 1.5, -3, null]) { + withSandbox(({ project, root }) => { + const victim = join(root, "victim"); + mkdirSync(victim, { recursive: true }); + writeFileSync(join(victim, "keep.txt"), "outside the project"); + const recipes = join(project, ".media/recipes"); + mkdirSync(join(recipes, "promo"), { recursive: true }); + // A received project can ship the folder the crafted path walks through, and the real + // v1 archive a fallback to version 1 would otherwise have replaced. + mkdirSync(join(recipes, "promo@v1"), { recursive: true }); + writeFileSync(join(recipes, "promo@v1/recipe.json"), '{"name":"promo","version":1}'); + mkdirSync(join(recipes, "promo@v"), { recursive: true }); + writeFileSync( + join(recipes, "promo/recipe.json"), + JSON.stringify({ name: "promo", version, workflow: "product-launch-video" }), + ); + + const frozen = freezeRecipe({ + projectDir: project, + name: "promo", + workflow: "product-launch-video", + }); + + assert.equal(readFileSync(join(victim, "keep.txt"), "utf8"), "outside the project"); + assert.ok(!existsSync(join(victim, "recipe.json")), `version ${JSON.stringify(version)}`); + assert.equal( + readFileSync(join(recipes, "promo@v1/recipe.json"), "utf8"), + '{"name":"promo","version":1}', + ); + assert.equal(frozen.version, 3); + const archived = JSON.parse(readFileSync(join(recipes, "promo@v2/recipe.json"), "utf8")); + assert.deepEqual(archived.version, version); + }); + } +}); + +test("re-freezing never replaces an archive already on disk", () => { + withSandbox(({ project }) => { + const recipes = join(project, ".media/recipes"); + mkdirSync(join(recipes, "promo"), { recursive: true }); + mkdirSync(join(recipes, "promo@v1"), { recursive: true }); + mkdirSync(join(recipes, "promo@v4"), { recursive: true }); + writeFileSync(join(recipes, "promo@v1/keep.txt"), "first approval"); + writeFileSync(join(recipes, "promo@v4/keep.txt"), "fourth approval"); + writeFileSync( + join(recipes, "promo/recipe.json"), + JSON.stringify({ name: "promo", version: 1, workflow: "product-launch-video" }), + ); + + const frozen = freezeRecipe({ + projectDir: project, + name: "promo", + workflow: "product-launch-video", + }); + + assert.equal(readFileSync(join(recipes, "promo@v1/keep.txt"), "utf8"), "first approval"); + assert.equal(readFileSync(join(recipes, "promo@v4/keep.txt"), "utf8"), "fourth approval"); + assert.equal( + JSON.parse(readFileSync(join(recipes, "promo@v5/recipe.json"), "utf8")).version, + 1, + ); + assert.equal(frozen.version, 6); + }); +}); + +test("re-freezing refuses, before moving anything, when no safe version number is left", () => { + for (const [version, archive] of [ + [Number.MAX_SAFE_INTEGER, undefined], + ["1/../victim", `promo@v${Number.MAX_SAFE_INTEGER}`], + ]) { + withSandbox(({ project }) => { + const recipes = join(project, ".media/recipes"); + mkdirSync(join(recipes, "promo"), { recursive: true }); + if (archive) mkdirSync(join(recipes, archive), { recursive: true }); + const current = JSON.stringify({ name: "promo", version, workflow: "product-launch-video" }); + writeFileSync(join(recipes, "promo/recipe.json"), current); + const before = readdirSync(recipes).sort(); + + assert.throws( + () => + freezeRecipe({ projectDir: project, name: "promo", workflow: "product-launch-video" }), + /no version number left/, + ); + assert.equal(readFileSync(join(recipes, "promo/recipe.json"), "utf8"), current); + assert.deepEqual(readdirSync(recipes).sort(), before); + }); + } +}); + test("re-freezing without a brief does not import the previous version's brief", () => { withSandbox(({ project, root }) => { writeFileSync(join(project, "BRIEF.md"), BRIEF); diff --git a/skills-manifest.json b/skills-manifest.json index dd1aef8a4e..bc45732cec 100644 --- a/skills-manifest.json +++ b/skills-manifest.json @@ -18,7 +18,7 @@ "files": 13 }, "media-use": { - "hash": "ade4f491789eec78", + "hash": "13edb278adc41238", "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 9e1ac57487..6a40c3db82 100644 --- a/skills/media-use/scripts/lib/recipe-store.mjs +++ b/skills/media-use/scripts/lib/recipe-store.mjs @@ -196,6 +196,18 @@ function readRecipeJson(dir) { } } +/** The highest `@v` archive number in `recipesDir`, 0 when there is none. */ +function highestArchive(recipesDir, slug) { + const prefix = `${slug}@v`; + let highest = 0; + for (const entry of readdirSync(recipesDir)) { + if (!entry.startsWith(prefix)) continue; + const number = Number(entry.slice(prefix.length)); + if (Number.isSafeInteger(number) && number > highest) highest = number; + } + return highest; +} + function prefValue(prefs, key) { return prefs[key]?.value; } @@ -224,10 +236,21 @@ export function freezeRecipe({ projectDir, name, workflow, blocks }) { let version = 1; const previous = existsSync(dir) ? readRecipeJson(dir) : null; if (previous) { - version = (Number.isInteger(previous.version) ? previous.version : 1) + 1; - const archive = `${dir}@v${previous.version ?? 1}`; - rmSync(archive, { recursive: true, force: true }); - renameSync(dir, archive); + // recipe.json travels with the project, so its version is untrusted text: only a positive + // integer may name the archive, and an archive already on disk is never replaced. A + // malformed version, or one whose archive exists, moves to the first slot above them all. + const claimed = + Number.isSafeInteger(previous.version) && previous.version > 0 ? previous.version : 0; + const slot = + claimed && !existsSync(`${dir}@v${claimed}`) + ? claimed + : Math.max(claimed, highestArchive(projectRecipesDir(root), slug)) + 1; + version = slot + 1; + // Checked before either folder moves, and keeps the next freeze's version valid too. + if (!Number.isSafeInteger(version)) { + throw new Error(`recipe "${slug}" has no version number left to freeze into`); + } + renameSync(dir, `${dir}@v${slot}`); } mkdirSync(dir, { recursive: true });