Skip to content

Commit 6c5403f

Browse files
vanceingallsclaude
andcommitted
fix(producer): drop the ReDoS-prone literal-argv regex for a linear scan
CodeQL flagged js/redos on the all-literal argv matcher. It was right: the `(?:"[^"]*"\s*,?\s*)+` form nests a quantifier inside a quantifier with an optional separator, so whitespace can be matched two ways and a long non-matching argv backtracks exponentially. Replaced with a linear scan — find the spawn head, slice to the closing bracket, and check the entries — plus small named helpers. Same behaviour: an all-literal argv is treated as taking no input, an argv with a bare identifier still has to be understood (verified by adding one and watching the guard fail). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent c81f68b commit 6c5403f

1 file changed

Lines changed: 48 additions & 14 deletions

File tree

‎packages/producer/src/utils/ffprobeArgvContract.test.ts‎

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,54 @@ function mentionsProbe(src: string): boolean {
7777
// Comments and doc prose describing a spawn are not a spawn:
7878
// `tts.test.mjs` explains `ffprobeDuration's spawnSync("ffprobe", ...) call`
7979
// in a comment and was reported as an unclassified caller.
80-
// A probe spawned with an all-literal argv takes no runtime input, so
81-
// there is nothing to terminate: `spawnSync("ffprobe", ["-version"])` is a
82-
// capability check, not a file probe. Dropping those before the test keeps
83-
// them out of the unclassified list without weakening it — an argv carrying
84-
// a bare identifier (a path) still has to be understood.
85-
const NO_INPUT_PROBE =
86-
/(?:spawn|spawnSync|execFile\w*|exec)\s*\(\s*["'`]ff(?:probe|mpeg)["'`]\s*,\s*\[\s*(?:"[^"]*"\s*,?\s*)+\]/g;
87-
const code = src
88-
.replace(/\/\*[\s\S]*?\*\//g, "")
89-
.replace(/\/\/[^\n]*/g, "")
90-
.replace(NO_INPUT_PROBE, "");
91-
return /(?:spawn|spawnSync|execFile\w*|exec)\s*\(\s*[^,)]*(?:ffprobe|ffProbe|probeBin|probePath)/i.test(
92-
code,
93-
);
80+
// A probe spawned with an all-literal argv takes no runtime input, so there
81+
// is nothing to terminate: `spawnSync("ffprobe", ["-version"])` is a
82+
// capability check, not a file probe. Dropping those keeps them out of the
83+
// unclassified list without weakening it — an argv carrying a bare
84+
// identifier (a path) still has to be understood.
85+
//
86+
// Split into a linear scan plus a per-body check rather than one regex.
87+
// The obvious `(?:"[^"]*"\s*,?\s*)+` form nests a quantifier inside a
88+
// quantifier with an optional separator, so whitespace can be matched two
89+
// ways and it backtracks exponentially on a long non-matching argv — CodeQL
90+
// flagged it as js/redos, correctly.
91+
const code = stripComments(src);
92+
return CALLS_PROBE.test(withoutNoInputProbes(code));
93+
}
94+
95+
const PROBE_SPAWN_HEAD =
96+
/(?:spawn|spawnSync|execFile\w*|exec)\s*\(\s*["'`]ff(?:probe|mpeg)["'`]\s*,\s*\[/g;
97+
const CALLS_PROBE =
98+
/(?:spawn|spawnSync|execFile\w*|exec)\s*\(\s*[^,)]*(?:ffprobe|ffProbe|probeBin|probePath)/i;
99+
100+
function stripComments(src: string): string {
101+
return src.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/[^\n]*/g, "");
102+
}
103+
104+
/** Every entry a string literal → no runtime path in this argv. */
105+
function isLiteralOnlyArgv(body: string): boolean {
106+
const entries = body
107+
.split(",")
108+
.map((e) => e.trim())
109+
.filter((e) => e !== "");
110+
return entries.length > 0 && entries.every((e) => /^"[^"]*"$/.test(e));
111+
}
112+
113+
/** Blank out `spawn("ffprobe", [ ...all literals... ])` occurrences. */
114+
function withoutNoInputProbes(code: string): string {
115+
let out = "";
116+
let cursor = 0;
117+
PROBE_SPAWN_HEAD.lastIndex = 0;
118+
for (let m = PROBE_SPAWN_HEAD.exec(code); m !== null; m = PROBE_SPAWN_HEAD.exec(code)) {
119+
const bodyStart = m.index + m[0].length;
120+
const bodyEnd = code.indexOf("]", bodyStart);
121+
if (bodyEnd === -1) continue;
122+
if (!isLiteralOnlyArgv(code.slice(bodyStart, bodyEnd))) continue;
123+
out += code.slice(cursor, m.index);
124+
cursor = bodyEnd + 1;
125+
PROBE_SPAWN_HEAD.lastIndex = cursor;
126+
}
127+
return out + code.slice(cursor);
94128
}
95129

96130
const SKIP_DIRS = new Set(["node_modules", "dist"]);

0 commit comments

Comments
 (0)