[PR #8316] fix: prevent path traversal via symlinks and cross-drive paths #12703

Open
opened 2026-02-16 18:17:36 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/8316

State: open
Merged: No


Fixes #8313

Summary

Enhanced Filesystem.contains() to prevent directory escape attacks via symlinks and cross-drive paths on Windows.

Changes

  • src/util/filesystem.ts: Enhanced contains() function
    • Use realpathSync() to resolve symlinks to canonical paths
    • Add Windows cross-drive validation (check drive letters match)
    • Fallback to lexical check if path doesn't exist
  • src/file/index.ts: Removed resolved TODO comments (4 lines)

Security Improvements

Prevents symlink escape attacks
Prevents Windows cross-drive path bypass
Maintains backward compatibility

Testing

Tested on macOS (darwin-arm64):

  • Created symlink pointing outside project directory
  • Verified contains() correctly rejects escaped paths
  • Verified normal paths still work correctly

Test scenario

# Create test symlink
ln -s /etc/passwd ./packages/opencode/test-symlink

# Verify it's rejected by containsPath()
# Expected: Access denied error


**Original Pull Request:** https://github.com/anomalyco/opencode/pull/8316 **State:** open **Merged:** No --- Fixes #8313 ## Summary Enhanced `Filesystem.contains()` to prevent directory escape attacks via symlinks and cross-drive paths on Windows. ## Changes - **`src/util/filesystem.ts`**: Enhanced `contains()` function - Use `realpathSync()` to resolve symlinks to canonical paths - Add Windows cross-drive validation (check drive letters match) - Fallback to lexical check if path doesn't exist - **`src/file/index.ts`**: Removed resolved TODO comments (4 lines) ## Security Improvements Prevents symlink escape attacks Prevents Windows cross-drive path bypass Maintains backward compatibility ## Testing Tested on macOS (darwin-arm64): - Created symlink pointing outside project directory - Verified `contains()` correctly rejects escaped paths - Verified normal paths still work correctly ### Test scenario ```bash # Create test symlink ln -s /etc/passwd ./packages/opencode/test-symlink # Verify it's rejected by containsPath() # Expected: Access denied error
yindo added the pull-request label 2026-02-16 18:17:36 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12703