Catch an edge case in expand._assert_local() #3595
Merged
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 of changes
Using
str.startswith()
for sandboxing file access has an edge case where someone can access files outside the root directory. For example, consider the case where the root directory is "/home/user/my-package" but some secrets are stored in "/home/user/my-package-secrets". Evaluating a check that "/home/user/my-package-secrets".startswith("/home/user/my-package") will return True, but the statement's intention is that no file outside of "/home/user/my-package" can be accessed.Using
pathlib.Path.resolve()
andpathlib.Path.parents
eliminates this edge case.See https://salvatoresecurity.com/preventing-directory-traversal-vulnerabilities-in-python/ for more details.
Caveats
pathlib.Path.resolve()
will raise aRuntimeError
.pathlib.Path.resolve()
resolves symlinks, whereas the old code usingos.pathlib.abspath()
did not.I am not intimately familiar with setuptools and could use some guidance as to whether or not either of these two caveats would cause an issue. My guess would be that the first would not impact users and the second might, but probably won't. If either of the above caveats is an issue, I can provide an alternate implementation using
os.path.abspath()
, though that comes with its own caveats.Pull Request Checklist
changelog.d/
. - This PR should not change the way users interact with setuptools, with the possible exception of the caveats above.