Created
October 16, 2014 13:18
-
-
Save Atala/05ac6f8487fc0e81c197 to your computer and use it in GitHub Desktop.
Unzip a file in python - note that extractall() is sensible to traversal attacks
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 zipfile,os.path | |
def unzip(source_filename, dest_dir): | |
with zipfile.ZipFile(source_filename) as zf: | |
for member in zf.infolist(): | |
# Path traversal defense copied from | |
# http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789 | |
words = member.filename.split('/') | |
path = dest_dir | |
for word in words[:-1]: | |
drive, word = os.path.splitdrive(word) | |
head, word = os.path.split(word) | |
if word in (os.curdir, os.pardir, ''): continue | |
path = os.path.join(path, word) | |
zf.extract(member, path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment