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
/// JSX prop names that the extractor follows through a spread of a local
/// object literal (`const props = { dangerouslySetInnerHTML: x }; <div {...props} />`).
///
/// The security catalogue owns the `jsx-attr` sink rows. A catalogue test
/// asserts that every `jsx-attr` row pattern is in this list, so a new JSX sink
/// row cannot silently lose spread coverage.
pub const JSX_SPREAD_SINK_PROPS: &[&str] = &[
"dangerouslySetInnerHTML",
"srcDoc",
"srcdoc",
"injectedJavaScript",
];
/// Node.js built-in modules whose bindings resolve to a `node:<module>.<member>`
/// sink callee path. The `node:` root is not a valid identifier, so the path
/// proves the import provenance on its own.
///
/// The extractor writes these paths for sink sites, and the policy effect
/// lookup builds the same paths for callee uses, so both read this list.
pub const NODE_BINDING_MODULES: &[&str] = &["vm", "child_process", "fs"];
/// The `node:` root of an import or `require()` source when the module is in
/// [`NODE_BINDING_MODULES`]. `vm` and `node:vm` both give `node:vm`.
/// `fs/promises` gives `node:fs`, because its members take the same paths.
#[must_use]
pub fn node_binding_module(source: &str) -> Option<String> {
let module = source.strip_prefix("node:").unwrap_or(source);
let module = if module == "fs/promises" {
"fs"
} else {
module
};
NODE_BINDING_MODULES
.contains(&module)
.then(|| format!("node:{module}"))
}
#[cfg(test)]
mod tests {
use super::node_binding_module;
#[test]
fn node_binding_module_admits_the_prefix_and_fs_promises() {
assert_eq!(node_binding_module("vm").as_deref(), Some("node:vm"));
assert_eq!(
node_binding_module("node:child_process").as_deref(),
Some("node:child_process")
);
assert_eq!(
node_binding_module("fs/promises").as_deref(),
Some("node:fs")
);
assert_eq!(node_binding_module("fs-extra"), None);
assert_eq!(node_binding_module("path"), None);
}
}