[Security] tarfile.extractall without member validation in examples/tarfile_extractall.py
Open
Beginner friendly
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8.3k
- Forks
- 836
- Avg merge
- 5d 3h
- Merged PRs (30d)
- 1
Description
Severity: HIGH (Bandit B202)
File: examples/tarfile_extractall.py
Vulnerability
tarfile.extractall() without member validation allows path traversal (zip slip). A malicious archive can write files outside the target directory.
Fix
import os
SAFE_ID = __import__("re").compile(r"^[a-zA-Z0-9_.-]+$")
def _is_within_directory(directory, target):
abs_directory = os.path.realpath(directory)
abs_target = os.path.realpath(target)
return abs_target.startswith(abs_directory + os.sep) or abs_target == 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(f"Path traversal in tar: {member.name}")
tar.extractall(path, members, numeric_owner=numeric_owner)
References
- CWE-22: Path Traversal
- Bandit B202
- OWASP: Zip Slip
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with examples/tarfile_extractall.py and inspect how tarfile.extractall() is called. Add the requested member-path validation so archive entries cannot escape the target directory, while safe entries still extract; verify the example no longer triggers Bandit B202.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- security
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 84/100