diff --git a/README.md b/README.md index 242b349..047c443 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ + + + # Learn Python Programming 3rd Edition Welcome to Learn Python Programming, 3rd Edition. @@ -29,6 +32,16 @@ that might suit you better. We have given the virtual environment folder the name `.venv`. Feel free to choose any other name that you might want. +--- + +Note: the above procedure might fail in some of the chapter folders, due +to the presence of files that aren't meant to be run. If that is the case, you +should choose another folder where to place your virtual environment. You can +create one within the chapter folder itself, or place the virtual environment +outside of the chapter folder altogether. + +--- + Next step is to activate the virtual environment: $ source .venv/bin/activate @@ -52,3 +65,7 @@ find all the instructions you need in the chapter's text. **Note**: Always remember to activate the virtual environment before you install third-party libraries. +### Download a free PDF + + If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
+

https://packt.link/free-ebook/9781801815093

\ No newline at end of file diff --git a/ch08/files/compression/tar.py b/ch08/files/compression/tar.py index 69af6c2..593c0fc 100644 --- a/ch08/files/compression/tar.py +++ b/ch08/files/compression/tar.py @@ -8,4 +8,26 @@ tar.add('subfolder/content4.txt') with tarfile.open('example.tar.gz', 'r:gz') as tar: - tar.extractall('extract_tar') + + import os + + 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(tar, "extract_tar")