From b7e4ba98f5af854649cefd1030751235ce55dd9c Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Tue, 13 Oct 2026 12:49:13 -0400 Subject: [PATCH] fix(security): detect interpolated exec calls after imports --- .../security-scan-code-patterns.test.ts | 24 +++++++++- .../src/commands/security-code-patterns.ts | 44 +++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/v3/@claude-flow/cli/__tests__/security-scan-code-patterns.test.ts b/v3/@claude-flow/cli/__tests__/security-scan-code-patterns.test.ts index 2c66640822..9c1d447585 100644 --- a/v3/@claude-flow/cli/__tests__/security-scan-code-patterns.test.ts +++ b/v3/@claude-flow/cli/__tests__/security-scan-code-patterns.test.ts @@ -72,6 +72,16 @@ describe('security scan code patterns: false positives seen on a real tree', () it('does not report a URL or a pattern inside a string as a comment boundary', async () => { expect(await findings({ 'u.ts': "const u = 'http://x/*'; const v = 1;\n" })).toEqual([]); }); + + it('does not report shell-call examples inside strings or static command arguments', async () => { + expect(await findings({ + 'docs.js': [ + 'const example = "execSync(`git log ${branch}`)";', + "execSync('git ' + 'status');", + 'execSync(`echo \\${literal}`);', + ].join('\n'), + })).toEqual([]); + }); }); describe('security scan code patterns: real findings still reported', () => { @@ -85,6 +95,19 @@ describe('security scan code patterns: real findings still reported', () => { }))).toEqual(['Command Injection b.ts:1', 'Command Injection b.ts:2']); }); + it('reports interpolated exec calls even when child_process was imported on another line', async () => { + expect(types(await findings({ + 'shell.js': [ + "const { execSync, execFileSync } = require('child_process');", + 'function run(req) { return execSync(`git log ${req.query.branch}`); }', + 'function run2(req) { return execSync("git log " + req.query.branch); }', + 'function run3(req) { return childProcess.exec(`git show ${req.query.ref}`); }', + "execFileSync('git', ['log', branch]);", + "execSync('git status');", + ].join('\n'), + }))).toEqual(['Command Injection shell.js:2', 'Command Injection shell.js:3', 'Command Injection shell.js:4']); + }); + it('reports an interpolated value in SQL, also next to a placeholder', async () => { expect(types(await findings({ 'c.ts': 'const sql = `SELECT * FROM users WHERE id = ${id}`;\nsql += ` AND a = $${n} AND b = ${name}`;\n', @@ -272,4 +295,3 @@ describe('security scan code patterns: shapes the narrowing must not hide', () = expect(Date.now() - started).toBeLessThan(1_000); }); }); - diff --git a/v3/@claude-flow/cli/src/commands/security-code-patterns.ts b/v3/@claude-flow/cli/src/commands/security-code-patterns.ts index 9a0e160aae..ad4ca47f37 100644 --- a/v3/@claude-flow/cli/src/commands/security-code-patterns.ts +++ b/v3/@claude-flow/cli/src/commands/security-code-patterns.ts @@ -6,8 +6,8 @@ * - every rule is tested after comment text is blanked, so a comment naming `child_process.execFile` or `eval(` is * not a finding; * - eval needs a word start: "retrieval (" in prose is not a call; `eval(` and `window.eval(` still are; - * - command injection keeps the old rule's reach (any `exec` after `child_process` on the line), and no longer skips - * execSync as `exec[^S]` did; + * - command injection keeps the old rule's reach (any `exec` after `child_process` on the line), and also catches + * dynamic exec/execSync command strings when the import was on another line; * - a pg placeholder such as `$${paramCount + 1}` is a parameter number, not a value in the SQL text. It is exempt only * when its expression is a number, a loop letter, `params.length`-style, or a name containing `param` or `placeholder`, it * stands where SQL takes a value, and it is not inside a quoted SQL string. @@ -24,6 +24,44 @@ export type CodePattern = { /** No `g` flag, so `test` keeps no lastIndex between lines. */ const re = (pattern: RegExp) => (line: string) => pattern.test(line) +/** Detect a dynamic shell command at an exec/execSync call site, independent of its import. */ +const interpolatedExec = (line: string): boolean => { + let quote: "'" | '"' | '`' | null = null + for (let i = 0; i < line.length; i++) { + const ch = line[i] + if (quote !== null) { + if (ch === '\\') { i++; continue } + if (ch === quote) quote = null + continue + } + if (ch === "'" || ch === '"' || ch === '`') { quote = ch; continue } + if (i > 0 && /[\w$]/.test(line[i - 1])) continue + const name = line.startsWith('execSync', i) ? 'execSync' : line.startsWith('exec', i) ? 'exec' : '' + if (!name) continue + let arg = i + name.length + while (/\s/.test(line[arg] ?? '')) arg++ + if (line[arg++] !== '(') continue + while (/\s/.test(line[arg] ?? '')) arg++ + const delimiter = line[arg] + if (delimiter !== '`' && delimiter !== "'" && delimiter !== '"') continue + for (let end = arg + 1; end < line.length; end++) { + if (line[end] === '\\') { end++; continue } + if (delimiter === '`' && line[end] === '$' && line[end + 1] === '{') return true + if (line[end] !== delimiter) continue + if (delimiter !== '`') { + let next = end + 1 + while (/\s/.test(line[next] ?? '')) next++ + if (line[next++] === '+') { + while (/\s/.test(line[next] ?? '')) next++ + if (/[A-Za-z_$]/.test(line[next] ?? '')) return true + } + } + break + } + } + return false +} + // A `.length` counts only on a param-, value- or arg-named array: `req.body.length` is whatever the client sent. const COUNTER = /^\s*(?:\d+|[ijkn]|(?:[A-Za-z_$][\w$]*(?:Params?|Placeholders?|Values?|Args?)|params?|placeholders?|values?|args?)\.length|[A-Za-z_$]*(?:[Pp]aram|[Pp]laceholder)[\w$]*)\s*(?:[+-]\s*\d+\s*)?$/ // Where a pg parameter stands in SQL: after a comparison, a list or call opener, or a clause keyword. @@ -70,7 +108,7 @@ export const CODE_PATTERNS: readonly CodePattern[] = [ { match: re(/(? /child_process.*exec/.test(line) || interpolatedExec(line), type: 'Command Injection', severity: 'high', desc: 'Possible command injection' }, { match: line => sqlInterpolation(line) && sqlInterpolation(withoutPlaceholders(line)), type: 'SQL Injection', severity: 'high', desc: 'Possible SQL injection' }, ]