Django DEBUG = True Hardcoded in settings.py
Flags hardcoded DEBUG = True assignments in Django settings.py files that expose detailed technical 404/500 tracebacks, SQL logs, and settings values to external visitors.
- Rule Slug
- django-debug-true-hardcoded
- Affected Files
- *settings*.py, config.py, *.py
- Detection Method
- Deterministic Regex
Vulnerable Pattern vs. Hardened Fix
# project/settings.py
# HIGH RISK: Hardcoded DEBUG = True leaks tracebacks & settings in production
DEBUG = True
ALLOWED_HOSTS = ['*']# project/settings.py
import os
DEBUG = os.environ.get('DJANGO_DEBUG', 'False').lower() in ('true', '1', 'yes')
ALLOWED_HOSTS = [
h.strip() for h in os.environ.get('DJANGO_ALLOWED_HOSTS', '').split(',') if h.strip()
]Why This Fails in Production
Django’s built-in yellow technical error page is helpful during local debugging, and equally dangerous if left active in production. Hardcoding DEBUG = True in settings.py causes Django to serve full Python stack traces, local variable values at every stack frame, installed middleware lists, full URL routing tables (on any 404), and a complete dump of your settings module to any unauthenticated browser request that triggers an exception.
Although Django attempts to redact settings whose names contain KEY, SECRET, TOKEN, or PASSWORD via SafeExceptionReporterFilter, custom setting names (such as STRIPE_LIVE_CREDENTIAL, REDIS_CONN, SMTP_AUTH, or internal webhook URLs) as well as local variables captured inside views or third-party SDK frames regularly leak verbatim inside the rendered HTML traceback.
Beyond direct credential disclosure, running Django with DEBUG = True in production causes progressive memory exhaustion because Django records every single executed SQL query in memory inside django.db.connection.queries for debugging, steadily inflating worker RAM until Gunicorn or uWSGI workers crash under OOM kills. It also relaxes ALLOWED_HOSTS validation (allowing localhost fallbacks) and disables certain security middleware defaults.
Always derive DEBUG explicitly from an environment variable that defaults safely to False (for example, DEBUG = os.environ.get("DJANGO_DEBUG", "False").lower() == "true"), split base/production settings cleanly, and run python manage.py check --deploy in CI.
Remediation Steps
- 1Replace hardcoded DEBUG = True with an environment-driven boolean that defaults to False: DEBUG = os.environ.get("DJANGO_DEBUG", "False").lower() == "true".
- 2Configure an explicit ALLOWED_HOSTS list for your production domain names before disabling DEBUG.
- 3Run python manage.py check --deploy to verify SECURE_SSL_REDIRECT, SESSION_COOKIE_SECURE, CSRF_COOKIE_SECURE, and HSTS settings.
- 4Configure Django LOGGING or an APM tool (like Sentry) so exceptions are captured privately without rendering tracebacks to users.
Read the Django production configuration walkthrough on BackendRecipes.
Frequently Asked Questions
- Why does my Django site stop serving CSS/JS static files when I set DEBUG = False?
- Django’s runserver only serves static files automatically when DEBUG = True. In production, run python manage.py collectstatic and serve STATIC_ROOT via WhiteNoise, Nginx, or a CDN.
- Does Django automatically hide all secrets on the DEBUG error page?
- No. Django only masks settings matching a narrow list of substrings (like SECRET or PASSWORD) and frequently exposes non-standard credential names, URLs with embedded passwords, and local function variables.
- Will PushCheck flag DEBUG = os.getenv("DEBUG") == "True"?
- No. This rule specifically matches hardcoded DEBUG = True assignments so safe environment-driven expressions pass cleanly.