fix(parsers): refuse media paths with digits between a percent sign and a wildcard (#8648)

* fix(parsers): refuse media paths with digits between a percent sign and a wildcard

* test(parsers): end ffprobe options with -- in the installed-ffprobe check
This commit is contained in:
Michael Wang
2026-11-24 07:20:43 +00:00
committed by GitHub
parent 7b29b81011
commit 40c4f98ba4
2 changed files with 68 additions and 3 deletions
+65
View File
@@ -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();
+3 -3
View File
@@ -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.",
);
}