fallow-extract 3.1.0

AST extraction engine for fallow codebase intelligence (parser, complexity, SFC / Astro / MDX / CSS)
Documentation
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
#[allow(
    clippy::wildcard_imports,
    reason = "many taint source helper AST types used"
)]
use oxc_ast::ast::*;
use oxc_span::GetSpan;

use fallow_types::extract::TaintedBinding;

use super::super::{ModuleInfoExtractor, extract_destructured_names};
use super::{
    DIRECT_TAINT_HOP, FRAMEWORK_REQUEST_SOURCE, GRAPHQL_ARGS_SOURCE, MAX_TAINT_BINDING_HOPS,
    MCP_TOOL_INPUT_SOURCE, NEXT_FORM_DATA_SOURCE, NEXT_REQUEST_SOURCE, QUEUE_JOB_SOURCE,
    TRPC_INPUT_SOURCE, apply_source_return_path, binding_source_path_candidates, callback_params,
    callee_method_name, collect_chained_taint_idents, destructure_source_path,
    extract_function_body_final_return_expr, extract_object_pattern_bindings, flatten_callee_path,
    function_body_has_use_server, function_like_params, is_framework_route_receiver_path,
    is_http_route_handler_name, is_route_registration_method, is_trpc_procedure_callee,
    is_trpc_procedure_method, last_callback_params, route_callback_params, source_returning_helper,
    unwrap_parens,
};

impl ModuleInfoExtractor {
    /// Record a tainted binding together with its chain-hop depth, deduped on
    /// `(local, source_path)`. On a duplicate the existing record keeps its
    /// span (first read wins, matching the analyze layer's name-keyed
    /// `or_insert`) and the SHALLOWER hop depth, so a direct source capture
    /// (hop 1) is never demoted past the chain cap by a chained re-record of
    /// the same pair, and a chained record is never inflated by a later one.
    fn push_tainted_binding(&mut self, binding: TaintedBinding, hop: u8) {
        debug_assert_eq!(
            self.tainted_bindings.len(),
            self.tainted_binding_hops.len(),
            "tainted_binding_hops must stay aligned with tainted_bindings"
        );
        if let Some(index) = self
            .tainted_bindings
            .iter()
            .position(|b| b.local == binding.local && b.source_path == binding.source_path)
        {
            self.tainted_binding_hops[index] = self.tainted_binding_hops[index].min(hop);
            return;
        }
        self.tainted_bindings.push(binding);
        self.tainted_binding_hops.push(hop);
    }

    /// Record tainted-source bindings for `const <name> = <object>.<prop>` and
    /// for one-hop local expressions that embed a source member access. The
    /// recorded candidates include the exact member path and the flattened
    /// object path, so `const id = req.query.id` still records `req.query`
    /// while leaf sources such as `const ref = document.referrer` can match
    /// exact source rows.
    /// Captured at any scope (no `is_module_scope` gate): a sink inside a route
    /// handler reading a function-local source is exactly the target case.
    pub(super) fn record_tainted_source_binding(&mut self, name: &str, expr: &Expression<'_>) {
        for source_path in binding_source_path_candidates(expr) {
            self.push_tainted_binding(
                TaintedBinding {
                    local: name.to_string(),
                    source_path,
                    // The initializer member-access read (`req.query.id`); anchors
                    // the taint trace's source node at the real read line.
                    source_span_start: expr.span().start,
                },
                DIRECT_TAINT_HOP,
            );
        }
    }

    fn record_tainted_param_binding(&mut self, name: &str, source_path: &'static str) {
        self.push_tainted_binding(
            TaintedBinding {
                local: name.to_string(),
                source_path: source_path.to_string(),
                // Synthetic framework-handler-param source (e.g. `framework.request`);
                // no concrete member-access read expression, so the analyze layer
                // anchors at the sink rather than a spurious line. `0` signals "no
                // captured read span".
                source_span_start: 0,
            },
            DIRECT_TAINT_HOP,
        );
    }

    /// Chain a declarator's taint from already-tainted local bindings that its
    /// initializer references through the conservative #1095 expression shapes
    /// plus a bare-identifier alias (issue #1146). Each chained binding carries
    /// the ORIGINAL `source_path` and `source_span_start`, so the analyze layer
    /// needs no changes: the chained local matches `source_tainted_locals` and
    /// anchors its trace at the original source read. Chains only form in
    /// lexical declarator order within one extractor walk; use-before-declaration
    /// flows and cross-SFC-block chains are silent false negatives by design
    /// (FN-preferring, matching the #885 doctrine).
    pub(super) fn record_chained_tainted_binding(&mut self, name: &str, init: &Expression<'_>) {
        let mut idents = Vec::new();
        collect_chained_taint_idents(init, &mut idents);
        if idents.is_empty() {
            return;
        }
        let chained = self.chained_bindings_for_idents(name, &idents);
        for (binding, hop) in chained {
            self.push_tainted_binding(binding, hop);
        }
    }

    /// Chained `(binding, hop)` records for a new local named `name` whose
    /// initializer references `idents`. Referenced bindings already at
    /// `MAX_TAINT_BINDING_HOPS` contribute nothing, so an over-cap chain is
    /// simply not recorded and the sink degrades to module-level reachability;
    /// a false arg-level claim past the cap is structurally impossible.
    fn chained_bindings_for_idents(
        &self,
        name: &str,
        idents: &[String],
    ) -> Vec<(TaintedBinding, u8)> {
        let mut out = Vec::new();
        for ident in idents {
            // No `nested_scope_shadows` guard on the referenced ident: tainted
            // bindings are recorded scope-insensitively and name-keyed (the
            // direct recorders do the same), and the chain root is normally a
            // local declared in the SAME function body, which the nested
            // declaration stack already contains. Guarding on it would reject
            // the common `const a = req.query.id; const b = ...a...` case
            // inside a route handler. Cross-scope name collisions are the
            // accepted Risk 1, bounded by the hop cap.
            if ident == name {
                continue;
            }
            for (index, binding) in self.tainted_bindings.iter().enumerate() {
                if binding.local != *ident {
                    continue;
                }
                let hop = self.tainted_binding_hops[index];
                if hop >= MAX_TAINT_BINDING_HOPS {
                    tracing::debug!(
                        local = name,
                        via = ident.as_str(),
                        source_path = binding.source_path.as_str(),
                        "taint binding chain dropped: would exceed MAX_TAINT_BINDING_HOPS"
                    );
                    continue;
                }
                out.push((
                    TaintedBinding {
                        local: name.to_string(),
                        source_path: binding.source_path.clone(),
                        source_span_start: binding.source_span_start,
                    },
                    hop + 1,
                ));
            }
        }
        out
    }

    /// Chain an object destructure from a tainted local (`const { id } = a`
    /// where `a` is source-tainted), mirroring the shipped
    /// destructure-from-source capture (issue #1146). Only a bare-identifier
    /// initializer chains; member-expression initializers stay out for the
    /// same reason member roots are excluded from
    /// `collect_chained_taint_idents`.
    pub(super) fn record_chained_tainted_destructure_bindings(
        &mut self,
        obj_pat: &ObjectPattern<'_>,
        init: &Expression<'_>,
    ) {
        let Expression::Identifier(ident) = unwrap_parens(init) else {
            return;
        };
        let idents = vec![ident.name.to_string()];
        let mut chained = Vec::new();
        for local in extract_destructured_names(obj_pat) {
            chained.extend(self.chained_bindings_for_idents(&local, &idents));
        }
        for (binding, hop) in chained {
            self.push_tainted_binding(binding, hop);
        }
    }

    fn record_first_param_source(
        &mut self,
        params: &FormalParameters<'_>,
        source_path: &'static str,
    ) {
        self.record_param_source_at_index(params, 0, source_path);
    }

    fn record_param_source_at_index(
        &mut self,
        params: &FormalParameters<'_>,
        index: usize,
        source_path: &'static str,
    ) {
        let Some(param) = params.items.get(index) else {
            return;
        };
        match &param.pattern {
            BindingPattern::BindingIdentifier(id) => {
                self.record_tainted_param_binding(id.name.as_str(), source_path);
            }
            BindingPattern::ObjectPattern(obj_pat) => {
                for local in extract_destructured_names(obj_pat) {
                    self.record_tainted_param_binding(&local, source_path);
                }
            }
            _ => {}
        }
    }

    fn record_named_param_source(
        &mut self,
        params: &FormalParameters<'_>,
        names: &[&str],
        source_path: &'static str,
    ) {
        for param in &params.items {
            match &param.pattern {
                BindingPattern::BindingIdentifier(id)
                    if names.iter().any(|name| *name == id.name.as_str()) =>
                {
                    self.record_tainted_param_binding(id.name.as_str(), source_path);
                }
                BindingPattern::ObjectPattern(obj_pat) => {
                    for (local, key) in extract_object_pattern_bindings(obj_pat) {
                        if names
                            .iter()
                            .any(|name| key == *name || key.starts_with(&format!("{name}.")))
                        {
                            self.record_tainted_param_binding(&local, source_path);
                        }
                    }
                }
                _ => {}
            }
        }
    }

    pub(super) fn record_graphql_resolver_args_source(&mut self, expr: &Expression<'_>) {
        let Some(params) = function_like_params(expr) else {
            return;
        };
        let Some(param) = params.items.get(1) else {
            return;
        };
        match &param.pattern {
            BindingPattern::BindingIdentifier(id) if id.name == "args" => {
                self.record_param_source_at_index(params, 1, GRAPHQL_ARGS_SOURCE);
            }
            BindingPattern::ObjectPattern(_) => {
                self.record_param_source_at_index(params, 1, GRAPHQL_ARGS_SOURCE);
            }
            _ => {}
        }
    }

    pub(super) fn record_next_function_param_sources(&mut self, func: &Function<'_>) {
        if func
            .id
            .as_ref()
            .is_some_and(|id| is_http_route_handler_name(id.name.as_str()))
        {
            self.record_first_param_source(&func.params, NEXT_REQUEST_SOURCE);
        }
        if function_body_has_use_server(func.body.as_deref()) {
            self.record_named_param_source(&func.params, &["formData"], NEXT_FORM_DATA_SOURCE);
        }
    }

    pub(super) fn record_next_arrow_param_sources(&mut self, expr: &ArrowFunctionExpression<'_>) {
        if function_body_has_use_server(Some(&expr.body)) {
            self.record_named_param_source(&expr.params, &["formData"], NEXT_FORM_DATA_SOURCE);
        }
    }

    pub(super) fn record_framework_callback_param_sources(&mut self, call: &CallExpression<'_>) {
        let callee_path = flatten_callee_path(&call.callee);
        let Some(method) = callee_method_name(&call.callee, callee_path.as_deref()) else {
            return;
        };
        if is_route_registration_method(method) {
            let Some(callee_path) = callee_path.as_deref() else {
                return;
            };
            if !is_framework_route_receiver_path(callee_path, method) {
                return;
            }
            if let Some(params) = route_callback_params(&call.arguments, method) {
                self.record_first_param_source(params, FRAMEWORK_REQUEST_SOURCE);
            }
            return;
        }
        if method == "process"
            && let Some(params) = last_callback_params(&call.arguments)
        {
            self.record_first_param_source(params, QUEUE_JOB_SOURCE);
            return;
        }
        if method == "tool"
            && let Some(params) = last_callback_params(&call.arguments)
        {
            self.record_first_param_source(params, MCP_TOOL_INPUT_SOURCE);
            return;
        }
        if is_trpc_procedure_method(method)
            && is_trpc_procedure_callee(&call.callee, method)
            && let Some(params) = last_callback_params(&call.arguments)
        {
            self.record_named_param_source(params, &["input"], TRPC_INPUT_SOURCE);
        }
    }

    pub(super) fn record_queue_worker_constructor_param_sources(
        &mut self,
        expr: &NewExpression<'_>,
    ) {
        let Some(callee_path) = flatten_callee_path(&expr.callee) else {
            return;
        };
        if callee_path.rsplit('.').next() != Some("Worker") {
            return;
        }
        if let Some(params) = expr.arguments.iter().skip(1).find_map(callback_params) {
            self.record_first_param_source(params, QUEUE_JOB_SOURCE);
        }
    }

    pub(super) fn record_tainted_helper_call_binding(&mut self, name: &str, expr: &Expression<'_>) {
        let Expression::CallExpression(call) = unwrap_parens(expr) else {
            return;
        };
        let Expression::Identifier(callee) = &call.callee else {
            return;
        };
        if self.nested_scope_shadows(callee.name.as_str()) {
            return;
        }
        let Some(helper) = self
            .source_returning_helpers
            .get(callee.name.as_str())
            .cloned()
        else {
            return;
        };

        let mut source_paths = Vec::new();
        for path in &helper.paths {
            let Some(arg_expr) = call
                .arguments
                .get(path.arg_index)
                .and_then(Argument::as_expression)
            else {
                continue;
            };
            source_paths.extend(apply_source_return_path(arg_expr, &path.suffixes));
        }
        source_paths.sort();
        source_paths.dedup();
        for source_path in source_paths {
            self.push_tainted_binding(
                TaintedBinding {
                    local: name.to_string(),
                    source_path,
                    // One-hop helper-return source: the real read lives inside the
                    // helper body, not at this binding, so no concrete read span is
                    // available here. `0` makes the analyze layer anchor at the sink.
                    source_span_start: 0,
                },
                DIRECT_TAINT_HOP,
            );
        }
    }

    fn record_source_returning_function_helper(
        &mut self,
        name: &str,
        params: &FormalParameters<'_>,
        body: &FunctionBody<'_>,
    ) {
        if !self.is_module_scope() {
            return;
        }
        let Some(expr) = extract_function_body_final_return_expr(body) else {
            self.source_returning_helpers.remove(name);
            return;
        };
        if let Some(helper) = source_returning_helper(params, expr) {
            self.source_returning_helpers
                .insert(name.to_string(), helper);
        } else {
            self.source_returning_helpers.remove(name);
        }
    }

    pub(super) fn record_source_returning_function_declaration(&mut self, function: &Function<'_>) {
        let (Some(id), Some(body)) = (function.id.as_ref(), function.body.as_deref()) else {
            return;
        };
        self.record_source_returning_function_helper(id.name.as_str(), &function.params, body);
    }

    /// Record tainted-source bindings for `const { a, b } = <object>.<prop>`,
    /// where the destructured initializer is a member-access chain (or bare
    /// identifier root). Each bound local maps to the FULL flattened init path:
    /// `const { id } = req.query` records `{ local: "id", source_path:
    /// "req.query" }`. Rest patterns are skipped (whole-object capture is out of
    /// the cheap scope). Nested patterns are not destructured.
    pub(super) fn record_tainted_destructure_bindings(
        &mut self,
        obj_pat: &ObjectPattern<'_>,
        expr: &Expression<'_>,
    ) {
        let Some(source_path) = destructure_source_path(expr) else {
            return;
        };
        for local in extract_destructured_names(obj_pat) {
            self.push_tainted_binding(
                TaintedBinding {
                    local,
                    source_path: source_path.clone(),
                    // The destructured initializer read (`req.query` in
                    // `const { id } = req.query`); anchors the source node at the
                    // real read line.
                    source_span_start: expr.span().start,
                },
                DIRECT_TAINT_HOP,
            );
        }
    }
}