views:

192

answers:

3

By default, Tomcat's error pages disclose both the existence of Tomcat and the exact version of the container that's handling the requests. This is nice for development, but in a production context this information is a potential security hole and it would be nice to disable it.

Thus I would like to know what the best (as in most straightforward/comprehensive) solution is to completely suppress Tomcat's default error pages. I am aware of the <error-page> option in web.xml, but it seems to fail on both desired counts, partly because I would have to list the same alternative error page many times (one for each response code I want to handle), and because this strikes me as possibly not 100% robust; if an attacker can somehow get an error code returned that I haven't explicitly listed, they would get the default error page.

Ideally, a simple option to set a universal custom error page, or to flat out disable sending any HTML along with the error code in the default error page, would be best. If neither of those options are possible, I'd be interested in finding out what the typical way to implement this functionality is (bonus points for discussing/showing why those hypothetical options don't exist, since it seems my requirement would be quite standard for anyone using Tomcat in production...).

+1  A: 

A possible option would be to set up a Servlet Filter, that could redirect error pages to exactly the page you want ... You would only code this once, and it would work for all error codes..

KLE
An interesting approach... I suspect it might be a bit fragile *and* kill performance as I'd need to parse every single response to try and work out from the raw characters whether it was a default error page or not. Or is there a better way to identify the problem pages?
Andrzej Doyle
@dtsazza Concerning performance, if you compare the dozens instructions implied to the dozen thousands that are involved in the whole processing of the request, it's less than 0.1%. **Performance impact is negligible when the code is so high level**.
KLE
+2  A: 

<error-page> is the right answer, but you don't want to just redirect all error codes to some generic message. You have to think about how you want to handle each error. If you're afraid you might miss one of the codes, check out the constants in the HttpServletResponse interface.

Jeremy Stein
But I kind of do want to - since it's a self-contained webapp, from the user's perspective either things are working or they're not; the actual response they see really can be generic (logs will show technical staff the actual problem plus the HTTP response code is still intact). Accepting your answer anyway, on the basis of the enumeration of response codes that Tomcat may send.
Andrzej Doyle
+3  A: 

Some of the errors are sent directly by the container and your application doesn't have a chance to deal with them. For example, when a non-existent resource is requested, the 404 error will be sent. The only way your application can do something about it is to declare the appropriate <error-page> entry in the web.xml.

I agree with Jeremy Stein that the <error-page> is the right answer. After all the error codes aren't unlimited.

Read also the discussion here, about how errors are handled with Spring MVC. I believe that it is most important is to handle your own errors (the exceptions that if they don't get caught will result in a 500 Internal Server Error).

kgiannakakis