Skip to content

🛡️ Sentinel: [CRITICAL] Fix Path Traversal in FileSystemContext::new#193

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

🛡️ Sentinel: [CRITICAL] Fix Path Traversal in FileSystemContext::new#193
bashandbone wants to merge 1 commit into
mainfrom
sentinel/fix-path-traversal-4933271394501377337

Conversation

@bashandbone
Copy link
Copy Markdown
Contributor

@bashandbone bashandbone commented May 4, 2026

🚨 Severity: CRITICAL
đź’ˇ Vulnerability: FileSystemContext::new previously stored base_path without canonicalizing it. Path traversal validation logic (FileSystemContext::secure_path) assumes that the base_path is absolute and fully resolved, relying on a naive starts_with bounds check. This allows path traversal attacks if symlinks and .. references are cleverly manipulated against the uncanonicalized base path, bypassing the lexical checks before symlink validation can catch them.
🎯 Impact: Attackers could potentially read or overwrite sensitive files outside of the intended sandbox directory, compromising the system or leaking secrets.
đź”§ Fix: Updated FileSystemContext::new to eagerly resolve and canonicalize the base_path upon construction, returning a ServiceResult.
âś… Verification: Tested the new logic via cargo test -p thread-services and ensured the secure_path checks correctly block traversal with fully canonicalized base bounds.


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

Summary by Sourcery

Canonicalize FileSystemContext base paths to close a path traversal vulnerability and update callers and security notes accordingly.

Bug Fixes:

  • Canonicalize the FileSystemContext base_path on construction and return a failure if the base directory cannot be resolved to prevent path traversal outside the sandbox.

Documentation:

  • Add a Sentinel security note documenting the previously uncanonicalized base_path vulnerability, its impact, and the preventive fix.

Tests:

  • Update existing FileSystemContext usage in tests to handle the new fallible constructor signature.

🚨 Severity: CRITICAL
đź’ˇ Vulnerability: `FileSystemContext::new` previously stored `base_path` without canonicalizing it. Path traversal validation logic (`FileSystemContext::secure_path`) assumes that the base_path is absolute and fully resolved, relying on a naive `starts_with` bounds check. This allows path traversal attacks if symlinks and `..` references are cleverly manipulated against the uncanonicalized base path, bypassing the lexical checks before symlink validation can catch them.
🎯 Impact: Attackers could potentially read or overwrite sensitive files outside of the intended sandbox directory, compromising the system or leaking secrets.
đź”§ Fix: Updated `FileSystemContext::new` to eagerly resolve and canonicalize the `base_path` upon construction, returning a `ServiceResult`.
âś… Verification: Tested the new logic via `cargo test -p thread-services` and ensured the `secure_path` checks correctly block traversal with fully canonicalized base bounds.

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 May 4, 2026 18:45
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 4, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Canonicalizes FileSystemContext base paths at construction time and propagates errors, tightening path traversal protections and updating tests and internal docs accordingly.

Sequence diagram for canonicalized FileSystemContext::new and secure_path

sequenceDiagram
    actor Caller
    participant FileSystemContext
    participant OS

    Caller->>FileSystemContext: new(base_path)
    FileSystemContext->>OS: canonicalize(base_path)
    alt canonicalization_success
        OS-->>FileSystemContext: canonical_base_path
        FileSystemContext-->>Caller: Ok(FileSystemContext{ base_path: canonical_base_path })
        Caller->>FileSystemContext: secure_path(relative_path)
        FileSystemContext->>FileSystemContext: join(base_path, relative_path)
        FileSystemContext->>FileSystemContext: validate_starts_with(base_path)
        FileSystemContext->>OS: canonicalize(joined_path)
        OS-->>FileSystemContext: resolved_path
        FileSystemContext-->>Caller: Ok(resolved_path)
    else canonicalization_failure
        OS-->>FileSystemContext: error
        FileSystemContext->>FileSystemContext: ServiceError::execution_dynamic(message)
        FileSystemContext-->>Caller: Err(ServiceError)
    end
Loading

Class diagram for updated FileSystemContext construction

classDiagram
    class FileSystemContext {
        +PathBuf base_path
        +new(base_path: P) ServiceResult_FileSystemContext
        +secure_path(relative_path: &str) ServiceResult_PathBuf
    }

    class ServiceResult_FileSystemContext {
        +ok(value: FileSystemContext)
        +err(error: ServiceError)
    }

    class ServiceResult_PathBuf {
        +ok(value: PathBuf)
        +err(error: ServiceError)
    }

    class ServiceError {
        +execution_dynamic(message: String) ServiceError
    }

    class PathBuf
    class Path

    FileSystemContext --> PathBuf : base_path
    FileSystemContext ..> ServiceResult_FileSystemContext : returns
    FileSystemContext ..> ServiceResult_PathBuf : returns
    ServiceResult_FileSystemContext --> ServiceError : error
    ServiceResult_PathBuf --> ServiceError : error
    FileSystemContext ..> Path : uses
    FileSystemContext ..> ServiceError : on_invalid_base_path
Loading

File-Level Changes

Change Details Files
Make FileSystemContext::new canonicalize the base path and return a fallible result type.
  • Change FileSystemContext::new to return ServiceResult instead of Self.
  • Canonicalize the provided base_path using Path::canonicalize during context construction.
  • Map canonicalization failures into ServiceError::execution_dynamic with a descriptive error message.
  • Store only the canonicalized base path inside FileSystemContext.
crates/services/src/lib.rs
Update tests and call sites to handle the new fallible FileSystemContext::new API.
  • Adjust file_system_context_security test to unwrap the ServiceResult returned by FileSystemContext::new.
  • Update the thread integration test to unwrap the result from FileSystemContext::new so the type remains usable in re-exports.
crates/services/src/lib.rs
crates/thread/tests/integration.rs
Document the addressed vulnerability and its mitigation in the Sentinel security notes.
  • Add a Sentinel markdown entry describing the path traversal issue caused by an uncanonicalized base path.
  • Record the fix, key learning, and prevention guidance for future changes to filesystem security logic.
.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

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="crates/services/src/lib.rs" line_range="146-149" />
<code_context>
+        let canonical_base = base_path
+            .as_ref()
+            .canonicalize()
+            .map_err(|e| ServiceError::execution_dynamic(format!("Invalid base path: {}", e)))?;
+
+        Ok(Self {
</code_context>
<issue_to_address>
**suggestion:** Including the offending path in the error message would make debugging invalid base paths easier.

Right now the message only includes the IO error (`e`) and not which `base_path` failed. Including the path itself (e.g. `format!("Invalid base path '{}': {}", base_path.as_ref().display(), e)`) will make logs much clearer, especially when multiple contexts are constructed.

```suggestion
        let canonical_base = base_path
            .as_ref()
            .canonicalize()
            .map_err(|e| {
                ServiceError::execution_dynamic(format!(
                    "Invalid base path '{}': {}",
                    base_path.as_ref().display(),
                    e
                ))
            })?;
```
</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 +146 to +149
let canonical_base = base_path
.as_ref()
.canonicalize()
.map_err(|e| ServiceError::execution_dynamic(format!("Invalid base path: {}", e)))?;
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.

suggestion: Including the offending path in the error message would make debugging invalid base paths easier.

Right now the message only includes the IO error (e) and not which base_path failed. Including the path itself (e.g. format!("Invalid base path '{}': {}", base_path.as_ref().display(), e)) will make logs much clearer, especially when multiple contexts are constructed.

Suggested change
let canonical_base = base_path
.as_ref()
.canonicalize()
.map_err(|e| ServiceError::execution_dynamic(format!("Invalid base path: {}", e)))?;
let canonical_base = base_path
.as_ref()
.canonicalize()
.map_err(|e| {
ServiceError::execution_dynamic(format!(
"Invalid base path '{}': {}",
base_path.as_ref().display(),
e
))
})?;

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 thread-services filesystem execution context by ensuring FileSystemContext is constructed with a fully resolved, canonical base directory, preventing path traversal/symlink-escape bypasses that rely on an uncanonicalized base.

Changes:

  • Change FileSystemContext::new to canonicalize base_path at construction time and return a ServiceResult<Self>.
  • Update call sites/tests to handle the new fallible constructor (.unwrap() in tests).
  • Add a Sentinel note documenting the vulnerability and the prevention strategy.

Reviewed changes

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

File Description
crates/services/src/lib.rs Make FileSystemContext::new canonicalize the base path and return ServiceResult; adjust unit test call site.
crates/thread/tests/integration.rs Update integration test to unwrap the now-fallible constructor.
.jules/sentinel.md Document the path traversal issue and mitigation as an internal security learning note.

đź’ˇ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

.as_ref()
.canonicalize()
.map_err(|e| ServiceError::execution_dynamic(format!("Invalid base path: {}", e)))?;

Self {
base_path: base_path.as_ref().to_path_buf(),
}
pub fn new<P: AsRef<Path>>(base_path: P) -> ServiceResult<Self> {
Comment on lines +145 to +149
pub fn new<P: AsRef<Path>>(base_path: P) -> ServiceResult<Self> {
let canonical_base = base_path
.as_ref()
.canonicalize()
.map_err(|e| ServiceError::execution_dynamic(format!("Invalid base path: {}", e)))?;
Comment on lines +146 to +149
let canonical_base = base_path
.as_ref()
.canonicalize()
.map_err(|e| ServiceError::execution_dynamic(format!("Invalid base path: {}", e)))?;
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