Repository navigation
zipfile.ZipFile overwrites files outside destination path #51221
Description
Activity
ZipFile.extractall happily overwrites any file on the filesystem. One
can put files with a name like "//etc/password" in a zip file and
extractall will overwrite /etc/password (with sufficient rights).The docs say:
ZipFile.extractall([path[, members[, pwd]]])
Extract all members from the archive to the current workingdirectory. path specifies a different directory to extract to. members
is optional and must be a subset of the list returned by namelist(). pwd
is the password used for encrypted files.I read that as: it will put all files into path or a subdirectory.
Using names like "../../../etc/password" also leads to files being
written outside that path directory.- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-securityA security issueA security issue
on Sep 22, 2009 - changed the title
[-]zipfile.ZipFile[/-][+]zipfile.ZipFile overwrites files outside destination path[/+]on Sep 22, 2009 Do people have an opinion as to whether this should be fixed with a
docfix, fixed as default (with option to allow path traversal) or fixed as
a non-default option? The same issue exists in ZipFile.extract, but in
that case you're presumably passing a path you've already vetted.Either way, I'll write a test case for it tomorrow.
I think this should clearly be fixed in the code. The current code tries
to handle absolute paths by removing the first slash (unfortunately not
the second), so it looks like it tries to be safe and only write to the
destination directory. That should be the default operation.
I even think that there should be *no* option to allow overriding files
outside the destination path (on unix one can always use / as
destination if he feels like overwriting his /etc/passwd)
The documentation should also mention that it's unsafe to use this
method in python <2.6.2.The documentation should also mention that it's unsafe to use this
method in python <= 2.6.2. 2.6.2 is also unsafe.The tarfile module solved this issue with a documentation warning:
http://docs.python.org/library/tarfile.html#tarfile.TarFile.extractallMy working solution is to iterate through members, and ensuring that
os.path.abspath(os.path.join(path, member)) always .startswith(path).
This seems like a better solution than trying to trap on a pattern in
the string. Presumably the same fix can be made to tarfile.For what it's worth, OS X's BOMArchiveManager will place a file stored
as '../foo.txt' in the extract path, not the directory right outside it.While we're on the topic, there may also be a bug in this, or the
tarfile package that would allow a malicious archive to extract a
symlink to an existing directory somewhere on the target machine, and
files extracted to that symlink. I haven't really thought that through,
but I'm sure that my fix won't correct that possible issue.Uploading test.
Uploading patch. This actually should fix my theoretical symlink bug
since realpath() properly follows symlinks. The only thing that I
haven't been able to test is the behavior of realpath() on
case-insensitive operating systems. This should do the right thing, the
path should be normalized, but can someone confirm this?As for the documentation, it might be a wise idea to up date the current
documentation to mention this issue, until the next release. I'm not
really sure what the process is for doing that, though...Patches to the docs, just like patches to the code (the docs are in the
Doc subdirectory). Once committed, they get auto-generated and uploaded.Documentation note added (copied from tarfile) in trunk r75149,
release26-maint r75150 (hopefully in time for 2.6.3 but thats up to
Barry).33 remaining items
New changeset ebef003a2acd by Serhiy Storchaka in branch '2.7':
Fix the test and remove trailing dots on Windows for issue bpo-6972.
http://hg.python.org/cpython/rev/ebef003a2acdNew changeset 5a68052b52ea by Serhiy Storchaka in branch '2.7':
Preserve backslashes in malicious zip files for testing issue bpo-6972.
http://hg.python.org/cpython/rev/5a68052b52eaNew changeset ab4b8da79a5f by Serhiy Storchaka in branch '2.7':
Fix test for issue bpo-6972.
http://hg.python.org/cpython/rev/ab4b8da79a5fNew changeset 434b50c7bbed by Serhiy Storchaka in branch '3.2':
Fix the test for issue bpo-6972.
http://hg.python.org/cpython/rev/434b50c7bbedNew changeset 8b33f3a4a200 by Serhiy Storchaka in branch '3.3':
Fix the test for issue bpo-6972.
http://hg.python.org/cpython/rev/8b33f3a4a200New changeset 7a47fd7f2fdc by Serhiy Storchaka in branch 'default':
Fix the test for issue bpo-6972.
http://hg.python.org/cpython/rev/7a47fd7f2fdcI believe this is all done after Serhiy's fixes.
There are 2 issues with the documentation changes introduced by these patches.
-
for 2.7, the note added by the doc patch is in the wrong place, at the setpassword method instead of the extract or extractall method
-
for 3.x the "Never extract archives from untrusted sources..." warning got removed but it's still useful for users that read the documentation online and therefore get the updated docs but haven't updated Python to the latest patch release and therefore don't have the fix. For example, anybody reading the docs for 3.2 or 3.3 today doesn't see that extractall is dangerous and there is no released Python containing the fix so by all practical means extractall is still dangerous today.
To address point 2, I think the warning should be kept with an extra mention regarding exact version where it got fixed so that, when reading the documentation, everybody can assess exactly whether extractall is safe for them to use or not.
I can't reopen the bug since I don't have tracker privileges but since it's a security issue I think it's important for these to get addressed.
-
reopening as documentation mixups remain to be fixed.
New changeset d73fb6b06891 by Gregory P. Smith in branch '2.7':
Issue bpo-6972: fix the documentation mis applied patch.
http://hg.python.org/cpython/rev/d73fb6b06891New changeset 1c2d41850147 by Gregory P. Smith in branch '3.2':
Issue bpo-6972: keep the warning about untrusted extraction and mention
http://hg.python.org/cpython/rev/1c2d41850147New changeset 5fbca37de9b1 by Gregory P. Smith in branch '3.3':
Issue bpo-6972: keep the warning about untrusted extraction and mention
http://hg.python.org/cpython/rev/5fbca37de9b1New changeset f5e3f2f0fe79 by Gregory P. Smith in branch 'default':
Issue bpo-6972: keep the warning about untrusted extraction and mention
http://hg.python.org/cpython/rev/f5e3f2f0fe79
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: