@yagyeshvyas/vibeguard 1.3.0 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/.vibeguard-mcp.example.json +51 -0
  2. package/CHANGELOG.md +604 -216
  3. package/MCP-FIREWALL.md +296 -0
  4. package/README.md +355 -31
  5. package/bin/cli.js +355 -27
  6. package/bin/cmd-agent-monitor.js +45 -0
  7. package/bin/cmd-agent-risk.js +64 -0
  8. package/bin/cmd-gif.js +62 -0
  9. package/bin/cmd-git-scan.js +74 -0
  10. package/bin/cmd-llm-proxy.js +164 -0
  11. package/bin/cmd-mcp-firewall.js +191 -0
  12. package/bin/cmd-pentest.js +103 -0
  13. package/bin/cmd-platform.js +137 -0
  14. package/bin/cmd-pr-scan.js +135 -0
  15. package/bin/cmd-sbom-diff.js +38 -0
  16. package/bin/cmd-skill-scan.js +35 -0
  17. package/docs/AISVS.md +25 -0
  18. package/docs/ARCHITECTURE.md +81 -0
  19. package/docs/INSTALLATION.md +139 -0
  20. package/docs/WINDOWS.md +74 -0
  21. package/docs/security-policy.strict.example.json +134 -0
  22. package/docs/security-policy.strict.example.pub.pem +3 -0
  23. package/package.json +45 -7
  24. package/src/.vibeguard/cache/file-hashes.json +60 -0
  25. package/src/.vibeguard/latest-snapshot.json +36 -0
  26. package/src/.vibeguard/mcp-pins.json +1 -0
  27. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/auto.js +428 -0
  28. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/daemon.js +344 -0
  29. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/dashboard.js +279 -0
  30. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/engine.js +297 -0
  31. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/hook.js +151 -0
  32. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/install.js +299 -0
  33. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/manifest.json +65 -0
  34. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/mcp-server.js +2051 -0
  35. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/plugin.js +168 -0
  36. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/pre-deploy.js +362 -0
  37. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/repl.js +150 -0
  38. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/rules.js +1611 -0
  39. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/sbom.js +164 -0
  40. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/shell-guard.js +292 -0
  41. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/slopsquat.js +144 -0
  42. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/trace.js +220 -0
  43. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/auto.js +428 -0
  44. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/daemon.js +344 -0
  45. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/engine.js +297 -0
  46. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/install.js +299 -0
  47. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/manifest.json +37 -0
  48. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/mcp-server.js +2055 -0
  49. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/plugin.js +168 -0
  50. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/pre-deploy.js +362 -0
  51. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/trace.js +220 -0
  52. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/auto.js +428 -0
  53. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/daemon.js +344 -0
  54. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/dashboard.js +279 -0
  55. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/engine.js +297 -0
  56. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/hook.js +151 -0
  57. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/install.js +299 -0
  58. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/manifest.json +65 -0
  59. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/mcp-server.js +2055 -0
  60. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/plugin.js +168 -0
  61. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/pre-deploy.js +362 -0
  62. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/repl.js +150 -0
  63. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/rules.js +1611 -0
  64. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/sbom.js +164 -0
  65. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/shell-guard.js +292 -0
  66. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/slopsquat.js +144 -0
  67. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/trace.js +220 -0
  68. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/auto.js +428 -0
  69. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/daemon.js +344 -0
  70. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/dashboard.js +279 -0
  71. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/engine.js +297 -0
  72. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/hook.js +151 -0
  73. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/install.js +299 -0
  74. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/manifest.json +65 -0
  75. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/mcp-server.js +2054 -0
  76. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/plugin.js +168 -0
  77. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/pre-deploy.js +362 -0
  78. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/repl.js +150 -0
  79. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/rules.js +1611 -0
  80. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/sbom.js +164 -0
  81. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/shell-guard.js +292 -0
  82. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/slopsquat.js +144 -0
  83. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/trace.js +220 -0
  84. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/auto.js +428 -0
  85. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/daemon.js +344 -0
  86. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/engine.js +297 -0
  87. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/install.js +299 -0
  88. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/manifest.json +37 -0
  89. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/mcp-server.js +2055 -0
  90. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/plugin.js +168 -0
  91. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/pre-deploy.js +362 -0
  92. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/trace.js +220 -0
  93. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/auto.js +428 -0
  94. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/daemon.js +344 -0
  95. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/engine.js +297 -0
  96. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/manifest.json +33 -0
  97. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/mcp-server.js +2055 -0
  98. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/rules.js +1611 -0
  99. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/shell-guard.js +292 -0
  100. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/trace.js +220 -0
  101. package/src/.vibeguard-baseline.json +5226 -0
  102. package/src/action-guard.js +84 -6
  103. package/src/agent-monitor.js +262 -0
  104. package/src/agent-policy.js +642 -0
  105. package/src/agent-risk.js +271 -0
  106. package/src/agentic.js +1513 -0
  107. package/src/ast.js +165 -7
  108. package/src/audit-trail.js +212 -67
  109. package/src/auto.js +427 -427
  110. package/src/autofix.js +11 -4
  111. package/src/bundles.js +198 -0
  112. package/src/daemon.js +343 -343
  113. package/src/dashboard.js +278 -278
  114. package/src/engine.js +3 -0
  115. package/src/entropy.js +187 -0
  116. package/src/firewall.js +19 -3
  117. package/src/gif.js +134 -0
  118. package/src/gitscan.js +282 -0
  119. package/src/hook.js +151 -151
  120. package/src/injection-decode.js +136 -0
  121. package/src/install.js +298 -280
  122. package/src/integrity.js +16 -1
  123. package/src/integrity.json +43 -13
  124. package/src/interceptor.js +12 -9
  125. package/src/llm-proxy.js +986 -0
  126. package/src/mcp-audit.js +61 -5
  127. package/src/mcp-firewall.js +971 -0
  128. package/src/mcp-server.js +2500 -2047
  129. package/src/obfuscation.js +315 -0
  130. package/src/owasp-agentic.js +314 -0
  131. package/src/pentest.js +589 -0
  132. package/src/pii.js +4 -0
  133. package/src/platform/aisvs.js +38 -0
  134. package/src/platform/approval.js +187 -0
  135. package/src/platform/canonical.js +73 -0
  136. package/src/platform/contracts.js +158 -0
  137. package/src/platform/delegation.js +73 -0
  138. package/src/platform/http-gateway.js +177 -0
  139. package/src/platform/identity.js +253 -0
  140. package/src/platform/jwt.js +86 -0
  141. package/src/platform/mcp-http-bridge.js +199 -0
  142. package/src/platform/memory-guard.js +64 -0
  143. package/src/platform/os-sandbox.js +144 -0
  144. package/src/platform/policy.js +332 -0
  145. package/src/platform/sandbox-manager.js +92 -0
  146. package/src/platform/signed-audit.js +357 -0
  147. package/src/platform/ssrf.js +129 -0
  148. package/src/plugin.js +182 -168
  149. package/src/pre-deploy.js +170 -55
  150. package/src/privacy-audit.js +272 -272
  151. package/src/repl.js +150 -150
  152. package/src/report.js +7 -1
  153. package/src/routes.js +1210 -0
  154. package/src/rules-2026.js +1407 -0
  155. package/src/rules-pack.js +3068 -3019
  156. package/src/rules.js +376 -60
  157. package/src/sandbox.js +200 -10
  158. package/src/sbom.js +193 -164
  159. package/src/scanner.js +189 -33
  160. package/src/shell-guard.js +11 -0
  161. package/src/skill-scan.js +477 -0
  162. package/src/slopsquat.js +144 -144
  163. package/src/surface.js +408 -0
  164. package/src/taint-ast.js +204 -37
  165. package/src/taint.js +15 -3
  166. package/src/tokenlean.js +579 -0
  167. package/src/trace.js +220 -220
package/src/taint-ast.js CHANGED
@@ -27,6 +27,9 @@ const SOURCE_PROPS = new Set([
27
27
  'queryStringParameters', 'pathParameters',
28
28
  ]);
29
29
 
30
+ // The only `process` properties that carry input from outside the program.
31
+ const PROCESS_SOURCE_PROPS = new Set(['argv', 'argv0', 'env']);
32
+
30
33
  function isSourceNode(node) {
31
34
  if (!node) return false;
32
35
  // process.argv, location.search, location.hash, document.URL
@@ -38,6 +41,13 @@ function isSourceNode(node) {
38
41
  if (prop === 'searchParams') return true;
39
42
  // location.search, location.hash, document.URL
40
43
  if ((root === 'location' || root === 'window' || root === 'document') && SOURCE_PROPS.has(prop)) return true;
44
+ // `process` is only partly attacker-influenced. argv/env carry outside
45
+ // input; execPath, platform, version, pid, cwd() and friends are facts
46
+ // about the running process that no attacker controls. Treating the whole
47
+ // namespace as a source made `spawn(process.execPath, [script])` — the
48
+ // standard way to launch a child Node process — look like command
49
+ // injection.
50
+ if (root === 'process') return PROCESS_SOURCE_PROPS.has(prop);
41
51
  // request.body, req.body etc. — also match deeper: req.body.x
42
52
  if (SOURCE_ROOTS.has(root)) return true;
43
53
  return false;
@@ -49,6 +59,24 @@ function isSourceNode(node) {
49
59
  return false;
50
60
  }
51
61
 
62
+ // Allowlist/whitelist guard: `allowed.includes(v) ? v : "default"` (or
63
+ // `X.indexOf(v) !== -1 ? v : "default"`, `X.has(v) ? v : d`) only ever returns a
64
+ // value constrained to the container (or the safe default), so the conditional is
65
+ // NOT a taint source. Used by both exprIsSource and exprTaintInfo so the
66
+ // allowlist pattern is not flagged as traversal/injection.
67
+ function isAllowlistGuardConditional(node) {
68
+ if (!node || node.type !== 'ConditionalExpression') return false;
69
+ const memProp = (n) =>
70
+ (n && n.type === 'MemberExpression' && n.property &&
71
+ (n.property.type === 'Identifier' ? n.property.name : ''));
72
+ const memCall = (n) =>
73
+ n && n.type === 'CallExpression' &&
74
+ ['includes', 'has', 'indexOf', 'startsWith', 'endsWith'].includes(memProp(n.callee)) &&
75
+ (n.arguments || []).length > 0;
76
+ const t = node.test;
77
+ return memCall(t) || (t && t.type === 'BinaryExpression' && (memCall(t.left) || memCall(t.right)));
78
+ }
79
+
52
80
  // ---------------------------------------------------------------------------
53
81
  // Sinks — matched on CallExpression nodes.
54
82
  // Kept identical ruleId/severity/title/message/fix to the regex SINKS in taint.js.
@@ -132,8 +160,18 @@ const SINKS_AST = [
132
160
  },
133
161
  {
134
162
  match: (call) => {
135
- const prop = (call.callee && call.callee.property && call.callee.property.name) || '';
136
- return prop === 'send' || prop === 'write' || prop === 'end';
163
+ const callee = call.callee || {};
164
+ const prop = (callee.property && callee.property.name) || '';
165
+ if (prop !== 'send' && prop !== 'write' && prop !== 'end') return false;
166
+ // process.stdout/stderr and console writes are NOT HTML responses —
167
+ // CLI tools log user input to stderr routinely; that is not reflected XSS.
168
+ const root = ast.rootName(callee);
169
+ if (root === 'process' || root === 'console' || root === 'logger' || root === 'log') return false;
170
+ // Client/transport-side calls — req.end(), socket.write() — are NOT
171
+ // HTML responses. Only response-ish objects (res, response, reply,
172
+ // ctx) are XSS sinks. (req.end() is how a client finishes a request.)
173
+ if (root === 'req' || root === 'request' || root === 'socket' || root === 'connection' || root === 'clientRequest' || root === 'upstreamReq' || root === 'upstreamRes') return false;
174
+ return true;
137
175
  },
138
176
  ruleId: 'taint.xss-reflected',
139
177
  severity: 'high',
@@ -400,6 +438,27 @@ function exprTaintInfo(node, scope) {
400
438
  // (We handle this at the assignment site too, but check here for safety.)
401
439
  const sanitizers = scope._sanitizers || DEFAULT_SANITIZERS;
402
440
  if (isSanitizerCall(node, sanitizers)) return null;
441
+
442
+ // Return-value taint through a thin accessor:
443
+ // function getId(r) { return r.body.id; }
444
+ // db.query(`... ${getId(req)}`)
445
+ // The helper was recorded as returning a value derived from param N, so
446
+ // the call is tainted exactly when argument N is a request-like source.
447
+ // Requiring a real source at the call site keeps `getId(config)` clean.
448
+ const returnsParam = scope._funcReturnsParam;
449
+ if (returnsParam) {
450
+ const calleeName = node.callee && node.callee.type === 'Identifier' ? node.callee.name : null;
451
+ const slots = calleeName ? returnsParam.get(calleeName) : null;
452
+ if (slots) {
453
+ for (const slot of slots) {
454
+ const arg = (node.arguments || [])[slot];
455
+ if (arg && exprIsSource(arg)) {
456
+ return { via: `${calleeName}() returns param:${slot}`, sanitized: false };
457
+ }
458
+ }
459
+ }
460
+ }
461
+
403
462
  // Check if any argument is tainted (taint through function call, best-effort).
404
463
  for (const arg of node.arguments || []) {
405
464
  const info = exprTaintInfo(arg, scope);
@@ -411,6 +470,9 @@ function exprTaintInfo(node, scope) {
411
470
  return null;
412
471
  }
413
472
  case 'ConditionalExpression': {
473
+ // Allowlist/whitelist guard: `allowed.includes(v) ? v : "default"` only ever
474
+ // returns a value constrained to the container — result is NOT tainted.
475
+ if (isAllowlistGuardConditional(node)) return null;
414
476
  const c = exprTaintInfo(node.consequent, scope);
415
477
  if (c) return c;
416
478
  return exprTaintInfo(node.alternate, scope);
@@ -468,6 +530,40 @@ function exprTaintInfo(node, scope) {
468
530
  }
469
531
  }
470
532
 
533
+ /*
534
+ * Is the expression a BARE source — the untouched user value itself, rather
535
+ * than something built out of it?
536
+ *
537
+ * req.body.id -> bare
538
+ * req.query.f -> bare
539
+ * `SELECT ... ${req.body.id}` -> NOT bare (constructed)
540
+ * "SELECT ... " + req.body.id -> NOT bare (constructed)
541
+ *
542
+ * The distinction decides whether the AST taint pass may defer a sink argument
543
+ * to the regex rules. Deferring is only defensible for a bare source, where the
544
+ * regex layer sees the same single expression the taint layer would. The moment
545
+ * the value is interpolated or concatenated into a larger string, the regex
546
+ * layer is looking at different text and the deferral silently drops the
547
+ * finding — which is exactly how `db.query(`... ${req.body.id}`)`, the single
548
+ * most common shape of SQL injection in AI-generated code, used to scan clean.
549
+ */
550
+ function exprIsBareSource(node) {
551
+ if (!node) return false;
552
+ if (isSourceNode(node)) return true;
553
+ switch (node.type) {
554
+ // Property access off a source is still the raw value: req.body.user.id.
555
+ case 'MemberExpression':
556
+ return exprIsBareSource(node.object);
557
+ case 'ChainExpression':
558
+ return exprIsBareSource(node.expression);
559
+ case 'AwaitExpression':
560
+ return exprIsBareSource(node.argument);
561
+ // Everything else builds a NEW value out of the source. Not bare.
562
+ default:
563
+ return false;
564
+ }
565
+ }
566
+
471
567
  // Does the expression directly contain a source?
472
568
  function exprIsSource(node) {
473
569
  if (!node) return false;
@@ -482,6 +578,9 @@ function exprIsSource(node) {
482
578
  case 'CallExpression':
483
579
  return (node.arguments || []).some(exprIsSource);
484
580
  case 'ConditionalExpression':
581
+ // An allowlist-guarded ternary is not a raw source — the value is
582
+ // constrained to the container (e.g. `allowed.includes(x) ? x : d`).
583
+ if (isAllowlistGuardConditional(node)) return false;
485
584
  return exprIsSource(node.consequent) || exprIsSource(node.alternate);
486
585
  case 'LogicalExpression':
487
586
  return exprIsSource(node.left) || exprIsSource(node.right);
@@ -507,12 +606,15 @@ function exprIsSource(node) {
507
606
  function analyzeTaintAst(content, lines, relPath, tree, sanitizers) {
508
607
  const walk = ast.getWalk();
509
608
  if (!walk || !tree) return [];
510
- const ext = require('path').extname(relPath || '').toLowerCase();
511
609
  const sanList = sanitizers || DEFAULT_SANITIZERS;
512
610
 
513
611
  const findings = [];
612
+ // funcName -> Set(paramIndex) the function returns a value derived from.
613
+ // Declared before the root scope because every scope carries a reference.
614
+ const funcReturnsParam = new Map();
514
615
  const rootScope = new TaintScope(null, getScopeDeclarations(tree));
515
616
  rootScope._sanitizers = sanList;
617
+ rootScope._funcReturnsParam = funcReturnsParam;
516
618
 
517
619
  // Scope stack: maintain current scope as we traverse.
518
620
  let currentScope = rootScope;
@@ -651,6 +753,46 @@ function analyzeTaintAst(content, lines, relPath, tree, sanitizers) {
651
753
  if (sinks.length > 0) {
652
754
  funcSinkInfo.set(name, sinks);
653
755
  }
756
+
757
+ // Return-value taint: does this function hand BACK a value derived from one
758
+ // of its own parameters?
759
+ //
760
+ // function getId(r) { return r.body.id; }
761
+ // db.query(`SELECT * FROM u WHERE id = ${getId(req)}`);
762
+ //
763
+ // The param->sink analysis above covers taint flowing INTO a helper. This
764
+ // covers the other direction, which is how request objects are laundered in
765
+ // practice: a thin accessor pulls the value out and the caller interpolates
766
+ // it. Recording only the parameter index keeps this precise — the call site
767
+ // still has to pass an actually-tainted argument for anything to fire, so a
768
+ // helper called with a constant stays clean.
769
+ const returnsParams = new Set();
770
+ function walkForReturns(node) {
771
+ if (!node || typeof node !== 'object') return;
772
+ if (node !== info.node && isScopeNode(node) && node.type !== 'BlockStatement') return;
773
+ const returned =
774
+ node.type === 'ReturnStatement'
775
+ ? node.argument
776
+ : // Expression-bodied arrow: `const getId = (r) => r.body.id;`
777
+ node === info.node && info.node.type === 'ArrowFunctionExpression' && info.node.body && info.node.body.type !== 'BlockStatement'
778
+ ? info.node.body
779
+ : null;
780
+ if (returned) {
781
+ const ti = exprTaintInfo(returned, fnScope);
782
+ if (ti && !ti.sanitized) {
783
+ const m = /param:(\d+)/.exec(ti.via || '');
784
+ if (m) returnsParams.add(parseInt(m[1], 10));
785
+ }
786
+ }
787
+ for (const key in node) {
788
+ if (key === 'type' || key === 'loc' || key === 'start' || key === 'end' || key === 'range') continue;
789
+ const child = node[key];
790
+ if (Array.isArray(child)) child.forEach(walkForReturns);
791
+ else if (child && typeof child === 'object' && child.type) walkForReturns(child);
792
+ }
793
+ }
794
+ walkForReturns(info.node);
795
+ if (returnsParams.size > 0) funcReturnsParam.set(name, returnsParams);
654
796
  }
655
797
 
656
798
  // Now walk the full tree, maintaining scopes and tracking taint.
@@ -660,6 +802,7 @@ function analyzeTaintAst(content, lines, relPath, tree, sanitizers) {
660
802
  const decls = getScopeDeclarations(node);
661
803
  const scope = new TaintScope(currentScope, decls);
662
804
  scope._sanitizers = sanList;
805
+ scope._funcReturnsParam = funcReturnsParam;
663
806
  currentScope = scope;
664
807
  }
665
808
 
@@ -850,16 +993,65 @@ function analyzeTaintAst(content, lines, relPath, tree, sanitizers) {
850
993
  // For execFile/spawn: only arg[0] (command) — args array (arg[1]) is safe.
851
994
  // For query/execute/raw: only arg[0] (SQL string) — params array (arg[1]) is safe.
852
995
  const name = ast.calleeName(call);
853
- const isFirstArgOnly = /(?:^|\.)(?:execFile|execFileSync|spawn|spawnSync)$/.test(name) ||
854
- (sinkDef.ruleId === 'taint.sql-injection');
996
+ const isShellExecCall = /(?:^|\\.)(?:execFile|execFileSync|spawn|spawnSync)$/.test(name);
997
+ let isFirstArgOnly = isShellExecCall || (sinkDef.ruleId === 'taint.sql-injection');
998
+ // Shell-exec caveat: execFile/spawn args are normally safe because no shell
999
+ // interprets them — UNLESS the command IS a shell binary and an arg is a
1000
+ // shell flag (-c / -k / /c / -command). Then the remaining args are
1001
+ // shell-interpreted: the classic spawn("sh", ["-c", tainted]) RCE. In that
1002
+ // case the whole args array is dangerous, so check every argument.
1003
+ if (isShellExecCall) {
1004
+ const cargs = call.arguments || [];
1005
+ const cmd = cargs[0];
1006
+ const cmdStr = cmd && cmd.type === 'Literal' && typeof cmd.value === 'string' ? cmd.value : null;
1007
+ const isShellBinary = !!cmdStr && /^(?:sh|bash|zsh|ksh|dash|fish|csh|tcsh|cmd|cmd\.exe|powershell|powershell\.exe|pwsh|nu|busybox)$/i.test(cmdStr);
1008
+ // spawn/execFile take the args as a single array: spawn("sh", ["-c", "cd "+d]).
1009
+ // Gather every string literal among the non-command args (including inside
1010
+ // arrays/objects) to detect a shell flag (-c/-k//c/...).
1011
+ let hasShellFlag = false;
1012
+ const collectStr = (node) => {
1013
+ if (!node) return;
1014
+ if (node.type === 'Literal' && typeof node.value === 'string') {
1015
+ if (/^(?:-c|-k|-command|\/c|\/k)$/i.test(node.value.trim())) hasShellFlag = true;
1016
+ return;
1017
+ }
1018
+ if (node.elements) node.elements.forEach(collectStr);
1019
+ if (node.properties) node.properties.forEach((p) => { if (p.value) collectStr(p.value); });
1020
+ if (node.arguments) node.arguments.forEach(collectStr);
1021
+ };
1022
+ cargs.slice(1).forEach(collectStr);
1023
+ if (isShellBinary && hasShellFlag) isFirstArgOnly = false;
1024
+ }
855
1025
  const argsToCheck = isFirstArgOnly ? (call.arguments || []).slice(0, 1) : (call.arguments || []);
856
1026
 
857
1027
  for (const arg of argsToCheck) {
858
- // Skip if the argument itself is a source — regex rules handle that.
859
- // BUT: for PII-to-LLM and XSS-reflected sinks, the source may be nested
860
- // deep inside an object (e.g. { messages: [{ content: req.body.x }] })
861
- // and regex rules don't catch that — so don't skip for those sinks.
862
- if (sinkDef.ruleId !== 'taint.pii-to-llm' && sinkDef.ruleId !== 'taint.xss-reflected' && exprIsSource(arg)) continue;
1028
+ // Defer to the regex rules only when the argument is the BARE source —
1029
+ // there the regex layer sees the same expression we would, so firing
1030
+ // both would just duplicate the finding.
1031
+ //
1032
+ // A CONSTRUCTED argument (template literal, concatenation, object) is a
1033
+ // different matter: the regex layer is matching different text and
1034
+ // routinely misses it, so the taint pass must own that case. Deferring
1035
+ // it is how `db.query(`SELECT ... ${req.body.id}`)` — textbook SQL
1036
+ // injection — used to produce a clean Grade A scan.
1037
+ //
1038
+ // PII-to-LLM and XSS-reflected never defer: their sources nest deep
1039
+ // inside objects (`{ messages: [{ content: req.body.x }] }`) where the
1040
+ // regex layer has no chance at all.
1041
+ //
1042
+ // The three code-execution-grade sinks (SQL / shell / eval) never defer
1043
+ // either. Handing `db.query(req.body.sql)` to the regex layer assumed a
1044
+ // regex rule covers it; none does, so that shape scanned clean too.
1045
+ // Duplicate coverage on a line is resolved by dedupeFindings(), which
1046
+ // keeps the dataflow-confirmed finding and drops the regex one.
1047
+ const NEVER_DEFER = new Set([
1048
+ 'taint.pii-to-llm',
1049
+ 'taint.xss-reflected',
1050
+ 'taint.sql-injection',
1051
+ 'taint.command-injection',
1052
+ 'taint.code-injection',
1053
+ ]);
1054
+ if (!NEVER_DEFER.has(sinkDef.ruleId) && exprIsBareSource(arg)) continue;
863
1055
 
864
1056
  // For PII-to-LLM: only fire if the tainted value is a whole-object source
865
1057
  // (req.body, JSON.stringify(userProfile)) or a PII-specific field.
@@ -870,7 +1062,7 @@ function analyzeTaintAst(content, lines, relPath, tree, sanitizers) {
870
1062
  if (!containsPiiSource(arg, PII_FIELDS)) continue;
871
1063
  }
872
1064
 
873
- let taintInfo = exprTaintInfo(arg, currentScope);
1065
+ const taintInfo = exprTaintInfo(arg, currentScope);
874
1066
  if (!taintInfo) {
875
1067
  // Also check: is the arg a call to a function whose param reaches a sink,
876
1068
  // and is the call's argument tainted? (cross-function, return-value taint)
@@ -908,7 +1100,7 @@ function analyzeTaintAst(content, lines, relPath, tree, sanitizers) {
908
1100
  const calleeName = call.callee && call.callee.type === 'Identifier' ? call.callee.name : '';
909
1101
  const sinkInfo = funcSinkInfo.get(calleeName);
910
1102
  if (sinkInfo) {
911
- for (const { paramIndex, sinkDef, node: sinkNode } of sinkInfo) {
1103
+ for (const { paramIndex, sinkDef, node: _sinkNode } of sinkInfo) {
912
1104
  const arg = (call.arguments || [])[paramIndex];
913
1105
  if (!arg) continue;
914
1106
  if (exprIsSource(arg)) {
@@ -963,31 +1155,6 @@ function analyzeTaintAst(content, lines, relPath, tree, sanitizers) {
963
1155
  // (No action needed here — exprTaintInfo already checks for sanitizer calls.)
964
1156
  }
965
1157
 
966
- // Collect all identifier names referenced in a subtree.
967
- function collectAllRefs(node, out) {
968
- out = out || new Set();
969
- if (!node || typeof node !== 'object') return out;
970
- if (node.type === 'Identifier') {
971
- out.add(node.name);
972
- return out;
973
- }
974
- if (node.type === 'MemberExpression') {
975
- const r = ast.rootName(node);
976
- if (r) out.add(r);
977
- return out;
978
- }
979
- for (const key in node) {
980
- if (key === 'type' || key === 'loc' || key === 'start' || key === 'end' || key === 'range') continue;
981
- const child = node[key];
982
- if (Array.isArray(child)) {
983
- child.forEach((c) => collectAllRefs(c, out));
984
- } else if (child && typeof child === 'object' && child.type) {
985
- collectAllRefs(child, out);
986
- }
987
- }
988
- return out;
989
- }
990
-
991
1158
  // Start the walk.
992
1159
  visit(tree);
993
1160
 
package/src/taint.js CHANGED
@@ -182,7 +182,18 @@ function analyzeTaintRegex(content, lines, relPath) {
182
182
 
183
183
  // Primary entry point: tries AST-based taint first, falls back to regex.
184
184
  // Accepts an optional pre-parsed tree to avoid re-parsing (one parse per file).
185
- function analyzeTaint(content, lines, relPath, tree) {
185
+ /*
186
+ * `onDegrade` (optional): called when the AST pass fails and analysis silently
187
+ * drops to the weaker regex engine.
188
+ *
189
+ * The fallback itself is correct — regex taint is better than no taint. What is
190
+ * NOT acceptable is doing it quietly: a crash in the highest-precision pass
191
+ * makes every dataflow-confirmed finding disappear while the scan still reports
192
+ * a clean grade. That is a security scanner failing open and telling the user
193
+ * everything is fine. The scanner wires this to its degraded-coverage
194
+ * diagnostics so `--strict` can refuse to call such a scan clean.
195
+ */
196
+ function analyzeTaint(content, lines, relPath, tree, onDegrade) {
186
197
  const ext = path.extname(relPath || '').toLowerCase();
187
198
  if (!TAINT_EXT.has(ext)) return [];
188
199
 
@@ -196,8 +207,9 @@ function analyzeTaint(content, lines, relPath, tree) {
196
207
  return analyzeTaintAst(content, lines, relPath, t);
197
208
  }
198
209
  }
199
- } catch {
200
- // fall through to regex
210
+ } catch (err) {
211
+ // Fall through to regex, but never silently.
212
+ if (typeof onDegrade === 'function') onDegrade(err);
201
213
  }
202
214
 
203
215
  // Fallback: regex-based taint (no scopes, no AST).