diff --git a/packages/parsers/src/ffBinaries.test.ts b/packages/parsers/src/ffBinaries.test.ts index 66ed48b99d..dae19d65da 100644 --- a/packages/parsers/src/ffBinaries.test.ts +++ b/packages/parsers/src/ffBinaries.test.ts @@ -2,6 +2,7 @@ import { spawnSync } from "node:child_process"; import { type PathLike, chmodSync, + copyFileSync, existsSync, mkdirSync, mkdtempSync, @@ -565,6 +566,9 @@ describe("assertLiteralMediaInput", () => { "/job/x%*/still.png", "/job/x%[ab]/still.png", "/job/x%{a,b}/still.png", + "/job/x%3*/still.png", + "/job/x%03?.png", + "/job/x%1{a,b}.png", "file:/job/decoy%d.png", ]) { expect(() => assertLiteralMediaInput(path), path).toThrow(/pattern of other files/); @@ -625,6 +629,67 @@ describe("assertLiteralMediaInput", () => { }); }); +describe("assertLiteralMediaInput against the installed ffprobe", () => { + it("lets through only names ffprobe opens as the one file written", async () => { + vi.resetModules(); + vi.doUnmock("node:fs"); + vi.doUnmock("node:child_process"); + const { assertLiteralMediaInput, findFfBinary } = await importFresh(); + const [ffmpeg, ffprobe] = [findFfBinary("ffmpeg"), findFfBinary("ffprobe")]; + if (!ffmpeg || !ffprobe) return; + const dir = mkdtempSync(join(tmpdir(), "hf-image2-forms-")); + onTestFinished(() => rmSync(dir, { recursive: true, force: true })); + const still = (size: number, at: string) => + spawnSync(ffmpeg, [ + ...["-v", "error", "-y", "-f", "lavfi", "-i", `color=red:s=${size}x${size}`], + ...["-frames:v", "1", "-c:v", "png", "-f", "image2", "-update", "1", at], + ]); + const [small, big] = [join(dir, "small.png"), join(dir, "big.png")]; + still(16, small); + still(32, big); + if (!existsSync(small) || !existsSync(big)) return; + const width = (path: string) => + spawnSync( + ffprobe, + [ + "-v", + "error", + "-select_streams", + "v:0", + "-show_entries", + "stream=width", + "-of", + "csv=p=0", + "--", + path, + ], + { encoding: "utf8" }, + ).stdout.trim(); + const tails = "!#$&'()*+,-.:;<=>?@[]^_`{|}~ adisx%"; + const forms = ["", "1", "03"].flatMap((digits) => + [...tails].map((tail) => `%${digits}${tail}`), + ); + const misread: string[] = []; + forms.forEach((form, index) => { + const at = join(dir, `case${index}`); + mkdirSync(at); + for (const decoy of ["x y.png", "x!y.png", "x0y.png", "x1y.png", "x001y.png"]) { + copyFileSync(big, join(at, decoy)); + } + const literal = join(at, `x${form}y.png`); + copyFileSync(small, literal); + let refused = false; + try { + assertLiteralMediaInput(literal); + } catch { + refused = true; + } + if (!refused && width(literal) !== "16") misread.push(form); + }); + expect(misread).toEqual([]); + }, 60_000); +}); + describe.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)("concatListInputArgs", () => { it("refuses a listed entry that is not a local file", async () => { const { concatListInputArgs } = await importFresh(); diff --git a/packages/parsers/src/literalMediaInput.ts b/packages/parsers/src/literalMediaInput.ts index 3c37ffce3b..4c32fb5c2f 100644 --- a/packages/parsers/src/literalMediaInput.ts +++ b/packages/parsers/src/literalMediaInput.ts @@ -1,4 +1,4 @@ -const PATTERN_IMAGE2_EXPANDS = /%(?:\d*d|[%*?[\]{}])/; +const PATTERN_IMAGE2_EXPANDS = /%\d*[d%*?[\]{}]/; const URL_SCHEME = /^[a-z][a-z0-9+.-]+:/i; /** ffmpeg's image2 demuxer would open other files, folders included, than the one a project check approved. */ @@ -6,7 +6,7 @@ export function assertLiteralMediaInput(input: string): void { const local = !URL_SCHEME.test(input) || /^file:/i.test(input); if (!local || !PATTERN_IMAGE2_EXPANDS.test(input)) return; throw new Error( - `[FFmpeg] refused media path "${input.split(/[\\/]/).pop()}": a "%" followed by a number and "d", ` + - 'another "%" or a wildcard would make ffmpeg read it as a pattern of other files. Rename the file or folder.', + `[FFmpeg] refused media path "${input.split(/[\\/]/).pop()}": a "%" followed by "d", another "%" or a wildcard, ` + + "with or without digits between, would make ffmpeg read it as a pattern of other files. Rename the file or folder.", ); }