I'm trying to extract user-submitted zip and tar files to a directory. The documentation for zipfile's extractall method (similarly with tarfile's extractall) states that it's possible for paths to be absolute or contain ..
paths that go outside the destination path. Instead, I could use extract
myself, like this:
some_path = '/destination/path'
some_zip = '/some/file.zip'
zipf = zipfile.ZipFile(some_zip, mode='r')
for subfile in zipf.namelist():
zipf.extract(subfile, some_path)
Is this safe? Is it possible for a file in the archive to wind up outside of some_path
in this case? If so, what way can I ensure that files will never wind up outside the destination directory?
Contrary to the popular answer, unzipping files safely is not completely solved as of Python 2.7.4. The extractall method is still dangerous and can lead to path traversal, either directly or through the unzipping of symbolic links. Here was my final solution which should prevent both attacks in all versions of Python, even versions prior to Python 2.7.4 where the extract method was vulnerable:
Edited: Fixed variable name clash. Thanks Juuso Ohtonen.
Copy the zipfile to an empty directory. Then use
os.chroot
to make that directory the root directory. Then unzip there.Alternatively, you can call
unzip
itself with the-j
flag, which ignores the directories:Note: Starting with python 2.7.4, this is a non-issue for ZIP archives. Details at the bottom of the answer. This answer focuses on tar archives.
To figure out where a path really points to, use
os.path.abspath()
(but note the caveat about symlinks as path components). If you normalize a path from your zipfile withabspath
and it does not contain the current directory as a prefix, it's pointing outside it.But you also need to check the value of any symlink extracted from your archive (both tarfiles and unix zipfiles can store symlinks). This is important if you are worried about a proverbial "malicious user" that would intentionally bypass your security, rather than an application that simply installs itself in system libraries.
That's the aforementioned caveat:
abspath
will be misled if your sandbox already contains a symlink that points to a directory. Even a symlink that points within the sandbox can be dangerous: The symlinksandbox/subdir/foo -> ..
points tosandbox
, so the pathsandbox/subdir/foo/../.bashrc
should be disallowed. The easiest way to do so is to wait until the previous files have been extracted and useos.path.realpath()
. Fortunatelyextractall()
accepts a generator, so this is easy to do.Since you ask for code, here's a bit that explicates the algorithm. It prohibits not only the extraction of files to locations outside the sandbox (which is what was requested), but also the creation of links inside the sandbox that point to locations outside the sandbox. I'm curious to hear if anyone can sneak any stray files or links past it.
Edit: Starting with python 2.7.4, this is a non-issue for ZIP archives: The method
zipfile.extract()
prohibits the creation of files outside the sandbox:The
tarfile
class has not been similarly sanitized, so the above answer still apllies.Use
ZipFile.infolist()
/TarFile.next()
/TarFile.getmembers()
to get the information about each entry in the archive, normalize the path, open the file yourself, useZipFile.open()
/TarFile.extractfile()
to get a file-like for the entry, and copy the entry data yourself.