Skip to content

Fix: enforce AuthnContextClassRef validation in SAML broker (comparison=exact) - #50044

Closed
stav-berkovitch wants to merge 1 commit into
keycloak:mainfrom
stav-berkovitch:fix/saml-authn-context-classref-validation
Closed

stav-berkovitch wants to merge 1 commit into
keycloak:mainfrom
stav-berkovitch:fix/saml-authn-context-classref-validation

Conversation

@stav-berkovitch

Copy link
Copy Markdown

Problem

Keycloak sends authnContextClassRefs in the outbound AuthnRequest but never
validates the ClassRef returned in the SAML Response. An attacker controlling
an IdP can return any ClassRef (e.g. unspecified) and Keycloak accepts it,
bypassing authentication-level requirements such as MFA.

Vulnerable code: SAMLEndpoint.java — after extracting the AuthnStatement,
callback.authenticated() is called immediately without comparing the returned
ClassRef against the configured list.

Fix

After extracting the AuthnStatement, validate that the returned
AuthnContextClassRef is present in config.getAuthnContextClassRefs() when
comparison=exact (SAML 2.0 Core §3.3.2.2.1). If it doesn't match, reject
with INVALID_SAML_RESPONSE.

Testing

  • Legitimate IdP returning correct ClassRef → accepted ✅
  • Attacker IdP returning mismatched ClassRef → rejected ✅

Backwards compatibility

Realms that do not configure authnContextClassRefs are unaffected.

…on=exact)

Keycloak configured authnContextClassRefs in the outbound AuthnRequest but
never validated the ClassRef returned in the SAML Response. An attacker
controlling an IdP (or performing a MitM on the SAML flow) could return any
AuthnContextClassRef — including the weakest
(urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified) — and Keycloak would
accept it unconditionally, effectively bypassing authentication-level
requirements such as MFA or PasswordProtectedTransport.

Fix: after extracting the AuthnStatement, validate that the returned
AuthnContextClassRef is present in the configured list when comparison=exact
(the SAML 2.0 default). If validation fails, the response is rejected with
INVALID_SAML_RESPONSE and the user sees IDENTITY_PROVIDER_AUTHENTICATION_FAILED.

Backwards-compatible: realms that do not configure authnContextClassRefs are
unaffected. comparison=minimum/maximum/better are not enforced here as they
require an authentication-class hierarchy not defined in the codebase.

Ref: SAML 2.0 Core §3.3.2.2.1
@stav-berkovitch
stav-berkovitch requested a review from a team as a code owner June 16, 2026 16:25
@stav-berkovitch

Copy link
Copy Markdown
Author

@NadavWizman @danSayag — thanks for the collaboration on this fix.

@ahus1

ahus1 commented Jun 16, 2026

Copy link
Copy Markdown
Member

Thank you for contributing this PR. Please link this PR to an issue, and also sign-off your commit as outlined in CONTRIBUTING.md

@mjatin2007

Copy link
Copy Markdown
Contributor

Hi @stav-berkovitch and @ahus1,

I was reviewing the SAML broker flow and saw that the response-side AuthnContextClassRef check proposed here still appears to be missing on current main. I also saw the request for an issue link and DCO sign-off.

If @stav-berkovitch is no longer planning to pick it up, I’d be happy to help with current-main regression tests and any needed implementation updates, while keeping credit for the existing work. Should I open the requested issue first, contribute here, or leave this with you?

@danSayag

Copy link
Copy Markdown

to pick it up, I’d be happy to help with current-main regression tests and any needed implementation updates, while keeping credit for the existing work. Should I open the requested issue first, contribute

Hi,

Just to clarify, I was part of the team working with @stav-berkovitch when we investigated and identified this issue.

I’d be happy to help move this forward. How would you recommend we proceed from here? Should I open the requested issue and work on the regression tests and any necessary implementation updates, or would you prefer a different approach?

Thanks!

@mjatin2007

Copy link
Copy Markdown
Contributor

Thanks @danSayag, and thanks to you and the team for the original work. Would you be happy to open the issue @ahus1 requested and link it here? I’d be glad to help wherever useful, including implementation, tests, or review, once we agree on the next steps together.

@ahus1

ahus1 commented Nov 17, 2026

Copy link
Copy Markdown
Member

The original author didn't respond, and didn't add a sign-off which we as an open source project need. This PR is stale and can't move further without that.

I've opened an issue, but it would need more input from SAML experts: #55261

As this PR is stale, I close it to open the path for a new PR that has a sign-off.

@ahus1 ahus1 closed this Nov 17, 2026
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.

6 participants