CCyberwiki

Error handling and stack traces

Error handling and stack traces
ClassSecurity misconfiguration
Also known asVerbose error messages, information exposure through an error message
CWECWE-209: Generation of Error Message Containing Sensitive Information
OWASP Top 10A10:2025 Mishandling of Exceptional Conditions
AffectsAny web app or API; most often frameworks left in development mode and PHP with display_errors on

A request that makes a PHP page fail can come back with a line like this one:1

Warning: odbc_fetch_array() expects parameter /1 to be resource, boolean given
in D:\app\index_new.php on line 188

The line names the database function the page calls, shows that the query before it failed, and gives the folder the application is installed in. Error handling is the code that decides what happens after an operation fails: what the visitor is shown, what goes into the log, and what state the program is left in. Poor error handling shows up in two ways. The response carries internal detail, often a stack trace, or the program carries on in a broken state.

What a detailed error reveals

A Java application that cannot parse a number may answer with a full exception report: the status line HTTP Status 500, the exception class java.lang.NumberFormatException, a list of frames that name the Struts 2 classes in use, and a closing note that gives the server as Apache Tomcat 7.0.56.1 Names and versions like these are what an attacker collects first, during reconnaissance, the phase before an attack.1

Errors also help with attacks already under way. A Path traversal attempt that fails may print the full path of the application, which tells the attacker how many ../ steps reach a target file. A failed SQL injection attempt may print the broken query, which exposes the query logic and sometimes a password or other value written into the query.2 The attacker then sends input that causes more errors and refines the injection from what each message shows.3 Some messages leak data directly. In two published vulnerabilities, one program printed a password when certain database errors occurred, and a blog system's error page for a missing blog told visitors which user names existed.2 The second case is a form of Account enumeration.

Development defaults

The four stacks in Table 1 print full errors on purpose while the developer is working, and switch them off through one setting. The detail reaches visitors when that setting is wrong on the live server. Django's settings reference gives the rule in one line: "Never deploy a site into production with DEBUG turned on."4 With DEBUG on, an exception page lists the traceback and every Django setting, apart from those whose names suggest a secret, such as SECRET_KEY.4 The same switch controls other developer pages; Debug mode in production covers them.

Table 1. What four common stacks send when a request fails
StackDevelopmentProduction
ExpressThe built-in handler writes err.stack into the response bodyWith NODE_ENV=production, only the HTML of the status message
Next.js (App Router)The original message of a Server Component error is sent to the browserA generic message and an identifier in error.digest
DjangoDEBUG = True, which a new project's settings.py sets: traceback and settingsDEBUG = False: no detailed error page
PHPdisplay_errors defaults to "1"; php.ini-development sets it Onphp.ini-production sets it Off

The Express and PHP rows depend on configuration that is easy to miss. Express decides from the environment variable NODE_ENV, so a server started without it sends stack traces.5 PHP prints errors unless php.ini turns that off, and its manual says the display feature "should never be used on production systems".6 Next.js separates the two cases itself: in production an error from a Server Component (code that runs on the server) reaches the browser as a generic message plus a hash, while an error thrown in a Client Component keeps its original message.7

A generic answer and a full log

The usual design splits the error in two. The visitor gets a short, generic response with the right status code; the details go to a log on the server, where the team can read them.1 One handler that catches everything the specific handlers missed keeps this the same across the whole application.3 In Express that is an error middleware with four arguments, registered after all routes:5

// Express: registered last, after every route
app.use((err, req, res, next) => {
  if (res.headersSent) return next(err);
  console.error(err); // the stack trace stays in the server log
  res.status(500).type("application/problem+json").json({
    type: "about:blank",
    title: "Internal Server Error",
    status: 500
  });
});

The body follows RFC 9457, the standard JSON format for API errors. With the type about:blank, the title is the plain status phrase.8 A problem details object is written for the client, not for debugging the server, and every field placed in it has to be checked for what it could leak.8

A message that says too little confuses the people who see it.2 An identifier that appears both in the response and in the log line connects the two without exposing anything. Next.js does this with error.digest; RFC 9457 has an instance field for the same purpose.78 What the log itself may hold is covered in Security logging and alerting and Personal data in logs.

Failing closed

The second half of error handling is the state the program is left in. The 2025 edition of the OWASP Top 10 gave this a category of its own, grouping leaking error messages with failing open (not failing securely) and with logic errors; some of these weaknesses were earlier filed under poor code quality.3 A transfer that debits one account, credits another and writes a log entry has to undo every step if any one fails. If it stops halfway, an attacker who can interrupt the network at the right moment may drain an account or send the same money twice.3 Exceptions that leave files or connections open can use up those resources one request at a time, which ends in a denial of service.3 In Express, a rejected promise that is neither returned nor passed to next is unhandled and crashes the process.5

Prompt for a coding agent

Review how this project handles errors. 1) Find every place an error message, exception, stack trace or database error is sent to the browser or an API client, and replace it with a generic message and the right status code, logging the full error on the server instead. 2) Make sure there is one global error handler (an Express error middleware with four arguments, app/error.js and app/global-error.js in Next.js, or the framework equivalent) that catches anything the routes miss. 3) Check that production runs with NODE_ENV=production, DEBUG = False in Django, or display_errors = Off in PHP. 4) Find operations with several steps that change data and make them roll back completely when one step fails. List each file you changed.

In SecFlash

On the Exposure page, SecFlash reports responses that contain stack traces as SF-EXP-206 and a development build served in production as SF-EXP-103. On the Security page, error details shown to visitors appear as SF-SEC-636, debug or PHP error messages in a page as SF-SEC-637, a readable ASP.NET ELMAH error log as SF-SEC-649, and code that turns on the developer error page without checking the environment as SF-SEC-763.

See also

References

  1. a b c d OWASP. "Error Handling Cheat Sheet". OWASP Cheat Sheet Series.
  2. a b c "CWE-209: Generation of Error Message Containing Sensitive Information". Common Weakness Enumeration. MITRE.
  3. a b c d e "A10:2025 Mishandling of Exceptional Conditions". OWASP Top 10:2025. OWASP Foundation.
  4. a b Django Software Foundation. "Settings: DEBUG". Django 5.2 documentation.
  5. a b c OpenJS Foundation. "Error Handling". Express documentation.
  6. back The PHP Group. "Runtime Configuration: Error Handling and Logging". PHP Manual.
  7. a b Vercel. "error.js". Next.js documentation.
  8. a b c Nottingham, M.; Wilde, E.; Dalal, S. (July 2023). "RFC 9457: Problem Details for HTTP APIs". IETF.