Skip to content

GroupMembershipMapper: don't mutate the cached mapper config in setClaim - #54761

Merged
mposolda merged 1 commit into
keycloak:mainfrom
Inf1mum:fix/54733-group-membership-mapper-cache
Nov 16, 2026
Merged

mposolda merged 1 commit into
keycloak:mainfrom
Inf1mum:fix/54733-group-membership-mapper-cache

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #54733

Problem

GroupMembershipMapper.setClaim force-enables the multivalued flag by writing directly into the passed ProtocolMapperModel on every token issuance:

mappingModel.getConfig().put(ProtocolMapperUtils.MULTIVALUED, "true");

That model is not a per-request copy: cached clients/scopes (AbstractCachedClientScope.mappersById) hold ProtocolMapperModel instances whose config map is shared by every token issued through the client. A concurrent structural modification of that HashMap (e.g. two first tokens after a boot triggering the resize for the seventh key) can lose stored entries such as claim.name or the *.token.claim flags, permanently dropping the groups claim for that client until the cache entry is rebuilt or the server restarts.

Fix

Follow the existing ParameterizedScopeMapper pattern: force multivalued on a copy of the model (new ProtocolMapperModel(mappingModel)) instead of the shared one. Behavior for a single token is unchanged — mapClaim sees exactly the same config values.

Testing

Added GroupMembershipMapperTest (services unit tests):

  • setClaim produces the groups claim as a list (forced multivalued) while leaving the mapping model config untouched (multivalued must not appear in the config of the shared model);
  • repeated setClaim invocations 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().put call sites in org.keycloak.protocol.oidc.mappers: the ones in ScriptBasedOIDCProtocolMapper, UserRealmRoleMappingMapper, UserClientRoleMappingMapper, UserAttributeMapper and ParameterizedScopeUserPropertyMapper are inside static create(...) factories, and ParameterizedScopeMapper.setClaim already copies the model first — so GroupMembershipMapper.setClaim was 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.

@Inf1mum
Inf1mum requested a review from a team as a code owner November 6, 2026 14:05
Copilot AI balanced review requested due to automatic review settings November 6, 2026 14:05

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.

🟢 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 existing ParameterizedScopeMapper pattern.
  • 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

@jayhemnani9910

Copy link
Copy Markdown

Confirmed this on main (ea78b16, JDK 21): a test that calls setClaim() and compares the mapper config before and after fails without this change (multivalued=true is added to the cached config) and passes with it. The org.keycloak.protocol.oidc.mappers tests pass with the change.

@mposolda mposolda self-assigned this Nov 16, 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.

@Inf1mum Thanks!

@mposolda
mposolda merged commit eafe81d into keycloak:main Nov 16, 2026
96 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants