Skip to content

Commit 539a11d

Browse files
committed
Encode postLogoutRedirectUri query params
Closes gh-11379
1 parent e97c5a5 commit 539a11d

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/web/server/logout/OidcClientInitiatedServerLogoutSuccessHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ public Mono<Void> onLogoutSuccess(WebFilterExchange exchange, Authentication aut
8585
return Mono.empty();
8686
}
8787
String idToken = idToken(authentication);
88-
URI postLogoutRedirectUri = postLogoutRedirectUri(exchange.getExchange().getRequest());
88+
String postLogoutRedirectUri = postLogoutRedirectUri(exchange.getExchange().getRequest());
8989
return Mono.just(endpointUri(endSessionEndpoint, idToken, postLogoutRedirectUri));
9090
})
9191
.switchIfEmpty(
9292
this.serverLogoutSuccessHandler.onLogoutSuccess(exchange, authentication).then(Mono.empty())
9393
)
94-
.flatMap((endpointUri) -> this.redirectStrategy.sendRedirect(exchange.getExchange(), endpointUri));
94+
.flatMap((endpointUri) -> this.redirectStrategy.sendRedirect(exchange.getExchange(), URI.create(endpointUri)));
9595
// @formatter:on
9696
}
9797

@@ -106,20 +106,20 @@ private URI endSessionEndpoint(ClientRegistration clientRegistration) {
106106
return null;
107107
}
108108

109-
private URI endpointUri(URI endSessionEndpoint, String idToken, URI postLogoutRedirectUri) {
109+
private String endpointUri(URI endSessionEndpoint, String idToken, String postLogoutRedirectUri) {
110110
UriComponentsBuilder builder = UriComponentsBuilder.fromUri(endSessionEndpoint);
111111
builder.queryParam("id_token_hint", idToken);
112112
if (postLogoutRedirectUri != null) {
113113
builder.queryParam("post_logout_redirect_uri", postLogoutRedirectUri);
114114
}
115-
return builder.encode(StandardCharsets.UTF_8).build().toUri();
115+
return builder.encode(StandardCharsets.UTF_8).build().toUriString();
116116
}
117117

118118
private String idToken(Authentication authentication) {
119119
return ((OidcUser) authentication.getPrincipal()).getIdToken().getTokenValue();
120120
}
121121

122-
private URI postLogoutRedirectUri(ServerHttpRequest request) {
122+
private String postLogoutRedirectUri(ServerHttpRequest request) {
123123
if (this.postLogoutRedirectUri == null) {
124124
return null;
125125
}
@@ -131,7 +131,7 @@ private URI postLogoutRedirectUri(ServerHttpRequest request) {
131131
.build();
132132
return UriComponentsBuilder.fromUriString(this.postLogoutRedirectUri)
133133
.buildAndExpand(Collections.singletonMap("baseUrl", uriComponents.toUriString()))
134-
.toUri();
134+
.toUriString();
135135
// @formatter:on
136136
}
137137

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/web/server/logout/OidcClientInitiatedServerLogoutSuccessHandlerTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirect(
150150
"https://endpoint?" + "id_token_hint=id-token&" + "post_logout_redirect_uri=https://rp.example.org");
151151
}
152152

153+
// gh-11379
154+
@Test
155+
public void logoutWhenUsingPostLogoutRedirectUriWithQueryParametersThenBuildsItForRedirect() {
156+
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
157+
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
158+
given(this.exchange.getPrincipal()).willReturn(Mono.just(token));
159+
this.handler.setPostLogoutRedirectUri("https://rp.example.org/context?forwardUrl=secured%3Fparam%3Dtrue");
160+
WebFilterExchange f = new WebFilterExchange(this.exchange, this.chain);
161+
this.handler.onLogoutSuccess(f, token).block();
162+
assertThat(redirectedUrl(this.exchange)).isEqualTo("https://endpoint?id_token_hint=id-token&"
163+
+ "post_logout_redirect_uri=https://rp.example.org/context?forwardUrl%3Dsecured%253Fparam%253Dtrue");
164+
}
165+
153166
@Test
154167
public void setPostLogoutRedirectUriWhenGivenNullThenThrowsException() {
155168
assertThatIllegalArgumentException().isThrownBy(() -> this.handler.setPostLogoutRedirectUri((URI) null));

0 commit comments

Comments
 (0)