All files security-detectors.ts

100% Statements 107/107
84.21% Branches 112/133
100% Functions 13/13
100% Lines 83/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555            4x   4x         20x               65x   66x 63x 5x   66x   3x                                                                         65x       61x   62x   1x                                                                   61x       61x   62x   1x                                                   61x       61x   62x 59x   1x                                                   61x       62x   63x     2x                                                                         62x       63x   64x 61x             1x 1x   1x 1x     1x 1x   3x                                                                           63x       64x   65x   4x 2x 2x 4x                                                                             2x       64x       64x   65x   5x 4x 3x 2x 2x 2x 5x                                                                             2x       64x           63x   64x   4x 4x 3x   2x 2x 4x   4x                                                                 2x       63x           64x   65x   5x 5x 4x     3x 4x   3x 4x         2x 2x                                                                 2x           1x                                                               1x         64x    
import { isTestFile } from './utils.js';
 
import type { FileEntry, Finding } from './types.js';
 
type FindingDraft = Omit<Finding, 'id'>;
 
const NESTED_QUANTIFIER_RE = /(\(.+[+*]\))[+*]|(\(.+\?\))\{/;
 
const toSecurityFinding = (
  draft: FindingDraft,
  ruleId: string,
  confidence: 'high' | 'medium' | 'low',
  evidence: Record<string, unknown>,
): FindingDraft => ({
  ...draft,
  ruleId,
  confidence,
  evidence,
});
 
export function detectHardcodedSecrets(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    const secrets = (entry.suspiciousStrings || []).filter((s) =>
      s.kind === 'hardcoded-secret' && s.context !== 'regex-definition' && s.context !== 'error-message',
    );
    if (secrets.length === 0) continue;
    for (const s of secrets) {
      findings.push(toSecurityFinding({
        severity: 'high',
        category: 'hardcoded-secret',
        file: entry.file,
        lineStart: s.lineStart,
        lineEnd: s.lineEnd,
        title: `Potential hardcoded secret${s.snippet ? `: ${s.snippet.slice(0, 20)}…` : ''}`,
        reason: `String literal matches a secret pattern (password, API key, token, high-entropy string). Secrets in source code risk credential leaks. Validate: use localSearchCode to find the variable, then lspFindReferences to check if it is used in auth or network calls.`,
        files: [entry.file],
        suggestedFix: {
          strategy: 'Move secret to environment variable or secrets manager.',
          steps: [
            'Replace the hardcoded value with process.env.YOUR_SECRET.',
            'Add the variable to your .env file (excluded from git).',
            'Verify the secret is not committed in git history.',
          ],
        },
        impact: 'Credential leak in source code exposes API access, database credentials, or authentication tokens to anyone with repo access.',
        tags: ['security', 'secrets'],
        lspHints: [
          {
            tool: 'lspFindReferences',
            symbolName: s.snippet?.split(/[=:]/)[0]?.trim() || 'secret',
            lineHint: s.lineStart,
            file: entry.file,
            expectedResult: `find all usages of this secret value — if used only in tests or as a regex pattern, it is a false positive`,
          },
        ],
      }, 'security.hardcoded-secret', 'high', {
        source: s.snippet || '',
        sink: 'runtime usage',
        context: s.context || 'literal',
        sanitizerStatus: 'missing',
        propagationSteps: [`${entry.file}:${s.lineStart}`],
      }));
    }
  }
  return findings;
}
 
export function detectEvalUsage(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    for (const loc of entry.evalUsages || []) {
      findings.push(toSecurityFinding({
        severity: 'critical',
        category: 'eval-usage',
        file: entry.file,
        lineStart: loc.lineStart,
        lineEnd: loc.lineEnd,
        title: 'Dynamic code execution (eval/Function)',
        reason: 'eval(), new Function(), or string-based setTimeout/setInterval allows arbitrary code execution. This is a code injection vector.',
        files: [entry.file],
        suggestedFix: {
          strategy: 'Replace dynamic code execution with safe alternatives.',
          steps: [
            'For JSON parsing: use JSON.parse() instead of eval().',
            'For dynamic dispatch: use a lookup table or switch statement.',
            'For setTimeout: pass a function reference, not a string.',
          ],
        },
        impact: 'Arbitrary code execution enables full application takeover — the most severe class of injection vulnerability.',
        tags: ['security', 'injection', 'critical'],
        lspHints: [
          {
            tool: 'lspCallHierarchy',
            symbolName: 'eval',
            lineHint: loc.lineStart,
            file: entry.file,
            expectedResult: `trace callers to find how user input reaches the eval site`,
          },
        ],
      }, 'security.eval-usage', 'high', {
        sink: `eval at ${entry.file}:${loc.lineStart}-${loc.lineEnd}`,
        sanitizerStatus: 'missing',
      }));
    }
  }
  return findings;
}
 
export function detectUnsafeHtml(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    for (const loc of entry.unsafeHtmlAssignments || []) {
      findings.push(toSecurityFinding({
        severity: 'high',
        category: 'unsafe-html',
        file: entry.file,
        lineStart: loc.lineStart,
        lineEnd: loc.lineEnd,
        title: 'Unsafe HTML manipulation',
        reason: 'innerHTML, outerHTML, dangerouslySetInnerHTML, or document.write can execute unsanitized user input as HTML/script. XSS vector.',
        files: [entry.file],
        suggestedFix: {
          strategy: 'Use safe DOM APIs or sanitize input before insertion.',
          steps: [
            'Replace innerHTML with textContent for plain text.',
            'Use a sanitizer library (e.g. DOMPurify) if HTML is required.',
            'In React, avoid dangerouslySetInnerHTML — use JSX instead.',
          ],
        },
        impact: 'Unsanitized HTML insertion enables cross-site scripting (XSS) — attackers can steal sessions, credentials, or execute actions as the victim.',
        tags: ['security', 'xss'],
      }, 'security.unsafe-html', 'high', {
        sink: 'DOM assignment',
        sanitizerStatus: 'missing',
        propagationSteps: ['html assignment'],
      }));
    }
  }
  return findings;
}
 
export function detectSqlInjectionRisk(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    const sqls = (entry.suspiciousStrings || []).filter((s) => s.kind === 'sql-injection');
    for (const s of sqls) {
      findings.push(toSecurityFinding({
        severity: 'high',
        category: 'sql-injection-risk',
        file: entry.file,
        lineStart: s.lineStart,
        lineEnd: s.lineEnd,
        title: 'SQL query built with template literal interpolation',
        reason: 'Template literals with SQL keywords and interpolated expressions risk SQL injection if user input flows into the query.',
        files: [entry.file],
        suggestedFix: {
          strategy: 'Use parameterized queries or a query builder.',
          steps: [
            'Replace template literal with parameterized query (e.g. db.query(sql, [param])).',
            'Use an ORM or query builder that handles escaping.',
            'If raw SQL is necessary, validate and sanitize all interpolated values.',
          ],
        },
        impact: 'SQL injection can expose, modify, or destroy database contents and potentially escalate to full server compromise.',
        tags: ['security', 'injection', 'sql'],
      }, 'security.sql-injection-risk', 'high', {
        sink: `sql template literal`,
        sanitizerStatus: 'missing',
        propagationSteps: [`${entry.file}:${s.lineStart}-${s.lineEnd}`],
      }));
    }
  }
  return findings;
}
 
export function detectUnsafeRegex(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    for (const re of entry.regexLiterals || []) {
      if (NESTED_QUANTIFIER_RE.test(re.pattern)) {
        findings.push(toSecurityFinding({
          severity: 'medium',
          category: 'unsafe-regex',
          file: entry.file,
          lineStart: re.lineStart,
          lineEnd: re.lineEnd,
          title: 'Regex with catastrophic backtracking risk',
          reason: `Pattern "${re.pattern.slice(0, 40)}" has nested quantifiers that can cause exponential backtracking (ReDoS).`,
          files: [entry.file],
          suggestedFix: {
            strategy: 'Simplify the regex or use atomic groups / possessive quantifiers.',
            steps: [
              'Remove nested quantifiers — e.g. change (a+)+ to a+.',
              'Use a regex linter (e.g. safe-regex) to validate patterns.',
              'Consider using string methods instead of complex regexes.',
            ],
          },
          impact: 'Catastrophic backtracking causes CPU exhaustion — a single crafted input string can hang the event loop (ReDoS).',
          tags: ['security', 'regex', 'performance'],
          lspHints: [
            {
              tool: 'lspFindReferences',
              symbolName: re.pattern.slice(0, 20),
              lineHint: re.lineStart,
              file: entry.file,
              expectedResult: `find where this regex is used to assess if user input reaches it`,
            },
          ],
        }, 'security.unsafe-regex', 'medium', {
          source: re.pattern,
          sink: `Regex execution`,
          sanitizerStatus: 'not-applicable',
          propagationSteps: [`${entry.file}:${re.lineStart}-${re.lineEnd}`],
        }));
      }
    }
  }
  return findings;
}
 
export function detectPrototypePollutionRisk(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    if (!entry.prototypePollutionSites || entry.prototypePollutionSites.length === 0) continue;
    for (const site of entry.prototypePollutionSites) {
      // Use guarded field to downgrade severity: guarded computed-property-writes are low risk
      let severity: Finding['severity'];
      let confidence: 'high' | 'medium' | 'low';
      if (site.kind === 'computed-property-write') {
        if (site.guarded) {
          severity = 'low';
          confidence = 'low';
        } else {
          severity = 'high';
          confidence = 'medium';
        }
      } else {
        severity = 'medium';
        confidence = 'medium';
      }
      findings.push(toSecurityFinding({
        severity,
        category: 'prototype-pollution-risk',
        file: entry.file,
        lineStart: site.lineStart,
        lineEnd: site.lineEnd,
        title: `Prototype pollution risk: ${site.kind}${site.guarded ? ' (guarded)' : ''}`,
        reason: `${site.detail}${site.guarded ? ' — guards detected (internal iteration or key check), likely false positive. Verify the key variable does not trace to external input.' : ''}`,
        files: [entry.file],
        suggestedFix: {
          strategy: 'Guard against __proto__, constructor, and prototype keys before merging.',
          steps: [
            'Validate keys: reject "__proto__", "constructor", "prototype" before assignment.',
            'Use Object.create(null) as the target for merges when possible.',
            'Replace custom deep-merge with a hardened library (e.g. lodash.merge with prototype guard).',
            'For Object.assign, ensure the source is sanitized or use structuredClone().',
          ],
        },
        impact: 'Prototype pollution can override built-in methods, bypass security checks, or achieve remote code execution.',
        tags: ['security', 'prototype-pollution', 'injection'],
        lspHints: [
          {
            tool: 'lspCallHierarchy',
            symbolName: site.kind === 'computed-property-write' ? 'bracket-assignment' : site.detail.split('(')[0],
            lineHint: site.lineStart,
            file: entry.file,
            expectedResult: `trace callers to determine if user-controlled data reaches this site — if key comes from Object.keys() on internal object, dismiss as false positive`,
          },
        ],
      }, 'security.prototype-pollution-risk', confidence, {
        source: site.kind,
        sink: site.detail,
        guarded: site.guarded,
        sanitizerStatus: site.guarded ? 'present' : 'missing',
        propagationSteps: [`${entry.file}:${site.lineStart}`],
      }));
    }
  }
  return findings;
}
 
export function detectUnvalidatedInputSink(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    for (const src of entry.inputSources || []) {
      if (!src.hasSinkInBody || src.hasValidation) continue;
      const sinkLabel = src.sinkKinds.join(', ');
      const severity = src.paramConfidence === 'low' ? 'medium' : 'high';
      findings.push(toSecurityFinding({
        severity,
        category: 'unvalidated-input-sink',
        file: entry.file,
        lineStart: src.lineStart,
        lineEnd: src.lineEnd,
        title: `Unvalidated input reaches ${sinkLabel} sink in ${src.functionName}(${src.sourceParams.join(', ')})`,
        reason: `Parameter${src.sourceParams.length > 1 ? 's' : ''} '${src.sourceParams.join("', '")}' (external input) flow${src.sourceParams.length === 1 ? 's' : ''} into ${sinkLabel} without validation (no type guard, schema call, or conditional check).`,
        files: [entry.file],
        suggestedFix: {
          strategy: 'Add input validation before the sink operation.',
          steps: [
            'Add schema validation (e.g. zod, joi) for input parameters.',
            'Use parameterized APIs instead of template interpolation for SQL/exec.',
            `Trace data flow: lspCallHierarchy(outgoing) on ${src.functionName}.`,
          ],
        },
        impact: 'Unvalidated external input reaching a dangerous sink (eval, SQL, exec, innerHTML, file write) enables injection attacks.',
        tags: ['security', 'input-validation', 'injection'],
        lspHints: [
          {
            tool: 'lspCallHierarchy',
            symbolName: src.functionName,
            lineHint: src.lineStart,
            file: entry.file,
            expectedResult: `trace outgoing calls to see where ${src.sourceParams.join(', ')} data flows`,
          },
          {
            tool: 'lspFindReferences',
            symbolName: src.sourceParams[0],
            lineHint: src.lineStart,
            file: entry.file,
            expectedResult: `check all usages of ${src.sourceParams[0]} parameter within function`,
          },
        ],
      }, 'security.unvalidated-input-sink', severity === 'high' ? 'high' : 'medium', {
        sourceParameters: src.sourceParams,
        sink: sinkLabel,
        sanitizerStatus: src.hasValidation ? 'present' : 'missing',
        propagationSteps: src.callsWithInputArgs.map((call) => `${call.callee}:${call.lineStart}`),
      }));
    }
  }
  return findings;
}
 
export function detectInputPassthroughRisk(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    for (const src of entry.inputSources || []) {
      if (src.callsWithInputArgs.length === 0 || src.hasValidation) continue;
      if (src.hasSinkInBody) continue;
      if (src.paramConfidence === 'low') continue;
      const callees = src.callsWithInputArgs.map(c => c.callee);
      const uniqueCallees = [...new Set(callees)];
      const severity = src.paramConfidence === 'high' ? 'medium' : 'low';
      findings.push(toSecurityFinding({
        severity,
        category: 'input-passthrough-risk',
        file: entry.file,
        lineStart: src.lineStart,
        lineEnd: src.lineEnd,
        title: `Input passthrough without validation in ${src.functionName}(${src.sourceParams.join(', ')})`,
        reason: `Parameter${src.sourceParams.length > 1 ? 's' : ''} '${src.sourceParams.join("', '")}' (external input) ${src.sourceParams.length === 1 ? 'is' : 'are'} passed to ${uniqueCallees.join(', ')} without validation. Downstream callees may not validate either.`,
        files: [entry.file],
        suggestedFix: {
          strategy: 'Validate input before passing to downstream functions.',
          steps: [
            'Add schema validation (e.g. zod, joi) at the entry point.',
            `Trace downstream: lspCallHierarchy(outgoing) on ${src.functionName} to verify callees validate.`,
            'Search for validation middleware: localSearchCode for guard/validate/sanitize patterns.',
          ],
        },
        impact: 'Unchecked input passed downstream can reach sinks in callees — validation gaps compound across the call chain.',
        tags: ['security', 'input-validation', 'passthrough'],
        lspHints: [
          {
            tool: 'lspCallHierarchy',
            symbolName: src.functionName,
            lineHint: src.lineStart,
            file: entry.file,
            expectedResult: `trace outgoing calls to verify downstream validation of ${src.sourceParams.join(', ')}`,
          },
          {
            tool: 'lspFindReferences',
            symbolName: src.sourceParams[0],
            lineHint: src.lineStart,
            file: entry.file,
            expectedResult: `find all usages of ${src.sourceParams[0]} to check if validation occurs upstream`,
          },
        ],
      }, 'security.input-passthrough-risk', severity, {
        sourceParameters: src.sourceParams,
        sink: uniqueCallees.join(', '),
        sanitizerStatus: src.hasValidation ? 'present' : 'missing',
        propagationSteps: src.callsWithInputArgs.map((call) => `${call.callee}:${call.lineStart}`),
      }));
    }
  }
  return findings;
}
 
// ─── Path Traversal Risk ──────────────────────────────────────────────────────
 
export function detectPathTraversalRisk(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    for (const src of entry.inputSources || []) {
      const fsReadSinks = src.sinkKinds.filter(k => k === 'fs-read' || k === 'path-resolve');
      if (fsReadSinks.length === 0) continue;
      if (src.paramConfidence === 'low') continue;
 
      const hasValidation = src.hasValidation;
      const severity: Finding['severity'] = hasValidation ? 'medium' : 'high';
      const sinkLabel = fsReadSinks.join(', ');
 
      findings.push(toSecurityFinding({
        severity,
        category: 'path-traversal-risk',
        file: entry.file,
        lineStart: src.lineStart,
        lineEnd: src.lineEnd,
        title: `Path traversal risk: ${src.functionName}(${src.sourceParams.join(', ')}) → ${sinkLabel}`,
        reason: `Parameter${src.sourceParams.length > 1 ? 's' : ''} '${src.sourceParams.join("', '")}' (external input) flow${src.sourceParams.length === 1 ? 's' : ''} into ${sinkLabel} ${hasValidation ? 'with partial validation — verify path normalization + prefix check + realpath resolution' : 'without validation. Path traversal (e.g. ../../etc/passwd) can read or write arbitrary files'}.`,
        files: [entry.file],
        suggestedFix: {
          strategy: 'Add multi-layer path validation before file system operations.',
          steps: [
            'Normalize the path: path.resolve(basePath, userInput).',
            'Prefix check: resolvedPath.startsWith(basePath + path.sep).',
            'Resolve symlinks: fs.realpathSync() to prevent symlink escape.',
            'Re-validate after symlink resolution.',
          ],
        },
        impact: 'Path traversal enables reading sensitive files (credentials, configs, source code) or writing to arbitrary locations (code injection via file overwrite).',
        tags: ['security', 'path-traversal', 'agentic'],
        lspHints: [
          {
            tool: 'lspCallHierarchy',
            symbolName: src.functionName,
            lineHint: src.lineStart,
            file: entry.file,
            expectedResult: `trace incoming callers to determine if path parameter comes from user input — then trace outgoing to the fs/path call`,
          },
        ],
      }, 'security.path-traversal-risk', src.paramConfidence === 'high' ? 'high' : 'medium', {
        sourceParameters: src.sourceParams,
        sink: sinkLabel,
        sanitizerStatus: hasValidation ? 'partial' : 'missing',
        propagationSteps: src.callsWithInputArgs.map((call) => `${call.callee}:${call.lineStart}`),
      }));
    }
  }
  return findings;
}
 
// ─── Command Injection Risk ──────────────────────────────────────────────────
 
export function detectCommandInjectionRisk(fileSummaries: FileEntry[]): FindingDraft[] {
  const findings: FindingDraft[] = [];
  for (const entry of fileSummaries) {
    if (isTestFile(entry.file)) continue;
    for (const src of entry.inputSources || []) {
      const execSinks = src.sinkKinds.filter(k => k === 'exec');
      if (execSinks.length === 0) continue;
      if (src.paramConfidence === 'low') continue;
 
      // Check callee names to distinguish exec (dangerous) from spawn with array args (safer)
      const execCallees = src.callsWithInputArgs.filter(c =>
        /\.exec\b|^exec$|^execSync$|child_process\.exec/.test(c.callee),
      );
      const spawnCallees = src.callsWithInputArgs.filter(c =>
        /\.spawn\b|^spawn$|^spawnSync$|child_process\.spawn/.test(c.callee),
      );
 
      // exec with string interpolation is critical, spawn with shell:true is high
      if (execCallees.length > 0) {
        const severity: Finding['severity'] = src.paramConfidence === 'high' ? 'critical' : 'high';
        findings.push(toSecurityFinding({
          severity,
          category: 'command-injection-risk',
          file: entry.file,
          lineStart: src.lineStart,
          lineEnd: src.lineEnd,
          title: `Command injection risk: ${src.functionName}(${src.sourceParams.join(', ')}) → exec`,
          reason: `Parameter${src.sourceParams.length > 1 ? 's' : ''} '${src.sourceParams.join("', '")}' (external input) flow${src.sourceParams.length === 1 ? 's' : ''} into exec/execSync. exec() runs commands through a shell — string interpolation enables command injection.`,
          files: [entry.file],
          suggestedFix: {
            strategy: 'Replace exec with spawn using array arguments (no shell interpretation).',
            steps: [
              'Replace child_process.exec(cmd) with child_process.spawn(binary, [args]).',
              'Never interpolate user input into command strings.',
              'Use an allowlist for permitted commands if dynamic dispatch is needed.',
              'If shell features are required, validate input against a strict allowlist.',
            ],
          },
          impact: 'Command injection enables arbitrary OS command execution — full server compromise, data exfiltration, or lateral movement.',
          tags: ['security', 'command-injection', 'critical', 'agentic'],
          lspHints: [
            {
              tool: 'lspCallHierarchy',
              symbolName: src.functionName,
              lineHint: src.lineStart,
              file: entry.file,
              expectedResult: `trace incoming callers to verify if user input reaches the exec call — check for allowlist or sanitization`,
            },
          ],
        }, 'security.command-injection-risk', src.paramConfidence === 'high' ? 'high' : 'medium', {
          sourceParameters: src.sourceParams,
          sink: 'exec',
          sanitizerStatus: src.hasValidation ? 'partial' : 'missing',
          propagationSteps: execCallees.map((call) => `${call.callee}:${call.lineStart}`),
        }));
      }
 
      if (spawnCallees.length > 0 && execCallees.length === 0) {
        // spawn is safer but shell:true makes it equivalent to exec
        findings.push(toSecurityFinding({
          severity: 'high',
          category: 'command-injection-risk',
          file: entry.file,
          lineStart: src.lineStart,
          lineEnd: src.lineEnd,
          title: `Potential command injection: ${src.functionName}(${src.sourceParams.join(', ')}) → spawn`,
          reason: `Parameter${src.sourceParams.length > 1 ? 's' : ''} '${src.sourceParams.join("', '")}' (external input) flow${src.sourceParams.length === 1 ? 's' : ''} into spawn. If shell:true is set, this is equivalent to exec. Verify spawn uses array args without shell option.`,
          files: [entry.file],
          suggestedFix: {
            strategy: 'Ensure spawn uses array arguments without shell: true.',
            steps: [
              'Verify spawn is called as spawn(binary, [arg1, arg2]) — NOT spawn(cmd, { shell: true }).',
              'Remove shell: true if present.',
              'Validate command arguments against an allowlist.',
            ],
          },
          impact: 'spawn with shell:true enables the same command injection as exec. Without shell:true, spawn with array args is safe from injection.',
          tags: ['security', 'command-injection', 'agentic'],
          lspHints: [
            {
              tool: 'lspCallHierarchy',
              symbolName: src.functionName,
              lineHint: src.lineStart,
              file: entry.file,
              expectedResult: `trace incoming callers — check if spawn uses shell:true option`,
            },
          ],
        }, 'security.command-injection-risk', 'medium', {
          sourceParameters: src.sourceParams,
          sink: 'spawn',
          sanitizerStatus: src.hasValidation ? 'partial' : 'missing',
          propagationSteps: spawnCallees.map((call) => `${call.callee}:${call.lineStart}`),
        }));
      }
    }
  }
  return findings;
}