bandit does not consistently detect extractall with TarFile
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8.3k
- Forks
- 836
- Avg merge
- 5d 3h
- Merged PRs (30d)
- 1
Description
Describe the bug
Hello bandit team,
I observed TarFile.extractall is not detected as vulnerable (B202:tarfile_unsafe_members) without explicit import tarfile line present (even if it is not actually used).
I created three simple files to demonstrate the issue:
case1.pyuses bothTarFile.extractallandtarfile.extractallcase2.pyusesTarFile.extractallonlycase3.pyusesTarFile.extractallonly but with "useless"import tarfile
# bandit-repro/case1.py
import tarfile
from pathlib import Path
from tarfile import TarFile
def extractall_TarFile(p: Path, out: Path):
"bandit detects this correctly as vulnerable"
with TarFile(p) as tar:
tar.extractall(out)
def extract_tarfile(p: Path, out: Path):
"bandit detects this correctly as vulnerable"
with tarfile.open(p) as tar:
tar.extractall(out)
# bandit-repro/case2.py
from pathlib import Path
from tarfile import TarFile
def extractall_bandit_does_not_detect(p: Path, out: Path):
"bandit *does not* detect this correctly as vulnerable"
with TarFile(p) as tar:
tar.extractall(out)
# bandit-repro/case3.py
import tarfile # noqa: F401
from pathlib import Path
from tarfile import TarFile
def extractall_bandit_detects(p: Path, out: Path):
"bandit *does* detect this correctly as vulnerable with surplus tarfile import"
with TarFile(p) as tar:
tar.extractall(out)
Then, when I ran bandit, it did not detect that the vulnerability is present in case2.py as well:
$ bandit -r -a file -f txt bandit-repro
Run started:2024-09-03 22:52:08.975866
Test results:
>> Issue: [B202:tarfile_unsafe_members] tarfile.extractall used without any validation. Please check and discard dangerous members.
Severity: High Confidence: High
CWE: CWE-22 (https://cwe.mitre.org/data/definitions/22.html)
More Info: https://bandit.readthedocs.io/en/1.7.9/plugins/b202_tarfile_unsafe_members.html
Location: bandit-repro/case1.py:9:8
8 with TarFile(p) as tar:
9 tar.extractall(out)
10
--------------------------------------------------
>> Issue: [B202:tarfile_unsafe_members] tarfile.extractall used without any validation. Please check and discard dangerous members.
Severity: High Confidence: High
CWE: CWE-22 (https://cwe.mitre.org/data/definitions/22.html)
More Info: https://bandit.readthedocs.io/en/1.7.9/plugins/b202_tarfile_unsafe_members.html
Location: bandit-repro/case1.py:15:8
14 with tarfile.open(p) as tar:
15 tar.extractall(out)
--------------------------------------------------
>> Issue: [B202:tarfile_unsafe_members] tarfile.extractall used without any validation. Please check and discard dangerous members.
Severity: High Confidence: High
CWE: CWE-22 (https://cwe.mitre.org/data/definitions/22.html)
More Info: https://bandit.readthedocs.io/en/1.7.9/plugins/b202_tarfile_unsafe_members.html
Location: bandit-repro/case3.py:9:8
8 with TarFile(p) as tar:
9 tar.extractall(out)
--------------------------------------------------
Code scanned:
Total lines of code: 24
Total lines skipped (#nosec): 0
Total potential issues skipped due to specifically being disabled (e.g., #nosec BXXX): 0
Run metrics:
Total issues (by severity):
Undefined: 0
Low: 0
Medium: 0
High: 3
Total issues (by confidence):
Undefined: 0
Low: 0
Medium: 0
High: 3
Files skipped (0):
Reproduction steps
1. pipx install bandit
2. create folder bandit-repro with case1.py, case2.py and case3.py (as shown above) in it
3. bandit -r -a file -f txt bandit-repro
4. observe case2.py in not in the report
Expected behavior
TarFile.extractall should be reported as vulnerable no matter if import tarfile is present.
Bandit version
1.7.9 (Default)
Python version
3.12 (Default)
Additional context
Thanks for the project!
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 the B202:tarfile_unsafe_members plugin and reproduce the report using case1.py, case2.py, and case3.py with the supplied bandit command. Trace why detection depends on the unused import, then update the relevant coverage so case2.py is reported while the existing cases remain correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- security, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100