PyCQA / PyCQA/bandit

False negative: B202 unsafe `tarfile.extract()` not detected

Open Beginner friendly
#1,392 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
8.3k
Forks
836
Avg merge
5d 3h
Merged PRs (30d)
1

Description

Describe the bug

B202 is intended to detect unsafe tar extraction patterns (CWE-22), but the current implementation only checks for extractall calls and misses unsafe extract usage.

Reproduction steps
cat > /tmp/repro_b202_extract_fn.py <<'PY'
import tarfile
import tempfile
from pathlib import Path
def unsafe_archive_handler(filename):
    target_dir = Path(tempfile.mkdtemp())
    tar = tarfile.open(filename)
    for member in tar.getmembers():
        # Vulnerable: no validation of member.name/linkname
        tar.extract(member, path=target_dir)
    tar.close()
PY
bandit -r /tmp/repro_b202_extract_fn.py -t B202
Expected behavior

B202 should also detect an unsafe tarfile.extract() usage, not only extractall().

Bandit version

1.9.1 (Default)

Python version

3.14 (Default)

Additional context

No response

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.

Research direction

Start with the B202 rule and run the reproduction script at /tmp/repro_b202_extract_fn.py using the reported bandit command. Compare detection of tar.extract() with the existing extractall() behavior; done means unsafe extract usage is reported without losing the current check.

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
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.