- Introduction
- Add Default Character Set Filter
- CORS Filter
- CSRF Prevention Filter
- CSRF Prevention Filter for REST APIs
- Expires Filter
- Failed Request Filter
- HTTP Header Security Filter
- Remote Address Filter
- Remote Host Filter
- Remote CIDR Filter
- Remote IP Filter
- Introduction
- Filter Class Name
- Basic configuration to handle 'x-forwarded-for'
- Basic configuration to handle 'x-forwarded-for' and 'x-forwarded-proto'
- Advanced configuration with internal proxies
- Advanced configuration with trusted proxies
- Advanced configuration with internal and trusted proxies
- Advanced configuration with an untrusted proxy
- Initialisation parameters
- Request Dumper Filter
- Session Initializer Filter
- Set Character Encoding Filter
- WebDAV Fix Filter
Container Provided Filters
Table of Contents
Introduction
Tomcat provides a number of Filters which may be
configured for use with all web applications using
$CATALINA_BASE/conf/web.xml or may be configured for individual
web applications by configuring them in the application's
WEB-INF/web.xml. Each filter is described below.
This description uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.
Add Default Character Set Filter
Introduction
The HTTP specification is clear that if no character set is specified for media sub-types of the "text" media type, the ISO-8859-1 character set must be used. However, browsers may attempt to auto-detect the character set. This may be exploited by an attacker to perform an XSS attack. Internet Explorer and other browsers have an option to enable this behavior.
This filter prevents the attack by explicitly setting a character set. Unless the provided character set is explicitly overridden by the user the browser will adhere to the explicitly set character set, thus preventing the XSS attack.
Filter Class Name
The filter class name for the Add Default Character Set Filter is
org.apache.catalina.filters.AddDefaultCharsetFilter
.
Initialisation parameters
The Add Default Character Set Filter supports the following initialization parameters:
| Attribute | Description |
|---|---|
encoding |
Name of the character set which should be set, if no other character set
was set explicitly by a Servlet. This parameter has two special values
|
CORS Filter
Introduction
This filter is an implementation of W3C's CORS (Cross-Origin Resource Sharing) specification, which is a mechanism that enables cross-origin requests.
The filter works by adding required Access-Control-* headers
to HttpServletResponse object. The filter also protects against HTTP
response splitting. If request is invalid, or is not permitted, then request
is rejected with HTTP status code 403 (Forbidden). A
flowchart that
demonstrates request processing by this filter is available.
The minimal configuration required to use this filter is:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>The above configuration enables the filter but does not relax the cross-origin policy. As a minimum, you will need to add a cors.allowed.origins initialisation parameter as described below to enable cross-origin requests. Depending on your requirements, you may need to provide additional configuration.
An instance of this filter can only implement one policy. If you want to apply different policies (e.g. different allowed origins) to different URLs or sets of URLs within your web application you will need to configure a separate instance of this filter for each policy you wish to configure.
Filter Class Name
The filter class name for the CORS Filter is
org.apache.catalina.filters.CorsFilter.
Initialisation parameters
The CORS Filter supports following initialisation parameters:
| Attribute | Description |
|---|---|
cors.allowed.origins |
A list of origins
that are allowed to access the resource. A |
cors.allowed.methods |
A comma separated list of HTTP methods that can be used to access the
resource, using cross-origin requests. These are the methods which will
also be included as part of |
cors.allowed.headers |
A comma separated list of request headers that can be used when
making an actual request. These headers will also be returned as part
of |
cors.exposed.headers |
A comma separated list of headers other than simple response headers
that browsers are allowed to access. These are the headers which will
also be included as part of |
cors.preflight.maxage |
The amount of seconds, browser is allowed to cache the result of the
pre-flight request. This will be included as part of
|
cors.support.credentials |
A flag that indicates whether the resource supports user credentials.
This flag is exposed as part of
|
cors.request.decorate |
A flag to control if CORS specific attributes should be added to
HttpServletRequest object or not. Defaults:
|
Here's an example of a more advanced configuration, that overrides defaults:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>https://www.apache.org</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>CORS Filter and HttpServletRequest attributes
CORS Filter adds information about the request, in HttpServletRequest
object, for consumption downstream. Following attributes are set, if
cors.request.decorate initialisation parameter is
true:
- cors.isCorsRequest: Flag to determine if request is a CORS request.
- cors.request.origin: The Origin URL, i.e. the URL of the page from where the request originated.
- cors.request.type: Type of CORS request. Possible
values:
SIMPLE: A request which is not preceded by a pre-flight request.ACTUAL: A request which is preceded by a pre-flight request.PRE_FLIGHT: A pre-flight request.NOT_CORS: A normal same-origin request.INVALID_CORS: A cross-origin request, which is invalid.
- cors.request.headers: Request headers sent as
Access-Control-Request-Headersheader, for a pre-flight request.
CSRF Prevention Filter
Introduction
This filter provides basic CSRF protection for a web application. The
filter assumes that it is mapped to /* and that all URLs
returned to the client are encoded via a call to
HttpServletResponse#encodeRedirectURL(String) or
HttpServletResponse#encodeURL(String).
This filter prevents CSRF by generating a nonce and storing it in the session. URLs are also encoded with the same nonce. When the next request is received the nonce in the request is compared to the nonce in the session and only if they are the same is the request allowed to continue.
Filter Class Name
The filter class name for the CSRF Prevention Filter is
org.apache.catalina.filters.CsrfPreventionFilter
.
Initialisation parameters
The CSRF Prevention Filter supports the following initialisation parameters:
| Attribute | Description |
|---|---|
denyStatus |
HTTP response status code that is used when rejecting denied
request. The default value is |
entryPoints |
A comma separated list of URLs that will not be tested for the presence of a valid nonce. They are used to provide a way to navigate back to a protected application after having navigated away from it. Entry points will be limited to HTTP GET requests and should not trigger any security sensitive actions. |
nonceCacheSize |
The number of previously issued nonces that will be cached on a LRU basis to support parallel requests, limited use of the refresh and back in the browser and similar behaviors that may result in the submission of a previous nonce rather than the current one. If not set, the default value of 5 will be used. |
randomClass |
The name of the class to use to generate nonces. The class must be an
instance of |
CSRF Prevention Filter for REST APIs
Introduction
This filter provides basic CSRF protection for REST APIs. The CSRF
protection is applied only for modifying HTTP requests (different from GET,
HEAD, OPTIONS) to protected resources. It is based on a custom header
X-CSRF-Token that provides a valid nonce.
CSRF protection mechanism for REST APIs consists of the following steps:
- Client asks for a valid nonce. This is performed with a non-modifying "Fetch" request to protected resource.
- Server responds with a valid nonce mapped to the current user session.
- Client provides this nonce in the subsequent modifying requests in the frame of the same user session.
- Server rejects all modifying requests to protected resources that do not contain a valid nonce.
Basic configuration sample
On the server side
- All CSRF protected REST APIs should be protected with an authentication mechanism.
- Protect modifying REST APIs with this filter.
- Provide at least one non-modifying operation.
<filter>
<filter-name>RestCSRF</filter-name>
<filter-class>org.apache.catalina.filters.RestCsrfPreventionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RestCSRF</filter-name>
<!-- Modifying operations -->
<url-pattern>/resources/removeResource</url-pattern>
<url-pattern>/resources/addResource</url-pattern>
<!-- Non-modifying operations -->
<url-pattern>/resources/listResources</url-pattern>
</filter-mapping>On the client side
- Make a non-modifying "Fetch" request in order to obtain a valid nonce.
This can be done with sending additional header
X-CSRF-Token: Fetch - Cache the returned session id and nonce in order to provide them in the subsequent modifying requests to protected resources.
- Modifying requests can be denied and header
X-CSRF-Token: Requiredwill be returned in case of invalid or missing nonce, expired session or in case the session id is changed by the server.
Client Request:
GET /rest/resources/listResources HTTP/1.1
X-CSRF-Token: Fetch
Authorization: Basic ...
Host: localhost:8080
...
Server Response:
HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=...; Path=/rest; HttpOnly
X-CSRF-Token: ...
...
Client Request:
POST /rest/resources/addResource HTTP/1.1
Cookie: JSESSIONID=...
X-CSRF-Token: ...
Authorization: Basic ...
Host: localhost:8080
...
Server Response:
HTTP/1.1 200 OK
...RestCsrfPreventionFilter and HttpServletRequest parameters
When the client is not able to insert custom headers in its calls to REST APIs there is additional capability to configure URLs for which a valid nonce will be accepted as a request parameter.
Note: If there is a X-CSRF-Token header, it will be taken
with preference over any parameter with the same name in the request.
Request parameters cannot be used to fetch new nonce, only header can be
used to request a new nonce.
<filter>
<filter-name>RestCSRF</filter-name>
<filter-class>org.apache.catalina.filters.RestCsrfPreventionFilter</filter-class>
<init-param>
<param-name>pathsAcceptingParams</param-name>
<param-value>/resources/removeResource,/resources/addResource</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>RestCSRF</filter-name>
<url-pattern>/resources/*</url-pattern>
</filter-mapping>Filter Class Name
The filter class name for the CSRF Prevention Filter for REST APIs is
org.apache.catalina.filters.RestCsrfPreventionFilter
.
Initialisation parameters
The CSRF Prevention Filter for REST APIs supports the following initialisation parameters:
| Attribute | Description |
|---|---|
denyStatus |
HTTP response status code that is used when rejecting denied
request. The default value is |
pathsAcceptingParams |
A comma separated list of URLs that can accept nonces via request
parameter |
randomClass |
The name of the class to use to generate nonces. The class must be an
instance of |
Expires Filter
Introduction
ExpiresFilter is a Java Servlet API port of Apache
mod_expires.
This filter controls the setting of the Expires HTTP header and the
max-age directive of the Cache-Control HTTP header in
server responses. The expiration date can set to be relative to either the
time the source file was last modified, or to the time of the client access.
