foxguard 0.12.0

A security scanner as fast as a linter, written in Rust. 200+ built-in rules across 12 source languages.
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
use super::common::AliasTable;
use tree_sitter::{Node, Tree};

/// Build a Python import alias table from a parsed tree.
///
/// Maps a local identifier (as it appears in the source) to its canonical
/// dotted path. For example `import pickle as p` produces `p -> pickle`,
/// and `from pickle import loads as d` produces `d -> pickle.loads`.
///
/// Used by Python rules so that call sites like `p.loads(x)` or
/// `d(x)` resolve back to `pickle.loads` before comparison against sink lists,
/// closing the obvious aliasing bypasses.
///
/// Scope is intentionally file-level only: function-local rebindings like
/// `pickle = something_else` inside one function are not tracked. Dynamic
/// forms such as `importlib.import_module(...)` are also out of scope and
/// tracked separately under the dataflow roadmap.
pub fn from_tree(source: &str, tree: &Tree) -> AliasTable {
    let mut aliases = AliasTable::new();
    walk_for_imports(&mut aliases, tree.root_node(), source);
    aliases
}

fn walk_for_imports(aliases: &mut AliasTable, node: Node, source: &str) {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "import_statement" => collect_import(aliases, child, source),
            "import_from_statement" => collect_import_from(aliases, child, source),
            // Recurse into top-level blocks so `if sys.version_info: import foo`
            // at the module level still contributes. Stop at function and class
            // bodies — alias resolution there is explicitly out of scope.
            "if_statement" | "try_statement" | "with_statement" | "block" | "module" => {
                walk_for_imports(aliases, child, source);
            }
            _ => {}
        }
    }
}

fn collect_import(aliases: &mut AliasTable, node: Node, source: &str) {
    // `import X`, `import X as Y`, `import X.Y`, `import X.Y as Z`
    let mut cursor = node.walk();
    for name in node.children_by_field_name("name", &mut cursor) {
        match name.kind() {
            "dotted_name" => {
                // `import urllib.request` makes the *first* identifier
                // (`urllib`) the accessible local name, and the full dotted
                // path (`urllib.request`) is what the source will write for
                // callees. Record both so either form resolves correctly.
                let full = node_text(name, source).to_string();
                if let Some(first) = name.child(0) {
                    let root = node_text(first, source).to_string();
                    aliases.entry_or_insert(root.clone(), root);
                }
                aliases.entry_or_insert(full.clone(), full);
            }
            "aliased_import" => {
                // `import X.Y as Z`  →  local `Z` → canonical `X.Y`
                let canonical = name
                    .child_by_field_name("name")
                    .map(|n| node_text(n, source).to_string());
                let alias = name
                    .child_by_field_name("alias")
                    .map(|n| node_text(n, source).to_string());
                if let (Some(canonical), Some(alias)) = (canonical, alias) {
                    aliases.insert(alias, canonical);
                }
            }
            _ => {}
        }
    }
}

fn collect_import_from(aliases: &mut AliasTable, node: Node, source: &str) {
    // `from MODULE import NAME [as ALIAS], ...`
    let Some(module_node) = node.child_by_field_name("module_name") else {
        return;
    };
    let module = node_text(module_node, source).to_string();
    if module.is_empty() {
        return;
    }

    let mut cursor = node.walk();
    for name in node.children_by_field_name("name", &mut cursor) {
        match name.kind() {
            "dotted_name" | "identifier" => {
                // `from pickle import loads`  →  `loads` → `pickle.loads`
                let local = node_text(name, source).to_string();
                if local.is_empty() {
                    continue;
                }
                let canonical = format!("{}.{}", module, local);
                aliases.insert(local, canonical);
            }
            "aliased_import" => {
                // `from pickle import loads as d`  →  `d` → `pickle.loads`
                let real = name
                    .child_by_field_name("name")
                    .map(|n| node_text(n, source).to_string());
                let alias = name
                    .child_by_field_name("alias")
                    .map(|n| node_text(n, source).to_string());
                if let (Some(real), Some(alias)) = (real, alias) {
                    let canonical = format!("{}.{}", module, real);
                    aliases.insert(alias, canonical);
                }
            }
            _ => {}
        }
    }
}

/// Resolve Python imports to file paths for cross-file taint analysis.
///
/// Given the source of a Python file and the path of that file, returns a
/// mapping from local import names to the resolved file paths. Only resolves:
/// - `from . import foo` (relative sibling import) -> `<dir>/foo.py`
/// - `from .foo import bar` (relative import) -> `<dir>/foo.py`
/// - `import foo` / `from foo import bar` -> `<dir>/foo.py` (sibling only)
///
/// Does not attempt full Python package resolution (sys.path, site-packages).
pub fn resolve_imports_to_paths(
    source: &str,
    tree: &Tree,
    current_file: &std::path::Path,
) -> std::collections::HashMap<String, std::path::PathBuf> {
    let mut result = std::collections::HashMap::new();
    let Some(parent_dir) = current_file.parent() else {
        return result;
    };

    resolve_imports_walk(&mut result, tree.root_node(), source, parent_dir);
    result
}

fn resolve_imports_walk(
    result: &mut std::collections::HashMap<String, std::path::PathBuf>,
    node: Node,
    source: &str,
    parent_dir: &std::path::Path,
) {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "import_from_statement" => {
                resolve_import_from(result, child, source, parent_dir);
            }
            "import_statement" => {
                resolve_plain_import(result, child, source, parent_dir);
            }
            "if_statement" | "try_statement" | "with_statement" | "block" | "module" => {
                resolve_imports_walk(result, child, source, parent_dir);
            }
            _ => {}
        }
    }
}

fn resolve_import_from(
    result: &mut std::collections::HashMap<String, std::path::PathBuf>,
    node: Node,
    source: &str,
    parent_dir: &std::path::Path,
) {
    let Some(module_node) = node.child_by_field_name("module_name") else {
        return;
    };
    let module = node_text(module_node, source);

    // `from . import queries` — module_name is ".", name is "queries"
    if module == "." {
        let mut cursor = node.walk();
        for name in node.children_by_field_name("name", &mut cursor) {
            let local = match name.kind() {
                "dotted_name" | "identifier" => node_text(name, source),
                "aliased_import" => {
                    // `from . import queries as q` — use the alias as the local name
                    // but resolve the real name to a file
                    if let Some(real_name) = name.child_by_field_name("name") {
                        let real = node_text(real_name, source);
                        let local = name
                            .child_by_field_name("alias")
                            .map(|a| node_text(a, source))
                            .unwrap_or(real);
                        let candidate = parent_dir.join(format!("{}.py", real));
                        if candidate.exists() {
                            result.insert(local.to_string(), candidate);
                        }
                        continue;
                    }
                    continue;
                }
                _ => continue,
            };
            let candidate = parent_dir.join(format!("{}.py", local));
            if candidate.exists() {
                result.insert(local.to_string(), candidate);
            }
        }
        return;
    }

    // `from .queries import run_query` — relative import with a module name
    if let Some(stripped) = module.strip_prefix('.') {
        if !stripped.is_empty() && !stripped.contains('.') {
            let candidate = parent_dir.join(format!("{}.py", stripped));
            if candidate.exists() {
                // Map each imported name to the module file
                let mut cursor = node.walk();
                for name in node.children_by_field_name("name", &mut cursor) {
                    let local = match name.kind() {
                        "dotted_name" | "identifier" => node_text(name, source),
                        "aliased_import" => name
                            .child_by_field_name("alias")
                            .or_else(|| name.child_by_field_name("name"))
                            .map(|n| node_text(n, source))
                            .unwrap_or(""),
                        _ => continue,
                    };
                    if !local.is_empty() {
                        // For `from .queries import run_query`, we want to be
                        // able to look up `run_query` as a function in `queries.py`.
                        // Store the module path keyed by the *module* name for
                        // attribute access (`queries.run_query`), but this form
                        // imports the function directly. We store it keyed by
                        // a special format: `__from_module__:<local_name>` ->
                        // file path, plus the module itself.
                        result.insert(
                            format!("__from__:{}:{}", stripped, local),
                            candidate.clone(),
                        );
                    }
                }
            }
        }
        return;
    }

    // `from queries import run_query` — bare module name, try sibling
    if !module.contains('.') {
        let candidate = parent_dir.join(format!("{}.py", module));
        if candidate.exists() {
            let mut cursor = node.walk();
            for name in node.children_by_field_name("name", &mut cursor) {
                let local = match name.kind() {
                    "dotted_name" | "identifier" => node_text(name, source),
                    "aliased_import" => name
                        .child_by_field_name("alias")
                        .or_else(|| name.child_by_field_name("name"))
                        .map(|n| node_text(n, source))
                        .unwrap_or(""),
                    _ => continue,
                };
                if !local.is_empty() {
                    result.insert(format!("__from__:{}:{}", module, local), candidate.clone());
                }
            }
        }
    }
}

fn resolve_plain_import(
    result: &mut std::collections::HashMap<String, std::path::PathBuf>,
    node: Node,
    source: &str,
    parent_dir: &std::path::Path,
) {
    // `import queries` — try sibling file
    let mut cursor = node.walk();
    for name in node.children_by_field_name("name", &mut cursor) {
        let (local, module_name) = match name.kind() {
            "dotted_name" => {
                let text = node_text(name, source);
                // Only resolve simple (non-dotted) module names to siblings
                if text.contains('.') {
                    continue;
                }
                (text.to_string(), text)
            }
            "aliased_import" => {
                let real = name
                    .child_by_field_name("name")
                    .map(|n| node_text(n, source))
                    .unwrap_or("");
                let alias = name
                    .child_by_field_name("alias")
                    .map(|n| node_text(n, source))
                    .unwrap_or(real);
                if real.contains('.') {
                    continue;
                }
                (alias.to_string(), real)
            }
            _ => continue,
        };
        let candidate = parent_dir.join(format!("{}.py", module_name));
        if candidate.exists() {
            result.insert(local, candidate);
        }
    }
}

fn node_text<'a>(node: Node, source: &'a str) -> &'a str {
    &source[node.byte_range()]
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::parser::parse_file;
    use crate::Language;

    fn build(source: &str) -> AliasTable {
        let tree = parse_file(source, Language::Python).expect("parse");
        from_tree(source, &tree)
    }

    #[test]
    fn plain_import_maps_root_to_itself() {
        let a = build("import pickle\n");
        assert_eq!(a.resolve("pickle.loads"), "pickle.loads");
        assert_eq!(a.resolve("pickle"), "pickle");
    }

    #[test]
    fn aliased_import_resolves_alias_to_module() {
        let a = build("import pickle as p\n");
        assert_eq!(a.resolve("p.loads"), "pickle.loads");
        assert_eq!(a.resolve("p.load"), "pickle.load");
    }

    #[test]
    fn from_import_resolves_bare_name_to_dotted_path() {
        let a = build("from pickle import loads\n");
        assert_eq!(a.resolve("loads"), "pickle.loads");
    }

    #[test]
    fn from_import_with_alias_resolves_alias() {
        let a = build("from pickle import loads as deserialize\n");
        assert_eq!(a.resolve("deserialize"), "pickle.loads");
    }

    #[test]
    fn from_import_multiple_names() {
        let a = build("from pickle import loads, load, dumps\n");
        assert_eq!(a.resolve("loads"), "pickle.loads");
        assert_eq!(a.resolve("load"), "pickle.load");
        assert_eq!(a.resolve("dumps"), "pickle.dumps");
    }

    #[test]
    fn cpickle_shadowing_pickle_resolves_to_cpickle() {
        // `import cPickle as pickle` is the classic shadowing form.
        let a = build("import cPickle as pickle\n");
        assert_eq!(a.resolve("pickle.loads"), "cPickle.loads");
    }

    #[test]
    fn dotted_module_import() {
        let a = build("import urllib.request\n");
        // `urllib.request.urlopen` should resolve untouched (not an alias).
        assert_eq!(
            a.resolve("urllib.request.urlopen"),
            "urllib.request.urlopen"
        );
    }

    #[test]
    fn dotted_module_import_with_alias() {
        let a = build("import urllib.request as ur\n");
        assert_eq!(a.resolve("ur.urlopen"), "urllib.request.urlopen");
    }

    #[test]
    fn unknown_identifier_passes_through_unchanged() {
        let a = build("import pickle\n");
        assert_eq!(a.resolve("json.loads"), "json.loads");
        assert_eq!(a.resolve("some_random_fn"), "some_random_fn");
    }

    #[test]
    fn multiple_imports_combine() {
        let a = build(
            r#"
import pickle as p
import yaml
from subprocess import Popen as SpawnProc
from hashlib import md5
"#,
        );
        assert_eq!(a.resolve("p.loads"), "pickle.loads");
        assert_eq!(a.resolve("yaml.load"), "yaml.load");
        assert_eq!(a.resolve("SpawnProc"), "subprocess.Popen");
        assert_eq!(a.resolve("md5"), "hashlib.md5");
    }

    #[test]
    fn from_import_with_alias_raw_map_stores_canonical() {
        let a = build("from pickle import loads as d\n");
        assert_eq!(a.get("d"), Some("pickle.loads"));
        assert_eq!(a.get("loads"), None);
    }

    #[test]
    fn empty_source_produces_empty_table() {
        let a = build("");
        assert_eq!(a.resolve("pickle.loads"), "pickle.loads");
    }
}