diff --git "a/Python\346\240\270\345\277\203\347\274\226\347\250\213/\347\275\221\347\273\234\347\274\226\347\250\213/\346\210\252\345\233\276\345\222\214\344\273\243\347\240\201/\346\246\202\350\277\260\343\200\201SOCKET/\345\244\232\350\277\233\347\250\213copy\346\226\207\344\273\266/test-\345\244\215\344\273\266/tarfile.py" "b/Python\346\240\270\345\277\203\347\274\226\347\250\213/\347\275\221\347\273\234\347\274\226\347\250\213/\346\210\252\345\233\276\345\222\214\344\273\243\347\240\201/\346\246\202\350\277\260\343\200\201SOCKET/\345\244\232\350\277\233\347\250\213copy\346\226\207\344\273\266/test-\345\244\215\344\273\266/tarfile.py" index e660ae9..ad5b1b1 100644 --- "a/Python\346\240\270\345\277\203\347\274\226\347\250\213/\347\275\221\347\273\234\347\274\226\347\250\213/\346\210\252\345\233\276\345\222\214\344\273\243\347\240\201/\346\246\202\350\277\260\343\200\201SOCKET/\345\244\232\350\277\233\347\250\213copy\346\226\207\344\273\266/test-\345\244\215\344\273\266/tarfile.py" +++ "b/Python\346\240\270\345\277\203\347\274\226\347\250\213/\347\275\221\347\273\234\347\274\226\347\250\213/\346\210\252\345\233\276\345\222\214\344\273\243\347\240\201/\346\246\202\350\277\260\343\200\201SOCKET/\345\244\232\350\277\233\347\250\213copy\346\226\207\344\273\266/test-\345\244\215\344\273\266/tarfile.py" @@ -2504,7 +2504,26 @@ def main(): if is_tarfile(src): with TarFile.open(src, 'r:*') as tf: - tf.extractall(path=curdir) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tf, path=curdir) if args.verbose: if curdir == '.': msg = '{!r} file is extracted.'.format(src) diff --git "a/Python\346\240\270\345\277\203\347\274\226\347\250\213/\347\275\221\347\273\234\347\274\226\347\250\213/\346\210\252\345\233\276\345\222\214\344\273\243\347\240\201/\346\246\202\350\277\260\343\200\201SOCKET/\345\244\232\350\277\233\347\250\213copy\346\226\207\344\273\266/test/tarfile.py" "b/Python\346\240\270\345\277\203\347\274\226\347\250\213/\347\275\221\347\273\234\347\274\226\347\250\213/\346\210\252\345\233\276\345\222\214\344\273\243\347\240\201/\346\246\202\350\277\260\343\200\201SOCKET/\345\244\232\350\277\233\347\250\213copy\346\226\207\344\273\266/test/tarfile.py" index e660ae9..ad5b1b1 100644 --- "a/Python\346\240\270\345\277\203\347\274\226\347\250\213/\347\275\221\347\273\234\347\274\226\347\250\213/\346\210\252\345\233\276\345\222\214\344\273\243\347\240\201/\346\246\202\350\277\260\343\200\201SOCKET/\345\244\232\350\277\233\347\250\213copy\346\226\207\344\273\266/test/tarfile.py" +++ "b/Python\346\240\270\345\277\203\347\274\226\347\250\213/\347\275\221\347\273\234\347\274\226\347\250\213/\346\210\252\345\233\276\345\222\214\344\273\243\347\240\201/\346\246\202\350\277\260\343\200\201SOCKET/\345\244\232\350\277\233\347\250\213copy\346\226\207\344\273\266/test/tarfile.py" @@ -2504,7 +2504,26 @@ def main(): if is_tarfile(src): with TarFile.open(src, 'r:*') as tf: - tf.extractall(path=curdir) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tf, path=curdir) if args.verbose: if curdir == '.': msg = '{!r} file is extracted.'.format(src)