Skip to content

Improve path traversal security in vault access - #63

Merged
acroca merged 1 commit into
mainfrom
claude/sleepy-ritchie-0alt1f
Nov 14, 2026
Merged

acroca merged 1 commit into
mainfrom
claude/sleepy-ritchie-0alt1f

Conversation

@acroca

@acroca acroca commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Summary

Refactored the _safe_path method to use os.path.realpath instead of Path.resolve for path validation, improving alignment with security best practices and CodeQL's path-injection sanitizer recognition.

Key Changes

  • Replaced Path.resolve() and Path.relative_to() with os.path.realpath() and string-based startswith() checks for path validation
  • Added explicit handling for the vault root itself (e.g., when accessed via symlink), returning the configured root path
  • Implemented proper path boundary checking using os.path.join(root, "") to ensure sibling directories cannot escape the vault
  • Updated comments to clarify the security model and explain why this approach is preferred by CodeQL's path-injection query
  • Converted final result back to Path object for API consistency

Implementation Details

The new validation approach uses:

  1. os.path.realpath() to resolve symlinks and normalize paths
  2. String prefix matching with a trailing separator guard to prevent sibling directory escape
  3. Special case handling for the vault root itself to return the configured path rather than a request-derived string

This implementation is more explicit about the security boundary and is recognized by CodeQL's path-injection sanitizer, improving code security analysis.

Code scanning alerts 7, 8 (companion.py attachment routes) and 9
(tools.py) all flow through VaultTools._safe_path. Path.resolve plus
relative_to is not a sanitizer CodeQL's py/path-injection query knows,
and the earlier realpath version let the root case bypass the
startswith guard, so its return value stayed tainted. Guard with
realpath + startswith(root + sep) only, and return the configured root
for the root itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0149otepaDuazZjsUFepPHyQ
@acroca
acroca merged commit 24546e2 into main Nov 14, 2026
6 checks passed
@acroca
acroca deleted the claude/sleepy-ritchie-0alt1f branch November 14, 2026 22:33
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