Skip to content

fix: prevent path traversal in FileOperationsManager sandbox (CWE-22)#159

Open
VoidChecksum wants to merge 1 commit into0x4m4:masterfrom
VoidChecksum:fix/path-traversal-sandbox-escape
Open

fix: prevent path traversal in FileOperationsManager sandbox (CWE-22)#159
VoidChecksum wants to merge 1 commit into0x4m4:masterfrom
VoidChecksum:fix/path-traversal-sandbox-escape

Conversation

@VoidChecksum
Copy link

Summary

Fixes a path traversal vulnerability (CWE-22) in FileOperationsManager that allows arbitrary file write, modify, delete, and list operations outside the intended sandbox directory (/tmp/hexstrike_files).

Affected methods: create_file(), modify_file(), delete_file(), list_files()

Attack vector: Supplying filenames containing ../ sequences or symlinks pointing outside the sandbox (e.g. ../../../var/www/html/shell.php).

Impact: Arbitrary file write leading to RCE via web shell injection, SSH key hijacking, cron job exploitation, or system configuration tampering.

Fix

Added a _validate_path() method that enforces sandbox boundaries on all file operations:

  1. Rejects absolute paths — prevents direct path specification like /etc/passwd
  2. Rejects null bytes — prevents path truncation attacks in OS APIs
  3. Resolves canonical paths — uses Path.resolve() to normalize .. sequences and follow symlinks before validation
  4. Verifies containment — uses Path.relative_to() to confirm the resolved path is under base_dir, avoiding prefix false-positives (e.g. /tmp/hexstrike_files_evil would not match /tmp/hexstrike_files)
  5. Resolves base_dir at initself.base_dir is now stored as its resolved canonical path to prevent TOCTOU issues

All four public methods (create_file, modify_file, delete_file, list_files) now call _validate_path() before any filesystem operation. ValueError exceptions from validation are caught by existing except Exception handlers and returned as error responses.

Test Results

Validated against the following attack vectors:

Input Result
normal.txt Allowed
subdir/file.txt Allowed
../../../tmp/shell.php Blocked
../../../var/www/html/shell.php Blocked
foo/../../bar/../../../etc/passwd Blocked
/etc/passwd Blocked
Symlink escaping sandbox Blocked

Closes #135

Add _validate_path() method that enforces sandbox boundary on all file
operations (create, modify, delete, list). The method:

- Rejects absolute paths outright
- Rejects null bytes that could truncate paths
- Resolves canonical paths (normalizes .., follows symlinks)
- Verifies resolved path is under base_dir using Path.relative_to()
  to avoid prefix false-positives

Resolves 0x4m4#135
Copilot AI review requested due to automatic review settings March 15, 2026 19:47
Copy link

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 FileOperationsManager’s filesystem sandboxing to prevent CWE-22 path traversal so file creation/modification/deletion/listing can’t escape /tmp/hexstrike_files.

Changes:

  • Canonicalizes and validates user-supplied paths via a new _validate_path() helper (absolute-path + null-byte rejection, resolve(), and relative_to() containment check).
  • Stores base_dir as a resolved path at initialization and applies _validate_path() to create_file, modify_file, delete_file, and list_files.

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

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +8932 to 8933
self.base_dir = Path(base_dir).resolve()
self.base_dir.mkdir(exist_ok=True)
def __init__(self, base_dir: str = "/tmp/hexstrike_files"):
self.base_dir = Path(base_dir)
self.base_dir = Path(base_dir).resolve()
self.base_dir.mkdir(exist_ok=True)
self.base_dir.mkdir(exist_ok=True)
self.max_file_size = 100 * 1024 * 1024 # 100MB

def _validate_path(self, filename: str) -> 'Path':
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.

Path Traversal in File Sandbox Allows Arbitrary File Write Outside Base Directory

2 participants