Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ request results in a `400 Bad Request` error. Use the `PATCH` method to manage g

== Partially updating a group

To modify specific group attributes, use a `PATCH` request:
To modify specific group attributes, use a `PATCH` request.

NOTE: A single PATCH request can contain at most 100 operations. Requests exceeding this limit are rejected with
a `400 Bad Request` response and `scimType: "tooMany"`.

[source,bash]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ The `PATCH` method allows you to modify specific attributes without replacing th
contains one or more operations, each specifying an action (`add`, `replace`, or `remove`), an optional path to
the attribute, and a value.

NOTE: A single PATCH request can contain at most 100 operations. Requests exceeding this limit are rejected with
a `400 Bad Request` response and `scimType: "tooMany"`.

=== Adding or replacing attributes

[source,bash]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

public abstract class AbstractScimResourceTypeProvider<M extends Model, R extends ResourceTypeRepresentation> implements ScimResourceTypeProvider<R> {

/**
* Maximum number of operations allowed in a single SCIM PATCH request.
* Exceeding this limit results in a {@code 400 Bad Request} with {@code scimType=tooMany}.
* This limit is not advertised via {@code /ServiceProviderConfig}.
*/
public static final int MAX_PATCH_OPERATIONS = 100;

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.

@martin-kanis I think this is a valid point: the /ServiceProviderConfig endpoint will advertise maxOperations: 0 (meaning "no limit" or "not supported" depending on interpretation) while the server actually enforces 100. These should be consistent. We do something very similar for the filter's maxResults config. The value that is enforced is returned in the ServiceProviderConfig even if filtering is disabled.

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.

After further reviewing, I think this is not a valid suggestion. Mixing bulk parameters with patch in the provider config is semantically wrong. There's nothing in the RFC that suggests patch should advertise its max operations constraint via the standard ServiceProviderConfig.


protected final KeycloakSession session;
private final ModelSchema<M, R> schema;
private final List<ModelSchema<M, R>> schemaExtensions;
Expand Down Expand Up @@ -107,6 +114,12 @@ public boolean delete(String id) {
public void patch(R existing, List<PatchOperation> operations) {
Objects.requireNonNull(existing, "existing cannot be null");
Objects.requireNonNull(operations, "operations cannot be null");

if (operations.size() > MAX_PATCH_OPERATIONS) {
throw new ScimPatchException(
"PATCH request exceeds maximum allowed number of %d operations".formatted(MAX_PATCH_OPERATIONS));
Comment on lines +118 to +120

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.

The issue does mention bounding member/value arrays, so it's a legitimate gap in the PR. However, I would say this is a separate concern from the top-level operation count and could be addressed in a follow-up. Raising it as a suggestion for a separate PR is fair, blocking this PR on it seems a bit excessive.

Comment on lines +118 to +120

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.

Not an issue, every patch-capable provider extends the same abstract class and future providers (if any) will follow the same pattern

}

M model = getModel(existing.getId());

if (!hasPermission(model, getRealmResourceType(), AdminPermissionsSchema.MANAGE)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2026 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.keycloak.scim.resource.spi;

/**
* Exception thrown when a PATCH request exceeds the maximum allowed number of operations.
*/
public class ScimPatchException extends RuntimeException {

public ScimPatchException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.keycloak.scim.filter.ScimFilterException;
import org.keycloak.scim.protocol.ForbiddenException;
import org.keycloak.scim.protocol.response.ErrorResponse;
import org.keycloak.scim.resource.spi.ScimPatchException;
import org.keycloak.theme.Theme;

import org.jboss.logging.Logger;
Expand All @@ -38,6 +39,8 @@ static Response toResponse(KeycloakSession session, Exception e) {
return errorResponse(Status.CONFLICT, "uniqueness", "A resource with the same unique attribute already exists");
} else if (e instanceof ScimFilterException) {
return badRequest("invalidFilter", e.getMessage());
} else if (e instanceof ScimPatchException) {
return badRequest("tooMany", e.getMessage());
} else if (e instanceof ForbiddenException) {
logger.debug("SCIM request denied: caller does not have the required permissions");
return forbidden();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import static org.keycloak.scim.resource.Scim.ENTERPRISE_USER_SCHEMA;
import static org.keycloak.scim.resource.Scim.USER_RESOURCE_TYPE;
import static org.keycloak.scim.resource.Scim.getCoreSchema;
import static org.keycloak.scim.resource.spi.AbstractScimResourceTypeProvider.MAX_PATCH_OPERATIONS;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand All @@ -65,6 +66,7 @@
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -773,6 +775,21 @@ public void testPatchRemove() {
assertEquals("5678", actual.getEnterpriseUser().getCostCenter());
}

@Test
public void testPatchExceedingMaxOperations() {
User expected = client.users().create(createUser());
PatchRequest.Builder builder = PatchRequest.create();
for (int i = 0; i <= MAX_PATCH_OPERATIONS; i++) {
builder.add("displayName", "name-" + i);
}
ScimClientException ce = assertThrows(ScimClientException.class,
() -> client.users().patch(expected.getId(), builder.build()));
assertNotNull(ce.getError());
assertEquals(Status.BAD_REQUEST.getStatusCode(), ce.getError().getStatusInt());
assertEquals("tooMany", ce.getError().getScimType());
assertTrue(ce.getError().getDetail().contains("maximum allowed number"));
}

@Test
public void testUserMembership() {
GroupRepresentation groupA = createGroup("Group A");
Expand Down
Loading