@yagyeshvyas/vibeguard 0.1.0 → 1.0.0

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.
package/src/taint.js CHANGED
@@ -1,19 +1,21 @@
1
1
  'use strict';
2
2
 
3
3
  /*
4
- * Lightweight intra-file taint analysis (no AST, no dependency).
4
+ * Intra-file taint analysis — AST-based when acorn is available, regex fallback.
5
5
  *
6
- * Regex rules only see one line. This follows user-controlled input across
7
- * assignments within a file, so it catches the multi-step case a single-line
8
- * regex misses:
6
+ * The primary path is the scope-aware AST analysis in taint-ast.js, which
7
+ * handles variable declarations, assignments, template literals, binary
8
+ * string concat, function parameters, object property reads/writes, block/
9
+ * function scope with shadowing, reassignment clearing, and sanitizer
10
+ * recognition. Sinks are matched on CallExpression nodes, not text.
9
11
  *
10
- * const name = req.body.name; // <- source, `name` becomes tainted
11
- * const cmd = "ls " + name; // <- taint propagates to `cmd`
12
- * exec(cmd); // <- tainted value reaches a shell sink
12
+ * When acorn is unavailable or a file doesn't parse, this module falls back
13
+ * to the original regex-based taint pass (below) so behavior never regresses
14
+ * on unparseable files.
13
15
  *
14
- * It is deliberately conservative (top-down, whole-file variable set, no scopes).
15
- * A dataflow-confirmed hit is HIGH confidence — we saw input actually reach the
16
- * sink — which is stronger than the regex heuristics.
16
+ * Public export analyzeTaint(content, lines, relPath) is unchanged; an
17
+ * optional tree argument is accepted to reuse a preparsed AST (one parse
18
+ * per file, threaded through scanFileContent).
17
19
  *
18
20
  * Scope: JS/TS-family files. Best-effort, not a substitute for a real taint tool.
19
21
  */
@@ -118,7 +120,7 @@ function referencesTainted(line, tainted) {
118
120
  return null;
119
121
  }
120
122
 
121
- function analyzeTaint(content, lines, relPath) {
123
+ function analyzeTaintRegex(content, lines, relPath) {
122
124
  const ext = path.extname(relPath || '').toLowerCase();
123
125
  if (!TAINT_EXT.has(ext)) return [];
124
126
 
@@ -165,4 +167,28 @@ function analyzeTaint(content, lines, relPath) {
165
167
  return findings;
166
168
  }
167
169
 
168
- module.exports = { analyzeTaint, SOURCE_RE, SINKS };
170
+ // Primary entry point: tries AST-based taint first, falls back to regex.
171
+ // Accepts an optional pre-parsed tree to avoid re-parsing (one parse per file).
172
+ function analyzeTaint(content, lines, relPath, tree) {
173
+ const ext = path.extname(relPath || '').toLowerCase();
174
+ if (!TAINT_EXT.has(ext)) return [];
175
+
176
+ // Try AST-based analysis (scope-aware, sanitizer-aware, cross-function).
177
+ try {
178
+ const ast = require('./ast');
179
+ if (ast.isAvailable()) {
180
+ const t = tree || ast.parseSource(content, relPath);
181
+ if (t) {
182
+ const { analyzeTaintAst } = require('./taint-ast');
183
+ return analyzeTaintAst(content, lines, relPath, t);
184
+ }
185
+ }
186
+ } catch {
187
+ // fall through to regex
188
+ }
189
+
190
+ // Fallback: regex-based taint (no scopes, no AST).
191
+ return analyzeTaintRegex(content, lines, relPath);
192
+ }
193
+
194
+ module.exports = { analyzeTaint, analyzeTaintRegex, SOURCE_RE, SINKS };