Skip to content

Commit d26f40f

Browse files
mpalourdioeleftherias
authored andcommitted
DefaultRedirectStrategy should redirect to root if the context-relative URL does not contain the context-path.
1 parent 1c53a78 commit d26f40f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

web/src/main/java/org/springframework/security/web/DefaultRedirectStrategy.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ protected String calculateRedirectUrl(String contextPath, String url) {
7373
return url;
7474
}
7575

76+
if (!url.contains(contextPath)) {
77+
return "";
78+
}
79+
7680
// Calculate the relative URL from the fully qualified URL, minus the last
7781
// occurrence of the scheme and base context.
7882
url = url.substring(url.lastIndexOf("://") + 3); // strip off scheme

web/src/test/java/org/springframework/security/web/DefaultRedirectStrategyTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,19 @@ public void contextRelativeUrlWithMultipleSchemesInHostnameIsHandledCorrectly()
5656

5757
assertThat(response.getRedirectedUrl()).isEqualTo("remainder");
5858
}
59+
60+
@Test
61+
public void contextRelativeShouldRedirectToRootIfURLDoesNotContainContextPath()
62+
throws Exception {
63+
DefaultRedirectStrategy rds = new DefaultRedirectStrategy();
64+
rds.setContextRelative(true);
65+
MockHttpServletRequest request = new MockHttpServletRequest();
66+
request.setContextPath("/context");
67+
MockHttpServletResponse response = new MockHttpServletResponse();
68+
69+
rds.sendRedirect(request, response,
70+
"https://redirectme.somewhere.else");
71+
72+
assertThat(response.getRedirectedUrl()).isEqualTo("");
73+
}
5974
}

0 commit comments

Comments
 (0)