Skip to content

🛡️ Sentinel: [CRITICAL] Fix path traversal in TypeScript module resolution#282

Open
bashandbone wants to merge 1 commit into
mainfrom
sentinel-fix-path-traversal-12883902628681392337
Open

🛡️ Sentinel: [CRITICAL] Fix path traversal in TypeScript module resolution#282
bashandbone wants to merge 1 commit into
mainfrom
sentinel-fix-path-traversal-12883902628681392337

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented Jun 1, 2026

🚨 Severity

CRITICAL

💡 Vulnerability

A path traversal vulnerability existed in the manual path normalization logic (resolved.components()) inside the TypeScript extractor's resolve_import_path. When system canonicalize fails (which happens when pointing to non-existent files), the fallback manual normalization unconditionally .pop()'d the path component list upon encountering a std::path::Component::ParentDir (..). This allowed it to maliciously pop the RootDir (/), Windows Prefix (C:\), or overwrite valid preceding .. references in naturally relative paths.

🎯 Impact

If resolve_import_path processes user-influenced input or aggressively nested .. module paths without validation, it could result in escaping the target directory bounds and traversing into unintended directories on the host file system.

🔧 Fix

Replaced the unconditional .pop() with a pattern-matching verification against the .last() element of the components list.

  • ParentDir (..) is prevented from popping foundational components like RootDir or Prefix.
  • If the current list is empty or already ends with a ParentDir, the new ParentDir is appended to correctly accumulate sequences like ../../dir.
  • It only performs a .pop() when it is safely matched against a Normal file/folder path component.
  • The learning from this vulnerability was added to .jules/sentinel.md.

✅ Verification

  • Analyzed the patch to ensure it mimics standard OS filesystem resolution behaviors.
  • Verified that the fix correctly parses paths inside the local testing framework.
  • Executed cargo test -p thread-flow --test extractor_typescript_tests and all 25 tests passed successfully.
  • Conducted formatting checks with cargo +nightly fmt.
  • Created and formatted the .jules/sentinel.md journal per project specifications.

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

Summary by Sourcery

Fix unsafe path normalization in the TypeScript dependency extractor to prevent path traversal and record the incident in the Sentinel security journal.

Bug Fixes:

  • Harden manual path normalization in TypeScript import resolution to avoid popping root/prefix components or collapsing legitimate parent-directory segments, preventing path traversal outside the intended directory.

Enhancements:

  • Apply minor Rust formatting and readability improvements across AST and rule engine modules for consistency.

Documentation:

  • Add a Sentinel security journal entry documenting the TypeScript import path traversal vulnerability, its root cause, and preventive guidance.

…ution

This commit resolves a critical path traversal vulnerability in the TypeScript extractor's `resolve_import_path` method. When `canonicalize` falls back to manual path normalization, the previous code unconditionally popped path components on `ParentDir` (`..`). This behavior was unsafe, as it could erroneously pop structural roots (like `RootDir` or `Prefix`) or drop valid preceding `..` references when resolving inherently relative paths (e.g. `../../a`).

A robust match on the last component is now employed to ensure that path traversal sequences are correctly accumulated or removed without escaping the legitimate hierarchy constraints.

A new entry has also been created in the `.jules/sentinel.md` journal capturing this security learning.

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 1, 2026 17:53
@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.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jun 1, 2026

Reviewer's Guide

Fixes a critical path traversal vulnerability in the TypeScript extractor’s manual path normalization by making ParentDir handling component-aware, and includes minor Rust formatting/clarity tweaks plus a new Sentinel security journal entry.

Flow diagram for secure ParentDir handling in TypeScript path normalization

flowchart TD
    A[Iterate resolved.components] --> B{component is ParentDir?}
    B -->|No| C["components.push(component)"]
    B -->|Yes| D{components.last exists?}

    D -->|No| E["components.push(ParentDir)"]

    D -->|Yes| F{last is Normal?}
    F -->|Yes| G["components.pop"]
    F -->|No| H{last is ParentDir?}

    H -->|Yes| I["components.push(ParentDir)"]
    H -->|No| J["Do nothing<br/>// keep RootDir or Prefix"]

    C --> K[Next component]
    E --> K
    G --> K
    I --> K
    J --> K
    K --> L{More components?}
    L -->|Yes| A
    L -->|No| M["Normalized components ready"]
Loading

File-Level Changes

Change Details Files
Harden manual path normalization in TypeScript import resolution to prevent path traversal.
  • Replace unconditional pop on encountering ParentDir with logic that inspects the last component before modifying the list.
  • Allow ParentDir to remove only preceding Normal components while preserving RootDir and Prefix boundaries.
  • Accumulate consecutive ParentDir components when the list is empty or already ends with ParentDir, maintaining relative paths like ../../dir.
  • Retain behavior for CurDir and other components, ensuring they are pushed unchanged.
crates/flow/src/incremental/extractors/typescript.rs
Apply minor style/formatting updates in AST and rule engine modules for consistency and readability.
  • Reformat a fallible UTF-8 conversion with unwrap_or_else into a multi-line chained call.
  • Split a long assert_eq! into multiple lines for readability in tests.
  • Reformat a match arm building a RapidSet to use multi-line method chaining.
  • Condense a read-lock unwrap_or_else expression into a single line while preserving behavior.
crates/ast-engine/src/tree_sitter/mod.rs
crates/rule-engine/src/rule/mod.rs
crates/rule-engine/src/rule/referent_rule.rs
Document the vulnerability and mitigation in the project’s Sentinel security journal.
  • Add a Sentinel entry summarizing the path traversal vulnerability in TypeScript import resolution.
  • Capture key learning about unsafe assumptions around popping path components on ParentDir.
  • Document the preventative pattern of matching the last component and blocking pops of RootDir/Prefix while correctly handling consecutive ParentDir components.
.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 reviewed your changes and they look great!


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.

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 the TypeScript dependency extractor’s relative import path normalization to prevent path traversal when canonicalize() fails (e.g., for non-existent paths), and records the incident in the Jules Sentinel journal.

Changes:

  • Fix manual .. handling in TypeScript module path resolution to avoid popping RootDir/Windows Prefix and to preserve consecutive .. sequences.
  • Apply minor formatting/readability changes in the rule engine and AST engine code.
  • Add a Sentinel journal entry documenting the vulnerability and prevention guidance.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
crates/flow/src/incremental/extractors/typescript.rs Harden fallback path normalization for relative imports to prevent traversal above root/prefix.
crates/rule-engine/src/rule/referent_rule.rs Formatting-only change to condense a chained read/clone call.
crates/rule-engine/src/rule/mod.rs Formatting-only change to improve readability of defined_vars() collection.
crates/ast-engine/src/tree_sitter/mod.rs Formatting-only changes in UTF-8 fallback handling and a test assertion.
.jules/sentinel.md Add Sentinel journal entry describing the vulnerability and mitigation guidance.

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

Comment thread .jules/sentinel.md
@@ -0,0 +1,4 @@
## 2025-06-01 - Path Traversal Vulnerability in TypeScript Import Resolution
**Vulnerability:** A path traversal vulnerability existed in the manual path normalization logic (`resolved.components()`) inside the TypeScript extractor's `resolve_import_path` when `canonicalize` fails. The code un-conditionally popped the last path component when encountering `std::path::Component::ParentDir`, which could erroneously pop `RootDir` (`/`) or `Prefix` (`C:\`), or incorrectly remove a legitimate preceding `../` in paths like `../../a`.
Comment thread .jules/sentinel.md
@@ -0,0 +1,4 @@
## 2025-06-01 - Path Traversal Vulnerability in TypeScript Import Resolution
Comment on lines +811 to +827
// 🛡️ SECURITY: Prevent path traversal by explicitly blocking Component::ParentDir
// from popping RootDir/Prefix. Also correctly handle when the component list
// is empty or already ends with ParentDir to preserve paths like ../../a
if let Some(last) = components.last() {
match last {
std::path::Component::Normal(_) => {
components.pop();
}
std::path::Component::ParentDir => {
components.push(component);
}
// Don't pop RootDir or Prefix
_ => {}
}
} else {
components.push(component);
}
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