fix: prevent path traversal in FileOperationsManager sandbox (CWE-22)#159
Open
VoidChecksum wants to merge 1 commit into0x4m4:masterfrom
Open
fix: prevent path traversal in FileOperationsManager sandbox (CWE-22)#159VoidChecksum wants to merge 1 commit into0x4m4:masterfrom
VoidChecksum wants to merge 1 commit into0x4m4:masterfrom
Conversation
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
There was a problem hiding this comment.
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(), andrelative_to()containment check). - Stores
base_diras a resolved path at initialization and applies_validate_path()tocreate_file,modify_file,delete_file, andlist_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': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a path traversal vulnerability (CWE-22) in
FileOperationsManagerthat 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:/etc/passwdPath.resolve()to normalize..sequences and follow symlinks before validationPath.relative_to()to confirm the resolved path is underbase_dir, avoiding prefix false-positives (e.g./tmp/hexstrike_files_evilwould not match/tmp/hexstrike_files)self.base_diris now stored as its resolved canonical path to prevent TOCTOU issuesAll four public methods (
create_file,modify_file,delete_file,list_files) now call_validate_path()before any filesystem operation.ValueErrorexceptions from validation are caught by existingexcept Exceptionhandlers and returned as error responses.Test Results
Validated against the following attack vectors:
normal.txtsubdir/file.txt../../../tmp/shell.php../../../var/www/html/shell.phpfoo/../../bar/../../../etc/passwd/etc/passwdCloses #135