Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Align OAuth2AccessToken.TokenType hashCode with case-insensitive equals - #20208

Open
amalsaidov wants to merge 1 commit into
spring-projects:mainfrom
amalsaidov:gh-tokentype-hashcode
Open

amalsaidov wants to merge 1 commit into
spring-projects:mainfrom
amalsaidov:gh-tokentype-hashcode

Conversation

@amalsaidov

Copy link
Copy Markdown

OAuth2AccessToken.TokenType overrides equals(Object) using equalsIgnoreCase (token types are case-insensitive per RFC 6749 §7.1), but hashCode() was derived from the case-sensitive String.hashCode():

public boolean equals(@Nullable Object obj) {
    ...
    return this.getValue().equalsIgnoreCase(that.getValue());
}

public int hashCode() {
    return this.getValue().hashCode();
}

Two instances that are equal but differ only in case therefore produced different hash codes, violating the equals/hashCode contract and breaking lookups in hash-based collections (HashSet/HashMap):

OAuth2AccessToken.TokenType bearer = new OAuth2AccessToken.TokenType("bearer");
bearer.equals(OAuth2AccessToken.TokenType.BEARER);                   // true  (value "Bearer")
bearer.hashCode() == OAuth2AccessToken.TokenType.BEARER.hashCode();  // false

The public TokenType(String) constructor (@since 6.5) lets callers/providers create arbitrary-case instances, and since gh-19377 call sites such as BearerTokenAuthentication rely on TokenType.equals(...), so the inconsistency is easily reachable.

This change canonicalizes the value to lower case when computing the hash, so equal token types always have equal hash codes. The case-insensitive equals (intentional and RFC-aligned, present since TokenType became a class) is unchanged.

A regression test is added that fails before the change and passes after; the spring-security-oauth2-core module check (tests + Checkstyle) passes.

OAuth2AccessToken.TokenType.equals(Object) compares values using
equalsIgnoreCase (token types are case-insensitive per RFC 6749
section 7.1), but hashCode() was derived from the case-sensitive
String.hashCode(). Two instances that are equal but differ only in
case therefore produced different hash codes, violating the
equals/hashCode contract and breaking lookups in hash-based
collections.

Canonicalize the value to lower case when computing the hash so that
equal token types always have equal hash codes.

Signed-off-by: amalsaidov <amalsaidov@users.noreply.github.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants