fix: prevent path prefix collision in _validate_file_paths#2346
Open
ashnaaseth2325-oss wants to merge 2 commits intoOWASP:masterfrom
Open
fix: prevent path prefix collision in _validate_file_paths#2346ashnaaseth2325-oss wants to merge 2 commits intoOWASP:masterfrom
ashnaaseth2325-oss wants to merge 2 commits intoOWASP:masterfrom
Conversation
Author
Collaborator
|
@ashnaaseth2325-oss, ok, fine, I guess. Remember to make sure your commits are verified. |
Replace abspath with realpath and append os.sep to base_path checks to block sibling-directory traversal (e.g. /base_evil bypassing /base). Mirrors the correct pattern already used in _safe_extractall (CWE-22). Signed-off-by: ashnaaseth2325-oss <ashnaaseth2325@gmail.com>
7cac812 to
2d4abf2
Compare
Author
|
Hi @sydseter |
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
This PR fixes a path-prefix collision vulnerability in
scripts/convert.py, specifically in_validate_file_paths().The function is intended to enforce that both
source_pathandoutput_dirremain confined withinconvert_vars.BASE_PATHbefore being passed to the LibreOffice subprocess. However, the containment check used a naïve string prefix comparison:This implementation incorrectly treats string prefix matching as filesystem containment. As a result, sibling directories such as:
would pass validation if
BASE_PATH = /opt/cornucopia.Notably, the same file already contains the correct containment pattern in
_safe_extractall()using+ os.sep, but that safeguard was not applied here.This creates a bypass of the security boundary that protects the LibreOffice subprocess.
Steps to Reproduce
Assume:
Create a sibling directory:
Place a valid document inside:
Run:
python scripts/convert.py \ --inputfile /opt/cornucopia_extra/sensitive.odt \ --outputfile output/test.pdf \ --pdfThe buggy check evaluates:
→
True(incorrectly allowed)LibreOffice then processes the file and writes output without error.
Impact
This vulnerability allows:
This maps to:
The issue is particularly critical because
_validate_file_paths()is the only explicit security gate before spawning an external process that parses untrusted document formats.Root Cause
The containment logic uses:
instead of:
This confuses string prefix comparison with filesystem boundary enforcement.
A correct implementation already exists in
_safe_extractall():However,
_validate_file_paths()was not updated when that fix was introduced, leading to inconsistent security enforcement within the same file.Fix
The fix mirrors the already-correct
_safe_extractall()implementation and ensures consistency:Changes include:
os.septo prevent prefix collisionsos.path.realpath()to resolve symlinks_safe_extractall()This is a minimal, contained fix with no behavioral change for legitimate paths.
Result
After this fix:
BASE_PATH/The fix restores the intended security boundary with minimal code changes and clear regression protection.