fix(scanner): ignore full-line comments for source rules · openclaw/openclaw@6548825
vincentkoc
·
2026-05-03
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -265,6 +265,15 @@ const match = /^keychain:(.+)$/.exec(value);
|
265 | 265 | expect(findings.some((f) => f.ruleId === "dangerous-exec")).toBe(false); |
266 | 266 | }); |
267 | 267 | |
| 268 | +it("does not use full-line comments as source-rule context", () => { |
| 269 | +const source = ` |
| 270 | +const env = process.env; |
| 271 | +// fetch() can reach the endpoint later. |
| 272 | +`; |
| 273 | +const findings = scanSource(source, "plugin.ts"); |
| 274 | +expect(findings.some((f) => f.ruleId === "env-harvesting")).toBe(false); |
| 275 | +}); |
| 276 | + |
268 | 277 | it("returns empty array for clean plugin code", () => { |
269 | 278 | const source = ` |
270 | 279 | export function greet(name: string): string { |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -233,9 +233,18 @@ function isBenignMemberExecMatch(line: string, match: RegExpExecArray): boolean
|
233 | 233 | return !/\b(?:cp|childProcess|child_process)\s*\.\s*exec\s*\(/.test(line); |
234 | 234 | } |
235 | 235 | |
| 236 | +function stripFullLineCommentsForHeuristics(source: string): string { |
| 237 | +return source |
| 238 | +.split("\n") |
| 239 | +.map((line) => (line.trimStart().startsWith("//") ? "" : line)) |
| 240 | +.join("\n"); |
| 241 | +} |
| 242 | + |
236 | 243 | export function scanSource(source: string, filePath: string): SkillScanFinding[] { |
237 | 244 | const findings: SkillScanFinding[] = []; |
238 | 245 | const lines = source.split("\n"); |
| 246 | +const heuristicSource = stripFullLineCommentsForHeuristics(source); |
| 247 | +const heuristicLines = heuristicSource.split("\n"); |
239 | 248 | const matchedLineRules = new Set<string>(); |
240 | 249 | |
241 | 250 | // --- Line rules --- |
@@ -291,18 +300,18 @@ export function scanSource(source: string, filePath: string): SkillScanFinding[]
|
291 | 300 | continue; |
292 | 301 | } |
293 | 302 | |
294 | | -if (!rule.pattern.test(source)) { |
| 303 | +if (!rule.pattern.test(heuristicSource)) { |
295 | 304 | continue; |
296 | 305 | } |
297 | | -if (rule.requiresContext && !rule.requiresContext.test(source)) { |
| 306 | +if (rule.requiresContext && !rule.requiresContext.test(heuristicSource)) { |
298 | 307 | continue; |
299 | 308 | } |
300 | 309 | |
301 | 310 | // Find the first matching line for evidence + line number |
302 | 311 | let matchLine = 0; |
303 | 312 | let matchEvidence = ""; |
304 | 313 | for (let i = 0; i < lines.length; i++) { |
305 | | -if (rule.pattern.test(lines[i])) { |
| 314 | +if (rule.pattern.test(heuristicLines[i] ?? "")) { |
306 | 315 | matchLine = i + 1; |
307 | 316 | matchEvidence = lines[i].trim(); |
308 | 317 | break; |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。