Skip to content

🛡️ Sentinel: [CRITICAL] Fix path traversal in typescript extractor#262

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

🛡️ Sentinel: [CRITICAL] Fix path traversal in typescript extractor#262
bashandbone wants to merge 1 commit into
mainfrom
sentinel/fix-path-traversal-1978998290590100611

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented May 25, 2026

🚨 Severity: CRITICAL

💡 Vulnerability: Path traversal and invalid bounds escaping in the TypeScript dependency extractor. When manually resolving paths (e.g., if canonicalize fails or is skipped), the code naively popped the last path component when encountering .. (std::path::Component::ParentDir). This permitted .. segments to pop RootDir (/) or Prefix, potentially making an absolute path relative or traversing outside intended boundaries. It also caused relative path traversals like ../../a to be incorrectly resolved by dropping the .. segments when the component list was empty.

🎯 Impact: If malicious or deeply nested ../ imports were processed, it could lead to the extractor escaping the project root, potentially reading or analyzing unauthorized files on the file system, or causing the analyzer to panic/crash due to malformed path state.

🔧 Fix: Replaced the naive .pop() call with secure path normalization logic. It now checks components.last():

  • Prevents popping if the last element is Component::RootDir or Component::Prefix.
  • Pushes Component::ParentDir if the component list is empty or the last element is also Component::ParentDir (to correctly preserve relative paths).
  • Otherwise, it safely pops the last component.

Verification:

  • Validated via cargo test -p thread-flow --test extractor_typescript_tests that existing functionality is preserved.
  • Manually verified via local sandbox scripts that the new logic correctly ignores .. after root (/../../etc/passwd resolves to /etc/passwd) and preserves relative paths (../../a).
  • Added entry to Sentinel journal.

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

Summary by Sourcery

Fix insecure manual path normalization in the TypeScript dependency extractor to prevent path traversal and document the vulnerability in Sentinel.

Bug Fixes:

  • Harden TypeScript dependency path resolution to avoid escaping project or filesystem roots when processing parent directory components.

Documentation:

  • Add Sentinel journal entry describing the TypeScript extractor path traversal vulnerability, its root cause, and prevention guidance.

This commit patches the manual path resolution logic inside
`crates/flow/src/incremental/extractors/typescript.rs`. Previously, it
naively called `components.pop()` when encountering
`std::path::Component::ParentDir`. This could allow popping the
`RootDir` or `Prefix`, turning an absolute path into a relative one or
escaping directory bounds. It also failed to correctly accumulate `..`
components for paths like `../../a`.

The new logic checks `components.last()` and prevents popping the
root/prefix, and properly pushes `ParentDir` when appropriate to
maintain safe and correct path resolution.

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

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Fixes critical path traversal in the TypeScript dependency extractor by hardening manual path normalization around ParentDir components and records the incident in the Sentinel journal.

Flow diagram for secure ParentDir handling in TypeScriptDependencyExtractor path normalization

flowchart TD
    A[Component is ParentDir] --> B{components.last exists?}

    B -- no --> C[Push ParentDir into components]

    B -- yes --> D{last is RootDir or Prefix}
    D -- yes --> E[Do nothing
// keep RootDir or Prefix]

    D -- no --> F{last is ParentDir}
    F -- yes --> G[Push ParentDir into components
// preserve relative traversal]
    F -- no --> H[Pop last component
// normal directory backtrack]
Loading

File-Level Changes

Change Details Files
Harden manual path normalization when processing .. in the TypeScript dependency extractor to avoid escaping root or losing relative traversal semantics.
  • Replace naive components.pop() on ParentDir with logic that inspects the last component before mutating the vector.
  • Disallow popping when the last component is RootDir or Prefix to prevent turning absolute paths into relative or escaping the filesystem root.
  • Preserve chained or leading .. by pushing ParentDir when the component list is empty or the last component is already ParentDir.
  • Maintain existing behavior for CurDir and normal components by ignoring CurDir and pushing other components as-is.
crates/flow/src/incremental/extractors/typescript.rs
Document the vulnerability, root cause, and prevention steps in the Sentinel security journal.
  • Add a Sentinel journal entry describing the TypeScript extractor path traversal issue, its impact, and the secure normalization pattern to use going forward.
.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 left some high level feedback:

  • The ParentDir handling branch is getting a bit dense; consider extracting this normalization logic into a small helper function (e.g., normalize_component(components, component)) so the main loop stays easier to read and reason about.
  • It might be worth adding a brief comment or assertion describing the intended invariant for components (e.g., never containing RootDir after a ParentDir), so future refactors to this path logic don’t accidentally reintroduce traversal issues.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `ParentDir` handling branch is getting a bit dense; consider extracting this normalization logic into a small helper function (e.g., `normalize_component(components, component)`) so the main loop stays easier to read and reason about.
- It might be worth adding a brief comment or assertion describing the intended invariant for `components` (e.g., never containing `RootDir` after a `ParentDir`), so future refactors to this path logic don’t accidentally reintroduce traversal issues.

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 addresses a critical path-normalization flaw in the TypeScript dependency extractor’s manual .. handling, preventing invalid “pop past root/prefix” behavior that could otherwise produce malformed paths and enable traversal-like outcomes when canonicalize() can’t be used. It also documents the vulnerability and guidance in the Sentinel journal.

Changes:

  • Harden manual path normalization for Component::ParentDir to avoid popping RootDir/Prefix and to preserve leading .. for relative paths.
  • Add a Sentinel journal entry describing the vulnerability, impact, and prevention guidance.

Reviewed changes

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

File Description
crates/flow/src/incremental/extractors/typescript.rs Replaces naive .. handling with root/prefix-safe normalization logic.
.jules/sentinel.md Adds a Sentinel journal entry documenting the issue and mitigation guidance.

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

Comment on lines +812 to +819
if matches!(
last,
std::path::Component::RootDir | std::path::Component::Prefix(_)
) {
// Security: prevent popping root or prefix
} else if matches!(last, std::path::Component::ParentDir) {
components.push(component);
} else {
Comment on lines 806 to +824
// 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 => {
components.pop();
if let Some(last) = components.last() {
if matches!(
last,
std::path::Component::RootDir | std::path::Component::Prefix(_)
) {
// Security: prevent popping root or prefix
} else if matches!(last, std::path::Component::ParentDir) {
components.push(component);
} else {
components.pop();
}
} else {
components.push(component);
}
Comment thread .jules/sentinel.md
@@ -0,0 +1,5 @@

## 2024-05-25 - Path Traversal in TypeScript Extractor
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