@yagyeshvyas/vibeguard 0.1.0 → 1.1.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
  */
@@ -112,13 +114,15 @@ function newlyTainted(line, tainted) {
112
114
  }
113
115
 
114
116
  function referencesTainted(line, tainted) {
117
+ // Strip string literals so we don't match variable names inside SQL strings.
118
+ const stripped = line.replace(/(["'`])(?:[^\\]|\\.)*?\1/g, '""');
115
119
  for (const t of tainted) {
116
- if (new RegExp(`\\b${t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`).test(line)) return t;
120
+ if (new RegExp(`\\b${t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`).test(stripped)) return t;
117
121
  }
118
122
  return null;
119
123
  }
120
124
 
121
- function analyzeTaint(content, lines, relPath) {
125
+ function analyzeTaintRegex(content, lines, relPath) {
122
126
  const ext = path.extname(relPath || '').toLowerCase();
123
127
  if (!TAINT_EXT.has(ext)) return [];
124
128
 
@@ -138,6 +142,17 @@ function analyzeTaint(content, lines, relPath) {
138
142
  // Skip the inline source case — the regex rules already report it; taint
139
143
  // exists to catch the *multi-step* case where the source is elsewhere.
140
144
  if (SOURCE_RE.test(line)) continue;
145
+ // Skip parameterized queries: if the tainted var is inside an array
146
+ // literal (second arg), it's a bound parameter, not SQL injection.
147
+ // Heuristic: if the line has "query(" or "execute(" or "raw(" and the
148
+ // tainted var appears after a comma followed by [, skip it.
149
+ if (sink.ruleId === 'taint.sql-injection' && /,\s*\[/.test(line)) {
150
+ // Check if the tainted var is ONLY in the array, not in the SQL string.
151
+ const beforeArray = line.split(/,\s*\[/)[0];
152
+ const afterArray = ',' + line.split(/,\s*\[/).slice(1).join(',[');
153
+ const viaSql = referencesTainted(beforeArray, tainted);
154
+ if (!viaSql) continue; // tainted var only in params array — safe
155
+ }
141
156
  const via = referencesTainted(line, tainted);
142
157
  if (via) {
143
158
  findings.push({
@@ -165,4 +180,28 @@ function analyzeTaint(content, lines, relPath) {
165
180
  return findings;
166
181
  }
167
182
 
168
- module.exports = { analyzeTaint, SOURCE_RE, SINKS };
183
+ // Primary entry point: tries AST-based taint first, falls back to regex.
184
+ // Accepts an optional pre-parsed tree to avoid re-parsing (one parse per file).
185
+ function analyzeTaint(content, lines, relPath, tree) {
186
+ const ext = path.extname(relPath || '').toLowerCase();
187
+ if (!TAINT_EXT.has(ext)) return [];
188
+
189
+ // Try AST-based analysis (scope-aware, sanitizer-aware, cross-function).
190
+ try {
191
+ const ast = require('./ast');
192
+ if (ast.isAvailable()) {
193
+ const t = tree || ast.parseSource(content, relPath);
194
+ if (t) {
195
+ const { analyzeTaintAst } = require('./taint-ast');
196
+ return analyzeTaintAst(content, lines, relPath, t);
197
+ }
198
+ }
199
+ } catch {
200
+ // fall through to regex
201
+ }
202
+
203
+ // Fallback: regex-based taint (no scopes, no AST).
204
+ return analyzeTaintRegex(content, lines, relPath);
205
+ }
206
+
207
+ module.exports = { analyzeTaint, analyzeTaintRegex, SOURCE_RE, SINKS };