Debug and testing modes enabled

Debug or testing mode enabled in a production Python web application

Description

Django and FastAPI debug responses can expose tracebacks, source, or configuration. Flask debug mode can expose the Werkzeug interactive debugger. Flask’s TESTING setting separately affects exception handling and extensions.

Potential impact

Detailed errors can disclose internal data to clients. An exposed interactive debugger poses a code-execution risk even when its PIN is enabled. With Flask testing enabled, exceptions propagate by default unless PROPAGATE_EXCEPTIONS is set explicitly.

Remediation

  • In production, set Django DEBUG, Flask DEBUG and TESTING, and FastAPI debug to the boolean False. Parse environment values before passing them to a boolean setting. Python treats the nonempty string "False" as true.
  • Keep development and test settings separate. Check effective values automatically before deploying. Run a production server with generic error responses, server-side logs, and alerts.

Examples

Django

Before

python
# settings/production.py
DEBUG = True

After

python
# settings/production.py
DEBUG = False

Flask debug mode

Before

python
from flask import Flask

app = Flask(__name__)
app.run(debug=True)

After

python
from flask import Flask

app = Flask(__name__)
app.config["DEBUG"] = False

# Run with a production WSGI server, not the Flask development server.

Flask testing mode

Before

python
from flask import Flask

app = Flask(__name__)
app.config.update(TESTING=True)
# app.testing = True enables the same setting.

After

python
from flask import Flask

app = Flask(__name__)
app.config.update(TESTING=False)

FastAPI

Before

python
from fastapi import FastAPI

app = FastAPI(debug=True)

After

python
from fastapi import FastAPI

app = FastAPI(debug=False)

References