Skip to content

Make the hybrid branch in GrantTypeCondition reachable - #52474

Open
HuzaifaChaudary wants to merge 1 commit into
keycloak:mainfrom
HuzaifaChaudary:fix/grant-type-condition-hybrid
Open

HuzaifaChaudary wants to merge 1 commit into
keycloak:mainfrom
HuzaifaChaudary:fix/grant-type-condition-hybrid

Conversation

@HuzaifaChaudary

Copy link
Copy Markdown

Closes #51159

the third branch in GrantTypeCondition.isGrantTypeMatching can never run :

if (parsedResponseType.hasResponseType(CODE)) {          // 1
    return isGrantMatching(AUTHORIZATION_CODE);
} else if (parsedResponseType.isImplicitFlow()) {        // 2
    return isGrantMatching(IMPLICIT);
} else if (parsedResponseType.isImplicitOrHybridFlow()) { // 3, unreachable
    return isGrantMatching(AUTHORIZATION_CODE) || isGrantMatching(IMPLICIT);
}

to reach 3 you need no code in the response type . but with no code ,
isImplicitFlow() is (token || id_token) && !code and isImplicitOrHybridFlow() is
(token || id_token) , which are the same test . so if 2 is false , 3 is false too .

what that costs : response_type=code token goes to branch 1 and only ever checks
authorization_code . a policy set up with grant_types=[implicit] votes no on it , so an
executor meant to cover the front channel token never runs , even though the redirect carries
one . same for code id_token and code id_token token .

putting implicit first , then hybrid , then code makes the branch reachable . nothing inside
the branches changed , it is a reorder plus a comment . plain code still matches
authorization_code , plain implicit still matches implicit , none still returns false ,
and hybrid with grant_types=[authorization_code] still votes yes .

five tests added . one fails without the change , all three hybrid types come back
[NO, NO, NO] where they should be [YES, YES, YES] . the other four pin the cases that must
not move and pass either way .

mvn -pl services test is 684 passing , 0 failures . the tree without this runs 679 , so the
only difference is the five new ones . spotless:check is clean .

i could not run the integration suites , docker is not available on this machine . i checked
by hand that no integration test asserts the old behaviour : ClientPoliciesTest
testClientGrantTypeCondition only sends response_type=code against
grant_types=[authorization_code] , which is unchanged here , and
testProhibitedImplicitOrHybridFlow uses AnyClientCondition with
RejectImplicitGrantExecutor , not this class .


ai usage , per the generative ai section of CONTRIBUTING . i used claude code ( claude opus 5 )
as an agent to help find the dead branch , write the patch and the tests , and draft this
description . i went through the reasoning myself , confirmed the branch is unreachable from
the two predicate definitions , and ran everything quoted above locally .

Copilot AI balanced review requested due to automatic review settings September 5, 2026 17:43
@HuzaifaChaudary
HuzaifaChaudary requested a review from a team as a code owner September 5, 2026 17:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@@ -100,15 +100,16 @@ private boolean isGrantMatching(AuthorizationRequestContext request) {
if (request == null) return false;
try {
OIDCResponseType parsedResponseType = OIDCResponseType.parse(request.getAuthorizationEndpointRequest().getResponseType());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this use request.getParsedResponseType() instead of re-parsing the raw string?

AuthorizationEndpoint parses it once in checkResponseType() and passes the result into the context (L172, L211), and the AUTHORIZATION_REQUEST event only fires once that check has passed, so the field is non-null and equal to what this line recomputes. It would also make the IllegalArgumentException catch below unnecessary.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes , you are right . line 211 is the only place that builds this context and it comes after checkResponseType , which either sets the field or returns the error redirect . so the parse here could never fail and the catch was dead . the re parse was there before this change , i kept it without looking at where the context comes from .

done in 1594fd5 . the tests already passed a parsed type into the context so they did not need to change . mvn -pl services test is still 684 passing , and putting the old branch order back still gives [NO, NO, NO] on the hybrid test .

@mposolda mposolda self-assigned this Sep 21, 2026
Copilot AI review requested due to automatic review settings September 21, 2026 23:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@HuzaifaChaudary
HuzaifaChaudary force-pushed the fix/grant-type-condition-hybrid branch from 1594fd5 to 21ebd38 Compare September 27, 2026 03:50
Copilot AI review requested due to automatic review settings September 27, 2026 03:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@HuzaifaChaudary

Copy link
Copy Markdown
Author

rebased on main to clear the conflict . main added the IMPLICIT_HYBRID_TOKEN_RESPONSE case , which reads the response type from the client note , so both paths now go through the same branch order . the request side still uses getParsedResponseType() like you asked .

added hybridTokenResponseMatchesImplicit for the new path . 7 of 7 pass , and with the old branch order put back 2 fail , the old hybrid test and the new one , both with code token voting no . spotless is clean .

@HuzaifaChaudary

Copy link
Copy Markdown
Author

@mposolda could you take a look when you have time ? the grant type condition never matched code token or code id_token token , so a client policy that checks the grant type was skipped in the hybrid flow . oleksii's review is applied , it is rebased on main , merges clean and ci is green .

@graziang graziang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HuzaifaChaudary thanks! @jimmychakkalakal do you want to take another look?

@jimmychakkalakal jimmychakkalakal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HuzaifaChaudary LGTM, I approve the PR from my end

the third branch could never run. to get to it you need no code in the response
type, and with no code isImplicitFlow and isImplicitOrHybridFlow are the same
test, so if the second branch is false the third one is false too.

that means response_type=code token went to the first branch and only ever
checked authorization_code. a policy set up with grant_types=[implicit] voted no
on it, so an executor meant to cover the front channel token never ran, even
though the redirect carries one.

putting implicit first, then hybrid, then code makes the branch the original
author already wrote reachable. nothing inside the branches changed. plain code
and plain implicit still land where they did before.

Signed-off-by: Huzaifa Iftikhar <chhuzaifaiftikhar@gmail.com>
@mposolda
mposolda force-pushed the fix/grant-type-condition-hybrid branch from 21ebd38 to 4dbcc34 Compare November 23, 2026 15:08
Copilot AI balanced review requested due to automatic review settings November 23, 2026 15:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@mposolda

Copy link
Copy Markdown
Contributor

@HuzaifaChaudary Thanks! Could you please fix the compilation error? Hopefully the PR is close to merge.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grant-type client policy condition misses hybrid implicit authorization responses

7 participants