Skip to content

Commit 80fd041

Browse files
committed
Evaluate parameter access token only if enabled in servlet stack
Issue gh-16038
1 parent f0a8173 commit 80fd041

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -53,7 +53,7 @@ public final class DefaultBearerTokenResolver implements BearerTokenResolver {
5353
@Override
5454
public String resolve(final HttpServletRequest request) {
5555
final String authorizationHeaderToken = resolveFromAuthorizationHeader(request);
56-
final String parameterToken = isParameterTokenSupportedForRequest(request)
56+
final String parameterToken = isParameterTokenEnabledForRequest(request)
5757
? resolveFromRequestParameters(request) : null;
5858
if (authorizationHeaderToken != null) {
5959
if (parameterToken != null) {
@@ -63,10 +63,7 @@ public String resolve(final HttpServletRequest request) {
6363
}
6464
return authorizationHeaderToken;
6565
}
66-
if (parameterToken != null && isParameterTokenEnabledForRequest(request)) {
67-
return parameterToken;
68-
}
69-
return null;
66+
return parameterToken;
7067
}
7168

7269
/**
@@ -129,10 +126,6 @@ private static String resolveFromRequestParameters(HttpServletRequest request) {
129126
throw new OAuth2AuthenticationException(error);
130127
}
131128

132-
private boolean isParameterTokenSupportedForRequest(final HttpServletRequest request) {
133-
return isFormEncodedRequest(request) || isGetRequest(request);
134-
}
135-
136129
private static boolean isGetRequest(HttpServletRequest request) {
137130
return HttpMethod.GET.name().equals(request.getMethod());
138131
}

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

Lines changed: 28 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.
@@ -107,6 +107,7 @@ public void resolveWhenHeaderWithInvalidCharactersIsPresentThenAuthenticationExc
107107

108108
@Test
109109
public void resolveWhenValidHeaderIsPresentTogetherWithFormParameterThenAuthenticationExceptionIsThrown() {
110+
this.resolver.setAllowFormEncodedBodyParameter(true);
110111
MockHttpServletRequest request = new MockHttpServletRequest();
111112
request.addHeader("Authorization", "Bearer " + TEST_TOKEN);
112113
request.setMethod("POST");
@@ -118,6 +119,7 @@ public void resolveWhenValidHeaderIsPresentTogetherWithFormParameterThenAuthenti
118119

119120
@Test
120121
public void resolveWhenValidHeaderIsPresentTogetherWithQueryParameterThenAuthenticationExceptionIsThrown() {
122+
this.resolver.setAllowUriQueryParameter(true);
121123
MockHttpServletRequest request = new MockHttpServletRequest();
122124
request.addHeader("Authorization", "Bearer " + TEST_TOKEN);
123125
request.setMethod("GET");
@@ -130,6 +132,7 @@ public void resolveWhenValidHeaderIsPresentTogetherWithQueryParameterThenAuthent
130132
// gh-10326
131133
@Test
132134
public void resolveWhenRequestContainsTwoAccessTokenQueryParametersThenAuthenticationExceptionIsThrown() {
135+
this.resolver.setAllowUriQueryParameter(true);
133136
MockHttpServletRequest request = new MockHttpServletRequest();
134137
request.setMethod("GET");
135138
request.addParameter("access_token", "token1", "token2");
@@ -140,6 +143,7 @@ public void resolveWhenRequestContainsTwoAccessTokenQueryParametersThenAuthentic
140143
// gh-10326
141144
@Test
142145
public void resolveWhenRequestContainsTwoAccessTokenFormParametersThenAuthenticationExceptionIsThrown() {
146+
this.resolver.setAllowFormEncodedBodyParameter(true);
143147
MockHttpServletRequest request = new MockHttpServletRequest();
144148
request.setMethod("POST");
145149
request.setContentType("application/x-www-form-urlencoded");
@@ -232,6 +236,7 @@ public void resolveWhenPostAndFormParameterIsSupportedAndQueryParameterIsPresent
232236

233237
@Test
234238
public void resolveWhenFormParameterIsPresentAndNotSupportedThenTokenIsNotResolved() {
239+
this.resolver.setAllowFormEncodedBodyParameter(false);
235240
MockHttpServletRequest request = new MockHttpServletRequest();
236241
request.setMethod("POST");
237242
request.setContentType("application/x-www-form-urlencoded");
@@ -258,4 +263,26 @@ public void resolveWhenQueryParameterIsPresentAndNotSupportedThenTokenIsNotResol
258263
assertThat(this.resolver.resolve(request)).isNull();
259264
}
260265

266+
// gh-16038
267+
@Test
268+
void resolveWhenRequestContainsTwoAccessTokenFormParametersAndSupportIsDisabledThenTokenIsNotResolved() {
269+
this.resolver.setAllowFormEncodedBodyParameter(false);
270+
MockHttpServletRequest request = new MockHttpServletRequest();
271+
request.setMethod("POST");
272+
request.setContentType("application/x-www-form-urlencoded");
273+
request.addParameter("access_token", "token1", "token2");
274+
assertThat(this.resolver.resolve(request)).isNull();
275+
}
276+
277+
// gh-16038
278+
@Test
279+
void resolveWhenRequestContainsTwoAccessTokenQueryParameterAndSupportIsDisabledThenTokenIsNotResolved() {
280+
this.resolver.setAllowUriQueryParameter(false);
281+
MockHttpServletRequest request = new MockHttpServletRequest();
282+
request.setMethod("GET");
283+
request.setQueryString("access_token=" + TEST_TOKEN);
284+
request.addParameter("access_token", "token1", "token2");
285+
assertThat(this.resolver.resolve(request)).isNull();
286+
}
287+
261288
}

0 commit comments

Comments
 (0)