Skip to content

🛡️ Sentinel: [CRITICAL] Fix Path Traversal Vulnerability in TypeScript Path Resolution#151

Open
bashandbone wants to merge 1 commit intomainfrom
sentinel-fix-path-traversal-typescript-extractor-16673417475008707265
Open

🛡️ Sentinel: [CRITICAL] Fix Path Traversal Vulnerability in TypeScript Path Resolution#151
bashandbone wants to merge 1 commit intomainfrom
sentinel-fix-path-traversal-typescript-extractor-16673417475008707265

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented Apr 15, 2026

🚨 Severity: CRITICAL
💡 Vulnerability: When manually resolving paths (such as import ... from '../../a'), encountering .. (ParentDir) blindly triggered components.pop(). This fails because popping an absolute path prefix (/ or C:\) strips boundaries, converting absolute to relative and allowing traversal escape. Conversely, popping on an empty stack does nothing, incorrectly normalizing paths like ../../a into a.
🎯 Impact: Exploitable path traversal during AST dependency extraction leading to arbitrary file read/resolution context escapes.
🔧 Fix: Added lexical stack validation before popping components. RootDir and Prefix are protected, and un-resolvable ParentDirs are preserved in relative paths.
✅ Verification: Ran cargo test -p thread-flow --test extractor_typescript_tests. Also cleaned up scratchpad scripts and recorded learning in .jules/sentinel.md.


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

Summary by Sourcery

Fix unsafe handling of parent directory components during TypeScript dependency path normalization to prevent traversal escapes.

Bug Fixes:

  • Correct path normalization in the TypeScript dependency extractor to avoid popping root/prefix components and to preserve leading parent-directory segments in relative paths.

Chores:

  • Record the discovered path traversal vulnerability and mitigation guidelines in the internal Sentinel notes.

This fixes a critical path traversal bypass in `TypeScriptDependencyExtractor` caused by incorrectly popping empty/`ParentDir` path components and improperly deleting absolute path boundaries (`RootDir` and `Prefix`). This enforces standard lexical normalization semantics on the component vector stack.

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 15, 2026 18:09
@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 Apr 15, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Fixes a critical path traversal vulnerability in the TypeScript dependency extractor by making ParentDir handling in manual path normalization respect path roots/prefixes and preserve unresolvable parent components in relative paths, and documents the issue and lessons learned in Sentinel notes.

Flow diagram for updated ParentDir normalization logic

flowchart TD
    A[Handle Component ParentDir] --> B[Get last component from stack]
    B -->|Last is RootDir or Prefix| C[Do nothing
Preserve absolute path boundary]
    B -->|Last is None| D[Push ParentDir onto stack
Preserve leading .. in relative path]
    B -->|Last is ParentDir| E[Push ParentDir onto stack
Accumulate unresolved ..]
    B -->|Last is normal component| F[Pop last component
Resolve ParentDir against previous segment]
Loading

File-Level Changes

Change Details Files
Harden ParentDir handling in manual path normalization to prevent escaping path roots and to preserve leading/unresolvable parent components in relative paths.
  • Replace blind components.pop() on ParentDir with a guarded pop that refuses to pop RootDir or Prefix and also avoids popping when the stack is empty or ends in ParentDir.
  • When a ParentDir cannot be safely resolved, push it back onto the components stack (as long as the stack top is not RootDir/Prefix), preserving relative traversal segments like ../../a.
  • Keep CurDir components as no-ops and continue pushing all other path components as before.
crates/flow/src/incremental/extractors/typescript.rs
Record the vulnerability, root cause, and prevention guidelines in Sentinel documentation.
  • Add a Sentinel note describing the original path traversal bug in the TypeScript extractor, including how incorrect ParentDir popping could convert absolute paths to relative and drop leading .. in relative paths.
  • Document key learnings about using PathBuf::components() safely and the requirement to explicitly check stack state before popping on ParentDir.
  • Capture prevention guidelines for future manual path resolution logic, emphasizing safe handling of RootDir, Prefix, and empty/ParentDir-only stacks.
.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 ParentDir handling branch has become fairly complex; consider extracting this logic into a small helper function (e.g., fn apply_parent_dir(components: &mut Vec<Component>)) to make the control flow clearer and easier to reason about.
  • The conditions around when to push vs. skip ParentDir (especially the second matches! on components.last()) could benefit from a brief comment explaining the intended invariants for absolute vs. relative paths to future readers.
  • Double-check whether Windows-specific Prefix variants (UNC, Verbatim paths, etc.) need any special-case handling beyond being treated as non-poppable, and if so, consider making that intent explicit in the code structure or naming.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `ParentDir` handling branch has become fairly complex; consider extracting this logic into a small helper function (e.g., `fn apply_parent_dir(components: &mut Vec<Component>)`) to make the control flow clearer and easier to reason about.
- The conditions around when to push vs. skip `ParentDir` (especially the second `matches!` on `components.last()`) could benefit from a brief comment explaining the intended invariants for absolute vs. relative paths to future readers.
- Double-check whether Windows-specific `Prefix` variants (UNC, Verbatim paths, etc.) need any special-case handling beyond being treated as non-poppable, and if so, consider making that intent explicit in the code structure or naming.

## Individual Comments

### Comment 1
<location path=".jules/sentinel.md" line_range="4" />
<code_context>
+**Prevention:** Whenever manual path resolution loop involves popping on `Component::ParentDir`, check the top of the component stack first to prevent popping prefixes/roots or ignoring `ParentDir` when the stack is empty/only contains `ParentDir`.
</code_context>
<issue_to_address>
**nitpick (typo):** Consider adding an article in "Whenever manual path resolution loop" for grammatical correctness.

Consider rephrasing the beginning to: "Whenever a manual path resolution loop involves popping on `Component::ParentDir`, ..." to improve grammar and readability.
</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 thread .jules/sentinel.md
## 2025-02-28 - [Path Traversal bypass in PathBuf resolution]
**Vulnerability:** Found a vulnerable path component normalization in `crates/flow/src/incremental/extractors/typescript.rs`. Manual handling of `..` (`ParentDir`) blindly called `components.pop()`. This fails to handle root directories or prefixes correctly (converting absolute paths to relative, allowing traversal escapes), and incorrectly ignores `..` at the beginning of relative paths (e.g., resolving `../../a` to `a`).
**Learning:** `std::path::PathBuf::components()` correctly yields `Component::RootDir` and `Component::ParentDir`. Normalization algorithms MUST explicitly check the previous component before popping. Popping `RootDir` is inherently unsafe as it changes path type. Empty states or states ending in `ParentDir` must push `ParentDir` instead of doing nothing or blindly popping.
**Prevention:** Whenever manual path resolution loop involves popping on `Component::ParentDir`, check the top of the component stack first to prevent popping prefixes/roots or ignoring `ParentDir` when the stack is empty/only contains `ParentDir`.
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.

nitpick (typo): Consider adding an article in "Whenever manual path resolution loop" for grammatical correctness.

Consider rephrasing the beginning to: "Whenever a manual path resolution loop involves popping on Component::ParentDir, ..." to improve grammar and readability.

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

Fixes unsafe manual path normalization in the TypeScript/JavaScript dependency extractor to avoid incorrectly collapsing .. segments and to prevent popping path boundaries (root/prefix) during module path resolution.

Changes:

  • Harden manual .. (ParentDir) handling during path normalization when canonicalize() fails.
  • Preserve unresolvable leading .. in relative paths instead of silently dropping them.
  • Add a Sentinel learning note documenting the vulnerability and prevention guidance.

Reviewed changes

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

File Description
crates/flow/src/incremental/extractors/typescript.rs Updates manual path normalization logic to avoid unsafe ParentDir popping and preserve unresolved traversal components.
.jules/sentinel.md Documents the identified path normalization vulnerability and prevention guidance.

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

Comment on lines +811 to +814
let pop = match components.last() {
Some(std::path::Component::RootDir)
| Some(std::path::Component::Prefix(_)) => false,
None | Some(std::path::Component::ParentDir) => false,
Comment on lines 810 to +815
std::path::Component::ParentDir => {
components.pop();
let pop = match components.last() {
Some(std::path::Component::RootDir)
| Some(std::path::Component::Prefix(_)) => false,
None | Some(std::path::Component::ParentDir) => false,
_ => true,
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