[Security] tarfile.extractall without member validation in src/crate/testing/layer.py

Open Beginner friendly
#794 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python
Domain
security

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

Dominant language
Python
Stars
85
Forks
34
Avg merge
3d 1h
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from crate/crate-python

All issues in crate/crate-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.