Fix ConcurrentModificationException in client-registration policy reads#50969
Merged
Conversation
The client-registration policies read their configuration with ComponentModel.getConfig().getList(key), which lazily inserts the key when it is absent. The ComponentModel is cached and shared across request threads, so concurrent dynamic client registrations structurally modify the same HashMap and one throws ConcurrentModificationException. Read the config the same non-mutating way as the rest of the codebase, getConfig().getOrDefault(key, emptyList()), which keeps the empty-on-absent contract getList provided and removes the mutation. Applied to the TrustedHost, ClientScopes, ProtocolMappers and WebOrigins policies. Closes keycloak#50286 Signed-off-by: Michael Stingl <mail@michaelstingl.com> Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com> Co-authored-by: Alexander Schwartz <alexander.schwartz@ibm.com>
Member
Author
|
@pruivo - please review when you have the time. Thanks! |
Unreported flaky test detectedIf 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#salesPostSigTransientTestKeycloak CI - Adapter IT Strict Cookies |
pruivo
reviewed
Jul 17, 2026
| if (allowedScopesConfig != null) { | ||
| allAllowed.addAll(allowedScopesConfig); | ||
| } | ||
| List<String> allowedScopesConfig = componentModel.getConfig().getOrDefault(ClientScopesClientRegistrationPolicyFactory.ALLOWED_CLIENT_SCOPES, Collections.emptyList()); |
Member
There was a problem hiding this comment.
@ahus1 this changes the semantics. Is this ok?
getList- if the key does not exist, it creates a list, stores in the map, and keeps track of anything added to the list.getOrDefault- if the key does not exist, it returns an unmodifiable list id
pruivo
reviewed
Jul 17, 2026
| public void validateConfiguration(KeycloakSession session, RealmModel realm, ComponentModel config) throws ComponentValidationException { | ||
| List<String> allowedScopesConfig = config.getConfig().getList(ClientScopesClientRegistrationPolicyFactory.ALLOWED_CLIENT_SCOPES); | ||
| List<String> allowedScopesConfig = config.getConfig().getOrDefault(ClientScopesClientRegistrationPolicyFactory.ALLOWED_CLIENT_SCOPES, Collections.emptyList()); | ||
| if (!getClientScopes(session).containsAll(allowedScopesConfig)) { |
Member
There was a problem hiding this comment.
getClientScopes(session).containsAll(allowedScopesConfig) is a O(N*M) complexity, where N is the size of getClientScopes list and M the size of allowedScopesConfig.
It may be problematic if these lists are large.
pruivo
approved these changes
Jul 17, 2026
pruivo
left a comment
Member
There was a problem hiding this comment.
Since this is a backport, I'm ok to approve it as is.
ahus1
enabled auto-merge (squash)
July 17, 2026 18:54
rmartinc
approved these changes
Jul 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The client-registration policies read their configuration with ComponentModel.getConfig().getList(key), which lazily inserts the key when it is absent. The ComponentModel is cached and shared across request threads, so concurrent dynamic client registrations structurally modify the same HashMap and one throws ConcurrentModificationException.
Read the config the same non-mutating way as the rest of the codebase, getConfig().getOrDefault(key, emptyList()), which keeps the empty-on-absent contract getList provided and removes the mutation. Applied to the TrustedHost, ClientScopes, ProtocolMappers and WebOrigins policies.
Closes #50286