PyCQA / PyCQA/bandit

Incorrect result for B202:tarfile_unsafe_members

Open
#1,038 4 comments 2 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

The B202:tarfile_unsafe_members documentation says to pass a callable as the members argument but that’s not supported in the official type signature and not implemented in CPython stdlib. members should be used as an Iterable[TarInfo] instead.

That change was introduced in v1.7.5 based on issue #207 and PR #549
cc @yilmi @ericwb @lukehinds @sigmavirus24

The following fixes are required to address this bug:

  1. The tarfile.extractalll(members=function(tarfile)) - LOW suggestion here seems to be wrong.
  2. The check on ast.Call node here should be fixed/removed.
  3. The extractall function name look up here is too coarse and can easily result in inaccurate results for other libraries that have the same function names, e.g., ZipFile.extractall.
Reproduction steps

This PR addresses the B202:tarfile_unsafe_members by validating members Iterable argument but Bandit cannot detect the filtering of members used to fix the issue (hence the need to suppress the error)

Expected behavior

The check on ast.Call node here should be fixed/removed. We should not assume the members argument to have a Callable type.

Bandit version

1.7.5 (Default)

Python version

3.11 (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:tarfile_unsafe_members implementation and documentation, then inspect the ast.Call check and extractall function-name lookup linked in the issue. Compare the behavior with tarfile's members Iterable and ZipFile.extractall, and consider the issue's three requested corrections complete only when the documented behavior and detection results agree.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
security, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.