Security: Path Traversal in RootedDirFileSystem._join#621
Open
tomaioo wants to merge 1 commit into
Open
Conversation
The `RootedDirFileSystem._join` method attempts to prevent path traversal by checking if the resolved path starts with the root path. However, the check uses `os.path.normpath` and `make_path_posix` which may not handle all path traversal techniques. Specifically, the `startswith` check is vulnerable if the root path is not properly normalized or if an attacker can craft a path that passes the check but still escapes the root. More critically, the `startswith` check with `root_posix` can be bypassed if the root path doesn't end with a separator and the path contains the root as a prefix. For example, if root is `/data/storage`, a path like `/data/storage_extra` would pass the startswith check but is outside the intended directory. Additionally, the method doesn't handle symlink attacks or race conditions. Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
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
Security: Path Traversal in RootedDirFileSystem._join
Problem
Severity:
High| File:fs_storage/rooted_dir_file_system.py:L28The
RootedDirFileSystem._joinmethod attempts to prevent path traversal by checking if the resolved path starts with the root path. However, the check usesos.path.normpathandmake_path_posixwhich may not handle all path traversal techniques. Specifically, thestartswithcheck is vulnerable if the root path is not properly normalized or if an attacker can craft a path that passes the check but still escapes the root. More critically, thestartswithcheck withroot_posixcan be bypassed if the root path doesn't end with a separator and the path contains the root as a prefix. For example, if root is/data/storage, a path like/data/storage_extrawould pass the startswith check but is outside the intended directory. Additionally, the method doesn't handle symlink attacks or race conditions.Solution
Ensure the root path ends with a path separator before using startswith, or use a more robust path containment check. Consider using
os.path.commonpathor ensuring both paths are resolved withos.path.realpathbefore comparison. Also handle the case where paths are exactly equal.Changes
fs_storage/rooted_dir_file_system.py(modified)