Created
April 5, 2021 09:31
-
-
Save kousu/bf5610187b608d79d415b1436091ab2d to your computer and use it in GitHub Desktop.
Sanitize a file path against directory traversal in python
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
import os.path | |
def sanitize_path(path): | |
""" | |
Sanitize a path against directory traversals | |
Based on https://stackoverflow.com/questions/13939120/sanitizing-a-file-path-in-python. | |
>>> sanitize_path('../test') | |
'test' | |
>>> sanitize_path('../../test') | |
'test' | |
>>> sanitize_path('../../abc/../test') | |
'test' | |
>>> sanitize_path('../../abc/../test/fixtures') | |
'test/fixtures' | |
>>> sanitize_path('../../abc/../.test/fixtures') | |
'.test/fixtures' | |
>>> sanitize_path('/test/foo') | |
'test/foo' | |
>>> sanitize_path('./test/bar') | |
'test/bar' | |
>>> sanitize_path('.test/baz') | |
'.test/baz' | |
>>> sanitize_path('qux') | |
'qux' | |
""" | |
# - pretending to chroot to the current directory | |
# - cancelling all redundant paths (/.. = /) | |
# - making the path relative | |
return os.path.relpath(os.path.join("/", path), "/") | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know why this is so hard. The top StackOverflow answer says to use
secure_filename()
but that only works on file_names_ -- it forces all'/
to_
-- which isn't my use case. When I wrote this I had in mind accessing HTML snippets or files in an upload folder, and for that I want to preserve some directory structure just not access to the whole system.Reading the docs, one thing that
resolve()
does is follow all symlinks. So my version is not safe against symlink poisoning. I assumed my apps don't make symlinks and no one else should be writing into their folder, but it's a risk. There's no way to protect against symlink poisoning without actually reading the disk.To be safe against that I guess you need to take a different approach and check if you're in a given restricted folder. But that's tricky to get right as well: