Description
I want to prevent any server info leak in error responses. I disabled whitelabel and stacktrace printing
server.error.include-stacktrace=never
server.error.whitelabel.enabled=false
I also tried to add my own error controller and custom error pages, but when I send corrupted request, embedded Tomcat responses with server info and stacktrace.
Example of corrupted request:
curl 'http://localhost:8080/foo' -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundaryHBic2VjAi3E215bk' --data-binary $'------WebKitFormBoundaryHBic2VjAi3E215bk\r\nContent-Disposition: form-data; name="file"; filename="doc-xlsx.xlsx"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n\r\n------WebKitFormBoundaryHBic2VjAi3E215bk\r\nContent-Disposition: '
I tried to turn off report and server info printing manually, but exception message still presents in responses (it contains tomcat's and spring's classes names).
@Bean
public EmbeddedServletContainerFactory servletContainer() {
// Disable tomcat version and exception logging by ErrorReportValve
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
ErrorReportValve errorReportValve = new ErrorReportValve();
errorReportValve.setShowReport(false);
errorReportValve.setShowServerInfo(false);
tomcat.addContextValves(errorReportValve);
return tomcat;
}
I think it's possible to overwrite TomcatEmbeddedServletContainerFactory#getEmbeddedServletContainer
to return Tomcat
instance with different host
property, StandardHost#errorReportValveClass
should be changed for another class in that case. I didn't try this solution, because it creates too much coupling to underlying implementations. Looks like spring-boot should be able to do this.