You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pa...@apache.org on 2020/08/07 20:43:31 UTC
[wicket] 02/04: WICKET-6786: reintroduced the configuration options
from CsrfPreventionRequestCycleListener
This is an automated email from the ASF dual-hosted git repository.
papegaaij pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git
commit c51372bd51e67276d86c8cf02336aa04e2f7a403
Author: Emond Papegaaij <em...@topicus.nl>
AuthorDate: Fri Aug 7 13:11:36 2020 +0200
WICKET-6786: reintroduced the configuration options from CsrfPreventionRequestCycleListener
---
.../http/DefaultResourceIsolationPolicy.java | 11 +-
.../http/FetchMetadataRequestCycleListener.java | 321 ++++++++++++++++++---
.../http/OriginBasedResourceIsolationPolicy.java | 10 +-
.../protocol/http/ResourceIsolationOutcome.java | 8 +
.../protocol/http/ResourceIsolationPolicy.java | 3 +-
.../FetchMetadataRequestCycleListenerTest.java | 28 +-
6 files changed, 325 insertions(+), 56 deletions(-)
diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/DefaultResourceIsolationPolicy.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/DefaultResourceIsolationPolicy.java
index b1d7cf7..e3de5a1 100644
--- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/DefaultResourceIsolationPolicy.java
+++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/DefaultResourceIsolationPolicy.java
@@ -34,12 +34,13 @@ public class DefaultResourceIsolationPolicy implements ResourceIsolationPolicy
{
@Override
- public boolean isRequestAllowed(HttpServletRequest request, IRequestablePage targetPage)
+ public ResourceIsolationOutcome isRequestAllowed(HttpServletRequest request,
+ IRequestablePage targetPage)
{
// request made by a legacy browser with no support for Fetch Metadata
if (!hasFetchMetadataHeaders(request))
{
- return true;
+ return ResourceIsolationOutcome.UNKNOWN;
}
String site = request.getHeader(SEC_FETCH_SITE_HEADER);
@@ -47,11 +48,13 @@ public class DefaultResourceIsolationPolicy implements ResourceIsolationPolicy
// Allow same-site and browser-initiated requests
if (SAME_ORIGIN.equals(site) || SAME_SITE.equals(site) || NONE.equals(site))
{
- return true;
+ return ResourceIsolationOutcome.ALLOWED;
}
// Allow simple top-level navigations except <object> and <embed>
- return isAllowedTopLevelNavigation(request);
+ return isAllowedTopLevelNavigation(request)
+ ? ResourceIsolationOutcome.ALLOWED
+ : ResourceIsolationOutcome.DISALLOWED;
}
private boolean isAllowedTopLevelNavigation(HttpServletRequest request)
diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/FetchMetadataRequestCycleListener.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/FetchMetadataRequestCycleListener.java
index ea9a8ef..d1500b9 100644
--- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/FetchMetadataRequestCycleListener.java
+++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/FetchMetadataRequestCycleListener.java
@@ -26,9 +26,11 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
-import java.util.Optional;
import java.util.Set;
+
import javax.servlet.http.HttpServletRequest;
+
+import org.apache.wicket.RestartResponseException;
import org.apache.wicket.core.request.handler.IPageRequestHandler;
import org.apache.wicket.core.request.handler.RenderPageRequestHandler;
import org.apache.wicket.request.IRequestHandler;
@@ -61,23 +63,151 @@ import org.slf4j.LoggerFactory;
*/
public class FetchMetadataRequestCycleListener implements IRequestCycleListener
{
-
private static final Logger log = LoggerFactory
.getLogger(FetchMetadataRequestCycleListener.class);
- public static final int ERROR_CODE = 403;
- public static final String ERROR_MESSAGE = "Forbidden";
+
+ public static final String ERROR_MESSAGE = "The request was blocked by a resource isolation policy";
public static final String VARY_HEADER_VALUE = SEC_FETCH_DEST_HEADER + ", "
+ SEC_FETCH_SITE_HEADER + ", " + SEC_FETCH_MODE_HEADER;
+ /**
+ * The action to perform when the outcome of the resource isolation policy is DISALLOWED or
+ * UNKNOWN.
+ */
+ public enum CsrfAction
+ {
+ /** Aborts the request and throws an exception when a CSRF request is detected. */
+ ABORT {
+ @Override
+ public String toString()
+ {
+ return "aborted";
+ }
+ },
+
+ /**
+ * Ignores the action of a CSRF request, and just renders the page it was targeted against.
+ */
+ SUPPRESS {
+ @Override
+ public String toString()
+ {
+ return "suppressed";
+ }
+ },
+
+ /** Detects a CSRF request, logs it and allows the request to continue. */
+ ALLOW {
+ @Override
+ public String toString()
+ {
+ return "allowed";
+ }
+ },
+ }
+
+ /**
+ * Action to perform when none resource isolation policies can determine the validity of the
+ * request.
+ */
+ private CsrfAction unknownOutcomeAction = CsrfAction.ABORT;
+
+ /**
+ * Action to perform when DISALLOWED is reported by a resource isolation policy.
+ */
+ private CsrfAction disallowedOutcomeAction = CsrfAction.ABORT;
+
+ /**
+ * The error code to report when the action to take for a CSRF request is
+ * {@link CsrfAction#ABORT}. Default {@code 403 FORBIDDEN}.
+ */
+ private int errorCode = javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
+
+ /**
+ * The error message to report when the action to take for a CSRF request is {@code ERROR}.
+ * Default {@code "The request was blocked by a resource isolation policy"}.
+ */
+ private String errorMessage = ERROR_MESSAGE;
+
+
private final Set<String> exemptedPaths = new HashSet<>();
private final List<ResourceIsolationPolicy> resourceIsolationPolicies = new ArrayList<>();
- public FetchMetadataRequestCycleListener(ResourceIsolationPolicy... additionalPolicies)
+ /**
+ * Create a new FetchMetadataRequestCycleListener with the given policies. If no policies are
+ * given, {@link DefaultResourceIsolationPolicy} and {@link OriginBasedResourceIsolationPolicy}
+ * will be used. The policies are checked in order. The first outcome that's not
+ * {@link ResourceIsolationOutcome#UNKNOWN} will be used.
+ *
+ * @param policies
+ * the policies to check requests against.
+ */
+ public FetchMetadataRequestCycleListener(ResourceIsolationPolicy... policies)
+ {
+ this.resourceIsolationPolicies.addAll(asList(policies));
+ if (policies.length == 0)
+ {
+ this.resourceIsolationPolicies.addAll(asList(new DefaultResourceIsolationPolicy(),
+ new OriginBasedResourceIsolationPolicy()));
+ }
+ }
+
+ /**
+ * Sets the action when none of the resource isolation policies can come to an outcome. Default
+ * {@code ABORT}.
+ *
+ * @param action
+ * the alternate action
+ *
+ * @return this (for chaining)
+ */
+ public FetchMetadataRequestCycleListener setUnknownOutcomeAction(CsrfAction action)
{
- this.resourceIsolationPolicies.addAll(
- asList(new DefaultResourceIsolationPolicy(), new OriginBasedResourceIsolationPolicy()));
+ this.unknownOutcomeAction = action;
+ return this;
+ }
- this.resourceIsolationPolicies.addAll(asList(additionalPolicies));
+ /**
+ * Sets the action when a request is disallowed by a resource isolation policy. Default is
+ * {@code ABORT}.
+ *
+ * @param action
+ * the alternate action
+ *
+ * @return this
+ */
+ public FetchMetadataRequestCycleListener setDisallowedOutcomeAction(CsrfAction action)
+ {
+ this.disallowedOutcomeAction = action;
+ return this;
+ }
+
+ /**
+ * Modifies the HTTP error code in the exception when a disallowed request is detected.
+ *
+ * @param errorCode
+ * the alternate HTTP error code, default {@code 403 FORBIDDEN}
+ *
+ * @return this
+ */
+ public FetchMetadataRequestCycleListener setErrorCode(int errorCode)
+ {
+ this.errorCode = errorCode;
+ return this;
+ }
+
+ /**
+ * Modifies the HTTP message in the exception when a disallowed request is detected.
+ *
+ * @param errorMessage
+ * the alternate message
+ *
+ * @return this
+ */
+ public FetchMetadataRequestCycleListener setErrorMessage(String errorMessage)
+ {
+ this.errorMessage = errorMessage;
+ return this;
}
public void addExemptedPaths(String... exemptions)
@@ -94,39 +224,121 @@ public class FetchMetadataRequestCycleListener implements IRequestCycleListener
log.debug("Processing request to: {}", containerRequest.getPathInfo());
}
+ /**
+ * Dynamic override for enabling/disabling the CSRF detection. Might be handy for specific
+ * tenants in a multi-tenant application. When false, the CSRF detection is not performed for
+ * the running request. Default {@code true}
+ *
+ * @return {@code true} when the CSRF checks need to be performed.
+ */
+ protected boolean isEnabled()
+ {
+ return true;
+ }
+
+ /**
+ * Override to limit whether the request to the specific page should be checked for a possible
+ * CSRF attack.
+ *
+ * @param targetedPage
+ * the page that is the target for the action
+ * @return {@code true} when the request to the page should be checked for CSRF issues.
+ */
+ protected boolean isChecked(IRequestablePage targetedPage)
+ {
+ return true;
+ }
+
+ /**
+ * Override to change the request handler types that are checked. Currently only action handlers
+ * (form submits, link clicks, AJAX events) are checked for a matching Origin HTTP header.
+ *
+ * @param handler
+ * the handler that is currently processing
+ * @return true when the Origin HTTP header should be checked for this {@code handler}
+ */
+ protected boolean isChecked(IRequestHandler handler)
+ {
+ return handler instanceof IPageRequestHandler
+ && !(handler instanceof RenderPageRequestHandler);
+ }
+
@Override
public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler)
{
- handler = unwrap(handler);
- IPageRequestHandler pageRequestHandler = getPageRequestHandler(handler);
- if (pageRequestHandler == null)
+ if (!isEnabled())
{
+ log.trace("CSRF listener is disabled, no checks performed");
return;
}
- IRequestablePage targetedPage = pageRequestHandler.getPage();
- HttpServletRequest containerRequest = (HttpServletRequest)cycle.getRequest()
- .getContainerRequest();
-
- String pathInfo = containerRequest.getPathInfo();
- if (exemptedPaths.contains(pathInfo))
+ handler = unwrap(handler);
+ if (isChecked(handler))
{
- if (log.isDebugEnabled())
+ IPageRequestHandler pageRequestHandler = (IPageRequestHandler)handler;
+ IRequestablePage targetedPage = pageRequestHandler.getPage();
+ HttpServletRequest containerRequest = (HttpServletRequest)cycle.getRequest()
+ .getContainerRequest();
+
+ if (!isChecked(targetedPage))
{
- log.debug("Allowing request to {} because it matches an exempted path",
- new Object[] { pathInfo });
+ if (log.isDebugEnabled())
+ {
+ log.debug("Targeted page {} was opted out of the CSRF origin checks, allowed",
+ targetedPage.getClass().getName());
+ }
+ return;
}
- return;
- }
- for (ResourceIsolationPolicy resourceIsolationPolicy : resourceIsolationPolicies)
- {
- if (!resourceIsolationPolicy.isRequestAllowed(containerRequest, targetedPage))
+ String pathInfo = containerRequest.getPathInfo();
+ if (exemptedPaths.contains(pathInfo))
{
- log.debug("Isolation policy {} has rejected a request to {}",
- Classes.simpleName(resourceIsolationPolicy.getClass()), pathInfo);
- throw new AbortWithHttpErrorCodeException(ERROR_CODE, ERROR_MESSAGE);
+ if (log.isDebugEnabled())
+ {
+ log.debug("Allowing request to {} because it matches an exempted path",
+ new Object[] { pathInfo });
+ }
+ return;
}
+
+ for (ResourceIsolationPolicy resourceIsolationPolicy : resourceIsolationPolicies)
+ {
+ ResourceIsolationOutcome outcome = resourceIsolationPolicy
+ .isRequestAllowed(containerRequest, targetedPage);
+ if (ResourceIsolationOutcome.DISALLOWED.equals(outcome))
+ {
+ log.debug("Isolation policy {} has rejected a request to {}",
+ Classes.simpleName(resourceIsolationPolicy.getClass()), pathInfo);
+ triggerAction(disallowedOutcomeAction, containerRequest, targetedPage);
+ }
+ else if (ResourceIsolationOutcome.ALLOWED.equals(outcome))
+ {
+ return;
+ }
+ }
+ triggerAction(unknownOutcomeAction, containerRequest, targetedPage);
+ }
+ else
+ {
+ if (log.isTraceEnabled())
+ log.trace("Resolved handler {} is not checked, no CSRF check performed",
+ handler.getClass().getName());
+ }
+ }
+
+ private void triggerAction(CsrfAction action, HttpServletRequest request, IRequestablePage page)
+ {
+ switch (action)
+ {
+ case ALLOW :
+ allowHandler(request, page);
+ break;
+ case SUPPRESS :
+ suppressHandler(request, page);
+ break;
+ case ABORT :
+ abortHandler(request, page);
+ break;
}
}
@@ -146,6 +358,52 @@ public class FetchMetadataRequestCycleListener implements IRequestCycleListener
}
}
+ /**
+ * Handles the case where the resource isolation policies resulted in
+ * {@link ResourceIsolationOutcome#UNKNOWN} or {@link ResourceIsolationOutcome#DISALLOWED} and
+ * the action was set to {#link {@link CsrfAction#ALLOW}.
+ *
+ * @param request
+ * the request
+ * @param page
+ * the page that is targeted with this request
+ */
+ protected void allowHandler(HttpServletRequest request, IRequestablePage page)
+ {
+ log.info("Possible CSRF attack, request URL: {}, action: allowed", request.getRequestURL());
+ }
+
+ /**
+ * Supresses the execution of the listener in the request because the outcome results in
+ * {@link CsrfAction#SUPPRESS}.
+ *
+ * @param request
+ * the request
+ * @param page
+ * the page that is targeted with this request
+ */
+ protected void suppressHandler(HttpServletRequest request, IRequestablePage page)
+ {
+ log.info("Possible CSRF attack, request URL: {}, action: suppressed",
+ request.getRequestURL());
+ throw new RestartResponseException(page);
+ }
+
+ /**
+ * Aborts the request because the outcome results in {@link CsrfAction#ABORT}.
+ *
+ * @param request
+ * the request
+ * @param page
+ * the page that is targeted with this request
+ */
+ protected void abortHandler(HttpServletRequest request, IRequestablePage page)
+ {
+ log.info("Possible CSRF attack, request URL: {}, action: aborted with error {} {}",
+ request.getRequestURL(), errorCode, errorMessage);
+ throw new AbortWithHttpErrorCodeException(errorCode, errorMessage);
+ }
+
private static IRequestHandler unwrap(IRequestHandler handler)
{
while (handler instanceof IRequestHandlerDelegate)
@@ -154,11 +412,4 @@ public class FetchMetadataRequestCycleListener implements IRequestCycleListener
}
return handler;
}
-
- private IPageRequestHandler getPageRequestHandler(IRequestHandler handler)
- {
- boolean isPageRequestHandler = handler instanceof IPageRequestHandler
- && !(handler instanceof RenderPageRequestHandler);
- return isPageRequestHandler ? (IPageRequestHandler)handler : null;
- }
}
diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/OriginBasedResourceIsolationPolicy.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/OriginBasedResourceIsolationPolicy.java
index bf07276..3df6bbf 100644
--- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/OriginBasedResourceIsolationPolicy.java
+++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/OriginBasedResourceIsolationPolicy.java
@@ -76,31 +76,31 @@ public class OriginBasedResourceIsolationPolicy implements ResourceIsolationPoli
* @return whether the request is allowed based on its origin
*/
@Override
- public boolean isRequestAllowed(HttpServletRequest request, IRequestablePage targetPage)
+ public ResourceIsolationOutcome isRequestAllowed(HttpServletRequest request, IRequestablePage targetPage)
{
String sourceUri = getSourceUri(request);
if (sourceUri == null || sourceUri.isEmpty())
{
log.debug("Source URI not present in request to {}", request.getPathInfo());
- return true;
+ return ResourceIsolationOutcome.UNKNOWN;
}
sourceUri = sourceUri.toLowerCase(Locale.ROOT);
// if the origin is a know and trusted origin, don't check any further but allow the request
if (isWhitelistedHost(sourceUri))
{
- return true;
+ return ResourceIsolationOutcome.ALLOWED;
}
// check if the origin HTTP header matches the request URI
if (!isLocalOrigin(request, sourceUri))
{
log.debug("Source URI conflicts with request origin");
- return false;
+ return ResourceIsolationOutcome.DISALLOWED;
}
- return true;
+ return ResourceIsolationOutcome.ALLOWED;
}
/**
diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/ResourceIsolationOutcome.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/ResourceIsolationOutcome.java
new file mode 100644
index 0000000..9b7dc38
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/ResourceIsolationOutcome.java
@@ -0,0 +1,8 @@
+package org.apache.wicket.protocol.http;
+
+public enum ResourceIsolationOutcome
+{
+ ALLOWED,
+ DISALLOWED,
+ UNKNOWN
+}
diff --git a/wicket-core/src/main/java/org/apache/wicket/protocol/http/ResourceIsolationPolicy.java b/wicket-core/src/main/java/org/apache/wicket/protocol/http/ResourceIsolationPolicy.java
index 1b68df1..6995cb2 100644
--- a/wicket-core/src/main/java/org/apache/wicket/protocol/http/ResourceIsolationPolicy.java
+++ b/wicket-core/src/main/java/org/apache/wicket/protocol/http/ResourceIsolationPolicy.java
@@ -51,5 +51,6 @@ public interface ResourceIsolationPolicy
String DEST_SCRIPT = "script";
String DEST_IMAGE = "image";
- boolean isRequestAllowed(HttpServletRequest request, IRequestablePage targetPage);
+ ResourceIsolationOutcome isRequestAllowed(HttpServletRequest request,
+ IRequestablePage targetPage);
}
diff --git a/wicket-core/src/test/java/org/apache/wicket/protocol/http/FetchMetadataRequestCycleListenerTest.java b/wicket-core/src/test/java/org/apache/wicket/protocol/http/FetchMetadataRequestCycleListenerTest.java
index f2c290c..d1a5960 100644
--- a/wicket-core/src/test/java/org/apache/wicket/protocol/http/FetchMetadataRequestCycleListenerTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/protocol/http/FetchMetadataRequestCycleListenerTest.java
@@ -47,6 +47,10 @@ public class FetchMetadataRequestCycleListenerTest extends WicketTestCase
{
WebApplication application = tester.getApplication();
+ if (this.fetchMetadataListener != null)
+ {
+ application.getRequestCycleListeners().remove(this.fetchMetadataListener);
+ }
this.fetchMetadataListener = fetchMetadataListener;
application.getRequestCycleListeners().add(fetchMetadataListener);
@@ -147,22 +151,24 @@ public class FetchMetadataRequestCycleListenerTest extends WicketTestCase
}
@Test
- void whenAtLeastOnePolicyRejectsRequest_thenRequestRejected()
+ void whenAtFirstNotUnkownRejectsRequest_thenRequestRejected()
{
- fetchMetadataListener = new FetchMetadataRequestCycleListener((request, page) -> true,
- (request, page) -> true, (request, page) -> false, (request, page) -> true);
-
- withCustomListener(fetchMetadataListener);
+ withCustomListener(new FetchMetadataRequestCycleListener(
+ (request, page) -> ResourceIsolationOutcome.UNKNOWN,
+ (request, page) -> ResourceIsolationOutcome.UNKNOWN,
+ (request, page) -> ResourceIsolationOutcome.DISALLOWED,
+ (request, page) -> ResourceIsolationOutcome.ALLOWED));
assertRequestAborted();
}
@Test
- void whenAllPoliciesAcceptRequest_thenRequestAccepted()
+ void whenFirstNotUnknownPolicieAcceptRequest_thenRequestAccepted()
{
- fetchMetadataListener = new FetchMetadataRequestCycleListener((request, page) -> true,
- (request, page) -> true, (request, page) -> true, (request, page) -> true);
-
- withCustomListener(fetchMetadataListener);
+ withCustomListener(new FetchMetadataRequestCycleListener(
+ (request, page) -> ResourceIsolationOutcome.UNKNOWN,
+ (request, page) -> ResourceIsolationOutcome.ALLOWED,
+ (request, page) -> ResourceIsolationOutcome.ALLOWED,
+ (request, page) -> ResourceIsolationOutcome.ALLOWED));
assertRequestAccepted();
}
@@ -181,7 +187,7 @@ public class FetchMetadataRequestCycleListenerTest extends WicketTestCase
{
tester.clickLink("link");
assertEquals(tester.getLastResponse().getStatus(),
- FetchMetadataRequestCycleListener.ERROR_CODE);
+ javax.servlet.http.HttpServletResponse.SC_FORBIDDEN);
assertEquals(tester.getLastResponse().getErrorMessage(),
FetchMetadataRequestCycleListener.ERROR_MESSAGE);
}