diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 0000000..cedef37 --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2025-02-18 - Prevent Path Traversal by blocking parent popping of prefix/root in path component manipulation +**Vulnerability:** In `crates/flow/src/incremental/extractors/typescript.rs` and potentially elsewhere, path normalization popped standard components blindly when encountering `Component::ParentDir`. This could allow relative paths to evaluate to dangerous arbitrary paths. +**Learning:** Path normalization routines built on top of `std::path::Component` iterations must verify the previous component before popping it for a parent directory escape. `Component::ParentDir` should push to the component stack if the stack is empty, only has another `ParentDir`, or if the last element is `Prefix` or `RootDir` it should be ignored. +**Prevention:** In loops managing components, match on `components.last()` during a `ParentDir` to ensure we do not `pop` root directories or prefixes. diff --git a/crates/ast-engine/src/tree_sitter/mod.rs b/crates/ast-engine/src/tree_sitter/mod.rs index 3762df9..7bd59d1 100644 --- a/crates/ast-engine/src/tree_sitter/mod.rs +++ b/crates/ast-engine/src/tree_sitter/mod.rs @@ -553,9 +553,8 @@ impl ContentExt for String { let mut bytes = std::mem::take(self).into_bytes(); let original_len = bytes.len(); bytes.splice(safe_start..safe_end, full_inserted); - *self = Self::from_utf8(bytes).unwrap_or_else(|e| { - Self::from_utf8_lossy(&e.into_bytes()).into_owned() - }); + *self = Self::from_utf8(bytes) + .unwrap_or_else(|e| Self::from_utf8_lossy(&e.into_bytes()).into_owned()); // We calculate new_end_byte using the difference in the new overall string length // to correctly align the end offset, taking any potential replacement bytes from @@ -791,7 +790,10 @@ mod test { let tree2 = parse_lang(|p| p.parse(&src, Some(&tree)), &Tsx.get_ts_language())?; let fresh_tree = parse(&src)?; - assert_eq!(tree2.root_node().to_sexp(), fresh_tree.root_node().to_sexp()); + assert_eq!( + tree2.root_node().to_sexp(), + fresh_tree.root_node().to_sexp() + ); Ok(()) } } diff --git a/crates/flow/src/incremental/extractors/typescript.rs b/crates/flow/src/incremental/extractors/typescript.rs index 1bdda4e..b186827 100644 --- a/crates/flow/src/incremental/extractors/typescript.rs +++ b/crates/flow/src/incremental/extractors/typescript.rs @@ -804,12 +804,19 @@ impl TypeScriptDependencyExtractor { resolved = canonical; } else { // If canonicalize fails (file doesn't exist), manually resolve - let mut components = Vec::new(); + let mut components: Vec = Vec::new(); for component in resolved.components() { match component { - std::path::Component::ParentDir => { - components.pop(); - } + std::path::Component::ParentDir => match components.last() { + Some(std::path::Component::Prefix(_)) + | Some(std::path::Component::RootDir) => {} + Some(std::path::Component::ParentDir) | None => { + components.push(component); + } + _ => { + components.pop(); + } + }, std::path::Component::CurDir => {} _ => components.push(component), } diff --git a/crates/rule-engine/src/check_var.rs b/crates/rule-engine/src/check_var.rs index 9e40105..17d6fa7 100644 --- a/crates/rule-engine/src/check_var.rs +++ b/crates/rule-engine/src/check_var.rs @@ -104,9 +104,9 @@ fn get_vars_from_rules<'r>(rule: &'r Rule, utils: &'r RuleRegistration) -> Rapid vars } -fn check_var_in_constraints<'r>( +fn check_var_in_constraints( mut vars: RapidSet, - constraints: &'r RapidMap, + constraints: &RapidMap, ) -> RResult> { for rule in constraints.values() { for var in rule.defined_vars() { @@ -125,9 +125,9 @@ fn check_var_in_constraints<'r>( Ok(vars) } -fn check_var_in_transform<'r>( +fn check_var_in_transform( mut vars: RapidSet, - transform: &'r Option, + transform: &Option, ) -> RResult> { let Some(transform) = transform else { return Ok(vars); diff --git a/crates/rule-engine/src/rule/mod.rs b/crates/rule-engine/src/rule/mod.rs index d1d7712..a2433f3 100644 --- a/crates/rule-engine/src/rule/mod.rs +++ b/crates/rule-engine/src/rule/mod.rs @@ -246,7 +246,11 @@ impl Rule { pub fn defined_vars(&self) -> RapidSet { match self { - Rule::Pattern(p) => p.defined_vars().into_iter().map(|s| s.to_string()).collect(), + Rule::Pattern(p) => p + .defined_vars() + .into_iter() + .map(|s| s.to_string()) + .collect(), Rule::Kind(_) => RapidSet::default(), Rule::Regex(_) => RapidSet::default(), Rule::NthChild(n) => n.defined_vars(), diff --git a/crates/rule-engine/src/rule/referent_rule.rs b/crates/rule-engine/src/rule/referent_rule.rs index dd947bc..74d480e 100644 --- a/crates/rule-engine/src/rule/referent_rule.rs +++ b/crates/rule-engine/src/rule/referent_rule.rs @@ -27,10 +27,7 @@ impl Clone for Registration { impl Registration { fn read(&self) -> Arc> { - self.0 - .read() - .unwrap_or_else(|e| e.into_inner()) - .clone() + self.0.read().unwrap_or_else(|e| e.into_inner()).clone() } pub(crate) fn contains_key(&self, key: &str) -> bool { self.read().contains_key(key)