exec.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #
  2. # Copyright 2014 Hewlett-Packard Development Company, L.P.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. r"""
  6. ==============================
  7. B102: Test for the use of exec
  8. ==============================
  9. This plugin test checks for the use of Python's `exec` method or keyword. The
  10. Python docs succinctly describe why the use of `exec` is risky.
  11. :Example:
  12. .. code-block:: none
  13. >> Issue: Use of exec detected.
  14. Severity: Medium Confidence: High
  15. CWE: CWE-78 (https://cwe.mitre.org/data/definitions/78.html)
  16. Location: ./examples/exec.py:2
  17. 1 exec("do evil")
  18. .. seealso::
  19. - https://docs.python.org/3/library/functions.html#exec
  20. - https://www.python.org/dev/peps/pep-0551/#background
  21. - https://www.python.org/dev/peps/pep-0578/#suggested-audit-hook-locations
  22. - https://cwe.mitre.org/data/definitions/78.html
  23. .. versionadded:: 0.9.0
  24. .. versionchanged:: 1.7.3
  25. CWE information added
  26. """
  27. import bandit
  28. from bandit.core import issue
  29. from bandit.core import test_properties as test
  30. def exec_issue():
  31. return bandit.Issue(
  32. severity=bandit.MEDIUM,
  33. confidence=bandit.HIGH,
  34. cwe=issue.Cwe.OS_COMMAND_INJECTION,
  35. text="Use of exec detected.",
  36. )
  37. @test.checks("Call")
  38. @test.test_id("B102")
  39. def exec_used(context):
  40. if context.call_function_name_qual == "exec":
  41. return exec_issue()