try_except_pass.py 2.8 KB

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