Debug mode in production
| Class | Security misconfiguration |
|---|---|
| Also known as | Leftover debug code, active debug code |
| CWE | CWE-489: Active Debug Code |
| OWASP Top 10 | A02:2025 Security Misconfiguration |
| Typical severity | Medium for detailed error pages; Critical for an interactive debugger |
| Affects | Django, Flask and Express servers, and Android and iOS builds, deployed with their development setting |
Django, Flask, Express and Android each read one setting that decides whether the program runs for its developer or for the public: DEBUG in Django, the --debug flag in Flask, the environment variable NODE_ENV in Express, the android:debuggable attribute in an Android app. In the development state the three server frameworks show every error in full detail, and Flask and Android let a debugger attach to the running program. Debug mode in production is that setting left in its development state on a live server or in a published build, so the detail and the tools reach anyone who sends a request.1
What the setting changes
| Stack | Setting | In development state | Production value |
|---|---|---|---|
| Django | DEBUG in settings.py | Error page with a traceback and the current settings; every SQL query kept in memory | DEBUG = False |
| Flask | --debug or app.run(debug=True) | Werkzeug debugger: an interactive traceback in the browser | No debug flag |
| Express | NODE_ENV | Error handler sends err.stack; view templates not cached | production |
| Android | android:debuggable in the manifest | A debugger can attach on an ordinary phone | "false", the default |
Django's debug page leaves out settings whose names look sensitive, such as SECRET_KEY, yet the file paths and configuration values it does print still describe the server to an attacker.2 In Express, an unhandled error returns the stack trace as the response body unless the environment is production; in production the body is the plain status message.3 Error handling shows how to replace that output with a generic page and a server-side log.
A debugger hands over control as well as information. Werkzeug, the development server built into Flask, opens an interactive console in the browser when a request fails, and that console runs any Python code typed into it. A PIN guards the console, and Flask's own documentation says the PIN should not be relied on for security.4 An Android build marked debuggable lets a debugger attach even on a phone in normal user mode.5 In many cases leftover debug code gives an attacker full control of the application and the server it runs on.1 Debug consoles and status pages that answer on their own paths, apart from any error, are described in Exposed API documentation and debug endpoints.
Debug mode also costs resources. Django in debug mode remembers every SQL query it executes, which fills the memory of a busy server.2 Express caches view templates and writes shorter error messages only when NODE_ENV is production, and that one variable can make an app up to three times faster.6
How it reaches a live server
The defaults point in different directions. Django's DEBUG is False when the line is absent, but the settings.py that django-admin startproject generates sets it to True; a project deployed with the generated file runs in debug mode until that line changes.2 Express works the other way round: production behaviour has to be asked for, because the stack trace is dropped only when NODE_ENV equals production.3 A Node.js server started from a shell, a container or a process manager without that variable sends full stacks to visitors whenever an error reaches the built-in handler.
Flask enables debug mode from the command, flask --app hello run --debug, or from debug=True in code, and the debugger comes on with it.4 In Android, the attribute defaults to "false" and has to be set to make a build debuggable.5
Patreon, 2015
On 28 September 2015 an attacker reached a debug version of Patreon, the creator funding site, that was publicly available.7 That copy held a snapshot of the production database. In his notice, founder Jack Conte listed registered names, email addresses, posts and some shipping addresses as accessed; passwords were stored as salted bcrypt hashes.8 In October more than 16 GB of data was published, with database records holding over 2.3 million unique email addresses and millions of personal messages.9
The security firm Detectify had seen the Werkzeug debugger answering on that host in Shodan, an index of internet-facing services, in a capture dated 11 September. It reported the host to Patreon on 23 September and was told the company knew and was working on it. Detectify names the debugger as the probable way in, not a confirmed one. Its write-up shows the mechanics: the debugger's error page carries a secret generated when the application starts, and with that secret a GET request of the form /?__debugger__=yes&cmd=...&frm=...&s=... runs a Python command and returns the result. Because the command travels in the URL, each one is written to the server's access log.7
Checking and turning it off
A request that makes the live application fail shows which state it is in. A framework traceback, an interactive console or a stack in the response body means the development setting is on; a short generic error page means it is off. Flask, for example, turns unhandled errors into a plain 500 page when debug mode is off.4
In Django, the production settings hold DEBUG = False.2 Flask's development server and its debugger are not run in production at all.4 For Express, the variable is set in the service definition of the server rather than in an interactive shell; with systemd, the Linux init system that starts services, that is one line in the unit file:6
[Service]
Environment=NODE_ENV=productionIn an Android manifest, android:debuggable is left out of the release build so the default of "false" applies.5 Where a live server does need inspecting, an external debugger can be started only for requests from the developer's own IP address, and only for a short time.4 The settings for Rails and Laravel are listed step by step in Securing Rails, Django and Laravel applications.
In SecFlash
On the Exposure page, SecFlash reports a development build served in production as SF-EXP-103, reachable debug and status pages as SF-EXP-205, and page data that says debug mode is on as SF-EXP-405. On the Security page, a framework debug or PHP error message in a response appears as SF-SEC-637, and debug mode left on in the code as SF-SEC-711. For mobile apps, a debug or development setting in the release build shows as SF-SEC-732 on the Security page, a debuggable Android build as SF-EXP-601 and an iOS build made with a development profile as SF-EXP-621 on the Exposure page.
See also
- Error handling and stack traces
- Exposed API documentation and debug endpoints
- Securing Rails, Django and Laravel applications
- Source maps in production
- Public environment variables
- Category: Security misconfiguration
References
- a b MITRE. "CWE-489: Active Debug Code". Common Weakness Enumeration, version 5.0.
- a b c d Django Software Foundation. "Settings: DEBUG". Django 5.2 documentation.
- a b Express. "Error handling". Express documentation.
- a b c d e Pallets. "Debugging Application Errors". Flask 3.1 documentation.
- a b c Google. "<application>". Android Developers.
- a b Express. "Production best practices: performance and reliability". Express documentation.
- a b Rosén, Frans (2 October 2015). "How Patreon got hacked: publicly exposed Werkzeug Debugger". Detectify Labs.
- back Biggs, John (5 October 2015). "Patreon Hacked, Gigabytes Of Data And Code Leaked". TechCrunch.
- back Have I Been Pwned. "Patreon Data Breach". haveibeenpwned.com.
External links
- Debugging Application Errors, Flask documentation
- CWE-489: Active Debug Code, Common Weakness Enumeration