Skip to content

Commit 5e684fe

Browse files
committed
Authorization Consent request state parameter is validated
Closes gh-503
1 parent 086a3b0 commit 5e684fe

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,20 @@ public Authentication convert(HttpServletRequest request) {
118118
}
119119
}
120120

121-
// state (RECOMMENDED)
121+
// state
122+
// RECOMMENDED for Authorization Request
122123
String state = parameters.getFirst(OAuth2ParameterNames.STATE);
123-
if (StringUtils.hasText(state) &&
124-
parameters.get(OAuth2ParameterNames.STATE).size() != 1) {
125-
throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.STATE);
124+
if (authorizationRequest) {
125+
if (StringUtils.hasText(state) &&
126+
parameters.get(OAuth2ParameterNames.STATE).size() != 1) {
127+
throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.STATE);
128+
}
129+
} else {
130+
// REQUIRED for Authorization Consent Request
131+
if (!StringUtils.hasText(state) ||
132+
parameters.get(OAuth2ParameterNames.STATE).size() != 1) {
133+
throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.STATE);
134+
}
126135
}
127136

128137
// code_challenge (REQUIRED for public clients) - RFC 7636 (PKCE)

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,24 @@ public void doFilterWhenAuthorizationRequestMultipleStateThenInvalidRequestError
222222
request -> request.addParameter(OAuth2ParameterNames.STATE, "state2"));
223223
}
224224

225+
@Test
226+
public void doFilterWhenAuthorizationConsentRequestMissingStateThenInvalidRequestError() throws Exception {
227+
doFilterWhenAuthorizationConsentRequestInvalidParameterThenError(
228+
TestRegisteredClients.registeredClient().build(),
229+
OAuth2ParameterNames.STATE,
230+
OAuth2ErrorCodes.INVALID_REQUEST,
231+
request -> request.removeParameter(OAuth2ParameterNames.STATE));
232+
}
233+
234+
@Test
235+
public void doFilterWhenAuthorizationConsentRequestMultipleStateThenInvalidRequestError() throws Exception {
236+
doFilterWhenAuthorizationConsentRequestInvalidParameterThenError(
237+
TestRegisteredClients.registeredClient().build(),
238+
OAuth2ParameterNames.STATE,
239+
OAuth2ErrorCodes.INVALID_REQUEST,
240+
request -> request.addParameter(OAuth2ParameterNames.STATE, "state2"));
241+
}
242+
225243
@Test
226244
public void doFilterWhenAuthorizationRequestMultipleCodeChallengeThenInvalidRequestError() throws Exception {
227245
doFilterWhenAuthorizationRequestInvalidParameterThenError(
@@ -534,6 +552,13 @@ private void doFilterWhenAuthorizationRequestInvalidParameterThenError(Registere
534552
parameterName, errorCode, requestConsumer);
535553
}
536554

555+
private void doFilterWhenAuthorizationConsentRequestInvalidParameterThenError(RegisteredClient registeredClient,
556+
String parameterName, String errorCode, Consumer<MockHttpServletRequest> requestConsumer) throws Exception {
557+
558+
doFilterWhenRequestInvalidParameterThenError(createAuthorizationConsentRequest(registeredClient),
559+
parameterName, errorCode, requestConsumer);
560+
}
561+
537562
private void doFilterWhenRequestInvalidParameterThenError(MockHttpServletRequest request,
538563
String parameterName, String errorCode, Consumer<MockHttpServletRequest> requestConsumer) throws Exception {
539564

@@ -564,6 +589,18 @@ private static MockHttpServletRequest createAuthorizationRequest(RegisteredClien
564589
return request;
565590
}
566591

592+
private static MockHttpServletRequest createAuthorizationConsentRequest(RegisteredClient registeredClient) {
593+
String requestUri = DEFAULT_AUTHORIZATION_ENDPOINT_URI;
594+
MockHttpServletRequest request = new MockHttpServletRequest("POST", requestUri);
595+
request.setServletPath(requestUri);
596+
597+
request.addParameter(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId());
598+
registeredClient.getScopes().forEach((scope) -> request.addParameter(OAuth2ParameterNames.SCOPE, scope));
599+
request.addParameter(OAuth2ParameterNames.STATE, "state");
600+
601+
return request;
602+
}
603+
567604
private static OAuth2AuthorizationCodeRequestAuthenticationToken.Builder authorizationCodeRequestAuthentication(
568605
RegisteredClient registeredClient, Authentication principal) {
569606
return OAuth2AuthorizationCodeRequestAuthenticationToken.with(registeredClient.getClientId(), principal)

0 commit comments

Comments
 (0)