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 any information revealing internal architecture -- since these can aid an attacker in understanding and exploiting the system.
Advertisement
AModel answer
Production error responses should never include raw exception messages, stack traces, internal file paths, database connection details, or any information revealing internal architecture -- since these can aid an attacker in understanding and exploiting the system. Instead, log the full exception details server-side (where only authorized personnel can see them) while returning a generic, safe message to the client, optionally including a correlation/trace ID that lets support staff look up the 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); // full details logged
await context.Response.WriteAsJsonAsync(new {
title = "An unexpected error occurred", // safe, generic message to client
traceId // lets support correlate with server-side logs without exposing internals
}, 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 UseDeveloperExceptionPage is strictly Development-only and production uses the safe, generic handler.
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