8000 [OAuth] Prefer authorization error on redirect_uri by tdiesler · Pull Request #48553 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

[OAuth] Prefer authorization error on redirect_uri - #48553

Closed
tdiesler wants to merge 1 commit into
keycloak:mainfrom
tdiesler:ghi48542
Closed

tdiesler wants to merge 1 commit into
keycloak:mainfrom
tdiesler:ghi48542

Conversation

@tdiesler
@tdiesler tdiesler commented Apr 28, 2026 •
Copy link
Copy Markdown
Contributor

closes #48542

This PR tries to be as unintrusive as possible. While processing an authorization request there are potential errors coming from

  1. checks in client policies
  2. various request parsers (e.g. url query, request object, request_uri)
  3. checks on request_uri, response_type, response_mode

most of these errors map to an unchecked ErrorPageException, which then displays an html error page.

When the request passes the above, errors tend to be reported back to the caller via redirect_uri already.

This PR proposes to first catch these ErrorPageExceptions and then (when configured to do so) make a "best effort" to use the redirect_uri path - otherwise, the error is returned to the caller as json object.

Without the authorization.preferErrorOnRedirect attribute on the realm, this PR changes nothing. As a result, no test cases needed to be touched. That was in fact the hard bit: change nothing, unless the headless error path is wanted.

Automated conformance tests are headless - they prefer errors to be delivered on the redirect_uri when possible.

If this approach gets accepted, we would likely want to expose that switch on the admin UI, which is not (yet) part of this PR.

This PR is at the bottom of all other HAIP conformance work.

@tdiesler tdiesler changed the title [OAuth] Avoid error page when redirect to client is possible/configured [OAuth] Avoid error page when redirect to client is possible Apr 28, 2026
@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.adapter.servlet.SAMLLoginResponseHandlingTest#testAttributes

Keycloak CI - Adapter IT Strict Cookies

org.openqa.selenium.JavascriptException: 
TypeError: can't access property "outerHTML", document.documentElement is null
Build info: version: '4.28.1', revision: '73f5ad48a2'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.17.0-1011-azure', java.version: '21.0.11'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
...

Report flaky test

@tdiesler
tdiesler force-pushed the ghi48542 branch 9 times, most recently from 2c3c43a to 6927d2a Compare April 29, 2026 03:33
@tdiesler tdiesler changed the title [OAuth] Avoid error page when redirect to client is possible [OAuth] Avoid error page in headless environments Apr 29, 2026
@tdiesler
tdiesler force-pushed the ghi48542 branch 3 times, most recently from 64cf932 to b2ea215 Compare April 29, 2026 05:33
@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.model.user.UserModelTest#testAddRemoveUser

Keycloak CI - Store Model Tests

org.opentest4j.MultipleFailuresError: 
Multiple Failures (2 failures)
	java.lang.NullPointerException: Cannot invoke "org.keycloak.models.KeycloakSessionFactory.create()" because "factory" is null
	java.lang.NullPointerException: Null keys are not supported!
	Suppressed: java.lang.NullPointerException: Cannot invoke "org.keycloak.models.KeycloakSessionFactory.create()" because "factory" is null
...

Report flaky test

@tdiesler
tdiesler marked this pull request as draft May 4, 2026 09:32
@tdiesler
tdiesler force-pushed the ghi48542 branch 2 times, most recently from 3d081eb to 0d1663e Compare May 4, 2026 11:53
@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.forms.BruteForceTest#testNoFailureResetForPermanentLockout

Keycloak CI - Base IT (5)

org.opentest4j.AssertionFailedError: Expected error event ==> expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
...

Report flaky test

event.detail(Details.CLIENT_POLICY_ERROR, cpe.getError());
event.detail(Details.CLIENT_POLICY_ERROR_DETAIL, cpe.getErrorDetail());
event.error(cpe.getError());
throw new ErrorPageException(session, authenticationSession, cpe.getErrorStatus(), cpe.getErrorDetail());
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.

cpe.getError() gets lost here, so a client policy raising a different OAuth error code than invalid_request will always be overridden by invalid_request now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is how the translation from ClientPolicyException => ErrorPageException works. Perhaps the html error page does not show the error property from the CPE. This code path has not changed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This has been fixed - the error code coming from ErrorPageException and AuthorizationCheckException are now passed on correctly.

@tdiesler
tdiesler force-pushed the ghi48542 branch 6 times, most recently from b40195a to 0707c36 Compare May 7, 2026 11:50
return handleRedirectErrorToClient(epex.getError(), errorMessage);
}

// [TODO >>>] Should we throw an ErrorResponseException instead?
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.

@tdiesler Would you happen to know if there is already an existing issue tracking the use of ErrorResponseException instead? If not, do you think it would be worthwhile creating one for future tracking?

Also, I noticed there are 14 failing checks. Do you think these are related to test flakiness, or does this PR require some changes in order for the tests to pass?

@tdiesler tdiesler May 8, 2026 •
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This needs to be discussed. The spec does not say that there should be a json error response at all. It also does not prohibit it AFAIK.

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.adapter.servlet.SAMLServletAdapterTest#salesMetadataTest

Keycloak CI - Adapter IT Strict Cookies

org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
...

Report flaky test

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unreported flaky test detected, please review

Signed-off-by: Thomas Diesler <tdiesler@proton.me>
@mposolda mposolda self-assigned this May 12, 2026
@mposolda mposolda 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.

In the error scenarios, we already redirect to client whenever we can safely do that. But we can do that just if combination of parameters redirect_uri, response_mode or response_type is valid and aligned with the client configuration. Otherwise the error page needs to be displayed.

  • We had security issues in the past due the issues with unsafe redirects. The redirects are OK just if we can be 100% sure they are safe. Otherwise the error page needs to be displayed.

  • Keycloak is already certified as the OIDC and FAPI provider and also pass the FAPI 2 testsuite. In some tests, it is expected that error page is displayed instead of redirect to the client. In such cases, it is expected to take a screenshot and attach it to the particular conformance test. This is perfectly fine to pass the certification.

  • Introducing the realm-wide option is not good IMO as the only use of such an option is the conformance testsuite. As in production, developers will figure that they use incorrect parameters in their OAuth/OIDC requests and they would fix it in their client applications to send correct requests. I don't think we need to introduce an option just because of the conformance testsuite (especially since conformance testsuite does not need it as there is a solution with the attachments)

@tdiesler tdiesler closed this May 12, 2026
@tdiesler
tdiesler deleted the ghi48542 branch May 12, 2026 11:04
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.

[OAuth] Prefer authorization error on redirect_uri

4 participants

0