-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We can rewrite symlinks to ensure they are always relative and remain within the extraction directory.
- Loading branch information
Andrew Fasano
committed
Feb 12, 2024
1 parent
49de1c6
commit 954c1cd
Showing
1 changed file
with
61 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
954c1cd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is a good idea to rewrite offensive symlinks to point to a safe place instead of deleting them, as some heuristics could save them.
Cherry-picking this commit, and adding a simple
fix_symlinks
implementation just for the tests, it is quite close to pass existing test with only 2 test failures, and some strange warnings.The implementation is also somewhat complex to validate due to the many cases, and is tricky to get right, as detailed below.
Tests could help to document what is happening with symlinks in certain cases (even if comments do help a lot), as it is quite complex code.
So this commit still needs some further work.
Unblob wants to ensure with
fix_extracted_directory
, that after unblob finished, no symlinks are pointing outside the directory.This is a rather hard thing to ensure, as paths can have symlink "directory" paths, which could twist the string based logic, and can be exploited:
would create a tree
would access
../passwd
, while both of the symlinks looks valid and safe in isolation.We try to catch things like above with the use of
is_safe_path
, which internally usesos.path.realpath
and notabspath
which usesnormpath
and is strings based, which can be tricked like above.in
escaped-dir
, the difference is this:This is a very subtle and brittle difference, as
realpath
works like this only when there is a matching filesystem, as once the file path part no longer has a matching file-system object, it falls back to string manipulation.Also, we had a lot of tests in
tests/test_extractor.py
that have checked problem cases with symlinks, these can be revived by adding the below to this file.With the above
fix_symlink
, 2 tests are failing:However, there are new warnings reported, that pytest could not remove some directories. Looking them up, they have directories with strange permissions:
954c1cd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#768