Skip to content

Commit f0a8173

Browse files
committed
Evaluate URI query parameter only if enabled in reactive stack
Issue spring-projectsgh-16038
1 parent 1782668 commit f0a8173

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/server/authentication/ServerBearerTokenAuthenticationConverter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -77,13 +77,13 @@ private String token(ServerHttpRequest request) {
7777
}
7878
return authorizationHeaderToken;
7979
}
80-
if (parameterToken != null && isParameterTokenSupportedForRequest(request)) {
81-
return parameterToken;
82-
}
83-
return null;
80+
return parameterToken;
8481
}
8582

86-
private static String resolveAccessTokenFromRequest(ServerHttpRequest request) {
83+
private String resolveAccessTokenFromRequest(ServerHttpRequest request) {
84+
if (!isParameterTokenSupportedForRequest(request)) {
85+
return null;
86+
}
8787
List<String> parameterTokens = request.getQueryParams().get("access_token");
8888
if (CollectionUtils.isEmpty(parameterTokens)) {
8989
return null;

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/server/authentication/ServerBearerTokenAuthenticationConverterTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -157,6 +157,7 @@ public void resolveWhenHeaderWithInvalidCharactersIsPresentAndNotSubscribedThenN
157157
@Test
158158
public void resolveWhenValidHeaderIsPresentTogetherWithQueryParameterThenAuthenticationExceptionIsThrown() {
159159
// @formatter:off
160+
this.converter.setAllowUriQueryParameter(true);
160161
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/")
161162
.queryParam("access_token", TEST_TOKEN)
162163
.header(HttpHeaders.AUTHORIZATION, "Bearer " + TEST_TOKEN);
@@ -205,6 +206,7 @@ public void resolveWhenQueryParameterIsPresentAndNotSupportedThenTokenIsNotResol
205206

206207
@Test
207208
void resolveWhenQueryParameterHasMultipleAccessTokensThenOAuth2AuthenticationException() {
209+
this.converter.setAllowUriQueryParameter(true);
208210
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/")
209211
.queryParam("access_token", TEST_TOKEN, TEST_TOKEN);
210212
assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> convertToToken(request))
@@ -217,6 +219,15 @@ void resolveWhenQueryParameterHasMultipleAccessTokensThenOAuth2AuthenticationExc
217219

218220
}
219221

222+
//gh-16038
223+
@Test
224+
void resoleWhenAllowUriQueryParameterIsFalseThenQueryParameterIsIgnored() {
225+
this.converter.setAllowUriQueryParameter(false);
226+
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/")
227+
.queryParam("access_token", TEST_TOKEN);
228+
assertThat(convertToToken(request)).isNull();
229+
}
230+
220231
private BearerTokenAuthenticationToken convertToToken(MockServerHttpRequest.BaseBuilder<?> request) {
221232
return convertToToken(request.build());
222233
}

0 commit comments

Comments
 (0)