Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

UN-4530 [FIX] Turn Django DEBUG off by default (#2410)

Athulce62a9249273
diff --git a/backend/backend/settings/base.py b/backend/backend/settings/base.py
index 687b5182f..0a0727545 100644
--- a/backend/backend/settings/base.py
+++ b/backend/backend/settings/base.py
@@ -479,7 +479,8 @@ SECRET_KEY = get_required_setting("DJANGO_SECRET_KEY")
ENCRYPTION_KEY = get_required_setting("ENCRYPTION_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
-DEBUG = True
+# Off unless DJANGO_DEBUG asks for it: the published image runs settings.dev.
+DEBUG = CommonUtils.str_to_bool(os.environ.get("DJANGO_DEBUG", "False"))
ALLOWED_HOSTS = ["*"]
CSRF_TRUSTED_ORIGINS = [WEB_APP_ORIGIN_URL, WEB_APP_ORIGIN_URL_WITH_WILD_CARD]
diff --git a/backend/backend/settings/dev.py b/backend/backend/settings/dev.py
index 5a0b81650..c0457d194 100644
--- a/backend/backend/settings/dev.py
+++ b/backend/backend/settings/dev.py
@@ -1,7 +1,5 @@
from backend.settings.base import * # noqa: F403
-DEBUG = True
-
X_FRAME_OPTIONS = "http://localhost:3000"
X_FRAME_OPTIONS = "ALLOW-FROM http://localhost:3000"
diff --git a/backend/backend/tests/test_debug_setting.py b/backend/backend/tests/test_debug_setting.py
new file mode 100644
index 000000000..ecca0d01f
--- /dev/null
+++ b/backend/backend/tests/test_debug_setting.py
@@ -0,0 +1,52 @@
+"""DEBUG is off unless DJANGO_DEBUG turns it on.
+
+The published backend image runs ``backend.settings.dev`` by default, so a
+hardcoded ``DEBUG = True`` there or in ``base.py`` reaches every self-hosted
+deployment: unhandled exceptions return Django's technical 500 page (stack
+trace, local variables, settings) to the client. Settings are read once at
+import, so each case loads them in a fresh interpreter.
+"""
+
+from __future__ import annotations
+
+import os
+import pathlib
+import subprocess
+import sys
+
+import pytest
+
+_BACKEND_DIR = pathlib.Path(__file__).resolve().parents[2]
+# base.py loads backend/.env, which may set DJANGO_DEBUG for local work, so the
+# probe disables dotenv and takes every setting from the environment it is given.
+_PROBE = (
+ "import dotenv, importlib, sys; "
+ "dotenv.load_dotenv = lambda *a, **k: False; "
+ "print(importlib.import_module(sys.argv[1]).DEBUG)"
+)
+
+
+def _debug_for(module: str, **env: str) -> bool:
+ base = {k: v for k, v in os.environ.items() if k != "DJANGO_DEBUG"}
+ result = subprocess.run(
+ [sys.executable, "-c", _PROBE, module],
+ cwd=_BACKEND_DIR,
+ env={**base, **env},
+ capture_output=True,
+ text=True,
+ timeout=120,
+ check=False,
+ )
+ assert result.returncode == 0, result.stderr[-3000:]
+ return result.stdout.strip().splitlines()[-1] == "True"
+
+
[email protected]("module", ["backend.settings.base", "backend.settings.dev"])
+def test_debug_is_off_when_unset(module: str) -> None:
+ assert _debug_for(module) is False
+
+
[email protected]("module", ["backend.settings.base", "backend.settings.dev"])
+def test_debug_turns_on_only_when_asked(module: str) -> None:
+ assert _debug_for(module, DJANGO_DEBUG="True") is True
+ assert _debug_for(module, DJANGO_DEBUG="False") is False
diff --git a/backend/sample.env b/backend/sample.env
index 33d4f535b..f8d194139 100644
--- a/backend/sample.env
+++ b/backend/sample.env
@@ -1,4 +1,7 @@
DJANGO_SETTINGS_MODULE='backend.settings.dev'
+# NOTE: Debug is off by default. Set to True only for local development; it
+# returns stack traces and internal state to any client that triggers an error.
+# DJANGO_DEBUG=True
# NOTE: Change below to True if you are running in HTTPS mode.
SESSION_COOKIE_SECURE=False