-
-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add check for "requests" calls without timeout (#743)
* add check for "requests" calls without timeout * change request_without_timeout confidence to low * Update bandit/plugins/request_without_timeout.py Co-authored-by: Eric Brown <[email protected]> * Update bandit/plugins/request_without_timeout.py Co-authored-by: Eric Brown <[email protected]> * Update doc/source/plugins/b113_request_without_timeout.rst Co-authored-by: Eric Brown <[email protected]> * Update doc/source/plugins/b113_request_without_timeout.rst Co-authored-by: Eric Brown <[email protected]> * Update bandit/plugins/request_without_timeout.py Co-authored-by: Eric Brown <[email protected]> * remove utf-8 * fix confidence in comment * Apply suggestions from code review * Update issue.py * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Eric Brown <[email protected]>
- Loading branch information
Showing
8 changed files
with
129 additions
and
16 deletions.
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
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,74 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
r""" | ||
======================================= | ||
B113: Test for missing requests timeout | ||
======================================= | ||
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: Low | ||
CWE: CWE-400 (https://cwe.mitre.org/data/definitions/400.html) | ||
More Info: https://bandit.readthedocs.io/en/latest/plugins/b113_request_without_timeout.html | ||
Location: examples/requests-missing-timeout.py:3:0 | ||
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: Low | ||
CWE: CWE-400 (https://cwe.mitre.org/data/definitions/400.html) | ||
More Info: https://bandit.readthedocs.io/en/latest/plugins/b113_request_without_timeout.html | ||
Location: examples/requests-missing-timeout.py:4:0 | ||
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 | ||
.. versionadded:: 1.7.5 | ||
""" # noqa: E501 | ||
import bandit | ||
from bandit.core import issue | ||
from bandit.core import test_properties as test | ||
|
||
|
||
@test.checks("Call") | ||
@test.test_id("B113") | ||
def request_without_timeout(context): | ||
http_verbs = ("get", "options", "head", "post", "put", "patch", "delete") | ||
if ( | ||
"requests" in context.call_function_name_qual | ||
and context.call_function_name in http_verbs | ||
): | ||
# check for missing timeout | ||
if context.check_call_arg_value("timeout") is None: | ||
return bandit.Issue( | ||
severity=bandit.MEDIUM, | ||
confidence=bandit.LOW, | ||
cwe=issue.Cwe.UNCONTROLLED_RESOURCE_CONSUMPTION, | ||
text="Requests call without timeout", | ||
) | ||
# check for timeout=None | ||
if context.check_call_arg_value("timeout", "None"): | ||
return bandit.Issue( | ||
severity=bandit.MEDIUM, | ||
confidence=bandit.LOW, | ||
cwe=issue.Cwe.UNCONTROLLED_RESOURCE_CONSUMPTION, | ||
text="Requests call with timeout set to None", | ||
) |
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 @@ | ||
----------------------------- | ||
B113: request_without_timeout | ||
----------------------------- | ||
|
||
.. 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
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