Skip to content

Commit 2899323

Browse files
authored
fix(security): expose every executable call in plugin scans (#116222)
* fix(security): report every scanner line match * fix(security): bound scanner match evidence
1 parent 48b3c1e commit 2899323

2 files changed

Lines changed: 79 additions & 23 deletions

File tree

‎src/skills/security/scanner.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,43 @@ afterEach(() => {
149149
// ---------------------------------------------------------------------------
150150

151151
describe("scanSource", () => {
152+
it("reports every dangerous execution call in a file", () => {
153+
const source = `
154+
import { execFile, spawn } from "node:child_process";
155+
spawn("node", ["first.js"]);
156+
spawn("node", ["second.js"]); execFile("node", ["third.js"]);
157+
`;
158+
159+
const findings = scanSource(source, "plugin.ts").filter(
160+
(candidate) => candidate.ruleId === "dangerous-exec",
161+
);
162+
163+
expect(findings.map((finding) => finding.line)).toEqual([3, 4, 4]);
164+
});
165+
166+
it("bounds dense line-rule findings and reports truncation", () => {
167+
const source = [
168+
`import { spawn } from "node:child_process";`,
169+
...Array.from({ length: 40 }, (_, index) => `spawn("node", ["${index}.js"]);`),
170+
].join("\n");
171+
172+
const findings = scanSource(source, "plugin.ts").filter((candidate) =>
173+
candidate.ruleId.startsWith("dangerous-exec"),
174+
);
175+
176+
expect(findings).toHaveLength(33);
177+
expect(findings.slice(0, -1).every((finding) => finding.ruleId === "dangerous-exec")).toBe(
178+
true,
179+
);
180+
expect(findings.at(-1)).toMatchObject({
181+
ruleId: "dangerous-exec-truncated",
182+
severity: "critical",
183+
line: 41,
184+
message: "8 additional dangerous-exec matches omitted after 32 findings",
185+
evidence: "[8 additional matches omitted after 32 findings]",
186+
});
187+
});
188+
152189
it("keeps bounded evidence free of lone surrogates", () => {
153190
const source = `${"a".repeat(119)}😀 child_process.exec("echo unsafe")`;
154191
const finding = scanSource(source, "plugin.ts").find(

‎src/skills/security/scanner.ts‎

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const SCANNABLE_EXTENSIONS = new Set([
5959

6060
const DEFAULT_MAX_SCAN_FILES = 500;
6161
const DEFAULT_MAX_FILE_BYTES = 1024 * 1024;
62+
const MAX_LINE_RULE_FINDINGS_PER_RULE = 32;
6263
const FILE_SCAN_CACHE_MAX = 5000;
6364
const DIR_ENTRY_CACHE_MAX = 5000;
6465
const TEST_DIRECTORY_NAMES = new Set(["__fixtures__", "__mocks__", "__tests__", "test", "tests"]);
@@ -392,47 +393,65 @@ export function scanSource(source: string, filePath: string): SkillScanFinding[]
392393
const lines = source.split("\n");
393394
const heuristicSource = stripCommentsForHeuristics(source);
394395
const heuristicLines = heuristicSource.split("\n");
395-
const matchedLineRules = new Set<string>();
396396

397397
// --- Line rules ---
398398
for (const rule of LINE_RULES) {
399-
if (matchedLineRules.has(rule.ruleId)) {
400-
continue;
401-
}
402-
403399
// Skip rule entirely if context requirement not met
404400
if (rule.requiresContext && !rule.requiresContext.test(source)) {
405401
continue;
406402
}
407403

404+
let acceptedMatches = 0;
405+
let omittedMatches = 0;
406+
let lastOmittedLine: number | undefined;
408407
for (const [i, line] of lines.entries()) {
409-
const match = rule.pattern.exec(line);
410-
if (!match) {
411-
continue;
412-
}
408+
const matches = line.matchAll(
409+
new RegExp(
410+
rule.pattern.source,
411+
rule.pattern.flags.includes("g") ? rule.pattern.flags : `${rule.pattern.flags}g`,
412+
),
413+
);
414+
for (const match of matches) {
415+
if (rule.ruleId === "dangerous-exec" && isBenignMemberExecMatch(line, match)) {
416+
continue;
417+
}
413418

414-
if (rule.ruleId === "dangerous-exec" && isBenignMemberExecMatch(line, match)) {
415-
continue;
416-
}
419+
// Special handling for suspicious-network: check port
420+
if (rule.ruleId === "suspicious-network") {
421+
const port = Number.parseInt(expectDefined(match[1], "scanner regex capture 1"), 10);
422+
if (STANDARD_PORTS.has(port)) {
423+
continue;
424+
}
425+
}
417426

418-
// Special handling for suspicious-network: check port
419-
if (rule.ruleId === "suspicious-network") {
420-
const port = Number.parseInt(expectDefined(match[1], "scanner regex capture 1"), 10);
421-
if (STANDARD_PORTS.has(port)) {
427+
if (acceptedMatches >= MAX_LINE_RULE_FINDINGS_PER_RULE) {
428+
omittedMatches += 1;
429+
lastOmittedLine = i + 1;
422430
continue;
423431
}
424-
}
425432

433+
// Retain distinct calls up to the cap, then aggregate every remaining match.
434+
// This keeps hostile output bounded without hiding that later sites exist.
435+
findings.push({
436+
ruleId: rule.ruleId,
437+
severity: rule.severity,
438+
file: filePath,
439+
line: i + 1,
440+
message: rule.message,
441+
evidence: formatScanEvidence(line),
442+
});
443+
acceptedMatches += 1;
444+
}
445+
}
446+
if (lastOmittedLine !== undefined) {
426447
findings.push({
427-
ruleId: rule.ruleId,
448+
ruleId: `${rule.ruleId}-truncated`,
428449
severity: rule.severity,
429450
file: filePath,
430-
line: i + 1,
431-
message: rule.message,
432-
evidence: formatScanEvidence(line),
451+
line: lastOmittedLine,
452+
message: `${omittedMatches} additional ${rule.ruleId} matches omitted after ${MAX_LINE_RULE_FINDINGS_PER_RULE} findings`,
453+
evidence: `[${omittedMatches} additional matches omitted after ${MAX_LINE_RULE_FINDINGS_PER_RULE} findings]`,
433454
});
434-
matchedLineRules.add(rule.ruleId);
435-
break; // one finding per line-rule per file
436455
}
437456
}
438457

0 commit comments

Comments
 (0)