|
|
|
|
@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Nested;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
import org.springframework.web.testfixture.servlet.MockFilterChain;
|
|
|
|
|
@ -46,290 +47,35 @@ import static org.mockito.Mockito.mock;
|
|
|
|
|
*/
|
|
|
|
|
public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
private static final String FORWARDED = "forwarded";
|
|
|
|
|
|
|
|
|
|
private static final String X_FORWARDED_PROTO = "x-forwarded-proto"; // SPR-14372 (case insensitive)
|
|
|
|
|
|
|
|
|
|
private static final String X_FORWARDED_HOST = "x-forwarded-host";
|
|
|
|
|
|
|
|
|
|
private static final String X_FORWARDED_PORT = "x-forwarded-port";
|
|
|
|
|
|
|
|
|
|
private static final String X_FORWARDED_SSL = "x-forwarded-ssl";
|
|
|
|
|
|
|
|
|
|
private static final String X_FORWARDED_PREFIX = "x-forwarded-prefix";
|
|
|
|
|
|
|
|
|
|
private static final String X_FORWARDED_SSL = "x-forwarded-ssl";
|
|
|
|
|
private static final String X_FORWARDED_FOR = "x-forwarded-for";
|
|
|
|
|
private static final String FORWARDED = "forwarded";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final ForwardedHeaderFilter filter = new ForwardedHeaderFilter();
|
|
|
|
|
|
|
|
|
|
private MockHttpServletRequest request;
|
|
|
|
|
|
|
|
|
|
private MockFilterChain filterChain;
|
|
|
|
|
private final MockFilterChain filterChain = new MockFilterChain(new HttpServlet() {});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
|
public void setup() throws Exception {
|
|
|
|
|
public void setup() {
|
|
|
|
|
this.request = new MockHttpServletRequest();
|
|
|
|
|
this.request.setScheme("http");
|
|
|
|
|
this.request.setServerName("localhost");
|
|
|
|
|
this.request.setServerPort(80);
|
|
|
|
|
this.filterChain = new MockFilterChain(new HttpServlet() {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void forwardedForEmpty() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_FOR, "");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(MockHttpServletRequest.DEFAULT_REMOTE_ADDR);
|
|
|
|
|
assertThat(actual.getRemoteHost()).isEqualTo(MockHttpServletRequest.DEFAULT_REMOTE_HOST);
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void forwardedForSingleIdentifier() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_FOR, "203.0.113.195");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void forwardedForMultipleIdentifiers() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_FOR, "203.0.113.195, 70.41.3.18, 150.172.238.178");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForIpV4Identifier() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "for=203.0.113.195");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForIpV6Identifier() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "for=\"[2001:db8:cafe::17]\"");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("2001:db8:cafe::17");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForUnknownIdentifier() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "for=unknown");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("unknown");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForObfuscatedIdentifier() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "for=_abc-12_d.e");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("_abc-12_d.e");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForIpV4IdentifierWithPort() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "for=\"203.0.113.195:47011\"");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(47011);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForIpV6IdentifierWithPort() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "For=\"[2001:db8:cafe::17]:47011\"");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("2001:db8:cafe::17");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(47011);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForUnknownIdentifierWithPort() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "for=\"unknown:47011\"");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("unknown");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(47011);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForObfuscatedIdentifierWithPort() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "for=\"_abc-12_d.e:47011\"");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("_abc-12_d.e");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(47011);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void standardizedForwardedForMultipleIdentifiers() throws Exception {
|
|
|
|
|
this.request.addHeader(FORWARDED, "for=203.0.113.195;proto=http, for=\"[2001:db8:cafe::17]\", for=unknown");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathEmpty() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "");
|
|
|
|
|
assertThat(filterAndGetContextPath()).isEqualTo("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathWithTrailingSlash() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/foo/bar/");
|
|
|
|
|
assertThat(filterAndGetContextPath()).isEqualTo("/foo/bar");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathWithTrailingSlashes() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/foo/bar/baz///");
|
|
|
|
|
assertThat(filterAndGetContextPath()).isEqualTo("/foo/bar/baz");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathWithForwardedPrefix() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/prefix");
|
|
|
|
|
this.request.setContextPath("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
String actual = filterAndGetContextPath();
|
|
|
|
|
assertThat(actual).isEqualTo("/prefix");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathWithForwardedPrefixTrailingSlash() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
|
|
|
|
|
this.request.setContextPath("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
String actual = filterAndGetContextPath();
|
|
|
|
|
assertThat(actual).isEqualTo("/prefix");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String filterAndGetContextPath() throws ServletException, IOException {
|
|
|
|
|
return filterAndGetWrappedRequest().getContextPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpServletRequest filterAndGetWrappedRequest() throws ServletException, IOException {
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
this.filter.doFilterInternal(this.request, response, this.filterChain);
|
|
|
|
|
return (HttpServletRequest) this.filterChain.getRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathPreserveEncoding() throws Exception {
|
|
|
|
|
this.request.setContextPath("/app%20");
|
|
|
|
|
this.request.setRequestURI("/app%20/path/");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("/app%20");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/app%20/path/");
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/app%20/path/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUri() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/");
|
|
|
|
|
this.request.setContextPath("/app");
|
|
|
|
|
this.request.setRequestURI("/app/path");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriWithTrailingSlash() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/");
|
|
|
|
|
this.request.setContextPath("/app");
|
|
|
|
|
this.request.setRequestURI("/app/path/");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/path/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriPreserveEncoding() throws Exception {
|
|
|
|
|
this.request.setContextPath("/app");
|
|
|
|
|
this.request.setRequestURI("/app/path%20with%20spaces/");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("/app");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/app/path%20with%20spaces/");
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/app/path%20with%20spaces/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriEqualsContextPath() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/");
|
|
|
|
|
this.request.setContextPath("/app");
|
|
|
|
|
this.request.setRequestURI("/app");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriRootUrl() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/");
|
|
|
|
|
this.request.setContextPath("/app");
|
|
|
|
|
this.request.setRequestURI("/app/");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriPreserveSemicolonContent() throws Exception {
|
|
|
|
|
this.request.setContextPath("");
|
|
|
|
|
this.request.setRequestURI("/path;a=b/with/semicolon");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/path;a=b/with/semicolon");
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/path;a=b/with/semicolon");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void caseInsensitiveForwardedPrefix() throws Exception {
|
|
|
|
|
this.request = new MockHttpServletRequest() {
|
|
|
|
|
|
|
|
|
|
@Override // SPR-14372: make it case-sensitive
|
|
|
|
|
public String getHeader(String header) {
|
|
|
|
|
Enumeration<String> names = getHeaderNames();
|
|
|
|
|
while (names.hasMoreElements()) {
|
|
|
|
|
String name = names.nextElement();
|
|
|
|
|
if (name.equals(header)) {
|
|
|
|
|
return super.getHeader(header);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/prefix");
|
|
|
|
|
this.request.setRequestURI("/path");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/prefix/path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
@ -339,6 +85,7 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
testShouldFilter(X_FORWARDED_PORT);
|
|
|
|
|
testShouldFilter(X_FORWARDED_PROTO);
|
|
|
|
|
testShouldFilter(X_FORWARDED_SSL);
|
|
|
|
|
testShouldFilter(X_FORWARDED_PREFIX);
|
|
|
|
|
testShouldFilter(X_FORWARDED_FOR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -365,6 +112,7 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain);
|
|
|
|
|
HttpServletRequest actual = (HttpServletRequest) this.filterChain.getRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual).isNotNull();
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("https://84.198.58.199/mvc-showcase");
|
|
|
|
|
assertThat(actual.getScheme()).isEqualTo("https");
|
|
|
|
|
assertThat(actual.getServerName()).isEqualTo("84.198.58.199");
|
|
|
|
|
@ -393,6 +141,7 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain);
|
|
|
|
|
HttpServletRequest actual = (HttpServletRequest) this.filterChain.getRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual).isNotNull();
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/mvc-showcase");
|
|
|
|
|
assertThat(actual.getScheme()).isEqualTo("http");
|
|
|
|
|
assertThat(actual.getServerName()).isEqualTo("localhost");
|
|
|
|
|
@ -420,6 +169,7 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain);
|
|
|
|
|
HttpServletRequest actual = (HttpServletRequest) this.filterChain.getRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual).isNotNull();
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("https://84.198.58.199/mvc-showcase");
|
|
|
|
|
assertThat(actual.getScheme()).isEqualTo("https");
|
|
|
|
|
assertThat(actual.getServerName()).isEqualTo("84.198.58.199");
|
|
|
|
|
@ -454,10 +204,153 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("https://www.mycompany.example/bar");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
|
class ForwardedPrefix {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathEmpty() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "");
|
|
|
|
|
assertThat(filterAndGetContextPath()).isEqualTo("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathWithTrailingSlash() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/foo/bar/");
|
|
|
|
|
assertThat(filterAndGetContextPath()).isEqualTo("/foo/bar");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathWithTrailingSlashes() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/foo/bar/baz///");
|
|
|
|
|
assertThat(filterAndGetContextPath()).isEqualTo("/foo/bar/baz");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathWithForwardedPrefix() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/prefix");
|
|
|
|
|
request.setContextPath("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
String actual = filterAndGetContextPath();
|
|
|
|
|
assertThat(actual).isEqualTo("/prefix");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathWithForwardedPrefixTrailingSlash() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
|
|
|
|
|
request.setContextPath("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
String actual = filterAndGetContextPath();
|
|
|
|
|
assertThat(actual).isEqualTo("/prefix");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String filterAndGetContextPath() throws ServletException, IOException {
|
|
|
|
|
return filterAndGetWrappedRequest().getContextPath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void contextPathPreserveEncoding() throws Exception {
|
|
|
|
|
request.setContextPath("/app%20");
|
|
|
|
|
request.setRequestURI("/app%20/path/");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("/app%20");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/app%20/path/");
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/app%20/path/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUri() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/");
|
|
|
|
|
request.setContextPath("/app");
|
|
|
|
|
request.setRequestURI("/app/path");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriWithTrailingSlash() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/");
|
|
|
|
|
request.setContextPath("/app");
|
|
|
|
|
request.setRequestURI("/app/path/");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/path/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriPreserveEncoding() throws Exception {
|
|
|
|
|
request.setContextPath("/app");
|
|
|
|
|
request.setRequestURI("/app/path%20with%20spaces/");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("/app");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/app/path%20with%20spaces/");
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/app/path%20with%20spaces/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriEqualsContextPath() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/");
|
|
|
|
|
request.setContextPath("/app");
|
|
|
|
|
request.setRequestURI("/app");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriRootUrl() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/");
|
|
|
|
|
request.setContextPath("/app");
|
|
|
|
|
request.setRequestURI("/app/");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriPreserveSemicolonContent() throws Exception {
|
|
|
|
|
request.setContextPath("");
|
|
|
|
|
request.setRequestURI("/path;a=b/with/semicolon");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getContextPath()).isEqualTo("");
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/path;a=b/with/semicolon");
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/path;a=b/with/semicolon");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void caseInsensitiveForwardedPrefix() throws Exception {
|
|
|
|
|
request = new MockHttpServletRequest() {
|
|
|
|
|
|
|
|
|
|
@Override // SPR-14372: make it case-sensitive
|
|
|
|
|
public String getHeader(String header) {
|
|
|
|
|
Enumeration<String> names = getHeaderNames();
|
|
|
|
|
while (names.hasMoreElements()) {
|
|
|
|
|
String name = names.nextElement();
|
|
|
|
|
if (name.equals(header)) {
|
|
|
|
|
return super.getHeader(header);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/prefix");
|
|
|
|
|
request.setRequestURI("/path");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRequestURI()).isEqualTo("/prefix/path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriWithForwardedPrefix() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/prefix");
|
|
|
|
|
this.request.setRequestURI("/mvc-showcase");
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/prefix");
|
|
|
|
|
request.setRequestURI("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/prefix/mvc-showcase");
|
|
|
|
|
@ -465,8 +358,8 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestUriWithForwardedPrefixTrailingSlash() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
|
|
|
|
|
this.request.setRequestURI("/mvc-showcase");
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
|
|
|
|
|
request.setRequestURI("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/prefix/mvc-showcase");
|
|
|
|
|
@ -474,8 +367,8 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldConcatenatePrefixes() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/first,/second");
|
|
|
|
|
this.request.setRequestURI("/mvc-showcase");
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/first,/second");
|
|
|
|
|
request.setRequestURI("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/first/second/mvc-showcase");
|
|
|
|
|
@ -483,8 +376,8 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldConcatenatePrefixesWithTrailingSlashes() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/first/,/second//");
|
|
|
|
|
this.request.setRequestURI("/mvc-showcase");
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/first/,/second//");
|
|
|
|
|
request.setRequestURI("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/first/second/mvc-showcase");
|
|
|
|
|
@ -492,19 +385,100 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void requestURLNewStringBuffer() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
|
|
|
|
|
this.request.setRequestURI("/mvc-showcase");
|
|
|
|
|
request.addHeader(X_FORWARDED_PREFIX, "/prefix/");
|
|
|
|
|
request.setRequestURI("/mvc-showcase");
|
|
|
|
|
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
actual.getRequestURL().append("?key=value");
|
|
|
|
|
assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/prefix/mvc-showcase");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
|
class ForwardedFor {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void xForwardedForEmpty() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_FOR, "");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(MockHttpServletRequest.DEFAULT_REMOTE_ADDR);
|
|
|
|
|
assertThat(actual.getRemoteHost()).isEqualTo(MockHttpServletRequest.DEFAULT_REMOTE_HOST);
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void xForwardedForSingleIdentifier() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_FOR, "203.0.113.195");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void xForwardedForMultipleIdentifiers() throws Exception {
|
|
|
|
|
request.addHeader(X_FORWARDED_FOR, "203.0.113.195, 70.41.3.18, 150.172.238.178");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void forwardedForIpV4Identifier() throws Exception {
|
|
|
|
|
request.addHeader(FORWARDED, "for=203.0.113.195");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void forwardedForIpV6Identifier() throws Exception {
|
|
|
|
|
request.addHeader(FORWARDED, "for=\"[2001:db8:cafe::17]\"");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("2001:db8:cafe:0:0:0:0:17");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void forwardedForIpV4IdentifierWithPort() throws Exception {
|
|
|
|
|
request.addHeader(FORWARDED, "for=\"203.0.113.195:47011\"");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(47011);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void forwardedForIpV6IdentifierWithPort() throws Exception {
|
|
|
|
|
request.addHeader(FORWARDED, "For=\"[2001:db8:cafe::17]:47011\"");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("2001:db8:cafe:0:0:0:0:17");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(47011);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void forwardedForMultipleIdentifiers() throws Exception {
|
|
|
|
|
request.addHeader(FORWARDED, "for=203.0.113.195;proto=http, for=\"[2001:db8:cafe::17]\", for=unknown");
|
|
|
|
|
HttpServletRequest actual = filterAndGetWrappedRequest();
|
|
|
|
|
|
|
|
|
|
assertThat(actual.getRemoteAddr()).isEqualTo(actual.getRemoteHost()).isEqualTo("203.0.113.195");
|
|
|
|
|
assertThat(actual.getRemotePort()).isEqualTo(MockHttpServletRequest.DEFAULT_SERVER_PORT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
|
class SendRedirect {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithAbsolutePath() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
|
|
|
|
|
String redirectedUrl = sendRedirect("/foo/bar");
|
|
|
|
|
assertThat(redirectedUrl).isEqualTo("https://example.com/foo/bar");
|
|
|
|
|
@ -512,10 +486,10 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test // SPR-16506
|
|
|
|
|
public void sendRedirectWithAbsolutePathQueryParamAndFragment() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
this.request.setQueryString("oldqp=1");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.setQueryString("oldqp=1");
|
|
|
|
|
|
|
|
|
|
String redirectedUrl = sendRedirect("/foo/bar?newqp=2#fragment");
|
|
|
|
|
assertThat(redirectedUrl).isEqualTo("https://example.com/foo/bar?newqp=2#fragment");
|
|
|
|
|
@ -523,10 +497,10 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithContextPath() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
this.request.setContextPath("/context");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.setContextPath("/context");
|
|
|
|
|
|
|
|
|
|
String redirectedUrl = sendRedirect("/context/foo/bar");
|
|
|
|
|
assertThat(redirectedUrl).isEqualTo("https://example.com/context/foo/bar");
|
|
|
|
|
@ -534,10 +508,10 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithRelativePath() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
this.request.setRequestURI("/parent/");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.setRequestURI("/parent/");
|
|
|
|
|
|
|
|
|
|
String redirectedUrl = sendRedirect("foo/bar");
|
|
|
|
|
assertThat(redirectedUrl).isEqualTo("https://example.com/parent/foo/bar");
|
|
|
|
|
@ -545,10 +519,10 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithFileInPathAndRelativeRedirect() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
this.request.setRequestURI("/context/a");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.setRequestURI("/context/a");
|
|
|
|
|
|
|
|
|
|
String redirectedUrl = sendRedirect("foo/bar");
|
|
|
|
|
assertThat(redirectedUrl).isEqualTo("https://example.com/context/foo/bar");
|
|
|
|
|
@ -556,10 +530,10 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithRelativePathIgnoresFile() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
this.request.setRequestURI("/parent");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.setRequestURI("/parent");
|
|
|
|
|
|
|
|
|
|
String redirectedUrl = sendRedirect("foo/bar");
|
|
|
|
|
assertThat(redirectedUrl).isEqualTo("https://example.com/foo/bar");
|
|
|
|
|
@ -567,9 +541,9 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithLocationDotDotPath() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
|
|
|
|
|
String redirectedUrl = sendRedirect("parent/../foo/bar");
|
|
|
|
|
assertThat(redirectedUrl).isEqualTo("https://example.com/foo/bar");
|
|
|
|
|
@ -577,9 +551,9 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithLocationHasScheme() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
|
|
|
|
|
String location = "http://company.example/foo/bar";
|
|
|
|
|
String redirectedUrl = sendRedirect(location);
|
|
|
|
|
@ -588,9 +562,9 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithLocationSlashSlash() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
|
|
|
|
|
String location = "//other.info/foo/bar";
|
|
|
|
|
String redirectedUrl = sendRedirect(location);
|
|
|
|
|
@ -599,9 +573,9 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWithLocationSlashSlashParentDotDot() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
|
|
|
|
|
String location = "//other.info/parent/../foo/bar";
|
|
|
|
|
String redirectedUrl = sendRedirect(location);
|
|
|
|
|
@ -622,10 +596,10 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWhenRequestOnlyAndXForwardedThenUsesRelativeRedirects() throws Exception {
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
this.request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
this.filter.setRelativeRedirects(true);
|
|
|
|
|
request.addHeader(X_FORWARDED_PROTO, "https");
|
|
|
|
|
request.addHeader(X_FORWARDED_HOST, "example.com");
|
|
|
|
|
request.addHeader(X_FORWARDED_PORT, "443");
|
|
|
|
|
filter.setRelativeRedirects(true);
|
|
|
|
|
String location = sendRedirect("/a");
|
|
|
|
|
|
|
|
|
|
assertThat(location).isEqualTo("/a");
|
|
|
|
|
@ -633,14 +607,14 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void sendRedirectWhenRequestOnlyAndNoXForwardedThenUsesRelativeRedirects() throws Exception {
|
|
|
|
|
this.filter.setRelativeRedirects(true);
|
|
|
|
|
filter.setRelativeRedirects(true);
|
|
|
|
|
String location = sendRedirect("/a");
|
|
|
|
|
|
|
|
|
|
assertThat(location).isEqualTo("/a");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String sendRedirect(final String location) throws ServletException, IOException {
|
|
|
|
|
Filter filter = new OncePerRequestFilter() {
|
|
|
|
|
Filter redirectFilter = new OncePerRequestFilter() {
|
|
|
|
|
@Override
|
|
|
|
|
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
|
|
|
|
|
FilterChain chain) throws IOException {
|
|
|
|
|
@ -648,12 +622,17 @@ public class ForwardedHeaderFilterTests {
|
|
|
|
|
res.sendRedirect(location);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
FilterChain filterChain = new MockFilterChain(mock(HttpServlet.class), this.filter, filter);
|
|
|
|
|
FilterChain filterChain = new MockFilterChain(mock(HttpServlet.class), filter, redirectFilter);
|
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
|
|
|
|
|
return response.getRedirectedUrl();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpServletRequest filterAndGetWrappedRequest() throws ServletException, IOException {
|
|
|
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
this.filter.doFilterInternal(this.request, response, this.filterChain);
|
|
|
|
|
return (HttpServletRequest) this.filterChain.getRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|