Never Expose Raw Errors or Stack Traces to Clients
Share
Returning raw error messages or stack traces leaks implementation details. Return generic messages with a requestId for server-side debugging. [CWE-209]
Why This Matters
prevents leaking database schemas, file paths, and internal implementation details
Never Expose Raw Errors or Stack Traces to Clients
Impact: MEDIUM (prevents leaking database schemas, file paths, and internal implementation details)
Returning raw error messages or stack traces to API consumers exposes internal implementation details that attackers use for reconnaissance. A database error might reveal table names and column types. A file system error might reveal your deployment path. A validation library error might reveal your schema structure. Always return generic error messages for 5xx errors and include a requestId so developers can correlate client errors with server logs.
The key distinction is between operational errors (expected failures like "user not found" or "invalid input") and programmer errors (unexpected failures like null pointer exceptions or database connection failures). Operational errors can have descriptive messages. Programmer errors must always be generic.
# Find raw error messages being returned to clientsgrep -rn "error\.message\|error\.stack" src/app/api --include="*.ts"# Find catch blocks returning the raw errorgrep -rn "catch.*error.*NextResponse\.json.*error" src/app/api --include="*.ts"