Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
if (savedRequest == null) {
String relayState = UaaStringUtils.getCleanedUserControlString(request.getParameter(Saml2ParameterNames.RELAY_STATE), UaaStringUtils.EMPTY_STRING);
if (UaaStringUtils.hasText(relayState) && UaaUrlUtils.isUrl(relayState)) {
log.debug("Redirecting to relayState URI: {}", relayState);
this.getRedirectStrategy().sendRedirect(request, response, relayState);
java.util.List<String> samlRelayStateWhitelist = java.util.Optional.ofNullable(
org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder.get().getConfig().getLinks().getLogout().getWhitelist()
).orElse(java.util.Collections.emptyList());
String fallbackUrl = this.getDefaultTargetUrl();
String redirectUri = UaaUrlUtils.findMatchingRedirectUri(samlRelayStateWhitelist, relayState, fallbackUrl);
log.debug("Redirecting after SAML login. requestedRelayState='{}' redirectUri='{}'", relayState, redirectUri);
this.getRedirectStrategy().sendRedirect(request, response, redirectUri);
} else {
super.onAuthenticationSuccess(request, response, authentication);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,37 @@ void invalidFormRedirectIsNotReturned() {
}

@Test
void onAuthenticationSuccess_noSavedRequest_hasRelayStateUrl() throws Exception {
void onAuthenticationSuccess_noSavedRequest_hasRelayStateUrl_notWhitelisted() throws Exception {
String redirectUri = "https://test.com/test2";
request.setParameter(Saml2ParameterNames.RELAY_STATE, redirectUri);

var response = new MockHttpServletResponse();
var authentication = mock(Authentication.class);
handler.onAuthenticationSuccess(request, response, authentication);

assertThat(response.getRedirectedUrl()).isEqualTo(redirectUri);
assertThat(response.getRedirectedUrl()).isEqualTo("/");
}

@Test
void onAuthenticationSuccess_noSavedRequest_hasRelayStateUrl_whitelisted() throws Exception {
String redirectUri = "https://test.com/test2";
request.setParameter(Saml2ParameterNames.RELAY_STATE, redirectUri);

org.cloudfoundry.identity.uaa.zone.IdentityZone zone = org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder.get();
java.util.List<String> originalWhitelist = zone.getConfig().getLinks().getLogout().getWhitelist();
zone.getConfig().getLinks().getLogout().setWhitelist(java.util.List.of(redirectUri));
org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder.set(zone);

try {
var response = new MockHttpServletResponse();
var authentication = mock(Authentication.class);
handler.onAuthenticationSuccess(request, response, authentication);

assertThat(response.getRedirectedUrl()).isEqualTo(redirectUri);
} finally {
zone.getConfig().getLinks().getLogout().setWhitelist(originalWhitelist);
org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder.set(zone);
}
}
Comment thread
joemahady-comm marked this conversation as resolved.

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,45 @@ void onAuthenticationSuccess_withSavedRequest_redirects_to_saved_url(ZoneRequest

assertThat(response.getRedirectedUrl()).isEqualTo(savedRedirectUrl);
}

@ParameterizedTest
@EnumSource(ZoneRequestPathMode.class)
void onAuthenticationSuccess_noSavedRequest_hasRelayStateUrl_notWhitelisted(ZoneRequestPathMode mode) throws Exception {
mode.setZone();
mode.applyRequestPath(request, "/login.do");
String redirectUri = "https://test.com/test2";
request.setParameter(org.springframework.security.saml2.core.Saml2ParameterNames.RELAY_STATE, redirectUri);

MockHttpServletResponse response = new MockHttpServletResponse();
Authentication authentication = mock(Authentication.class);

handler.onAuthenticationSuccess(request, response, authentication);

String expected = mode.redirectPrefix().isEmpty() ? "/" : mode.redirectPrefix() + "/";
assertThat(response.getRedirectedUrl()).isEqualTo(expected);
}

@ParameterizedTest
@EnumSource(ZoneRequestPathMode.class)
void onAuthenticationSuccess_noSavedRequest_hasRelayStateUrl_whitelisted(ZoneRequestPathMode mode) throws Exception {
mode.setZone();
mode.applyRequestPath(request, "/login.do");
String redirectUri = "https://test.com/test2";
request.setParameter(org.springframework.security.saml2.core.Saml2ParameterNames.RELAY_STATE, redirectUri);

org.cloudfoundry.identity.uaa.zone.IdentityZone zone = org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder.get();
java.util.List<String> originalWhitelist = zone.getConfig().getLinks().getLogout().getWhitelist();
zone.getConfig().getLinks().getLogout().setWhitelist(java.util.List.of(redirectUri));

try {
MockHttpServletResponse response = new MockHttpServletResponse();
Authentication authentication = mock(Authentication.class);

handler.onAuthenticationSuccess(request, response, authentication);

assertThat(response.getRedirectedUrl()).isEqualTo(redirectUri);
} finally {
zone.getConfig().getLinks().getLogout().setWhitelist(originalWhitelist);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ void relayStateRedirectFromIdpInitiatedLogin() {
webDriver.findElement(By.xpath(samlServerConfig.getLoginPromptXpathExpr()));
sendCredentials(testAccounts.getUserName(), testAccounts.getPassword());
Page.assertThatUrlEventuallySatisfies(webDriver,
assertUrl -> assertUrl.startsWith("https://www.google.com"));
assertUrl -> assertUrl.startsWith(baseUrl));

webDriver.get("%s/logout.do".formatted(baseUrl));
}
Expand Down Expand Up @@ -1228,7 +1228,7 @@ void backportFrom77RelayTest() {
sendCredentials(testAccounts.getUserName(), "koala");

Page.assertThatUrlEventuallySatisfies(webDriver,
assertUrl -> assertUrl.startsWith("https://www.google.com"));
assertUrl -> assertUrl.startsWith(zoneUrl));
webDriver.get(baseUrl + "/logout.do");
webDriver.get(zoneUrl + "/logout.do");
}
Expand Down
Loading