Privacy engineering

Prevent Sensitive Information from Leaking Through Error Messages

Give users a useful next step while keeping stack traces, credentials and internal infrastructure details out of public error responses.

An error response serves two audiences with different needs. The person using the application needs to know what happened, whether their work was saved and what to do next. The operator needs enough evidence to diagnose the failure. Sending the operator’s diagnostic details to the browser is a tempting shortcut, but it can expose data and infrastructure without helping the user recover.

Imagine a document portal whose search service becomes unavailable. The browser receives a database exception containing an internal hostname, a query fragment and part of a customer identifier. The page still does not explain whether the user should retry. The response has failed both audiences: it discloses information publicly and leaves the practical question unanswered.

A better design separates a stable public error contract from restricted operational evidence. Build that separation before a production incident forces someone to invent it under pressure.

Start with an error inventory

List the points at which the application can fail: input validation, authentication, authorization, dependency calls, file processing, background work and unexpected exceptions. Include failures before the application handler runs, such as a proxy rejecting a request or a storage service denying access.

The OWASP Error Handling Cheat Sheet describes the risk of revealing implementation details through errors and the need to separate generic public responses from server-side diagnostic handling. It provides a security foundation, but the product still needs to decide what its users can safely do after each failure.

For each error family, identify the public consequence. An invalid date can be corrected immediately. A denied action may require a different role. An unavailable dependency may warrant a later retry. A timeout after a write may leave the result uncertain. These differences should shape the message without requiring disclosure of a stack trace.

Record all response surfaces, not just the visible page. APIs, download endpoints, webhook replies, mobile clients and asynchronous job status pages can expose different details. A safe HTML page does not compensate for a JSON field that contains the original exception string.

Write a public contract

Define a small set of public fields with clear purposes. An application might return a stable error code, a human-readable explanation, an optional reference and a permitted next action. Avoid copying arbitrary fields from an underlying exception into that response.

A stable code allows a client to handle a known condition without parsing prose. Keep the explanation understandable and specific enough to guide action: the requested export could not be prepared, for example, rather than a vague statement that something went wrong. Do not claim the operation failed completely if the server cannot establish whether a side effect occurred.

Validation messages should identify the field and acceptable correction without echoing sensitive input. A password error can describe the relevant policy without repeating the submitted password. A malformed upload can explain the accepted format without displaying an unsafe filename as executable markup.

For authorization failures, consider whether identifying the resource would reveal private existence. A response that says a particular confidential project exists but belongs to another customer may disclose more than the requester is entitled to know. Align the public contract with the application’s resource authorization rules.

Use references without creating another secret

A correlation reference can help support locate the relevant event. Design it as a lookup aid, not as an access token. Possession of the reference must not grant access to private logs or another user’s operation details.

Generate and validate references according to the system’s architecture. If a client supplies a request identifier, treat it as untrusted input: constrain its shape, prevent log injection and distinguish it from an internally assigned identifier. Do not accept an arbitrary string and assume that it safely identifies a unique server event.

Keep the reference visible long enough for the user to report it, but avoid forcing them to copy a large technical object. A support interface can accept the reference and authenticate the person separately. The support agent should still verify the scope of information they may disclose.

Consider the case where an error occurs before a reference can be created. The fallback response should remain safe and useful. A dependency on the logging system must not make error handling itself throw another exception that exposes a framework debug page.

Make diagnostic records deliberate

Restricted logs can contain more operational detail than the public response, but they are not a dumping ground. Decide which fields are necessary to understand the failure and which should be removed, redacted or represented by a non-sensitive identifier.

Exception objects can contain request bodies, authorization headers, SQL values or upstream responses. Review the logging behavior of libraries and middleware as well as application code. A carefully written handler may still be preceded by instrumentation that captures the complete request.

Use the application logging guide to define permitted fields and retention. Restrict access to diagnostic systems, and make support workflows rely on the smallest useful view. Moving sensitive data from the response into a broadly accessible log is not a complete fix.

For the document portal example, an event might record the affected operation type, dependency category, duration, deployment version and internal reference. The original search text may not be needed at all. If a rare investigation requires additional data, use a controlled, time-limited process rather than permanently expanding every event.

Handle uncertain writes honestly

A timeout is especially difficult when the requested operation changes state. The server may have completed the action while the response was lost. Telling the user to submit again can create duplicate work, while claiming that nothing happened can be false.

Define how the application identifies the original operation and checks its outcome. Where the architecture supports idempotency, explain the intended repeat behavior and test it. Otherwise, provide a way to inspect the existing result before attempting another write. The public message should reflect what the system actually knows.

For example, a failed export request can direct the user to the export history if the job may already exist. A failed settings change can reload the current setting before offering another save. These are product decisions supported by state checks, not merely alternative error wording.

Include the uncertain state in support documentation. Agents should not advise repeated submission as a universal remedy. The right next step depends on the operation’s side effects and the evidence available.

Test the layers that can bypass your handler

Exercise errors in a production-like configuration with debug output disabled. Development behavior is useful for engineers but should not define the public contract. Verify the configuration actually used by the deployment, including reverse proxies and managed services.

Test missing routes, invalid methods, oversized requests, unsupported content types, malformed input and dependency failures. Use synthetic data and deliberately unusual input to identify accidental echoing. Inspect headers and response bodies as well as screenshots.

Background jobs need their own checks. A worker can persist an exception into a database field that the frontend later displays verbatim. Review the transformation from internal job failure to public job status, and ensure that old stored errors are not exposed after a frontend change.

Check cached responses too. An error page containing user-specific details should not be served to another person through a shared cache. The appropriate cache behavior depends on the endpoint, but it must be considered as part of the response contract rather than assumed from the status code alone.

Keep the contract stable through changes

Create a review table linking each error family to its public fields, permitted next action, restricted diagnostic event and test. Store it near the implementation so a new dependency or framework upgrade triggers a visible review.

Have someone unfamiliar with the internals follow the messages. Ask whether they can tell if their work was saved and what action is safe. Separately, ask an operator to diagnose the synthetic failure using only the restricted record and public reference. Both exercises are needed because clarity and confidentiality are different qualities.

The result should be a repeatable separation: public responses explain consequences and recovery, operational records support diagnosis, and neither channel collects more sensitive information than necessary. When a new failure appears, extend that contract deliberately instead of returning the raw exception and hoping the next page hides it.