Repository navigation
GroupMembershipMapper: don't mutate the cached mapper config in setClaim - #54761
Merged
mposolda merged 1 commit intoNov 16, 2026
Merged
Conversation
…aim (keycloak#54733) Signed-off-by: Inf1mum <vadim.lazuk@mail.ru>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
There are no outstanding review issues for the change to prevent shared mapper configuration mutations during token issuance.
No open issues 🎉
What changed in this PR
Addresses #54733 by preventing GroupMembershipMapper from mutating shared cached configuration during token issuance, while preserving multivalued group claims.
Changes:
- Copies the mapper model before forcing
multivalued, following the existingParameterizedScopeMapperpattern. - Adds unit tests for group claim output, configuration non-mutation, and repeated use of the same mapper model.
| File | Description |
|---|---|
| services/src/test/java/org/keycloak/protocol/oidc/mappers/GroupMembershipMapperTest.java | Adds regression tests for non-mutation and repeated token mapping. |
| services/src/main/java/org/keycloak/protocol/oidc/mappers/GroupMembershipMapper.java | Sets multivalued on a model copy instead of shared configuration. |
Review effort: Balanced
|
Confirmed this on main (ea78b16, JDK 21): a test that calls |
mposolda
approved these changes
Nov 16, 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.
Fixes #54733
Problem
GroupMembershipMapper.setClaimforce-enables themultivaluedflag by writing directly into the passedProtocolMapperModelon every token issuance:That model is not a per-request copy: cached clients/scopes (
AbstractCachedClientScope.mappersById) holdProtocolMapperModelinstances whoseconfigmap is shared by every token issued through the client. A concurrent structural modification of thatHashMap(e.g. two first tokens after a boot triggering the resize for the seventh key) can lose stored entries such asclaim.nameor the*.token.claimflags, permanently dropping thegroupsclaim for that client until the cache entry is rebuilt or the server restarts.Fix
Follow the existing
ParameterizedScopeMapperpattern: forcemultivaluedon a copy of the model (new ProtocolMapperModel(mappingModel)) instead of the shared one. Behavior for a single token is unchanged —mapClaimsees exactly the same config values.Testing
Added
GroupMembershipMapperTest(services unit tests):setClaimproduces thegroupsclaim as a list (forced multivalued) while leaving the mapping model config untouched (multivaluedmust not appear in the config of the shared model);setClaiminvocations reusing the same mapping model instance keep producing correct claims.The race itself is covered by the reproduction harness in the issue; the unit tests here pin the non-mutation contract that prevents it.
Other mappers
I checked the other
getConfig().putcall sites inorg.keycloak.protocol.oidc.mappers: the ones inScriptBasedOIDCProtocolMapper,UserRealmRoleMappingMapper,UserClientRoleMappingMapper,UserAttributeMapperandParameterizedScopeUserPropertyMapperare inside staticcreate(...)factories, andParameterizedScopeMapper.setClaimalready copies the model first — soGroupMembershipMapper.setClaimwas the only token-path write into the shared mapper model that I could find. If maintainers want, a broader guard (unmodifiable config map handed out by the cache, so token-time writes fail loudly) could be discussed separately.