Top Description Inners Fields Constructors Methods
org.apache.catalina.filters

public Class RestCsrfPreventionFilter

extends CsrfPreventionFilterBase
Class Inheritance
Imports
java.io.IOException, java.util.Arrays, .HashSet, .Objects, .Set, java.util.function.Predicate, java.util.regex.Pattern, jakarta.servlet.FilterChain, .FilterConfig, .ServletException, .ServletRequest, .ServletResponse, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, .HttpSession

Provides basic CSRF protection for REST APIs. The filter assumes that the clients have adapted the transfer of the nonce through the 'X-CSRF-Token' header.
Positive scenario:
          Client                            Server
             |                                 |
             | GET Fetch Request              \| JSESSIONID
             |---------------------------------| X-CSRF-Token
             |                                /| pair generation
             |/Response to Fetch Request       |
             |---------------------------------|
JSESSIONID   |\                                |
X-CSRF-Token |                                 |
pair cached  | POST Request with valid nonce  \| JSESSIONID
             |---------------------------------| X-CSRF-Token
             |                                /| pair validation
             |/ Response to POST Request       |
             |---------------------------------|
             |\                                |

Negative scenario:
          Client                            Server
             |                                 |
             | POST Request without nonce     \| JSESSIONID
             |---------------------------------| X-CSRF-Token
             |                                /| pair validation
             |/Request is rejected             |
             |---------------------------------|
             |\                                |

          Client                            Server
             |                                 |
             | POST Request with invalid nonce\| JSESSIONID
             |---------------------------------| X-CSRF-Token
             |                                /| pair validation
             |/Request is rejected             |
             |---------------------------------|
             |\                                |

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static enum
private static interface
private static interface
private static interface
private class

Field Summary

Modifier and TypeField and Description
private static final Pattern
private static final Predicate<String>
private Set<String>
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
doFilter(ServletRequest
The request to process
request
,
ServletResponse
The response associated with the request
response
,
FilterChain
Provides access to the next filter in the chain for this filter to pass the request and response to for further processing
chain
)

Implements jakarta.servlet.Filter.doFilter.

The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.

public Set<String>
public void
init(FilterConfig
The configuration information associated with the filter instance being initialised
filterConfig
)

Overrides org.apache.catalina.filters.CsrfPreventionFilterBase.init.

Overrides default jakarta.servlet.Filter.init.

Iterates over the configuration parameters and either logs a warning, or throws an exception for any parameter that does not have a matching setter in this filter.

public void
setPathsAcceptingParams(String
Comma separated list of URLs to be configured as paths accepting request parameters with nonce information.
pathsList
)

A comma separated list of URLs that can accept nonces via request parameter 'X-CSRF-Token'.

Inherited from org.apache.catalina.filters.CsrfPreventionFilterBase:
generateNoncegetDenyStatusgetLoggergetRequestedPathisConfigProblemFatalsetDenyStatussetRandomClass

Field Detail

NON_MODIFYING_METHODS_PATTERNback to summary
private static final Pattern NON_MODIFYING_METHODS_PATTERN
nonModifyingMethodsback to summary
private static final Predicate<String> nonModifyingMethods
pathsAcceptingParamsback to summary
private Set<String> pathsAcceptingParams
pathsDelimiterback to summary
private String pathsDelimiter

Constructor Detail

RestCsrfPreventionFilterback to summary
public RestCsrfPreventionFilter()

Method Detail

doFilterback to summary
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException

Implements jakarta.servlet.Filter.doFilter.

Doc from jakarta.servlet.Filter.doFilter.

The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

A typical implementation of this method would follow the following pattern:-
1. Examine the request
2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
4. a) Either invoke the next entity in the chain using the FilterChain object (chain.doFilter()),
4. b) or not pass on the request/response pair to the next entity in the filter chain to block the request processing
5. Directly set headers on the response after invocation of the next entity in the filter chain.

Parameters
request:ServletRequest

The request to process

response:ServletResponse

The response associated with the request

chain:FilterChain

Provides access to the next filter in the chain for this filter to pass the request and response to for further processing

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs during this filter's processing of the request
ServletException:
if the processing fails for any other reason
getPathsAcceptingParamsback to summary
public Set<String> getPathsAcceptingParams()
initback to summary
public void init(FilterConfig filterConfig) throws ServletException

Overrides org.apache.catalina.filters.CsrfPreventionFilterBase.init.

Overrides default jakarta.servlet.Filter.init.

Doc from org.apache.catalina.filters.FilterBase.init.

Iterates over the configuration parameters and either logs a warning, or throws an exception for any parameter that does not have a matching setter in this filter.

Parameters
filterConfig:FilterConfig

The configuration information associated with the filter instance being initialised

Annotations
@Override
Exceptions
ServletException:
if isConfigProblemFatal() returns true and a configured parameter does not have a matching setter
setPathsAcceptingParamsback to summary
public void setPathsAcceptingParams(String pathsList)

A comma separated list of URLs that can accept nonces via request parameter 'X-CSRF-Token'. For use cases when a nonce information cannot be provided via header, one can provide it via request parameters. 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.

Parameters
pathsList:String

Comma separated list of URLs to be configured as paths accepting request parameters with nonce information.

org.apache.catalina.filters back to summary

private Class RestCsrfPreventionFilter.FetchRequest

extends Object
implements RestCsrfPreventionStrategy
Class Inheritance
  • java.lang.Object
  • org.apache.catalina.filters.RestCsrfPreventionFilter.FetchRequest
All Implemented Interfaces
org.apache.catalina.filters.RestCsrfPreventionFilter.RestCsrfPreventionStrategy

Field Summary

Modifier and TypeField and Description
private final Predicate<String>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fetchRequestback to summary
private final Predicate<String> fetchRequest

Constructor Detail

FetchRequestback to summary
private FetchRequest()

Method Detail

applyback to summary
public boolean apply(HttpServletRequest request, HttpServletResponse response)

Implements org.apache.catalina.filters.RestCsrfPreventionFilter.RestCsrfPreventionStrategy.apply.

Annotations
@Override
org.apache.catalina.filters back to summary

private final Enum RestCsrfPreventionFilter.MethodType

extends Enum<RestCsrfPreventionFilter.MethodType>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final RestCsrfPreventionFilter.MethodType
public static final RestCsrfPreventionFilter.MethodType

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static RestCsrfPreventionFilter.MethodType
public static RestCsrfPreventionFilter.MethodType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

MODIFYING_METHODback to summary
public static final RestCsrfPreventionFilter.MethodType MODIFYING_METHOD
NON_MODIFYING_METHODback to summary
public static final RestCsrfPreventionFilter.MethodType NON_MODIFYING_METHOD

Constructor Detail

MethodTypeback to summary
private MethodType()

Method Detail

valueOfback to summary
public static RestCsrfPreventionFilter.MethodType valueOf(String name)
valuesback to summary
public static RestCsrfPreventionFilter.MethodType[] values()
org.apache.catalina.filters back to summary

private Interface RestCsrfPreventionFilter.NonceConsumer<T>

Annotations
@FunctionalInterface

Method Summary

Modifier and TypeMethod and Description
public void
setNonce(T consumer, String key, String value)

Method Detail

setNonceback to summary
public void setNonce(T consumer, String key, String value)
org.apache.catalina.filters back to summary

private Interface RestCsrfPreventionFilter.NonceSupplier<T, R>

Annotations
@FunctionalInterface

Method Summary

Modifier and TypeMethod and Description
public R
getNonce(T supplier, String key)

Method Detail

getNonceback to summary
public R getNonce(T supplier, String key)
org.apache.catalina.filters back to summary

private Interface RestCsrfPreventionFilter.RestCsrfPreventionStrategy

Known Direct Implementers
org.apache.catalina.filters.RestCsrfPreventionFilter.StateChangingRequest, org.apache.catalina.filters.RestCsrfPreventionFilter.FetchRequest

Field Summary

Modifier and TypeField and Description
public static final RestCsrfPreventionFilter.NonceSupplier<HttpServletRequest, String>
public static final RestCsrfPreventionFilter.NonceSupplier<HttpServletRequest, String[]>
public static final RestCsrfPreventionFilter.NonceSupplier<HttpSession, String>
public static final RestCsrfPreventionFilter.NonceConsumer<HttpServletResponse>
public static final RestCsrfPreventionFilter.NonceConsumer<HttpSession>

Method Summary

Modifier and TypeMethod and Description
public boolean

Field Detail

nonceFromRequestHeaderback to summary
public static final RestCsrfPreventionFilter.NonceSupplier<HttpServletRequest, String> nonceFromRequestHeader
nonceFromRequestParamsback to summary
public static final RestCsrfPreventionFilter.NonceSupplier<HttpServletRequest, String[]> nonceFromRequestParams
nonceFromSessionback to summary
public static final RestCsrfPreventionFilter.NonceSupplier<HttpSession, String> nonceFromSession
nonceToResponseback to summary
public static final RestCsrfPreventionFilter.NonceConsumer<HttpServletResponse> nonceToResponse
nonceToSessionback to summary
public static final RestCsrfPreventionFilter.NonceConsumer<HttpSession> nonceToSession

Method Detail

applyback to summary
public boolean apply(HttpServletRequest request, HttpServletResponse response) throws IOException
org.apache.catalina.filters back to summary

private Class RestCsrfPreventionFilter.StateChangingRequest

extends Object
implements RestCsrfPreventionStrategy
Class Inheritance
  • java.lang.Object
  • org.apache.catalina.filters.RestCsrfPreventionFilter.StateChangingRequest
All Implemented Interfaces
org.apache.catalina.filters.RestCsrfPreventionFilter.RestCsrfPreventionStrategy

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
private String
private String
private boolean
isValidStateChangingRequest(String reqNonce, String sessionNonce)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

StateChangingRequestback to summary
private StateChangingRequest()

Method Detail

applyback to summary
public boolean apply(HttpServletRequest request, HttpServletResponse response) throws IOException

Implements org.apache.catalina.filters.RestCsrfPreventionFilter.RestCsrfPreventionStrategy.apply.

Annotations
@Override
extractNonceFromRequestback to summary
private String extractNonceFromRequest(HttpServletRequest request)
extractNonceFromRequestParamsback to summary
private String extractNonceFromRequestParams(HttpServletRequest request)
isValidStateChangingRequestback to summary
private boolean isValidStateChangingRequest(String reqNonce, String sessionNonce)