request_without_timeout.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # SPDX-License-Identifier: Apache-2.0
  2. r"""
  3. =======================================
  4. B113: Test for missing requests timeout
  5. =======================================
  6. This plugin test checks for ``requests`` calls without a timeout specified.
  7. Nearly all production code should use this parameter in nearly all requests,
  8. Failure to do so can cause your program to hang indefinitely.
  9. When request methods are used without the timeout parameter set,
  10. Bandit will return a MEDIUM severity error.
  11. :Example:
  12. .. code-block:: none
  13. >> Issue: [B113:request_without_timeout] Requests call without timeout
  14. Severity: Medium Confidence: Low
  15. CWE: CWE-400 (https://cwe.mitre.org/data/definitions/400.html)
  16. More Info: https://bandit.readthedocs.io/en/latest/plugins/b113_request_without_timeout.html
  17. Location: examples/requests-missing-timeout.py:3:0
  18. 2
  19. 3 requests.get('https://gmail.com')
  20. 4 requests.get('https://gmail.com', timeout=None)
  21. --------------------------------------------------
  22. >> Issue: [B113:request_without_timeout] Requests call with timeout set to None
  23. Severity: Medium Confidence: Low
  24. CWE: CWE-400 (https://cwe.mitre.org/data/definitions/400.html)
  25. More Info: https://bandit.readthedocs.io/en/latest/plugins/b113_request_without_timeout.html
  26. Location: examples/requests-missing-timeout.py:4:0
  27. 3 requests.get('https://gmail.com')
  28. 4 requests.get('https://gmail.com', timeout=None)
  29. 5 requests.get('https://gmail.com', timeout=5)
  30. .. seealso::
  31. - https://requests.readthedocs.io/en/latest/user/advanced/#timeouts
  32. .. versionadded:: 1.7.5
  33. """ # noqa: E501
  34. import bandit
  35. from bandit.core import issue
  36. from bandit.core import test_properties as test
  37. @test.checks("Call")
  38. @test.test_id("B113")
  39. def request_without_timeout(context):
  40. http_verbs = ("get", "options", "head", "post", "put", "patch", "delete")
  41. if (
  42. "requests" in context.call_function_name_qual
  43. and context.call_function_name in http_verbs
  44. ):
  45. # check for missing timeout
  46. if context.check_call_arg_value("timeout") is None:
  47. return bandit.Issue(
  48. severity=bandit.MEDIUM,
  49. confidence=bandit.LOW,
  50. cwe=issue.Cwe.UNCONTROLLED_RESOURCE_CONSUMPTION,
  51. text="Requests call without timeout",
  52. )
  53. # check for timeout=None
  54. if context.check_call_arg_value("timeout", "None"):
  55. return bandit.Issue(
  56. severity=bandit.MEDIUM,
  57. confidence=bandit.LOW,
  58. cwe=issue.Cwe.UNCONTROLLED_RESOURCE_CONSUMPTION,
  59. text="Requests call with timeout set to None",
  60. )