Skip to content

Commit 148b570

Browse files
committed
Remove redundant validation for redirect-uri
Fixes gh-7706
1 parent 752d5f2 commit 148b570

File tree

8 files changed

+4
-105
lines changed

8 files changed

+4
-105
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationExchangeValidator.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,6 @@
3030
*/
3131
final class OAuth2AuthorizationExchangeValidator {
3232
private static final String INVALID_STATE_PARAMETER_ERROR_CODE = "invalid_state_parameter";
33-
private static final String INVALID_REDIRECT_URI_PARAMETER_ERROR_CODE = "invalid_redirect_uri_parameter";
3433

3534
static void validate(OAuth2AuthorizationExchange authorizationExchange) {
3635
OAuth2AuthorizationRequest authorizationRequest = authorizationExchange.getAuthorizationRequest();
@@ -44,10 +43,5 @@ static void validate(OAuth2AuthorizationExchange authorizationExchange) {
4443
OAuth2Error oauth2Error = new OAuth2Error(INVALID_STATE_PARAMETER_ERROR_CODE);
4544
throw new OAuth2AuthorizationException(oauth2Error);
4645
}
47-
48-
if (!authorizationResponse.getRedirectUri().equals(authorizationRequest.getRedirectUri())) {
49-
OAuth2Error oauth2Error = new OAuth2Error(INVALID_REDIRECT_URI_PARAMETER_ERROR_CODE);
50-
throw new OAuth2AuthorizationException(oauth2Error);
51-
}
5246
}
5347
}

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/OidcAuthorizationCodeAuthenticationProvider.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
*/
7979
public class OidcAuthorizationCodeAuthenticationProvider implements AuthenticationProvider {
8080
private static final String INVALID_STATE_PARAMETER_ERROR_CODE = "invalid_state_parameter";
81-
private static final String INVALID_REDIRECT_URI_PARAMETER_ERROR_CODE = "invalid_redirect_uri_parameter";
8281
private static final String INVALID_ID_TOKEN_ERROR_CODE = "invalid_id_token";
8382
private static final String INVALID_NONCE_ERROR_CODE = "invalid_nonce";
8483
private final OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> accessTokenResponseClient;
@@ -132,11 +131,6 @@ public Authentication authenticate(Authentication authentication) throws Authent
132131
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
133132
}
134133

135-
if (!authorizationResponse.getRedirectUri().equals(authorizationRequest.getRedirectUri())) {
136-
OAuth2Error oauth2Error = new OAuth2Error(INVALID_REDIRECT_URI_PARAMETER_ERROR_CODE);
137-
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
138-
}
139-
140134
OAuth2AccessTokenResponse accessTokenResponse;
141135
try {
142136
accessTokenResponse = this.accessTokenResponseClient.getTokenResponse(

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/authentication/OidcAuthorizationCodeReactiveAuthenticationManager.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public class OidcAuthorizationCodeReactiveAuthenticationManager implements
8080
ReactiveAuthenticationManager {
8181

8282
private static final String INVALID_STATE_PARAMETER_ERROR_CODE = "invalid_state_parameter";
83-
private static final String INVALID_REDIRECT_URI_PARAMETER_ERROR_CODE = "invalid_redirect_uri_parameter";
8483
private static final String INVALID_ID_TOKEN_ERROR_CODE = "invalid_id_token";
8584
private static final String INVALID_NONCE_ERROR_CODE = "invalid_nonce";
8685

@@ -131,11 +130,6 @@ public Mono<Authentication> authenticate(Authentication authentication) {
131130
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
132131
}
133132

134-
if (!authorizationResponse.getRedirectUri().equals(authorizationRequest.getRedirectUri())) {
135-
OAuth2Error oauth2Error = new OAuth2Error(INVALID_REDIRECT_URI_PARAMETER_ERROR_CODE);
136-
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
137-
}
138-
139133
OAuth2AuthorizationCodeGrantRequest authzRequest = new OAuth2AuthorizationCodeGrantRequest(
140134
authorizationCodeAuthentication.getClientRegistration(),
141135
authorizationCodeAuthentication.getAuthorizationExchange());

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -98,19 +98,6 @@ public void authenticateWhenAuthorizationResponseStateNotEqualAuthorizationReque
9898
}).isInstanceOf(OAuth2AuthorizationException.class).hasMessageContaining("invalid_state_parameter");
9999
}
100100

101-
@Test
102-
public void authenticateWhenAuthorizationResponseRedirectUriNotEqualAuthorizationRequestRedirectUriThenThrowOAuth2AuthorizationException() {
103-
OAuth2AuthorizationResponse authorizationResponse = success().redirectUri("https://example2.com").build();
104-
OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange(
105-
this.authorizationRequest, authorizationResponse);
106-
107-
assertThatThrownBy(() -> {
108-
this.authenticationProvider.authenticate(
109-
new OAuth2AuthorizationCodeAuthenticationToken(
110-
this.clientRegistration, authorizationExchange));
111-
}).isInstanceOf(OAuth2AuthorizationException.class).hasMessageContaining("invalid_redirect_uri_parameter");
112-
}
113-
114101
@Test
115102
public void authenticateWhenAuthorizationSuccessResponseThenExchangedForAccessToken() {
116103
OAuth2AccessTokenResponse accessTokenResponse = accessTokenResponse().refreshToken("refresh").build();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeReactiveAuthenticationManagerTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,13 +81,6 @@ public void authenticateWhenStateNotEqualThenOAuth2AuthorizationException() {
8181
.isInstanceOf(OAuth2AuthorizationException.class);
8282
}
8383

84-
@Test
85-
public void authenticateWhenRedirectUriNotEqualThenOAuth2AuthorizationException() {
86-
this.authorizationRequest.redirectUri("https://example.org/notequal");
87-
assertThatCode(() -> authenticate())
88-
.isInstanceOf(OAuth2AuthorizationException.class);
89-
}
90-
9184
@Test
9285
public void authenticateWhenValidThenSuccess() {
9386
when(this.accessTokenResponseClient.getTokenResponse(any())).thenReturn(Mono.just(this.tokenResponse.build()));

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2LoginAuthenticationProviderTests.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -151,20 +151,6 @@ public void authenticateWhenAuthorizationResponseStateNotEqualAuthorizationReque
151151
new OAuth2LoginAuthenticationToken(this.clientRegistration, authorizationExchange));
152152
}
153153

154-
@Test
155-
public void authenticateWhenAuthorizationResponseRedirectUriNotEqualAuthorizationRequestRedirectUriThenThrowOAuth2AuthenticationException() {
156-
this.exception.expect(OAuth2AuthenticationException.class);
157-
this.exception.expectMessage(containsString("invalid_redirect_uri_parameter"));
158-
159-
OAuth2AuthorizationResponse authorizationResponse =
160-
success().redirectUri("https://example2.com").build();
161-
OAuth2AuthorizationExchange authorizationExchange =
162-
new OAuth2AuthorizationExchange(this.authorizationRequest, authorizationResponse);
163-
164-
this.authenticationProvider.authenticate(
165-
new OAuth2LoginAuthenticationToken(this.clientRegistration, authorizationExchange));
166-
}
167-
168154
@Test
169155
public void authenticateWhenLoginSuccessThenReturnAuthentication() {
170156
OAuth2AccessTokenResponse accessTokenResponse = this.accessTokenSuccessResponse();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/authentication/OidcAuthorizationCodeAuthenticationProviderTests.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,6 @@ public void authenticateWhenAuthorizationResponseStateNotEqualAuthorizationReque
186186
new OAuth2LoginAuthenticationToken(this.clientRegistration, authorizationExchange));
187187
}
188188

189-
@Test
190-
public void authenticateWhenAuthorizationResponseRedirectUriNotEqualAuthorizationRequestRedirectUriThenThrowOAuth2AuthenticationException() {
191-
this.exception.expect(OAuth2AuthenticationException.class);
192-
this.exception.expectMessage(containsString("invalid_redirect_uri_parameter"));
193-
194-
OAuth2AuthorizationResponse authorizationResponse = success().redirectUri("https://example2.com").build();
195-
OAuth2AuthorizationExchange authorizationExchange =
196-
new OAuth2AuthorizationExchange(this.authorizationRequest, authorizationResponse);
197-
198-
this.authenticationProvider.authenticate(
199-
new OAuth2LoginAuthenticationToken(this.clientRegistration, authorizationExchange));
200-
}
201-
202189
@Test
203190
public void authenticateWhenTokenResponseDoesNotContainIdTokenThenThrowOAuth2AuthenticationException() {
204191
this.exception.expect(OAuth2AuthenticationException.class);

samples/boot/oauth2login/src/integration-test/java/org/springframework/security/samples/OAuth2LoginApplicationTests.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -255,42 +255,6 @@ public void requestAuthorizationCodeGrantWhenInvalidStateParamThenDisplayLoginPa
255255
assertThat(errorElement.asText()).contains("authorization_request_not_found");
256256
}
257257

258-
@Test
259-
public void requestAuthorizationCodeGrantWhenInvalidRedirectUriThenDisplayLoginPageWithError() throws Exception {
260-
HtmlPage page = this.webClient.getPage("/");
261-
URL loginPageUrl = page.getBaseURL();
262-
URL loginErrorPageUrl = new URL(loginPageUrl.toString() + "?error");
263-
264-
ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("google");
265-
266-
HtmlAnchor clientAnchorElement = this.getClientAnchorElement(page, clientRegistration);
267-
assertThat(clientAnchorElement).isNotNull();
268-
269-
WebResponse response = this.followLinkDisableRedirects(clientAnchorElement);
270-
271-
UriComponents authorizeRequestUriComponents = UriComponentsBuilder.fromUri(
272-
URI.create(response.getResponseHeaderValue("Location"))).build();
273-
274-
Map<String, String> params = authorizeRequestUriComponents.getQueryParams().toSingleValueMap();
275-
String code = "auth-code";
276-
String state = URLDecoder.decode(params.get(OAuth2ParameterNames.STATE), "UTF-8");
277-
String redirectUri = URLDecoder.decode(params.get(OAuth2ParameterNames.REDIRECT_URI), "UTF-8");
278-
redirectUri += "-invalid";
279-
280-
String authorizationResponseUri =
281-
UriComponentsBuilder.fromHttpUrl(redirectUri)
282-
.queryParam(OAuth2ParameterNames.CODE, code)
283-
.queryParam(OAuth2ParameterNames.STATE, state)
284-
.build().encode().toUriString();
285-
286-
page = this.webClient.getPage(new URL(authorizationResponseUri));
287-
assertThat(page.getBaseURL()).isEqualTo(loginErrorPageUrl);
288-
289-
HtmlElement errorElement = page.getBody().getFirstByXPath("div");
290-
assertThat(errorElement).isNotNull();
291-
assertThat(errorElement.asText()).contains("invalid_redirect_uri_parameter");
292-
}
293-
294258
private void assertLoginPage(HtmlPage page) {
295259
assertThat(page.getTitleText()).isEqualTo("Please sign in");
296260

0 commit comments

Comments
 (0)