Skip to content

Commit 791feee

Browse files
committed
Prevent downgraded usage of DPoP-bound access tokens
Issue gh-16574 Closes gh-16937
1 parent 178ca73 commit 791feee

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

Diff for: oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/authentication/BearerTokenAuthenticationFilter.java

+27-1
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-2025 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.security.oauth2.server.resource.web.authentication;
1818

1919
import java.io.IOException;
20+
import java.util.Map;
2021

2122
import jakarta.servlet.FilterChain;
2223
import jakarta.servlet.ServletException;
@@ -32,7 +33,11 @@
3233
import org.springframework.security.core.context.SecurityContext;
3334
import org.springframework.security.core.context.SecurityContextHolder;
3435
import org.springframework.security.core.context.SecurityContextHolderStrategy;
36+
import org.springframework.security.oauth2.core.ClaimAccessor;
3537
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
38+
import org.springframework.security.oauth2.server.resource.BearerTokenError;
39+
import org.springframework.security.oauth2.server.resource.BearerTokenErrors;
40+
import org.springframework.security.oauth2.server.resource.authentication.AbstractOAuth2TokenAuthenticationToken;
3641
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthenticationToken;
3742
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider;
3843
import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationEntryPoint;
@@ -45,6 +50,8 @@
4550
import org.springframework.security.web.context.RequestAttributeSecurityContextRepository;
4651
import org.springframework.security.web.context.SecurityContextRepository;
4752
import org.springframework.util.Assert;
53+
import org.springframework.util.CollectionUtils;
54+
import org.springframework.util.StringUtils;
4855
import org.springframework.web.filter.OncePerRequestFilter;
4956

5057
/**
@@ -135,6 +142,12 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
135142
try {
136143
AuthenticationManager authenticationManager = this.authenticationManagerResolver.resolve(request);
137144
Authentication authenticationResult = authenticationManager.authenticate(authenticationRequest);
145+
if (isDPoPBoundAccessToken(authenticationResult)) {
146+
// Prevent downgraded usage of DPoP-bound access tokens,
147+
// by rejecting a DPoP-bound access token received as a bearer token.
148+
BearerTokenError error = BearerTokenErrors.invalidToken("Invalid bearer token");
149+
throw new OAuth2AuthenticationException(error);
150+
}
138151
SecurityContext context = this.securityContextHolderStrategy.createEmptyContext();
139152
context.setAuthentication(authenticationResult);
140153
this.securityContextHolderStrategy.setContext(context);
@@ -217,4 +230,17 @@ public void setAuthenticationDetailsSource(
217230
this.authenticationDetailsSource = authenticationDetailsSource;
218231
}
219232

233+
private static boolean isDPoPBoundAccessToken(Authentication authentication) {
234+
if (!(authentication instanceof AbstractOAuth2TokenAuthenticationToken<?> accessTokenAuthentication)) {
235+
return false;
236+
}
237+
ClaimAccessor accessTokenClaims = accessTokenAuthentication::getTokenAttributes;
238+
String jwkThumbprintClaim = null;
239+
Map<String, Object> confirmationMethodClaim = accessTokenClaims.getClaimAsMap("cnf");
240+
if (!CollectionUtils.isEmpty(confirmationMethodClaim) && confirmationMethodClaim.containsKey("jkt")) {
241+
jwkThumbprintClaim = (String) confirmationMethodClaim.get("jkt");
242+
}
243+
return StringUtils.hasText(jwkThumbprintClaim);
244+
}
245+
220246
}

Diff for: oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/authentication/BearerTokenAuthenticationFilterTests.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.security.oauth2.server.resource.web.authentication;
1818

1919
import java.io.IOException;
20+
import java.util.Collections;
2021

2122
import jakarta.servlet.ServletException;
2223
import jakarta.servlet.http.HttpServletRequest;
@@ -41,10 +42,14 @@
4142
import org.springframework.security.core.context.SecurityContextHolderStrategy;
4243
import org.springframework.security.core.context.SecurityContextImpl;
4344
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
45+
import org.springframework.security.oauth2.core.OAuth2Error;
46+
import org.springframework.security.oauth2.jwt.Jwt;
47+
import org.springframework.security.oauth2.jwt.TestJwts;
4448
import org.springframework.security.oauth2.server.resource.BearerTokenError;
4549
import org.springframework.security.oauth2.server.resource.BearerTokenErrorCodes;
4650
import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
4751
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthenticationToken;
52+
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
4853
import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver;
4954
import org.springframework.security.web.AuthenticationEntryPoint;
5055
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
@@ -239,6 +244,25 @@ public void doFilterWhenCustomSecurityContextHolderStrategyThenUses() throws Ser
239244
verify(strategy).setContext(any());
240245
}
241246

247+
@Test
248+
public void doFilterWhenDPoPBoundTokenDowngradedThenPropagatesError() throws ServletException, IOException {
249+
Jwt jwt = TestJwts.jwt().claim("cnf", Collections.singletonMap("jkt", "jwk-thumbprint")).build();
250+
JwtAuthenticationToken authenticationResult = new JwtAuthenticationToken(jwt);
251+
given(this.bearerTokenResolver.resolve(this.request)).willReturn("token");
252+
given(this.authenticationManager.authenticate(any(BearerTokenAuthenticationToken.class)))
253+
.willReturn(authenticationResult);
254+
BearerTokenAuthenticationFilter filter = addMocks(
255+
new BearerTokenAuthenticationFilter(this.authenticationManager));
256+
filter.setAuthenticationFailureHandler(this.authenticationFailureHandler);
257+
filter.doFilter(this.request, this.response, this.filterChain);
258+
ArgumentCaptor<OAuth2AuthenticationException> exceptionCaptor = ArgumentCaptor
259+
.forClass(OAuth2AuthenticationException.class);
260+
verify(this.authenticationFailureHandler).onAuthenticationFailure(any(), any(), exceptionCaptor.capture());
261+
OAuth2Error error = exceptionCaptor.getValue().getError();
262+
assertThat(error.getErrorCode()).isEqualTo(BearerTokenErrorCodes.INVALID_TOKEN);
263+
assertThat(error.getDescription()).isEqualTo("Invalid bearer token");
264+
}
265+
242266
@Test
243267
public void setAuthenticationEntryPointWhenNullThenThrowsException() {
244268
BearerTokenAuthenticationFilter filter = new BearerTokenAuthenticationFilter(this.authenticationManager);

0 commit comments

Comments
 (0)