Information Exposure Through Error Message

ID

csharp.information_exposure_through_error_message

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

medium

Resource

Information Leak

Language

CSharp

Tags

ASVS50:v16.4.1, CWE:209, NIST.SP.800-53, OWASP:2025:A06, PCI-DSS:6.5.5

Description

Generation of error messages containing sensitive information.

Rationale

Error messages are an integral part of application development, used to communicate issues in the code to the developers. However, in a production environment, overly detailed error messages can become a security liability. For example, stack traces or database exception messages can disclose information about file paths, internal IP addresses, stack traces, database structure, or library versions, all of which can be exploited if in the hands of a malicious user.

The exposure happens when those details reach whoever sent the request, in the HTTP response. Details written to a server-side log are the recommended remediation, not the flaw, so they are not reported. A stack trace in the response is reported with a higher severity than an exception message.

The risk is the error detail reaching whoever sent the request. This detector reports a caught exception returned in the HTTP response: an ASP.NET Core action result (Ok, BadRequest, StatusCode, Problem, Content, Json…​), a minimal-API Results.*, Response.WriteAsync, System.Web Response.Write, or the value returned by an action.

[HttpGet("{id}")]
public IActionResult Get(string id)
{
    try
    {
        return Ok(_repository.Find(id));
    }
    catch (SqlException ex)
    {
        return StatusCode(500, $"Database error: {ex}"); // FLAW
    }
}

A stack trace in the response is reported with high severity: ex.StackTrace, ex.ToString(), and the exception itself in an interpolation, concatenation or serialized payload, which renders ToString() including the stack trace. Any other member (ex.Message) is reported with the detector’s configured severity, but only for a catch-all handler (catch (Exception ex)) or one catching a platform exception (SqlException, IOException, HttpRequestException…​). A narrow application handler such as catch (FormatException ex) typically carries an application-authored message.

Logging the exception server-side (_logger.LogError(ex, …​), Console.WriteLine(ex)) is not reported: that is the recommended remediation. Responses inside a development-only branch (if (env.IsDevelopment())) are not reported either.

Remediation

To remediate instances of information exposure through error messages, the primary action is to limit the detail provided in exceptions sent to the end-user. Consider the following steps:

  1. Generic Error Messages: Ensure that user-facing error messages do not reveal detailed system information. Use generic language and avoid technical specifics.

  2. Centralized Error Handling: Employ a global exception handling strategy using a framework that standardizes error responses across your applications.

  3. Logging: Utilize logging frameworks to log detailed error information securely, ensuring logs do not expose sensitive data.

  4. Security Review: Review all instances where exceptions are caught and handled, ensuring they follow the guidelines above, particularly in areas of code that are publicly accessible.

Following these steps reduces the risk of sensitive data exposure through error messages, thereby strengthening the security posture of the application.

The most idiomatic way to handle errors in ASP.NET Core is to use a custom middleware that captures and handles exceptions (registered with app.UseExceptionHandler()), ensuring that sensitive information is not exposed to the end-user. Full stack traces and technical details can be sent to the end-user only in development environments, but they can be logged using a logging framework for troubleshooting.

Another option is to use the Microsoft.AspNetCore.Mvc.ProblemDetails(RFC-7807) for exposing error details in REST APIs.

Configuration

The detector has the following configurable parameters:

  • allow_explicitly_thrown_errors to allow .

References

© 2026 Xygeni. All rights reserved