diff --git a/web/src/main/java/org/springframework/security/web/FilterInvocation.java b/web/src/main/java/org/springframework/security/web/FilterInvocation.java index 5e762118d8..e85ce6fe39 100644 --- a/web/src/main/java/org/springframework/security/web/FilterInvocation.java +++ b/web/src/main/java/org/springframework/security/web/FilterInvocation.java @@ -228,10 +228,15 @@ class DummyRequest extends HttpServletRequestWrapper { public void setQueryString(String queryString) { this.queryString = queryString; } + + @Override + public String getServerName() { + return null; + } } final class UnsupportedOperationExceptionInvocationHandler implements InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { throw new UnsupportedOperationException(method + " is not supported"); } -} \ No newline at end of file +} diff --git a/web/src/main/java/org/springframework/security/web/firewall/StrictHttpFirewall.java b/web/src/main/java/org/springframework/security/web/firewall/StrictHttpFirewall.java index f48f7153ab..2884c9c9a8 100644 --- a/web/src/main/java/org/springframework/security/web/firewall/StrictHttpFirewall.java +++ b/web/src/main/java/org/springframework/security/web/firewall/StrictHttpFirewall.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.function.Predicate; /** *

@@ -66,10 +67,15 @@ import java.util.Set; * Rejects URLs that contain a URL encoded percent. See * {@link #setAllowUrlEncodedPercent(boolean)} * + *

  • + * Rejects hosts that are not allowed. See + * {@link #setAllowedHostnames(Predicate)} + *
  • * * * @see DefaultHttpFirewall * @author Rob Winch + * @author Eddú Meléndez * @since 4.2.4 */ public class StrictHttpFirewall implements HttpFirewall { @@ -98,6 +104,8 @@ public class StrictHttpFirewall implements HttpFirewall { private Set allowedHttpMethods = createDefaultAllowedHttpMethods(); + private Predicate allowedHostnames = hostname -> true; + public StrictHttpFirewall() { urlBlacklistsAddAll(FORBIDDEN_SEMICOLON); urlBlacklistsAddAll(FORBIDDEN_FORWARDSLASH); @@ -297,6 +305,13 @@ public class StrictHttpFirewall implements HttpFirewall { } } + public void setAllowedHostnames(Predicate allowedHostnames) { + if (allowedHostnames == null) { + throw new IllegalArgumentException("allowedHostnames cannot be null"); + } + this.allowedHostnames = allowedHostnames; + } + private void urlBlacklistsAddAll(Collection values) { this.encodedUrlBlacklist.addAll(values); this.decodedUrlBlacklist.addAll(values); @@ -311,6 +326,7 @@ public class StrictHttpFirewall implements HttpFirewall { public FirewalledRequest getFirewalledRequest(HttpServletRequest request) throws RequestRejectedException { rejectForbiddenHttpMethod(request); rejectedBlacklistedUrls(request); + rejectedUntrustedHosts(request); if (!isNormalized(request)) { throw new RequestRejectedException("The request was rejected because the URL was not normalized."); @@ -352,6 +368,13 @@ public class StrictHttpFirewall implements HttpFirewall { } } + private void rejectedUntrustedHosts(HttpServletRequest request) { + String serverName = request.getServerName(); + if (serverName != null && !this.allowedHostnames.test(serverName)) { + throw new RequestRejectedException("The request was rejected because the domain " + serverName + " is untrusted."); + } + } + @Override public HttpServletResponse getFirewalledResponse(HttpServletResponse response) { return new FirewalledResponse(response); diff --git a/web/src/test/java/org/springframework/security/web/firewall/StrictHttpFirewallTests.java b/web/src/test/java/org/springframework/security/web/firewall/StrictHttpFirewallTests.java index 5edb67b0d3..01d97d6493 100644 --- a/web/src/test/java/org/springframework/security/web/firewall/StrictHttpFirewallTests.java +++ b/web/src/test/java/org/springframework/security/web/firewall/StrictHttpFirewallTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import org.springframework.mock.web.MockHttpServletRequest; /** * @author Rob Winch + * @author Eddú Meléndez */ public class StrictHttpFirewallTests { public String[] unnormalizedPaths = { "/..", "/./path/", "/path/path/.", "/path/path//.", "./path/../path//.", @@ -524,4 +525,20 @@ public class StrictHttpFirewallTests { this.firewall.getDecodedUrlBlacklist().removeAll(Arrays.asList("//")); assertThatCode(() -> this.firewall.getFirewalledRequest(request)).doesNotThrowAnyException(); } + + @Test + public void getFirewalledRequestWhenTrustedDomainThenNoException() { + this.request.addHeader("Host", "example.org"); + this.firewall.setAllowedHostnames(hostname -> hostname.equals("example.org")); + + assertThatCode(() -> this.firewall.getFirewalledRequest(this.request)).doesNotThrowAnyException(); + } + + @Test(expected = RequestRejectedException.class) + public void getFirewalledRequestWhenUntrustedDomainThenException() { + this.request.addHeader("Host", "example.org"); + this.firewall.setAllowedHostnames(hostname -> hostname.equals("myexample.org")); + + this.firewall.getFirewalledRequest(this.request); + } }