Resolve "Path directory prefix bypass in ubifs/output.py" - #140
Open
qkaiser wants to merge 3 commits into
Open
Conversation
using path.startswith(basedir) without a trailing directory separator allows directory traversal if a target directory shares a common prefix. For example, if basedir is /tmp/target, and path is ../target_hack, os.path.realpath resolves it to /tmp/target_hack, which passes the startswith check and allows writing outside the base directory. Fixed by using the standardized implementation we use in jefferson.
elektrischermoench
requested changes
Sep 4, 2026
elektrischermoench
left a comment
Contributor
There was a problem hiding this comment.
Just worth mentioning test is passing on old code also.
| ("/lib/out", "some/dir/../../../", False), | ||
| ("/lib/out", "some/dir/../../..", False), | ||
| ("/lib/out", "../file", False), | ||
| ("/lib/out", "/lib/out/../file", False), |
Contributor
There was a problem hiding this comment.
The fix is correct, but the test suite doesn't cover all effects of the bug. The old code accepted out_hack just because the name starts with out:
.venv/bin/python -m pytest tests/test_ubifs_output.py -v --no-cov
================================== test session starts ===================================
platform linux -- Python 3.14.7, pytest-9.1.1, pluggy-1.6.0 -- /tmp/claude-1000/-home-kai-development/281967f1-1e69-4fdb-beee-4285ab125719/scratchpad/ubi_reader/.venv/bin/python
cachedir: .pytest_cache
rootdir: /tmp/claude-1000/-home-kai-development/281967f1-1e69-4fdb-beee-4285ab125719/scratchpad/ubi_reader
configfile: pyproject.toml
plugins: cov-7.1.0
collected 11 items
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-/lib/out/file-True] PASSED [ 9%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-file-True] PASSED [ 18%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-dir/file-True] PASSED [ 27%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-some/dir/file-True] PASSED [ 36%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-some/dir/../file-True] PASSED [ 45%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-some/dir/../../file-True] PASSED [ 54%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-some/dir/../../../file-False] PASSED [ 63%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-some/dir/../../../-False] PASSED [ 72%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-some/dir/../../..-False] PASSED [ 81%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-../file-False] PASSED [ 90%]
tests/test_ubifs_output.py::test_is_safe_path[/lib/out-/lib/out/../file-False] PASSED [100%]
=================================== 11 passed in 0.09s ===================================
Maybe add tests like this:
("/lib/out", "../out_hack", False),
("/lib/out", "../outx", False),
("/lib/out", "../out.bak/file", False),
("/lib/out", "/lib/out_hack", False),
("/lib/out", "/lib/outfile", False),
("/lib/out", "some/dir/../../../out_hack/file", False),
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.
using path.startswith(basedir) without a trailing directory separator allows directory traversal if a target directory shares a common prefix.
For example, if basedir is /tmp/target, and path is ../target_hack, os.path.realpath resolves it to /tmp/target_hack, which passes the startswith check and allows writing outside the base directory.
Fixed by using the standardized implementation we use in jefferson. Implemented the test suite we use in unblob, and made sure we run the tests in CI.
Fixes #139