Skip to content

Commit 1e4736f

Browse files
manuelbljgrandja
authored andcommitted
Prevent double-escaping of authorize URL parameters
If the authorization URL in the OAuth2 provider configuration contained query parameters with escaped characters, these characters were escaped a second time. This commit fixes it. It is relevant to support the OIDC claims parameter (see https://openid.net/specs/openid-connect-core-1_0.html#ClaimsParameter). Fixes gh-7871
1 parent 0012e24 commit 1e4736f

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.util.MultiValueMap;
2424
import org.springframework.util.StringUtils;
2525
import org.springframework.web.util.UriComponentsBuilder;
26+
import org.springframework.web.util.UriUtils;
2627

2728
import java.io.Serializable;
2829
import java.nio.charset.StandardCharsets;
@@ -376,29 +377,34 @@ public OAuth2AuthorizationRequest build() {
376377

377378
private String buildAuthorizationRequestUri() {
378379
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
379-
parameters.set(OAuth2ParameterNames.RESPONSE_TYPE, this.responseType.getValue());
380-
parameters.set(OAuth2ParameterNames.CLIENT_ID, this.clientId);
380+
parameters.set(OAuth2ParameterNames.RESPONSE_TYPE, encodeQueryParam(this.responseType.getValue()));
381+
parameters.set(OAuth2ParameterNames.CLIENT_ID, encodeQueryParam(this.clientId));
381382
if (!CollectionUtils.isEmpty(this.scopes)) {
382383
parameters.set(OAuth2ParameterNames.SCOPE,
383-
StringUtils.collectionToDelimitedString(this.scopes, " "));
384+
encodeQueryParam(StringUtils.collectionToDelimitedString(this.scopes, " ")));
384385
}
385386
if (this.state != null) {
386-
parameters.set(OAuth2ParameterNames.STATE, this.state);
387+
parameters.set(OAuth2ParameterNames.STATE, encodeQueryParam(this.state));
387388
}
388389
if (this.redirectUri != null) {
389-
parameters.set(OAuth2ParameterNames.REDIRECT_URI, this.redirectUri);
390+
parameters.set(OAuth2ParameterNames.REDIRECT_URI, encodeQueryParam(this.redirectUri));
390391
}
391392
if (!CollectionUtils.isEmpty(this.additionalParameters)) {
392-
this.additionalParameters.forEach((k, v) -> parameters.set(k, v.toString()));
393+
this.additionalParameters.forEach((k, v) ->
394+
parameters.set(encodeQueryParam(k), encodeQueryParam(v.toString())));
393395
}
394396

395397
return UriComponentsBuilder.fromHttpUrl(this.authorizationUri)
396398
.queryParams(parameters)
397-
.encode(StandardCharsets.UTF_8)
398399
.build()
399400
.toUriString();
400401
}
401402

403+
// Encode query parameter value according to RFC 3986
404+
private static String encodeQueryParam(String value) {
405+
return UriUtils.encodeQueryParam(value, StandardCharsets.UTF_8);
406+
}
407+
402408
private LinkedHashSet<String> toLinkedHashSet(String... scope) {
403409
LinkedHashSet<String> result = new LinkedHashSet<>();
404410
Collections.addAll(result, scope);

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/endpoint/OAuth2AuthorizationRequestTests.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -307,4 +307,38 @@ public void buildWhenAuthorizationUriIncludesQueryParameterThenAuthorizationRequ
307307
"response_type=code&client_id=client-id&state=state&" +
308308
"redirect_uri=https://example.com/authorize/oauth2/code/registration-id");
309309
}
310+
311+
@Test
312+
public void buildWhenAuthorizationUriIncludesEscapedQueryParameterThenAuthorizationRequestUrlIncludesIt() {
313+
OAuth2AuthorizationRequest authorizationRequest =
314+
TestOAuth2AuthorizationRequests.request()
315+
.authorizationUri(AUTHORIZATION_URI +
316+
"?claims=%7B%22userinfo%22%3A%7B%22email_verified%22%3A%7B%22essential%22%3Atrue%7D%7D%7D").build();
317+
318+
assertThat(authorizationRequest.getAuthorizationRequestUri()).isNotNull();
319+
assertThat(authorizationRequest.getAuthorizationRequestUri())
320+
.isEqualTo("https://provider.com/oauth2/authorize?" +
321+
"claims=%7B%22userinfo%22%3A%7B%22email_verified%22%3A%7B%22essential%22%3Atrue%7D%7D%7D&" +
322+
"response_type=code&client_id=client-id&state=state&" +
323+
"redirect_uri=https://example.com/authorize/oauth2/code/registration-id");
324+
}
325+
326+
@Test
327+
public void buildWhenNonAsciiAdditionalParametersThenProperlyEncoded() {
328+
Map<String, Object> additionalParameters = new HashMap<>();
329+
additionalParameters.put("item amount", "19.95" + '\u20ac');
330+
additionalParameters.put("item name", "H" + '\u00c5' + "M" + '\u00d6');
331+
additionalParameters.put('\u00e2' + "ge", "4" + '\u00bd');
332+
OAuth2AuthorizationRequest authorizationRequest =
333+
TestOAuth2AuthorizationRequests.request()
334+
.additionalParameters(additionalParameters)
335+
.build();
336+
337+
assertThat(authorizationRequest.getAuthorizationRequestUri()).isNotNull();
338+
assertThat(authorizationRequest.getAuthorizationRequestUri())
339+
.isEqualTo("https://example.com/login/oauth/authorize?" +
340+
"response_type=code&client_id=client-id&state=state&" +
341+
"redirect_uri=https://example.com/authorize/oauth2/code/registration-id&" +
342+
"item%20amount=19.95%E2%82%AC&%C3%A2ge=4%C2%BD&item%20name=H%C3%85M%C3%96");
343+
}
310344
}

0 commit comments

Comments
 (0)