public class CsrfPreventionRequestCycleListener extends AbstractRequestCycleListener implements IRequestCycleListener
Origin HTTP header for cross
domain requests. By default only checks requests that try to perform an action on a component,
such as a form submit, or link click.
You can enable this CSRF prevention filter by adding it to the request cycle listeners in your
application's init method:
@Override
protected void init()
{
// ...
getRequestCycleListeners().add(new CsrfPreventionRequestCycleListener());
// ...
}
A missing Origin HTTP header is (by default) handled as if it were a good request and
accepted. You can configure the specific action to a
different value, suppressing or aborting the request when the Origin HTTP header is
missing.
When the Origin HTTP header is present and has the value null it is considered to
be from a "privacy-sensitive" context and will trigger the conflicting origin action. You can
customize what happens in those actions by overriding the respective onXXXX methods.
When the Origin HTTP header is present but doesn't match the requested URL this listener
will by default throw a HTTP error ( 400 BAD REQUEST) and abort the request. You can
configure this specific action.
When you want to accept certain cross domain request from a range of hosts, you can
whitelist those domains.
You can enable or disable this listener by overriding isEnabled().
You can customize whether a particular page should be
checked for CSRF requests. For example you can skip checking pages that have a
@NoCsrfCheck annotation, or only those pages that extend your base secure page class. For
example:
@Override
protected boolean isChecked(IRequestablePage requestedPage)
{
return requestedPage.getPage() instanceof SecurePage;
}
You can also tweak the request handlers that are checked. The CSRF prevention request cycle
listener checks only action handlers, not render handlers. Override
isChecked(IRequestHandler) to customize this behavior.
You can override the default actions that are performed by overriding the event handlers for them:
onWhitelisted(HttpServletRequest, String, IRequestablePage) when an origin was
whitelistedonMatchingOrigin(HttpServletRequest, String, IRequestablePage) when an origin was
matchingonAborted(HttpServletRequest, String, IRequestablePage) when an origin was in
conflict and the request should be abortedonAllowed(HttpServletRequest, String, IRequestablePage) when an origin was in
conflict and the request should be allowedonSuppressed(HttpServletRequest, String, IRequestablePage) when an origin was in
conflict and the request should be suppressed| Modifier and Type | Class and Description |
|---|---|
static class |
CsrfPreventionRequestCycleListener.CsrfAction
The action to perform when a missing or conflicting Origin header is detected.
|
| Constructor and Description |
|---|
CsrfPreventionRequestCycleListener() |
| Modifier and Type | Method and Description |
|---|---|
CsrfPreventionRequestCycleListener |
addAcceptedOrigin(String acceptedOrigin)
Adds an origin (host name/domain name) to the white list.
|
protected boolean |
isChecked(IRequestablePage targetedPage)
Override to limit whether the request to the specific page should be checked for a possible
CSRF attack.
|
protected boolean |
isChecked(IRequestHandler handler)
Override to change the request handler types that are checked.
|
protected boolean |
isEnabled()
Dynamic override for enabling/disabling the CSRF detection.
|
protected void |
onAborted(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
Override this method to customize the case where an Origin HTTP header was not present or did
not match the request origin, and the corresponding action (
noOriginAction or
conflictingOriginAction) is set to ABORTED. |
protected void |
onAllowed(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
Override this method to customize the case where an Origin HTTP header was not present or did
not match the request origin, and the corresponding action (
noOriginAction or
conflictingOriginAction) is set to ALLOW. |
void |
onBeginRequest(RequestCycle cycle)
Called when the request cycle object is beginning its response
|
protected void |
onMatchingOrigin(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
Called when the origin HTTP header matched the request.
|
void |
onRequestHandlerResolved(RequestCycle cycle,
IRequestHandler handler)
Called when an
IRequestHandler is resolved and will be executed. |
protected void |
onSuppressed(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
Override this method to customize the case where an Origin HTTP header was not present or did
not match the request origin, and the corresponding action (
noOriginAction or
conflictingOriginAction) is set to SUPPRESSED. |
protected void |
onWhitelisted(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
Called when the origin was available in the whitelist.
|
CsrfPreventionRequestCycleListener |
setConflictingOriginAction(CsrfPreventionRequestCycleListener.CsrfAction action)
Sets the action when a conflicting Origin header is detected.
|
CsrfPreventionRequestCycleListener |
setErrorCode(int errorCode)
Modifies the HTTP error code in the exception when a conflicting Origin header is detected.
|
CsrfPreventionRequestCycleListener |
setErrorMessage(String errorMessage)
Modifies the HTTP message in the exception when a conflicting Origin header is detected.
|
CsrfPreventionRequestCycleListener |
setNoOriginAction(CsrfPreventionRequestCycleListener.CsrfAction action)
Sets the action when no Origin header is present in the request.
|
onDetach, onEndRequest, onException, onExceptionRequestHandlerResolved, onRequestHandlerExecuted, onRequestHandlerScheduled, onUrlMappedclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitonDetach, onEndRequest, onException, onExceptionRequestHandlerResolved, onRequestHandlerExecuted, onRequestHandlerScheduled, onUrlMappedpublic CsrfPreventionRequestCycleListener setNoOriginAction(CsrfPreventionRequestCycleListener.CsrfAction action)
ALLOW.action - the alternate actionpublic CsrfPreventionRequestCycleListener setConflictingOriginAction(CsrfPreventionRequestCycleListener.CsrfAction action)
ERROR.action - the alternate actionpublic CsrfPreventionRequestCycleListener setErrorCode(int errorCode)
errorCode - the alternate HTTP error code, default 400 BAD REQUESTpublic CsrfPreventionRequestCycleListener setErrorMessage(String errorMessage)
errorMessage - the alternate messagepublic CsrfPreventionRequestCycleListener addAcceptedOrigin(String acceptedOrigin)
example.com is in the white list, this allows requests from (i.e. with an
Origin: header containing) example.com and blabla.example.com but
rejects requests from blablaexample.com and example2.com.acceptedOrigin - the acceptable originpublic void onBeginRequest(RequestCycle cycle)
IRequestCycleListeneronBeginRequest in interface IRequestCycleListeneronBeginRequest in class AbstractRequestCycleListenerprotected boolean isEnabled()
truetrue when the CSRF checks need to be performed.protected boolean isChecked(IRequestablePage targetedPage)
targetedPage - the page that is the target for the actiontrue when the request to the page should be checked for CSRF issues.protected boolean isChecked(IRequestHandler handler)
handler - the handler that is currently processinghandlerpublic void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler)
IRequestCycleListenerIRequestHandler is resolved and will be executed.onRequestHandlerResolved in interface IRequestCycleListeneronRequestHandlerResolved in class AbstractRequestCycleListenerprotected void onWhitelisted(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
request - the requestorigin - the contents of the Origin HTTP headerpage - the page that is targeted with this requestprotected void onMatchingOrigin(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
request - the requestorigin - the contents of the Origin HTTP headerpage - the page that is targeted with this requestprotected void onAllowed(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
noOriginAction or
conflictingOriginAction) is set to ALLOW.request - the requestorigin - the contents of the Origin HTTP header, may be null or emptypage - the page that is targeted with this requestprotected void onSuppressed(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
noOriginAction or
conflictingOriginAction) is set to SUPPRESSED.request - the requestorigin - the contents of the Origin HTTP header, may be null or emptypage - the page that is targeted with this requestprotected void onAborted(javax.servlet.http.HttpServletRequest request,
String origin,
IRequestablePage page)
noOriginAction or
conflictingOriginAction) is set to ABORTED.request - the requestorigin - the contents of the Origin HTTP header, may be null or emptypage - the page that is targeted with this requestCopyright © 2006–2016 Apache Software Foundation. All rights reserved.