-
-
Notifications
You must be signed in to change notification settings - Fork 759
Detect unsafe tarfile.extract() in B202 plugin #1409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonasboos
wants to merge
2
commits into
PyCQA:main
Choose a base branch
from
jonasboos:fix-1392-tarfile-extract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−14
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,15 @@ | |
| 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: | ||
|
|
||
| * ``tarfile.extractall(members=function(tarfile))`` - LOW | ||
| * ``tarfile.extractall(members=?)`` - member is not a function - MEDIUM | ||
| * ``tarfile.extractall()`` - members from the archive is trusted - HIGH | ||
| * ``tarfile.extract()`` - no member validation - HIGH | ||
|
|
||
| Use ``tarfile.extractall(members=function_name)`` and define a function | ||
| that will inspect each member. Discard files that contain a directory | ||
|
|
@@ -38,6 +40,7 @@ | |
| .. seealso:: | ||
|
|
||
| - https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extractall | ||
| - https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extract | ||
| - https://docs.python.org/3/library/tarfile.html#tarfile.TarInfo | ||
|
|
||
| .. versionadded:: 1.7.5 | ||
|
|
@@ -53,7 +56,7 @@ | |
| from bandit.core import test_properties as test | ||
|
|
||
|
|
||
| def exec_issue(level, members=""): | ||
| def exec_issue_extractall(level, members=""): | ||
| if level == bandit.LOW: | ||
| return bandit.Issue( | ||
| severity=bandit.LOW, | ||
|
|
@@ -83,6 +86,17 @@ def exec_issue(level, members=""): | |
| ) | ||
|
|
||
|
|
||
| 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.", | ||
| ) | ||
|
|
||
|
|
||
| def get_members_value(context): | ||
| for keyword in context.node.keywords: | ||
| if keyword.arg == "members": | ||
|
|
@@ -104,18 +118,23 @@ def is_filter_data(context): | |
| @test.test_id("B202") | ||
| @test.checks("Call") | ||
| def tarfile_unsafe_members(context): | ||
| if all( | ||
| [ | ||
| context.is_module_imported_exact("tarfile"), | ||
| "extractall" in context.call_function_name, | ||
| ] | ||
| ): | ||
| if not context.is_module_imported_exact("tarfile"): | ||
| return None | ||
|
|
||
| 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() | ||
|
Comment on lines
+124
to
+138
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot apply changes based on this feedback |
||
|
|
||
| return None | ||
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
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
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.
There was a problem hiding this comment.
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