Skip to content

Commit 4477e65

Browse files
committed
Merge branch 0.4.x into main
The following commits are merged using the default merge strategy. 9766d97 client_id authentication parameter must have printable ASCII characters 71b33e2 Update links to current version of OAuth 2.1
2 parents e52927c + 71b33e2 commit 4477e65

File tree

5 files changed

+83
-15
lines changed

5 files changed

+83
-15
lines changed

README.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ image:https://github.com/spring-projects/spring-authorization-server/workflows/C
44

55
= Spring Authorization Server
66

7-
The Spring Authorization Server project, led by the https://spring.io/projects/spring-security/[Spring Security] team, is focused on delivering https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-06#section-1.1[OAuth 2.1 Authorization Server] support to the Spring community.
7+
The Spring Authorization Server project, led by the https://spring.io/projects/spring-security/[Spring Security] team, is focused on delivering https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-1.1[OAuth 2.1 Authorization Server] support to the Spring community.
88

99
This project replaces the Authorization Server support provided by https://spring.io/projects/spring-security-oauth/[Spring Security OAuth].
1010

@@ -20,7 +20,7 @@ The Spring Authorization Server project provides software support through the ht
2020
https://tanzu.vmware.com/spring-runtime[Commercial support], which offers an extended support period, is also available from VMware.
2121

2222
== Getting Started
23-
The first place to start is to read the https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-01[OAuth 2.1 Authorization Framework] to gain an in-depth understanding on how to build an Authorization Server.
23+
The first place to start is to read the https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07[OAuth 2.1 Authorization Framework] to gain an in-depth understanding on how to build an Authorization Server.
2424
It is a critically important first step as the implementation must conform to the specification defined in the OAuth 2.1 Authorization Framework and the https://github.com/spring-projects/spring-authorization-server/wiki/OAuth-2.0-Specifications[related specifications].
2525

2626
The second place to start is to become very familiar with the codebase in the following Spring Security modules:

docs/src/docs/asciidoc/overview.adoc

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This site contains reference documentation and how-to guides for Spring Authoriz
66
[[introducing-spring-authorization-server]]
77
== Introducing Spring Authorization Server
88

9-
Spring Authorization Server is a framework that provides implementations of the https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05[OAuth 2.1] and https://openid.net/specs/openid-connect-core-1_0.html[OpenID Connect 1.0] specifications and other related specifications.
9+
Spring Authorization Server is a framework that provides implementations of the https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07[OAuth 2.1] and https://openid.net/specs/openid-connect-core-1_0.html[OpenID Connect 1.0] specifications and other related specifications.
1010
It is built on top of https://spring.io/projects/spring-security[Spring Security] to provide a secure, light-weight, and customizable foundation for building OpenID Connect 1.0 Identity Providers and OAuth2 Authorization Server products.
1111

1212
[[feature-list]]
@@ -25,10 +25,10 @@ Spring Authorization Server supports the following features:
2525
* Client Credentials
2626
* Refresh Token
2727
|
28-
* The OAuth 2.1 Authorization Framework (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05[draft])
29-
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05#section-4.1[Authorization Code Grant]
30-
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05#section-4.2[Client Credentials Grant]
31-
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05#section-4.3[Refresh Token Grant]
28+
* The OAuth 2.1 Authorization Framework (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07[draft])
29+
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-4.1[Authorization Code Grant]
30+
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-4.2[Client Credentials Grant]
31+
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-4.3[Refresh Token Grant]
3232
* OpenID Connect Core 1.0 (https://openid.net/specs/openid-connect-core-1_0.html[spec])
3333
** https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth[Authorization Code Flow]
3434

@@ -48,7 +48,7 @@ Spring Authorization Server supports the following features:
4848
* `private_key_jwt`
4949
* `none` (public clients)
5050
|
51-
* The OAuth 2.1 Authorization Framework (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05#section-2.4[Client Authentication])
51+
* The OAuth 2.1 Authorization Framework (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-2.4[Client Authentication])
5252
* JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication (https://tools.ietf.org/html/rfc7523[RFC 7523])
5353
* Proof Key for Code Exchange by OAuth Public Clients (PKCE) (https://tools.ietf.org/html/rfc7636[RFC 7636])
5454

@@ -64,9 +64,9 @@ Spring Authorization Server supports the following features:
6464
* xref:protocol-endpoints.adoc#oidc-user-info-endpoint[OpenID Connect 1.0 UserInfo Endpoint]
6565
* xref:protocol-endpoints.adoc#oidc-client-registration-endpoint[OpenID Connect 1.0 Client Registration Endpoint]
6666
|
67-
* The OAuth 2.1 Authorization Framework (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05[draft])
68-
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05#section-3.1[Authorization Endpoint]
69-
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-05#section-3.2[Token Endpoint]
67+
* The OAuth 2.1 Authorization Framework (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07[draft])
68+
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-3.1[Authorization Endpoint]
69+
** https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-3.2[Token Endpoint]
7070
* OAuth 2.0 Token Introspection (https://tools.ietf.org/html/rfc7662[RFC 7662])
7171
* OAuth 2.0 Token Revocation (https://tools.ietf.org/html/rfc7009[RFC 7009])
7272
* OAuth 2.0 Authorization Server Metadata (https://tools.ietf.org/html/rfc8414[RFC 8414])

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,29 @@ private static void validateRedirectUri(OAuth2AuthorizationCodeRequestAuthentica
102102

103103
String requestedRedirectHost = requestedRedirect.getHost();
104104
if (requestedRedirectHost == null || requestedRedirectHost.equals("localhost")) {
105-
// As per https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-01#section-9.7.1
105+
// As per https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-9.7.1
106106
// While redirect URIs using localhost (i.e., "http://localhost:{port}/{path}")
107107
// function similarly to loopback IP redirects described in Section 10.3.3,
108108
// the use of "localhost" is NOT RECOMMENDED.
109109
OAuth2Error error = new OAuth2Error(
110110
OAuth2ErrorCodes.INVALID_REQUEST,
111111
"localhost is not allowed for the redirect_uri (" + requestedRedirectUri + "). " +
112112
"Use the IP literal (127.0.0.1) instead.",
113-
"https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-01#section-9.7.1");
113+
"https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-9.7.1");
114114
throwError(error, OAuth2ParameterNames.REDIRECT_URI,
115115
authorizationCodeRequestAuthentication, registeredClient);
116116
}
117117

118118
if (!isLoopbackAddress(requestedRedirectHost)) {
119-
// As per https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-01#section-9.7
119+
// As per https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-9.7
120120
// When comparing client redirect URIs against pre-registered URIs,
121121
// authorization servers MUST utilize exact string matching.
122122
if (!registeredClient.getRedirectUris().contains(requestedRedirectUri)) {
123123
throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.REDIRECT_URI,
124124
authorizationCodeRequestAuthentication, registeredClient);
125125
}
126126
} else {
127-
// As per https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-01#section-10.3.3
127+
// As per https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#section-10.3.3
128128
// The authorization server MUST allow any port to be specified at the
129129
// time of the request for loopback IP redirect URIs, to accommodate
130130
// clients that obtain an available ephemeral port from the operating

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

+22
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
118118
this.authenticationDetailsSource.buildDetails(request));
119119
}
120120
if (authenticationRequest != null) {
121+
validateClientIdentifier(authenticationRequest);
121122
Authentication authenticationResult = this.authenticationManager.authenticate(authenticationRequest);
122123
this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, authenticationResult);
123124
}
@@ -201,4 +202,25 @@ private void onAuthenticationFailure(HttpServletRequest request, HttpServletResp
201202
this.errorHttpResponseConverter.write(errorResponse, null, httpResponse);
202203
}
203204

205+
private static void validateClientIdentifier(Authentication authentication) {
206+
if (!(authentication instanceof OAuth2ClientAuthenticationToken)) {
207+
return;
208+
}
209+
210+
// As per spec, in Appendix A.1.
211+
// https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-07#appendix-A.1
212+
// The syntax for client_id is *VSCHAR (%x20-7E):
213+
// -> Hex 20 -> ASCII 32 -> space
214+
// -> Hex 7E -> ASCII 126 -> tilde
215+
216+
OAuth2ClientAuthenticationToken clientAuthentication = (OAuth2ClientAuthenticationToken) authentication;
217+
String clientId = (String) clientAuthentication.getPrincipal();
218+
for (int i = 0; i < clientId.length(); i++) {
219+
char charAt = clientId.charAt(i);
220+
if (!(charAt >= 32 && charAt <= 126)) {
221+
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_REQUEST);
222+
}
223+
}
224+
}
225+
204226
}

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

+46
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.web;
1717

18+
import java.nio.charset.StandardCharsets;
19+
1820
import jakarta.servlet.FilterChain;
1921
import jakarta.servlet.http.HttpServletRequest;
2022
import jakarta.servlet.http.HttpServletResponse;
@@ -33,6 +35,7 @@
3335
import org.springframework.security.authentication.AuthenticationManager;
3436
import org.springframework.security.core.Authentication;
3537
import org.springframework.security.core.context.SecurityContextHolder;
38+
import org.springframework.security.crypto.codec.Hex;
3639
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
3740
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
3841
import org.springframework.security.oauth2.core.OAuth2Error;
@@ -130,6 +133,7 @@ public void doFilterWhenRequestDoesNotMatchThenNotProcessed() throws Exception {
130133
this.filter.doFilter(request, response, filterChain);
131134

132135
verify(filterChain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
136+
verifyNoInteractions(this.authenticationConverter);
133137
}
134138

135139
@Test
@@ -142,6 +146,7 @@ public void doFilterWhenRequestMatchesAndEmptyCredentialsThenNotProcessed() thro
142146
this.filter.doFilter(request, response, filterChain);
143147

144148
verify(filterChain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
149+
verifyNoInteractions(this.authenticationManager);
145150
}
146151

147152
@Test
@@ -164,6 +169,46 @@ public void doFilterWhenRequestMatchesAndInvalidCredentialsThenInvalidRequestErr
164169
assertThat(error.getErrorCode()).isEqualTo(OAuth2ErrorCodes.INVALID_REQUEST);
165170
}
166171

172+
// gh-889
173+
@Test
174+
public void doFilterWhenRequestMatchesAndClientIdContainsNonPrintableASCIIThenInvalidRequestError() throws Exception {
175+
// Hex 00 -> null
176+
String clientId = new String(Hex.decode("00"), StandardCharsets.UTF_8);
177+
assertWhenInvalidClientIdThenInvalidRequestError(clientId);
178+
179+
// Hex 0a61 -> line feed + a
180+
clientId = new String(Hex.decode("0a61"), StandardCharsets.UTF_8);
181+
assertWhenInvalidClientIdThenInvalidRequestError(clientId);
182+
183+
// Hex 1b -> escape
184+
clientId = new String(Hex.decode("1b"), StandardCharsets.UTF_8);
185+
assertWhenInvalidClientIdThenInvalidRequestError(clientId);
186+
187+
// Hex 1b61 -> escape + a
188+
clientId = new String(Hex.decode("1b61"), StandardCharsets.UTF_8);
189+
assertWhenInvalidClientIdThenInvalidRequestError(clientId);
190+
}
191+
192+
private void assertWhenInvalidClientIdThenInvalidRequestError(String clientId) throws Exception {
193+
when(this.authenticationConverter.convert(any(HttpServletRequest.class))).thenReturn(
194+
new OAuth2ClientAuthenticationToken(clientId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "secret", null));
195+
196+
MockHttpServletRequest request = new MockHttpServletRequest("POST", this.filterProcessesUrl);
197+
request.setServletPath(this.filterProcessesUrl);
198+
MockHttpServletResponse response = new MockHttpServletResponse();
199+
FilterChain filterChain = mock(FilterChain.class);
200+
201+
this.filter.doFilter(request, response, filterChain);
202+
203+
verifyNoInteractions(filterChain);
204+
verifyNoInteractions(this.authenticationManager);
205+
206+
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
207+
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
208+
OAuth2Error error = readError(response);
209+
assertThat(error.getErrorCode()).isEqualTo(OAuth2ErrorCodes.INVALID_REQUEST);
210+
}
211+
167212
@Test
168213
public void doFilterWhenRequestMatchesAndBadCredentialsThenInvalidClientError() throws Exception {
169214
when(this.authenticationConverter.convert(any(HttpServletRequest.class))).thenReturn(
@@ -179,6 +224,7 @@ public void doFilterWhenRequestMatchesAndBadCredentialsThenInvalidClientError()
179224
this.filter.doFilter(request, response, filterChain);
180225

181226
verifyNoInteractions(filterChain);
227+
verify(this.authenticationManager).authenticate(any());
182228

183229
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
184230
assertThat(response.getStatus()).isEqualTo(HttpStatus.UNAUTHORIZED.value());

0 commit comments

Comments
 (0)