Class CSRFDoubleSubmitCookieInterceptor<A extends ExchangeContext,B extends Exchange<A>>
- Type Parameters:
A
- the context typeB
- the exchange type
- All Implemented Interfaces:
ExchangeInterceptor<A,
B>
A security interceptor that protects against Cross-site request forgery attack using the double submit cookie technique.
This implementation uses the stateless double submit cookie technique as recommended by Cross-Site Request Forgery Prevention Cheat Sheet.
For any POST
, PUT
, PATCH
and DELETE
, it successively tries to compare the value of a previously generated token specified in an cookie to the value supplied as a
header, as a query parameter or as a form parameter (assuming the request is a application/x-www-form-urlencoded
request). If the two token values are equals, the request is authorized
otherwise a FORBIDDEN(403)
error is returned.
- Since:
- 1.5
- Author:
- Jeremy Kuhn
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A CSRF double submit cookie interceptor builder. -
Field Summary
Modifier and TypeFieldDescriptionprotected final String
The cookie name.static final String
The default cookie name:XSRF-TOKEN
.static final String
The default header name:X-CSRF-TOKEN
.static final String
The default parameter name:_csrf_token
.protected final String
The domain of the CSRF token cookie.protected final String
The header name.protected final boolean
Flag indicating whether the token cookie should be HTTP only.protected final Integer
The max age of the CSRF token cookie.protected final String
The paramerer name.protected final String
The path of the CSRF token cookie.protected final boolean
Flag indicating whether the token cookie should be secured. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Creates a CSRF double submit cookie interceptor builder.protected String
Generates the CSRF token.Returns the CSRF token cookie name.Returns the CSRF token header name.Returns the CSRF token parameter name.Intercepts the exchange before the exchange handler is invoked.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.inverno.mod.http.server.ExchangeInterceptor
andThen, compose
-
Field Details
-
DEFAULT_COOKIE_NAME
The default cookie name:XSRF-TOKEN
.- See Also:
-
DEFAULT_HEADER_NAME
The default header name:X-CSRF-TOKEN
.- See Also:
-
DEFAULT_PARAMETER_NAME
The default parameter name:_csrf_token
.- See Also:
-
cookieName
The cookie name. -
headerName
The header name. -
parameterName
The paramerer name. -
maxAge
The max age of the CSRF token cookie. -
domain
The domain of the CSRF token cookie. -
path
The path of the CSRF token cookie. -
secure
protected final boolean secureFlag indicating whether the token cookie should be secured. -
httpOnly
protected final boolean httpOnlyFlag indicating whether the token cookie should be HTTP only.
-
-
Constructor Details
-
CSRFDoubleSubmitCookieInterceptor
protected CSRFDoubleSubmitCookieInterceptor(String cookieName, String headerName, String parameterName, Integer maxAge, String domain, String path, Boolean secure, Boolean httpOnly) Creates a CSRF double submit cookie interceptor.
- Parameters:
cookieName
- the cookie nameheaderName
- the header nameparameterName
- the parameter namemaxAge
- the cookie max agedomain
- the cookie domainpath
- the cookie pathsecure
- the cookie secure flaghttpOnly
- the cookie HTTP only flag
-
-
Method Details
-
getCookieName
Returns the CSRF token cookie name.
- Returns:
- the cookie name
-
getHeaderName
Returns the CSRF token header name.
- Returns:
- the header name
-
getParameterName
Returns the CSRF token parameter name.
- Returns:
- the parameter name
-
builder
Creates a CSRF double submit cookie interceptor builder.
- Returns:
- a CSRF double submit cookie interceptor builder
-
intercept
Description copied from interface:ExchangeInterceptor
Intercepts the exchange before the exchange handler is invoked.
- Specified by:
intercept
in interfaceExchangeInterceptor<A extends ExchangeContext,
B extends Exchange<A>> - Parameters:
exchange
- the server exchange to handle- Returns:
- a Mono emitting the exchange or an instrumented exchange to continue the exchange handling chain or an empty Mono to stop the exchange handling chain
-
generateToken
Generates the CSRF token.
It shall be collision free and hardly forgeable.
- Returns:
- a CSRF token
-