Sub topic 12
Keeping internal details out of responses
This chapter assumes you can write a @RestControllerAdvice with @ExceptionHandler methods and return ProblemDetail bodies, as covered earlier. It looks at what those responses can accidentally reveal and how to stop that.
Why it matters
An error response goes to anyone who can reach your API. Stack traces, exception class names, SQL fragments, file paths and rejected input values tell an attacker which frameworks you use, how your tables are named and where your code lives. Clients need a status code, a stable title and enough detail to fix their own request. Everything else belongs in your logs.
Two paths, two sets of rules
Errors reach the client by one of two paths, and each path is controlled differently.
The server.error.* properties only affect the default /error response. Once your advice handles an exception, those properties no longer apply and the body contains whatever you put in it.
Lock down the default response
Spring Boot 3 hides these details by default, but setting them explicitly records the intent. It also protects you if someone changes a default or enables them in a profile for debugging.
# Return RFC 9457 problem details for exceptions raised by Spring MVC itself
spring.mvc.problemdetails.enabled=true
# Keep internal details out of the default /error response
server.error.include-message=never
server.error.include-binding-errors=never
server.error.include-stacktrace=never
server.error.include-exception=falseValues such as on-param let a client request a stack trace with ?trace=true. Do not use that outside local development.
Control your own handlers
In a handler, decide for each exception type whether its message is safe to send.
OrderNotFoundExceptionis safe. You wrote the message, and it contains only an id the caller already sent.- A generic
Exceptionis not safe. Its message may contain a JDBC URL, a SQL statement or a file path. DataAccessExceptionand similar infrastructure exceptions should never pass their message through.
For the catch-all handler, return a fixed message and log the real exception on the server. Adding an error id lets a client quote it in a support request, and you can then find the matching log line.
package com.example.orders;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class SafeFallbackHandler {
private static final Logger log = LoggerFactory.getLogger(SafeFallbackHandler.class);
@ExceptionHandler(Exception.class)
public ProblemDetail handleUnexpected(Exception ex) {
String errorId = UUID.randomUUID().toString();
log.error("Unhandled exception, errorId={}", errorId, ex);
ProblemDetail problem = ProblemDetail.forStatusAndDetail(
HttpStatus.INTERNAL_SERVER_ERROR, "An unexpected error occurred");
problem.setTitle("Internal server error");
problem.setProperty("errorId", errorId);
return problem;
}
}The response carries a random id and a fixed sentence. The stack trace appears only in the log.
Common leaks to check
- Echoing
ex.getMessage()from a handler forException,RuntimeExceptionor persistence exceptions. - Echoing rejected values in validation errors. Reporting
password: must not be blankis fine. Reporting the submitted value is not, because it may end up in client logs or proxies. - Parse errors. A malformed JSON body produces a message from Jackson that can mention internal class and field names. If you override
handleHttpMessageNotReadable, return a short fixed message. - Different responses for "missing" and "forbidden". If an attacker can tell that order 42 exists but belongs to someone else, you have leaked data. Return 404 for both when that matters.
- Debug properties left on in a shared profile. Check
application-*.propertiesfiles.
Exercise
Your advice class has @ExceptionHandler(Exception.class) that builds a ProblemDetail using ex.getMessage() as the detail. The properties file sets server.error.include-message=never. Does a database failure still leak its message to the client? Why, and what is the fix?
Show answer
Yes, it leaks. The server.error.* properties only affect the default /error response. Your handler catches the exception first and puts the message in the body itself. The fix is to use a fixed detail such as "An unexpected error occurred", log the exception with its stack trace on the server, and optionally return an error id that matches the log entry.