Skip to content

🛡️ Sentinel: [CRITICAL] Fix Path Traversal in TypeScript Module Resolution#171

Open
bashandbone wants to merge 1 commit intomainfrom
sentinel-fix-path-traversal-4760579896811727811
Open

🛡️ Sentinel: [CRITICAL] Fix Path Traversal in TypeScript Module Resolution#171
bashandbone wants to merge 1 commit intomainfrom
sentinel-fix-path-traversal-4760579896811727811

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented Apr 23, 2026

🚨 Severity: CRITICAL
💡 Vulnerability: A path traversal vulnerability existed in resolve_module_path during manual path normalization. The code blindly popped components when hitting Component::ParentDir (..). When resolving relative paths that traverse higher than the module base directory (e.g. ../../../file.ts), this logic completely failed and wiped path structures or falsely resolved them.
🎯 Impact: An attacker or malformed module specifier could traverse outside intended directory boundaries by providing paths with multiple ../ segments, potentially exposing or misresolving paths across the filesystem in the incremental extractor module.
🔧 Fix: Updated the manual path resolution code to correctly implement robust normalization. It now prevents popping Component::RootDir or Component::Prefix, and instead pushes Component::ParentDir when the component list is empty or already terminates in a ParentDir component.
✅ Verification: Ensure the existing cargo test -p thread-flow --test extractor_typescript_tests passes successfully. Code has been reviewed and tested.


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

Summary by Sourcery

Fix unsafe manual path normalization in the TypeScript incremental extractor to prevent directory traversal outside the intended base path.

Bug Fixes:

  • Correct handling of parent directory components during path normalization to avoid traversing beyond the root or base directory.

Documentation:

  • Add a Sentinel incident note documenting the path traversal vulnerability, its root cause, and prevention guidelines.

This commit resolves a path traversal vulnerability in `TypeScriptDependencyExtractor::resolve_module_path` where `std::path::Component::ParentDir` was blindly popped without respecting bounds. This allowed relative paths traversing above the root directory to incorrectly escape or lose path context. This fix ensures `RootDir` and `Prefix` components are never popped, and stacks `ParentDir` components when going above the base resolution context.

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 23, 2026 19:35
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 23, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refines manual path normalization in the TypeScript incremental extractor to safely handle parent directory components and prevent path traversal, and adds a Sentinel note documenting the vulnerability and its fix.

Flow diagram for updated ParentDir handling in manual path normalization

flowchart TD
    Start["Handle Component::ParentDir"] --> CheckEmpty["Is components list empty?"]

    CheckEmpty -->|Yes| PushParentDirEmpty["Push Component::ParentDir onto components"]
    PushParentDirEmpty --> End["Next path component"]

    CheckEmpty -->|No| CheckLastRootPrefix["Is last component RootDir or Prefix?"]

    CheckLastRootPrefix -->|Yes| DoNothing["Do not pop; keep RootDir or Prefix"]
    DoNothing --> End

    CheckLastRootPrefix -->|No| CheckLastParentDir["Is last component ParentDir?"]

    CheckLastParentDir -->|Yes| PushParentDirNonEmpty["Push Component::ParentDir onto components"]
    PushParentDirNonEmpty --> End

    CheckLastParentDir -->|No| PopLast["Pop last component from components"]
    PopLast --> End
Loading

File-Level Changes

Change Details Files
Harden handling of Component::ParentDir in manual path normalization to avoid escaping the base/root directory and to preserve leading .. segments correctly.
  • Replace unconditional components.pop() on Component::ParentDir with guarded logic that treats empty component lists and trailing ParentDir specially.
  • Prevent popping RootDir or Prefix components when processing ParentDir, effectively stopping traversal above the filesystem root or drive prefix.
  • Ensure additional ParentDir components are pushed when the list is empty or already ends with ParentDir, preserving relative paths like ../../a.
crates/flow/src/incremental/extractors/typescript.rs
Document the vulnerability, its root cause, and prevention guidelines in Sentinel metadata.
  • Add a Sentinel markdown entry describing the original path traversal bug in TypeScript path normalization and how it manifested.
  • Record lessons learned about correct handling of Component::ParentDir within manual path normalization logic.
  • Capture explicit prevention guidance for future implementations that manipulate std::path::Component directly.
.jules/sentinel.md

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 changes semantics for leading .. segments (they were previously dropped when components was empty and are now preserved); consider confirming or documenting that this new behavior is intentional for all call sites of resolve_module_path.
  • When skipping ParentDir components that come after RootDir or Prefix, it could be helpful to add a short comment about the intended security/normalization behavior on Windows prefixes and absolute paths, so future changes don’t accidentally reintroduce traversal issues.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `ParentDir` handling changes semantics for leading `..` segments (they were previously dropped when `components` was empty and are now preserved); consider confirming or documenting that this new behavior is intentional for all call sites of `resolve_module_path`.
- When skipping `ParentDir` components that come after `RootDir` or `Prefix`, it could be helpful to add a short comment about the intended security/normalization behavior on Windows prefixes and absolute paths, so future changes don’t accidentally reintroduce traversal issues.

## Individual Comments

### Comment 1
<location path="crates/flow/src/incremental/extractors/typescript.rs" line_range="815-817" />
<code_context>
+                                components.push(component);
+                            } else {
+                                match components.last().unwrap() {
+                                    std::path::Component::RootDir | std::path::Component::Prefix(_) => {
+                                        // Do not pop RootDir or Prefix
+                                    }
+                                    std::path::Component::ParentDir => {
+                                        components.push(component);
</code_context>
<issue_to_address>
**question (bug_risk):** Clarify semantics for `ParentDir` components immediately after a root or prefix

This logic drops `ParentDir` components when the last element is `RootDir` or `Prefix`, so inputs like `/../../foo` or `C:\..\..\foo` normalize to `/foo` or `C:\foo` instead of keeping leading `..`. Please confirm this is the intended behavior for syntactically absolute-but-odd paths, and either adjust the handling (e.g., only collapsing the first `ParentDir`) or add tests that explicitly lock in this behavior for the callers that consume these paths.
</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 +815 to +817
std::path::Component::RootDir | std::path::Component::Prefix(_) => {
// Do not pop RootDir or Prefix
}
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 (bug_risk): Clarify semantics for ParentDir components immediately after a root or prefix

This logic drops ParentDir components when the last element is RootDir or Prefix, so inputs like /../../foo or C:\..\..\foo normalize to /foo or C:\foo instead of keeping leading ... Please confirm this is the intended behavior for syntactically absolute-but-odd paths, and either adjust the handling (e.g., only collapsing the first ParentDir) or add tests that explicitly lock in this behavior for the callers that consume these paths.

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 updates the TypeScript/JavaScript dependency extractor’s module path resolution to harden manual path normalization around .. handling, addressing incorrect behavior when resolving paths with multiple parent traversals (especially when canonicalize() fails).

Changes:

  • Hardened manual normalization logic for Component::ParentDir to avoid popping past root/prefix and to preserve necessary .. components.
  • Added a Jules “sentinel” markdown note documenting the vulnerability and the prevention guidance.

Reviewed changes

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

File Description
crates/flow/src/incremental/extractors/typescript.rs Fixes manual .. normalization to avoid dropping RootDir/Prefix and to preserve excess .. components correctly.
.jules/sentinel.md Documents the vulnerability, learning, and prevention guidance for manual path normalization.
Comments suppressed due to low confidence (1)

crates/flow/src/incremental/extractors/typescript.rs:832

  • Add a regression test covering a relative import whose .. components exceed the source directory depth (e.g. source file at /project/src/a.ts importing ../../../b). This ensures the manual normalization path (when canonicalize() fails) keeps the path absolute (doesn’t drop RootDir/Prefix) and preserves the correct number of .. segments for relative paths.
            if let Ok(canonical) = resolved.canonicalize() {
                resolved = canonical;
            } else {
                // If canonicalize fails (file doesn't exist), manually resolve
                let mut components = Vec::new();
                for component in resolved.components() {
                    match component {
                        std::path::Component::ParentDir => {
                            if components.is_empty() {
                                components.push(component);
                            } else {
                                match components.last().unwrap() {
                                    std::path::Component::RootDir | std::path::Component::Prefix(_) => {
                                        // Do not pop RootDir or Prefix
                                    }
                                    std::path::Component::ParentDir => {
                                        components.push(component);
                                    }
                                    _ => {
                                        components.pop();
                                    }
                                }
                            }
                        }
                        std::path::Component::CurDir => {}
                        _ => components.push(component),
                    }
                }
                resolved = components.iter().collect();
            }

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

Comment on lines +814 to +825
match components.last().unwrap() {
std::path::Component::RootDir | std::path::Component::Prefix(_) => {
// Do not pop RootDir or Prefix
}
std::path::Component::ParentDir => {
components.push(component);
}
_ => {
components.pop();
}
}
}
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

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

The RootDir | Prefix(_) arm currently discards the ParentDir component (it neither pops nor pushes). On Windows, drive-relative paths like C:foo\..\bar have a Prefix without a following RootDir; in that case discarding .. changes semantics (effectively turning C:foo\..\bar into C:foo\bar). Consider handling Prefix differently when it is not immediately followed by RootDir (e.g., allow ParentDir to be preserved/pushed), so manual normalization matches the platform’s path semantics for drive-relative inputs.

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