spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

Refresh token clients ignore requested scopes when the response omits scope

Open ryujungkyun opened this issue 3 weeks ago • 0 comments

When refreshing an access token with narrower scopes, RestClientRefreshTokenTokenResponseClient and WebClientReactiveRefreshTokenTokenResponseClient use the previous access token’s scopes if the response omits scope.

For example:

  1. The existing access token has read write.
  2. The client requests a refreshed token with only read.
  3. The authorization server returns a successful response without scope.
  4. The resulting OAuth2AccessToken#getScopes() returns read write instead of read.

Expected behavior

RFC 6749, Section 5.1 allows the response to omit scope when it matches the scope requested by the client.

My understanding is that the resulting access token’s scopes should therefore contain only read in this case.

Actual behavior

Both clients fall back to the previous access token’s scopes in populateTokenResponse():

if (CollectionUtils.isEmpty(accessTokenResponse.getAccessToken().getScopes())) {
    tokenResponseBuilder.scopes(grantRequest.getAccessToken().getScopes());
}

This does not account for the scopes explicitly requested through grantRequest.getScopes().

The issue affects the scope metadata stored by the client. It does not mean that the authorization server grants additional permissions.

Reproduction

I reproduced this against the local 7.2.0-SNAPSHOT source by adding tests to both existing refresh token response client test classes.

The existing access token has read and write, and the refresh request explicitly requests only read:

OAuth2RefreshTokenGrantRequest grantRequest =
        new OAuth2RefreshTokenGrantRequest(
                clientRegistration,
                accessToken,
                refreshToken,
                Collections.singleton("read"));

The mock token endpoint returns a successful response without scope:

{
  "access_token": "access-token-1234",
  "token_type": "Bearer",
  "expires_in": 3600
}

The tests verify that the outgoing request contains scope=read and does not contain write. That check passes.

However, the following assertion fails for both clients:

assertThat(accessTokenResponse.getAccessToken().getScopes())
        .containsExactly("read");
Expected: ["read"]
Actual:   ["read", "write"]

When the response explicitly includes "scope": "read", both clients return the expected scope.

Suggested behavior

When the response omits scope, could the clients use the explicitly requested scopes first, keeping the existing fallback when the refresh request also omits scope?

Could you confirm whether this interpretation is correct? I have prepared reproducing tests and will wait for triage before submitting a fix.

[!NOTE] To avoid duplicate work, please hold off on opening a PR until the maintainers have reviewed this issue.

ryujungkyun avatar Nov 09 '26 14:11 ryujungkyun