-
-
Notifications
You must be signed in to change notification settings - Fork 626
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
add check for "requests" calls without timeout #743
Merged
Merged
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ad9cae2
add check for "requests" calls without timeout
mschfh 04fe7af
change request_without_timeout confidence to low
mschfh cc9bcb3
Merge branch 'master' into requests-timeout
ericwb 4887043
Update bandit/plugins/request_without_timeout.py
mschfh 2438ac0
Update bandit/plugins/request_without_timeout.py
mschfh 6697e8c
Update doc/source/plugins/b113_request_without_timeout.rst
mschfh 194b9a3
Update doc/source/plugins/b113_request_without_timeout.rst
mschfh 51e28b2
Update bandit/plugins/request_without_timeout.py
mschfh 7ed8a95
Merge branch 'master' into requests-timeout
ericwb 9eb94bf
remove utf-8
mschfh a0026ab
fix confidence in comment
mschfh 178b965
Merge branch 'master' into requests-timeout
mschfh 1bc84f7
Merge branch 'master' into requests-timeout
ericwb 029b3b1
Merge branch 'main' into requests-timeout
ericwb 79fea19
Apply suggestions from code review
ericwb 0e5004b
Update issue.py
ericwb c317850
Apply suggestions from code review
ericwb 1efecfe
Apply suggestions from code review
ericwb 72de00a
Apply suggestions from code review
ericwb dff1dda
Apply suggestions from code review
ericwb 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
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# -*- coding:utf-8 -*- | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r""" | ||
============================================= | ||
mschfh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
B113: Test for missing requests timeout | ||
============================================= | ||
mschfh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This plugin test checks for ``requests`` calls without a timeout specified. | ||
|
||
Nearly all production code should use this parameter in nearly all requests, | ||
Failure to do so can cause your program to hang indefinitely. | ||
|
||
When request methods are used without the timeout parameter set, | ||
Bandit will return a MEDIUM severity error. | ||
|
||
|
||
:Example: | ||
|
||
.. code-block:: none | ||
|
||
>> Issue: [B113:request_without_timeout] Requests call without timeout | ||
Severity: Medium Confidence: High | ||
Location: examples/requests-missing-timeout.py:3:0 | ||
More Info: https://bandit.readthedocs.io/en/latest/plugins/b113_request_without_timeout.html | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
2 | ||
3 requests.get('https://gmail.com') | ||
4 requests.get('https://gmail.com', timeout=None) | ||
|
||
-------------------------------------------------- | ||
>> Issue: [B113:request_without_timeout] Requests call with timeout set to None | ||
Severity: Medium Confidence: High | ||
Location: examples/requests-missing-timeout.py:4:0 | ||
More Info: https://bandit.readthedocs.io/en/latest/plugins/b113_request_without_timeout.html | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
3 requests.get('https://gmail.com') | ||
4 requests.get('https://gmail.com', timeout=None) | ||
5 requests.get('https://gmail.com', timeout=5) | ||
|
||
.. seealso:: | ||
|
||
- https://2.python-requests.org/en/master/user/quickstart/#timeouts | ||
|
||
mschfh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" # noqa: E501 | ||
|
||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import bandit | ||
from bandit.core import test_properties as test | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@test.checks('Call') | ||
@test.test_id('B113') | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def request_without_timeout(context): | ||
http_verbs = ('get', 'options', 'head', 'post', 'put', 'patch', 'delete') | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ('requests' in context.call_function_name_qual and | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
context.call_function_name in http_verbs): | ||
# check for missing timeout | ||
if context.check_call_arg_value('timeout') is None: | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
issue = bandit.Issue( | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
severity=bandit.MEDIUM, | ||
confidence=bandit.LOW, | ||
text="Requests call without timeout" | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
return issue | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# check for timeout=None | ||
if context.check_call_arg_value('timeout', 'None'): | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
issue = bandit.Issue( | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
severity=bandit.MEDIUM, | ||
confidence=bandit.LOW, | ||
text="Requests call with timeout set to None" | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
return issue | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
------------------------------------- | ||
mschfh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
B113: request_without_timeout | ||
------------------------------------- | ||
mschfh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. automodule:: bandit.plugins.request_without_timeout |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import requests | ||
|
||
requests.get('https://gmail.com') | ||
requests.get('https://gmail.com', timeout=None) | ||
requests.get('https://gmail.com', timeout=5) | ||
requests.post('https://gmail.com') | ||
requests.post('https://gmail.com', timeout=None) | ||
requests.post('https://gmail.com', timeout=5) | ||
requests.put('https://gmail.com') | ||
requests.put('https://gmail.com', timeout=None) | ||
requests.put('https://gmail.com', timeout=5) | ||
requests.delete('https://gmail.com') | ||
requests.delete('https://gmail.com', timeout=None) | ||
requests.delete('https://gmail.com', timeout=5) | ||
requests.patch('https://gmail.com') | ||
requests.patch('https://gmail.com', timeout=None) | ||
requests.patch('https://gmail.com', timeout=5) | ||
requests.options('https://gmail.com') | ||
requests.options('https://gmail.com', timeout=None) | ||
requests.options('https://gmail.com', timeout=5) | ||
requests.head('https://gmail.com') | ||
requests.head('https://gmail.com', timeout=None) | ||
requests.head('https://gmail.com', timeout=5) |
This file contains 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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import requests | ||
|
||
requests.get('https://gmail.com', verify=True) | ||
requests.get('https://gmail.com', verify=False) | ||
requests.post('https://gmail.com', verify=True) | ||
requests.post('https://gmail.com', verify=False) | ||
requests.put('https://gmail.com', verify=True) | ||
requests.put('https://gmail.com', verify=False) | ||
requests.delete('https://gmail.com', verify=True) | ||
requests.delete('https://gmail.com', verify=False) | ||
requests.patch('https://gmail.com', verify=True) | ||
requests.patch('https://gmail.com', verify=False) | ||
requests.options('https://gmail.com', verify=True) | ||
requests.options('https://gmail.com', verify=False) | ||
requests.head('https://gmail.com', verify=True) | ||
requests.head('https://gmail.com', verify=False) | ||
requests.get('https://gmail.com', timeout=30, verify=True) | ||
requests.get('https://gmail.com', timeout=30, verify=False) | ||
requests.post('https://gmail.com', timeout=30, verify=True) | ||
requests.post('https://gmail.com', timeout=30, verify=False) | ||
requests.put('https://gmail.com', timeout=30, verify=True) | ||
requests.put('https://gmail.com', timeout=30, verify=False) | ||
requests.delete('https://gmail.com', timeout=30, verify=True) | ||
requests.delete('https://gmail.com', timeout=30, verify=False) | ||
requests.patch('https://gmail.com', timeout=30, verify=True) | ||
requests.patch('https://gmail.com', timeout=30, verify=False) | ||
requests.options('https://gmail.com', timeout=30, verify=True) | ||
requests.options('https://gmail.com', timeout=30, verify=False) | ||
requests.head('https://gmail.com', timeout=30, verify=True) | ||
requests.head('https://gmail.com', timeout=30, verify=False) |
This file contains 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 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.
Not necessary unless there are unicode characters in this file.
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.
should I remove it?
most plugins seem to include the line, even if there are no unicode characters in the file
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.
Yes please. Recently Black was integrated as a format check. It'll flag this.