NokiaTube | New address | Mode Desktop | Off high quality for Nokia Browser
Skip to content

Resolve SCIM PATCH value filters on any sub-attribute - #55611

Open
Prasad484 wants to merge 5 commits into
keycloak:mainfrom
Prasad484:scim-patch-filter-sub-attribute
Open

Prasad484 wants to merge 5 commits into
keycloak:mainfrom
Prasad484:scim-patch-filter-sub-attribute

Conversation

@Prasad484

@Prasad484 Prasad484 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Closes #55607

Why

Microsoft Entra ID changes a user's email with a replace on emails[type eq "work"].value (Microsoft's SCIM tutorial, "Update User [Multi-valued properties]"). RFC 7644 §3.5.2 allows a value selection filter on any sub-attribute of a multi-valued attribute, so this is a valid request. Keycloak turns the filter into the value to remove, which only works for value, and answers 400 invalidSyntax. Entra then resends the refused change in every later PATCH for that user. When the user is unassigned, Entra sends active false in the same PATCH as the pending email change, and the whole PATCH fails. A user whose email ever changed in Entra can then never be disabled by Entra.

Decisions

  • A filter that names a sub-attribute other than value is matched against the entries the resource holds now, through the new Path.select. This applies only to a complex multi-valued attribute of the core schema that is returned by default, which today means a user's emails. An attribute returned only on request, such as a group's members, keeps the existing value-only handling, so a filtered remove never reads every member of a large group.
  • A filter on value alone, such as members[value eq "<id>"], takes the existing path unchanged, so member removals by id behave as before.
  • A filter that matches no current value fails remove and replace with 400 noTarget (RFC 7644 §3.5.2.2, §3.5.2.3 and §3.12), and nothing changes. Carrying on is not safe here. The email remover clears the email whatever entries it is given, and add ignores the filter, so replace emails[type eq "home"].value would overwrite the user's only email. A new ScimNoTargetException is mapped to this error in Error.
  • A filter naming a sub-attribute the complex type does not have, such as emails[kind eq "work"], still answers 400, now with "Unknown attribute kind". A typo is reported instead of being treated as "no match". Nested value filters are refused the same way.
  • The matcher supports eq, ne, co, sw, ew and pr, combined with and, or and not. On a boolean sub-attribute such as primary, only eq and ne apply, and only with true or false. gt, ge, lt and le on a string answer 400. The whole filter is checked before any entry is compared, so an invalid branch of an or is refused too.
  • String comparisons ignore case, because type, display and value of the core multi-valued attributes are not case exact in RFC 7643.

Not changed

add with a filtered path still ignores the filter. Entra sends a user's first email as an add with the same path, and that already worked. A user still has one email, which Keycloak reports as type: work.

Review focus

AbstractModelSchema.remove and selectsByCurrentEntries, which decide when a filter is matched against the current entries, Path.select, the private validate that checks the whole filter first, and the private ScimFilterEntryMatcher in Path, which compares one entry with the filter, and the noTarget mapping in Error.

Local validation (main 8590b1b)

  • Before the fix, with only the new tests applied: ./mvnw -pl scim/tests/base test -Dtest=UserTest fails only testPatchReplace, at the first filtered replace emails[type eq "work"].value, which main refuses with 400 invalidSyntax.
  • After the fix, with the distribution rebuilt from this branch (quarkus/dist, which the test server is unpacked from): ./mvnw -pl scim/core,scim/tests/base test passes, with 62 unit tests and 313 integration tests (2 skipped, both already @Disabled), including UserTest 111 (2 skipped) and PathTest 11/11.
  • ./mvnw -Pspotless spotless:check -pl scim/core,scim/model,scim/services,scim/tests/base passes.

Tested with Microsoft Entra ID

The first version of this change, on 26.8.2, was run against Microsoft Entra ID's provisioning service on 27 November, using provision on demand. Entra created the user (201). It changed the email with the filtered Replace (200). On unassignment it sent one PATCH with active and the filtered email Replace together (200), and the user was disabled with the new email. Entra writes op names capitalised, and it sent active as the string "False", not the boolean the tutorial shows. Both are already accepted.

AI assistance: this change was prepared with Claude Code, an AI coding agent. I have reviewed it and will handle the review.

Microsoft Entra ID changes a user's email with a filtered replace such
as emails[type eq "work"].value. The filter is now resolved against the
current values of the attribute, so it can select on any sub-attribute
of a core complex multi-valued attribute, not only value.

A filter that matches no current value returns 400 noTarget (RFC 7644
sections 3.5.2.2, 3.5.2.3 and 3.12) instead of falling through to the
add half of a replace, which would overwrite a value the filter did not
select.

Closes keycloak#55607

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Prasad Rahatekar <[email protected]>
@Prasad484
Prasad484 marked this pull request as ready for review November 27, 2026 10:35
@Prasad484
Prasad484 requested a review from a team as a code owner November 27, 2026 10:35
Copilot AI balanced review requested due to automatic review settings November 27, 2026 10:35

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.

🟡 Changes recommended

SCIM PATCH filtering can accept invalid operators and delete email entries instead of returning 400, while stale-state and case-sensitive lookups also reject valid operations.

4 open issues
What changed in this PR

This PR extends Keycloak’s SCIM PATCH handling to support email filters such as emails[type eq "work"].value, addressing Microsoft Entra ID provisioning failures that can block email updates and account disabling.

Changes:

  • Matches non-value filters against current entries for eligible core attributes, while preserving existing value-only handling.
  • Returns 400 noTarget when a filtered remove or replace finds no matching entry.
  • Adds unit and integration coverage for email selection, rejected filters, and combined email/deactivation requests.
File Description
scim/​tests/​base/​src/​test/​java/​org/​keycloak/​tests/​scim/​tck/​UserTest.java Adds Entra-style PATCH and error-handling regression tests.
scim/​services/​src/​main/​java/​org/​keycloak/​scim/​services/​Error.java Maps the new exception to 400 noTarget.
scim/​core/​src/​test/​java/​org/​keycloak/​scim/​resource/​schema/​path/​PathTest.java Tests entry selection and filter behavior.
scim/​core/​src/​main/​java/​org/​keycloak/​scim/​resource/​spi/​ScimNoTargetException.java Introduces an exception for unmatched PATCH targets.
scim/​core/​src/​main/​java/​org/​keycloak/​scim/​resource/​schema/​path/​Path.java Adds current-entry selection and filter evaluation.
scim/​core/​src/​main/​java/​org/​keycloak/​scim/​resource/​schema/​AbstractModelSchema.java Routes eligible filters through selection and rejects unmatched targets.

Review effort: Balanced

Prasad484 and others added 2 commits November 27, 2026 17:30
The filter in a path such as emails[type eq "work"].value is now checked
once, before any entry is compared. Each sub-attribute must be one of
the complex type's, matched ignoring case (RFC 7643 section 2.1), each
operator must apply to the sub-attribute's type, so a boolean such as
primary takes only eq and ne, and nested value filters are refused.

Before, these checks ran only while matching an entry, so an "or" or
"and" could skip an invalid branch, and a filter on an attribute with
no entries was never checked at all.

Closes keycloak#55607

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Prasad Rahatekar <[email protected]>
The operations of one PATCH request apply in sequence (RFC 7644 section
3.5.2). A filter was matched against the resource read before the
request, so it did not see what an earlier operation of the same
request changed. A remove of emails followed by a filtered replace of
the email went through instead of answering noTarget.

The current entries are now read from the model, which holds the
changes of the earlier operations.

Closes keycloak#55607

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Prasad Rahatekar <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 12:00

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.

🔵 Needs a closer look

Invalid boolean comparisons and nested filters can still cause unintended email deletion or overwriting, while the leaver test does not verify an actual active-to-inactive transition.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Validate boolean operands before matching

scim/​core/​src/​main/​java/​org/​keycloak/​scim/​resource/​schema/​path/​Path.java:174

Checking only the operator lets remove emails[primary ne "abcde"] match the stored primary: true and delete the email instead of rejecting an invalid boolean comparison. Validate non-null boolean operands as true/false before matching, as ScimJPAPredicateProvider.parseBoolean already does.

Medium severity Reject nested value paths before legacy fallback

scim/​core/​src/​main/​java/​org/​keycloak/​scim/​resource/​schema/​path/​Path.java:204

A nested filter such as emails[value[value eq "[email protected]"]].value contributes only value names here, so filtersOnlyOnValue() returns true and bypasses the new nested-filter rejection. The legacy converter then ignores the nesting, allowing a remove or replace to clear or overwrite the email; reject value-path nodes during classification so these invalid paths cannot reach that fallback.

Medium severity Reactivate user before testing leaver transition

scim/​tests/​base/​src/​test/​java/​org/​keycloak/​tests/​scim/​tck/​UserTest.java:1110

This user was already disabled by the earlier PATCH (lines 1071–1078), so the new leaver test still passes if the combined request ignores its active operation. Reactivate the user and assert that state before this PATCH so the test verifies an actual true-to-false transition.

4 resolved since last review

Review effort: Balanced

A boolean sub-attribute such as primary is now compared only with true
or false, so emails[primary ne "abcde"] answers 400 instead of
selecting the email. A nested value filter such as
emails[value[value eq "..."]] is no longer treated as a filter on
value, which would have ignored the nesting, and is refused.

The leaver test now switches the user on before the combined request,
so it checks that active changes from true to false.

Closes keycloak#55607

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Prasad Rahatekar <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 12:10

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.

🟡 Changes recommended

The exception message embeds an unmatched brace from user input, causing MessageFormat.format to throw and return a 500 instead of the intended 400.

1 open issue

Review effort: Balanced

The SCIM error mapper uses a ModelValidationException message as a
MessageFormat pattern. Putting the value from the filter into the
message itself let a value such as "{" break the pattern and turn the
intended 400 into a 500. The value is now a parameter of the message.

Closes keycloak#55607

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Prasad Rahatekar <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 12:17

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.

🔵 Needs a closer look

Human assessment is needed to evaluate a subtle SCIM path-matching issue where Unicode-escaped operands cause valid paths to return noTarget for existing values.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes a PR related to a bug team/core-iam

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SCIM PATCH with a filter on emails[type eq "work"] is rejected, so Microsoft Entra ID cannot change a user's email or disable them

3 participants