Skip to content

🛡️ Sentinel: [CRITICAL] Fix Path Traversal in Flow Extractor#159

Open
bashandbone wants to merge 1 commit intomainfrom
security/fix-path-traversal-15869152194601290260
Open

🛡️ Sentinel: [CRITICAL] Fix Path Traversal in Flow Extractor#159
bashandbone wants to merge 1 commit intomainfrom
security/fix-path-traversal-15869152194601290260

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented Apr 18, 2026

🚨 Severity: CRITICAL
💡 Vulnerability: Path Traversal vulnerability found in TypeScript incremental extractor where manual path normalization allowed directory boundary escapes.
🎯 Impact: An attacker could craft paths using .. to traverse up to the system root, potentially reading or extracting content from restricted directories outside the workspace context.
🔧 Fix: Hardened the normalization implementation to ensure ParentDir cannot pop RootDir or Prefix and properly handles leading parent segments. Also applied code cleanups based on clippy warnings.
Verification: Ran cargo test -p thread-flow --test extractor_typescript_tests and ran format and linter rules safely.


PR created automatically by Jules for task 15869152194601290260 started by @bashandbone

Summary by Sourcery

Prevent directory traversal in the TypeScript incremental dependency extractor and apply minor code cleanups across AST and rule engine modules.

Bug Fixes:

  • Harden path normalization in the TypeScript incremental dependency extractor so parent directory segments cannot escape the filesystem root or prefix.

Enhancements:

  • Simplify function signatures in the rule engine to avoid unnecessary lifetimes and references.
  • Tidy string handling and locking logic and reformat code in AST and rule engine modules for clarity and linter compliance.

Fixes a critical path traversal vulnerability in `crates/flow/src/incremental/extractors/typescript.rs` where the path normalization logic naively stripped components when encountering `..` (ParentDir). This fix hardens the directory popping rules to prevent escaping beyond the root/prefix or consuming intended relative structure. Also resolves related compiler warnings globally via `cargo clippy`.

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings April 18, 2026 17:44
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 18, 2026

Reviewer's Guide

Fixes a critical path traversal bug in the TypeScript incremental dependency extractor by hardening manual path normalization around parent directory components, and includes a set of small Clippy-driven cleanups and formatting adjustments across AST and rule engine modules.

Flow diagram for hardened ParentDir normalization in TypeScriptDependencyExtractor

flowchart TD
    A[Start normalizing resolved path components] --> B[Iterate over resolved.components]
    B --> C{component is ParentDir?}
    C -- No --> D[component is CurDir?]
    D -- Yes --> E[Skip component]
    D -- No --> F[Push component onto components]
    C -- Yes --> G{components.last}
    G -- RootDir or Prefix --> H[Do nothing
keep components unchanged]
    H --> B
    G -- ParentDir or None --> I[Push ParentDir onto components]
    I --> B
    G -- Other component --> J[Pop last component from components]
    J --> B
    B --> K[End of iteration]
    K --> L[Join components into normalized path]
    L --> M[End]
Loading

File-Level Changes

Change Details Files
Harden path normalization in the TypeScript incremental dependency extractor to prevent directory traversal above the filesystem root or prefix.
  • Adjust handling of std::path::Component::ParentDir when building normalized paths
  • Prevent popping RootDir or Prefix components when encountering ParentDir
  • Preserve leading ParentDir components when there is no prior non-root component to pop
  • Maintain existing behavior for CurDir and non-special components
crates/flow/src/incremental/extractors/typescript.rs
Apply minor code cleanups and formatting updates following Clippy and style suggestions.
  • Refactor String::from_utf8 error handling into a single-line unwrap_or_else call
  • Reformat long assert_eq! and iterator chains for readability
  • Simplify RwLock read() error handling by inlining unwrap_or_else
  • Remove unnecessary lifetime parameters from internal helper functions to satisfy lints
crates/ast-engine/src/tree_sitter/mod.rs
crates/rule-engine/src/check_var.rs
crates/rule-engine/src/rule/mod.rs
crates/rule-engine/src/rule/referent_rule.rs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The new ParentDir handling logic in the TypeScript extractor is fairly intricate; consider extracting this normalization into a small helper function (or dedicated path-normalization module) with a clearly documented invariant so the behavior is easier to reason about and reuse consistently across extractors.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `ParentDir` handling logic in the TypeScript extractor is fairly intricate; consider extracting this normalization into a small helper function (or dedicated path-normalization module) with a clearly documented invariant so the behavior is easier to reason about and reuse consistently across extractors.

## Individual Comments

### Comment 1
<location path="crates/flow/src/incremental/extractors/typescript.rs" line_range="811-819" />
<code_context>
                     match component {
                         std::path::Component::ParentDir => {
-                            components.pop();
+                            match components.last() {
+                                Some(std::path::Component::RootDir)
+                                | Some(std::path::Component::Prefix(_)) => {
+                                    // Do nothing, cannot go above root
+                                }
+                                Some(std::path::Component::ParentDir) | None => {
+                                    components.push(component);
+                                }
+                                _ => {
+                                    components.pop();
+                                }
</code_context>
<issue_to_address>
**question:** Check whether accumulating multiple leading `ParentDir` components is the intended behavior for the path normalization logic.

With this logic, a relative path like `../../foo` will keep (and potentially increase) leading `ParentDir` components instead of collapsing them. If callers expect a truly normalized path (never containing more `..` than can be applied), this behavior may be incorrect or surprising. If preserving the exact number of `..` is intentional, consider extracting a helper (e.g. `can_pop_parent(components: &Vec<Component>)`) to make that policy explicit. Otherwise, you may want to clamp `ParentDir` to at most a single leading segment for relative paths as well.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +811 to +819
match components.last() {
Some(std::path::Component::RootDir)
| Some(std::path::Component::Prefix(_)) => {
// Do nothing, cannot go above root
}
Some(std::path::Component::ParentDir) | None => {
components.push(component);
}
_ => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Check whether accumulating multiple leading ParentDir components is the intended behavior for the path normalization logic.

With this logic, a relative path like ../../foo will keep (and potentially increase) leading ParentDir components instead of collapsing them. If callers expect a truly normalized path (never containing more .. than can be applied), this behavior may be incorrect or surprising. If preserving the exact number of .. is intentional, consider extracting a helper (e.g. can_pop_parent(components: &Vec<Component>)) to make that policy explicit. Otherwise, you may want to clamp ParentDir to at most a single leading segment for relative paths as well.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens TypeScript incremental dependency path normalization to avoid incorrect .. handling that could allow directory boundary escapes during manual normalization (when canonicalize() fails), plus applies minor cleanups driven by clippy/formatting.

Changes:

  • Hardened manual path normalization in the TypeScript extractor to prevent ParentDir from popping RootDir/Prefix and to preserve leading .. segments.
  • Performed small refactors/formatting adjustments across rule-engine and ast-engine code (clippy/format driven).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/rule-engine/src/rule/referent_rule.rs Simplifies RwLock read/clone expression formatting.
crates/rule-engine/src/rule/mod.rs Reformats defined_vars() collection pipeline for readability/clippy.
crates/rule-engine/src/check_var.rs Removes unnecessary explicit lifetimes from helper signatures.
crates/flow/src/incremental/extractors/typescript.rs Updates manual .. normalization logic to avoid popping root/prefix and handle leading parent segments.
crates/ast-engine/src/tree_sitter/mod.rs Minor formatting and simplification around UTF-8 fallback and a test assertion.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 808 to +822
for component in resolved.components() {
match component {
std::path::Component::ParentDir => {
components.pop();
match components.last() {
Some(std::path::Component::RootDir)
| Some(std::path::Component::Prefix(_)) => {
// Do nothing, cannot go above root
}
Some(std::path::Component::ParentDir) | None => {
components.push(component);
}
_ => {
components.pop();
}
}
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new manual .. normalization logic is security-sensitive, but there’s no regression test coverage to prove the intended behavior (e.g., leading .. segments are preserved for relative paths, and .. never removes RootDir/Prefix on absolute/Windows-prefix paths). Please add targeted unit tests (likely in crates/flow/tests/extractor_typescript_tests.rs) that exercise these edge cases, including absolute paths and multiple leading ParentDir segments when canonicalize() fails.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants