app_debug.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #
  2. # Copyright 2015 Hewlett-Packard Development Company, L.P.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. r"""
  6. ======================================================
  7. B201: Test for use of flask app with debug set to true
  8. ======================================================
  9. Running Flask applications in debug mode results in the Werkzeug debugger
  10. being enabled. This includes a feature that allows arbitrary code execution.
  11. Documentation for both Flask [1]_ and Werkzeug [2]_ strongly suggests that
  12. debug mode should never be enabled on production systems.
  13. Operating a production server with debug mode enabled was the probable cause
  14. of the Patreon breach in 2015 [3]_.
  15. :Example:
  16. .. code-block:: none
  17. >> Issue: A Flask app appears to be run with debug=True, which exposes
  18. the Werkzeug debugger and allows the execution of arbitrary code.
  19. Severity: High Confidence: High
  20. CWE: CWE-94 (https://cwe.mitre.org/data/definitions/94.html)
  21. Location: examples/flask_debug.py:10
  22. 9 #bad
  23. 10 app.run(debug=True)
  24. 11
  25. .. seealso::
  26. .. [1] https://flask.palletsprojects.com/en/1.1.x/quickstart/#debug-mode
  27. .. [2] https://werkzeug.palletsprojects.com/en/1.0.x/debug/
  28. .. [3] https://labs.detectify.com/2015/10/02/how-patreon-got-hacked-publicly-exposed-werkzeug-debugger/
  29. .. https://cwe.mitre.org/data/definitions/94.html
  30. .. versionadded:: 0.15.0
  31. .. versionchanged:: 1.7.3
  32. CWE information added
  33. """ # noqa: E501
  34. import bandit
  35. from bandit.core import issue
  36. from bandit.core import test_properties as test
  37. @test.test_id("B201")
  38. @test.checks("Call")
  39. def flask_debug_true(context):
  40. if context.is_module_imported_like("flask"):
  41. if context.call_function_name_qual.endswith(".run"):
  42. if context.check_call_arg_value("debug", "True"):
  43. return bandit.Issue(
  44. severity=bandit.HIGH,
  45. confidence=bandit.MEDIUM,
  46. cwe=issue.Cwe.CODE_INJECTION,
  47. text="A Flask app appears to be run with debug=True, "
  48. "which exposes the Werkzeug debugger and allows "
  49. "the execution of arbitrary code.",
  50. lineno=context.get_lineno_for_call_arg("debug"),
  51. )