DeepSeek V4 Pro · Heavy

implement-access-token-context-encoding-framework-keycloak

Keycloak · Java · L · 300–799 lines · 6 files

The generalist plus 9 specialist lens agents in parallel, then the same verify and dedup steps. Judge: Claude Opus 5.5.

Gold bugs found
3 of 5
Comments published
6
Comments matching a bug
3
Comments not matching
3
Cost of this PR
$1.644

Published comments (6)

mediumbugservices/src/main/java/org/keycloak/protocol/oidc/grants/ClientCredentialsGrantTypeFactory.java:38-41

Client credentials token grant type encoded as na instead of cc — Without this attribute, `getTokenContextFromClientSessionContext` falls back to UNKNOWN and client-credentials tokens encode `na` instead of `cc`. Set `Constants.GRANT_TYPE` to `context.getGrantType()` on the client session context in `ClientCredentialsGrantType.process` before generating the access token.

mediumbugservices/src/main/java/org/keycloak/protocol/oidc/encode/AccessTokenContext.java:73

Constructor validates grantType twice and fails to validate rawTokenId — A null `rawTokenId` passes silently and later makes `encodeTokenId` emit a token id ending in `null`. Change the second `Objects.requireNonNull` to target `rawTokenId`.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:482-483

Access token ID matcher extracts grant-type shortcut at wrong offset — The grant shortcut occupies indices 4–5, so reading `substring(3, 5)` yields part of the token type plus the first grant character (for `trltcc` it gives `tc` instead of `cc`) and never inspects the complete shortcut. Use `substring(4, 6)` and invert the condition so a mismatch returns false.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:480-484

isAccessTokenId validates grant shortcut at wrong substring indexes — The encoder places the grant shortcut at indices 4–5 of the pre-colon context, so this matcher reads the wrong slice and can also throw on short contexts. Require `items[0].length() >= 6` and return `expectedGrantShortcut.equals(items[0].substring(4, 6)) && isUUID().matches(items[1])`.

mediumbugtestsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/AssertEvents.java:483

Inverted condition rejects correct grant type in isAccessTokenId — Returning false when the extracted shortcut equals the expected one rejects correct grant types, so these assertion helpers only check the UUID and never verify the grant type. Invert the condition so only a genuine mismatch returns false.

mediumbugserver-spi-private/src/main/java/org/keycloak/protocol/oidc/grants/OAuth2GrantTypeFactory.java:29-33

OAuth2 grant type shortcut length not constrained to two characters but encoder assumes it — A custom factory returning a shortcut length other than two produces token IDs that later throw `IllegalArgumentException` in `DefaultTokenContextEncoderProvider.getTokenContextFromTokenId`. Enforce an exact two-character shortcut here, validate it in `DefaultTokenContextEncoderProviderFactory.postInit`, or make the encoder handle variable lengths. **Also found in:** - `services/src/main/java/org/keycloak/protocol/oidc/encode/DefaultTokenContextEncoderProvider.java:109-117`

Gold bugs found (3)

mediumThe AccessTokenContext constructor checks grantType twice instead of rawTokenId, so a null rawTokenId passes validation and is encoded as ':null'.
mediumisAccessTokenId reads the grant shortcut at the wrong offset (3,5 instead of 4,6) and inverts the comparison, so the event assertions never verify the token's grant type.
mediumClient-credentials (and UMA permission) token issuance never sets the GRANT_TYPE attribute, so their access token ids are encoded with the unknown grant 'na' instead of their shortcut.

Gold bugs missed (2)

mediumtestIncorrectGrantType catches any RuntimeException, so it passes on a NullPointerException from the unset sessionFactory rather than verifying the unknown-grant IllegalArgumentException.
mediumThe legacy token-exchange path in AbstractTokenExchangeProvider never sets Constants.GRANT_TYPE on the client session context, so tokens it issues are encoded with grant shortcut 'na' instead of the token-exchange shortcut.