Skip to content

Commit 9766d97

Browse files
committed
client_id authentication parameter must have printable ASCII characters
Closes spring-projectsgh-889
1 parent 3792451 commit 9766d97

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

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 javax.servlet.FilterChain;
1921
import javax.servlet.http.HttpServletRequest;
2022
import javax.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)