Skip to content

Commit 1ed0b3a

Browse files
committed
Merge branch '1.1.x'
2 parents 88d604f + ae016cd commit 1ed0b3a

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/ClientSecretPostAuthenticationConverter.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 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.
@@ -23,6 +23,7 @@
2323
import org.springframework.security.core.Authentication;
2424
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
2525
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
26+
import org.springframework.security.oauth2.core.OAuth2Error;
2627
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
2728
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
2829
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken;
@@ -69,6 +70,17 @@ public Authentication convert(HttpServletRequest request) {
6970
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
7071
}
7172

73+
String queryString = request.getQueryString();
74+
if (StringUtils.hasText(queryString) &&
75+
(queryString.contains(OAuth2ParameterNames.CLIENT_ID) ||
76+
queryString.contains(OAuth2ParameterNames.CLIENT_SECRET))) {
77+
OAuth2Error error = new OAuth2Error(
78+
OAuth2ErrorCodes.INVALID_REQUEST,
79+
"Client credentials MUST NOT be included in the request URI.",
80+
null);
81+
throw new OAuth2AuthenticationException(error);
82+
}
83+
7284
Map<String, Object> additionalParameters = OAuth2EndpointUtils.getParametersIfMatchesAuthorizationCodeGrantRequest(request,
7385
OAuth2ParameterNames.CLIENT_ID,
7486
OAuth2ParameterNames.CLIENT_SECRET);

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2ClientCredentialsGrantTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.springframework.security.oauth2.core.AuthorizationGrantType;
6262
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
6363
import org.springframework.security.oauth2.core.OAuth2AccessToken;
64+
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
6465
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
6566
import org.springframework.security.oauth2.jose.TestJwks;
6667
import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService;
@@ -101,6 +102,7 @@
101102
import org.springframework.security.web.util.matcher.RequestMatcher;
102103
import org.springframework.test.web.servlet.MockMvc;
103104
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
105+
import org.springframework.web.util.UriComponentsBuilder;
104106

105107
import static org.assertj.core.api.Assertions.assertThat;
106108
import static org.mockito.ArgumentMatchers.any;
@@ -234,6 +236,37 @@ public void requestWhenTokenRequestPostsClientCredentialsThenTokenResponse() thr
234236
verify(jwtCustomizer).customize(any());
235237
}
236238

239+
// gh-1378
240+
@Test
241+
public void requestWhenTokenRequestWithClientCredentialsInQueryParamThenInvalidRequest() throws Exception {
242+
this.spring.register(AuthorizationServerConfiguration.class).autowire();
243+
244+
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build();
245+
this.registeredClientRepository.save(registeredClient);
246+
247+
String tokenEndpointUri = UriComponentsBuilder.fromUriString(DEFAULT_TOKEN_ENDPOINT_URI)
248+
.queryParam(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId())
249+
.toUriString();
250+
251+
this.mvc.perform(post(tokenEndpointUri)
252+
.param(OAuth2ParameterNames.CLIENT_SECRET, registeredClient.getClientSecret())
253+
.param(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.CLIENT_CREDENTIALS.getValue())
254+
.param(OAuth2ParameterNames.SCOPE, "scope1 scope2"))
255+
.andExpect(status().isBadRequest())
256+
.andExpect(jsonPath("$.error").value(OAuth2ErrorCodes.INVALID_REQUEST));
257+
258+
tokenEndpointUri = UriComponentsBuilder.fromUriString(DEFAULT_TOKEN_ENDPOINT_URI)
259+
.queryParam(OAuth2ParameterNames.CLIENT_SECRET, registeredClient.getClientSecret())
260+
.toUriString();
261+
262+
this.mvc.perform(post(tokenEndpointUri)
263+
.param(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId())
264+
.param(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.CLIENT_CREDENTIALS.getValue())
265+
.param(OAuth2ParameterNames.SCOPE, "scope1 scope2"))
266+
.andExpect(status().isBadRequest())
267+
.andExpect(jsonPath("$.error").value(OAuth2ErrorCodes.INVALID_REQUEST));
268+
}
269+
237270
@Test
238271
public void requestWhenTokenRequestPostsClientCredentialsAndRequiresUpgradingThenClientSecretUpgraded() throws Exception {
239272
this.spring.register(AuthorizationServerConfigurationCustomPasswordEncoder.class).autowire();

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/web/authentication/ClientSecretPostAuthenticationConverterTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,31 @@ public void convertWhenMultipleClientSecretsThenInvalidRequestError() {
7979
.isEqualTo(OAuth2ErrorCodes.INVALID_REQUEST);
8080
}
8181

82+
// gh-1378
83+
@Test
84+
public void convertWhenClientCredentialsInQueryParamThenInvalidRequestError() {
85+
MockHttpServletRequest request = new MockHttpServletRequest();
86+
request.addParameter(OAuth2ParameterNames.CLIENT_ID, "client-1");
87+
request.addParameter(OAuth2ParameterNames.CLIENT_SECRET, "client-secret");
88+
request.setQueryString("client_id=client-1");
89+
assertThatThrownBy(() -> this.converter.convert(request))
90+
.isInstanceOf(OAuth2AuthenticationException.class)
91+
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
92+
.satisfies(error -> {
93+
assertThat(error.getErrorCode()).isEqualTo(OAuth2ErrorCodes.INVALID_REQUEST);
94+
assertThat(error.getDescription()).isEqualTo("Client credentials MUST NOT be included in the request URI.");
95+
});
96+
97+
request.setQueryString("client_secret=client-secret");
98+
assertThatThrownBy(() -> this.converter.convert(request))
99+
.isInstanceOf(OAuth2AuthenticationException.class)
100+
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
101+
.satisfies(error -> {
102+
assertThat(error.getErrorCode()).isEqualTo(OAuth2ErrorCodes.INVALID_REQUEST);
103+
assertThat(error.getDescription()).isEqualTo("Client credentials MUST NOT be included in the request URI.");
104+
});
105+
}
106+
82107
@Test
83108
public void convertWhenPostWithValidCredentialsThenReturnClientAuthenticationToken() {
84109
MockHttpServletRequest request = new MockHttpServletRequest();

0 commit comments

Comments
 (0)