try_except_continue.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright 2016 IBM Corp.
  2. # Copyright 2014 Hewlett-Packard Development Company, L.P.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. r"""
  6. =============================================
  7. B112: Test for a continue in the except block
  8. =============================================
  9. Errors in Python code bases are typically communicated using ``Exceptions``.
  10. An exception object is 'raised' in the event of an error and can be 'caught' at
  11. a later point in the program, typically some error handling or logging action
  12. will then be performed.
  13. However, it is possible to catch an exception and silently ignore it while in
  14. a loop. This is illustrated with the following example
  15. .. code-block:: python
  16. while keep_going:
  17. try:
  18. do_some_stuff()
  19. except Exception:
  20. continue
  21. This pattern is considered bad practice in general, but also represents a
  22. potential security issue. A larger than normal volume of errors from a service
  23. can indicate an attempt is being made to disrupt or interfere with it. Thus
  24. errors should, at the very least, be logged.
  25. There are rare situations where it is desirable to suppress errors, but this is
  26. typically done with specific exception types, rather than the base Exception
  27. class (or no type). To accommodate this, the test may be configured to ignore
  28. 'try, except, continue' where the exception is typed. For example, the
  29. following would not generate a warning if the configuration option
  30. ``checked_typed_exception`` is set to False:
  31. .. code-block:: python
  32. while keep_going:
  33. try:
  34. do_some_stuff()
  35. except ZeroDivisionError:
  36. continue
  37. **Config Options:**
  38. .. code-block:: yaml
  39. try_except_continue:
  40. check_typed_exception: True
  41. :Example:
  42. .. code-block:: none
  43. >> Issue: Try, Except, Continue detected.
  44. Severity: Low Confidence: High
  45. CWE: CWE-703 (https://cwe.mitre.org/data/definitions/703.html)
  46. Location: ./examples/try_except_continue.py:5
  47. 4 a = i
  48. 5 except:
  49. 6 continue
  50. .. seealso::
  51. - https://security.openstack.org
  52. - https://cwe.mitre.org/data/definitions/703.html
  53. .. versionadded:: 1.0.0
  54. .. versionchanged:: 1.7.3
  55. CWE information added
  56. """
  57. import ast
  58. import bandit
  59. from bandit.core import issue
  60. from bandit.core import test_properties as test
  61. def gen_config(name):
  62. if name == "try_except_continue":
  63. return {"check_typed_exception": False}
  64. @test.takes_config
  65. @test.checks("ExceptHandler")
  66. @test.test_id("B112")
  67. def try_except_continue(context, config):
  68. node = context.node
  69. if len(node.body) == 1:
  70. if (
  71. not config["check_typed_exception"]
  72. and node.type is not None
  73. and getattr(node.type, "id", None) != "Exception"
  74. ):
  75. return
  76. if isinstance(node.body[0], ast.Continue):
  77. return bandit.Issue(
  78. severity=bandit.LOW,
  79. confidence=bandit.HIGH,
  80. cwe=issue.Cwe.IMPROPER_CHECK_OF_EXCEPT_COND,
  81. text=("Try, Except, Continue detected."),
  82. )