What information should be excluded from error responses in production to avoid leaking sensitive implementation details?
Short answer
Production error responses should never include raw exception messages, stack traces, internal file paths, database connection details, or anything revealing internal architecture, since these can aid an attacker -- instead, log the full exception server-side while returning a…
Advertisement
AModel answer
Production error responses should never include raw exception messages, stack traces, internal file paths, database connection details, or anything revealing internal architecture, since these can aid an attacker -- instead, log the full exception server-side while returning a generic, safe message to the client, optionally including a correlation/trace ID that lets support staff look up full details in logs without exposing them directly.
public async ValueTask<bool> TryHandleAsync(HttpContext context, Exception exception, CancellationToken ct) {
var traceId = context.TraceIdentifier;
_logger.LogError(exception, "Unhandled exception. TraceId: {TraceId}", traceId);
await context.Response.WriteAsJsonAsync(new { title = "An unexpected error occurred", traceId }, ct);
return true;
}Real-world example
A penetration test flags an API that was returning full .NET stack traces (including internal file paths and a database connection string fragment) in error responses -- fixed by ensuring the developer exception page is strictly Development-only.
Likely follow-ups
- How does a correlation/trace ID help support teams without exposing sensitive data?
- What's the risk specifically of exposing stack traces regarding internal architecture?
Related topics