[Security] tarfile.extractall without member validation in src/crate/testing/layer.py
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
Start in src/crate/testing/layer.py and inspect the tarfile.extractall() call and its callers. Review the proposed safe_extract logic and verify that archive members cannot write outside the target directory. Done means the Bandit B202 vulnerability is addressed without breaking the existing layer extraction behavior.
Written by the indexing model from the issue text.
Description
Severity: HIGH (Bandit B202)
File: src/crate/testing/layer.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
- Dominant language
- Python
- Stars
- 85
- Forks
- 34
- Avg merge
- 3d 1h
- Merged PRs (30d)
- 4
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.
More from crate/crate-python
-
enhancement
crate/crate-python#833 · 1 assignee ·
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 52/100
crate/crate-python#826 ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
crate/crate-python#820 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
crate/crate-python#751 · 7 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
crate/crate-python#729 · 1 comment ·
All issues in crate/crate-python
Similar issues
-
Add: hunch Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DiamondLightSource/dodal#2211 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
openml/openml-python#1749 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
sipyourdrink-ltd/bernstein#6191 ·