fix: detect unsafe tarfile.extract() in B202#1414
Open
dashitongzhi wants to merge 1 commit into
Open
Conversation
B202 previously only checked for tarfile.extractall() calls, missing the equally dangerous tarfile.extract() which can also be exploited for path traversal attacks (CWE-22). Changes: - Match both 'extract' and 'extractall' in the call function name check - Only check 'members' keyword for extractall (extract doesn't accept it) - Update docstring and example to document extract() coverage - Update severity message to be generic for both patterns Closes PyCQA#1392
There was a problem hiding this comment.
Pull request overview
This PR expands Bandit’s B202 (tarfile_unsafe_members) plugin to also flag unsafe tarfile.extract() usage (in addition to extractall()), addressing the false negative reported in #1392.
Changes:
- Update B202 detection to match both
extractandextractall, and scope thememberskeyword handling toextractallonly. - Generalize the HIGH-severity issue text to cover unsafe tar extraction broadly.
- Add an
unsafe_extract_handler()example toexamples/tarfile_extractall.py.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
bandit/plugins/tarfile_unsafe_members.py |
Extends B202 detection to include extract() and updates messaging/docs. |
examples/tarfile_extractall.py |
Adds an extract()-based unsafe example intended to trigger B202. |
Comments suppressed due to low confidence (1)
bandit/plugins/tarfile_unsafe_members.py:16
- The docstring severity breakdown still only describes
extractall()/members=...cases. Since the plugin now also flagsextract(), please update this section to document howextract()is classified (likely always HIGH because there is nomembers/filtermitigation path in this plugin forextract()).
Severity are set as follows:
* ``tarfile.extractall(members=function(tarfile))`` - LOW
* ``tarfile.extractall(members=?)`` - member is not a function - MEDIUM
* ``tarfile.extractall()`` - members from the archive is trusted - HIGH
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
108
to
112
| if all( | ||
| [ | ||
| context.is_module_imported_exact("tarfile"), | ||
| "extractall" in context.call_function_name, | ||
| context.call_function_name in ("extract", "extractall"), | ||
| ] |
|
|
||
| def unsafe_extract_handler(filename): | ||
| tar = tarfile.open(filename) | ||
| for member in tar.getmembers(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
B202 (
tarfile_unsafe_members) previously only detected unsafetarfile.extractall()calls, missing the equally dangeroustarfile.extract()usage pattern (reported in #1392).Problem
The condition
"extractall" in context.call_function_nameuses substring matching, which correctly matches"extractall"but does not match bare"extract". An attacker exploiting a vulnerabletarfile.extract()loop with malicious member names (e.g., path traversal via../) would go undetected.Changes
extractandextractallby changing the condition to an exact-set check:context.call_function_name in ("extract", "extractall")memberskeyword check to only apply forextractall(sinceextract()does not accept amembersparameter)extractallunsafe_extract_handler()inexamples/tarfile_extractall.pyTest plan
Reproduce the issue from #1392 and verify B202 now fires for
tarfile.extract():Closes #1392