Skip to content

Detect unsafe tarfile.extract() in B202 plugin#1409

Open
jonasboos wants to merge 2 commits into
PyCQA:mainfrom
jonasboos:fix-1392-tarfile-extract
Open

Detect unsafe tarfile.extract() in B202 plugin#1409
jonasboos wants to merge 2 commits into
PyCQA:mainfrom
jonasboos:fix-1392-tarfile-extract

Conversation

@jonasboos
Copy link
Copy Markdown

Problem

The B202 plugin only detects calls but misses equally unsafe calls. Both methods have the same path traversal risk (CWE-22) when members are not validated.

As reported in #1392, code like this goes undetected:

import tarfile
tar = tarfile.open(filename)
for member in tar.getmembers():
    tar.extract(member, path=target_dir)  # Not flagged by B202

Fix

Add detection for with the same severity model:

  • Without keyword → HIGH severity, HIGH confidence
  • With → No issue (safe pattern, same as extractall)

Also renames the internal function to for clarity and adds a separate function with an appropriate message.

Testing

  • Added example code for usage (with and without filter)
  • Updated functional test expectations
  • All existing tests pass

Resolves: #1392

The B202 plugin only detected tarfile.extractall() calls but missed
equally unsafe tarfile.extract() calls, which have the same path
traversal risk (CWE-22) when members are not validated.

Add detection for tarfile.extract() with HIGH severity and confidence.
The filter='data' keyword is respected as a safe pattern, same as for
extractall.

Add example code and update functional tests.

Resolves: PyCQA#1392
Copilot AI review requested due to automatic review settings May 10, 2026 19:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request extends Bandit’s B202 plugin to also flag unsafe tarfile.extract() usage (path traversal risk) in addition to existing tarfile.extractall() detection, and updates examples + functional test expectations accordingly.

Changes:

  • Add B202 detection for tarfile.extract() calls (unless filter="data" is present).
  • Update the examples/tarfile_extractall.py example file to include extract() scenarios.
  • Update functional test expected severity/confidence counts for the example scan.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
bandit/plugins/tarfile_unsafe_members.py Adds extract() detection and refactors issue construction helpers/messages.
examples/tarfile_extractall.py Adds new example functions covering tar.extract() with/without filter="data".
tests/functional/test_functional.py Updates expected finding counts for the tarfile example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +124 to +138
if "extractall" in context.call_function_name:
if "filter" in context.call_keywords and is_filter_data(context):
return None
if "members" in context.call_keywords:
members = get_members_value(context)
if "Function" in members:
return exec_issue(bandit.LOW, members)
return exec_issue_extractall(bandit.LOW, members)
else:
return exec_issue(bandit.MEDIUM, members)
return exec_issue(bandit.HIGH)
return exec_issue_extractall(bandit.MEDIUM, members)
return exec_issue_extractall(bandit.HIGH)

if "extract" in context.call_function_name:
if "filter" in context.call_keywords and is_filter_data(context):
return None
return exec_issue_extract()
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment on lines 5 to 13
=================================
B202: Test for tarfile.extractall
=================================

This plugin will look for usage of ``tarfile.extractall()``
This plugin will look for usage of ``tarfile.extractall()`` and
``tarfile.extract()``

Severity are set as follows:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment on lines +89 to +97
def exec_issue_extract():
return bandit.Issue(
severity=bandit.HIGH,
confidence=bandit.HIGH,
cwe=issue.Cwe.PATH_TRAVERSAL,
text="tarfile.extract used without member validation. "
"Untarred members should be inspected for directory traversal "
"sequences such as '../' and dangerous file types.",
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

@jonasboos
Copy link
Copy Markdown
Author

@copilot apply changes based on the comments in this thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants