8000 Reject a negative Max Clients Per Realm value in the client registration policy by Praiztl · Pull Request #55820 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Draft
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 @@ -76,5 +76,9 @@ public String getId() {
public void validateConfiguration(KeycloakSession session, RealmModel realm, ComponentModel config) throws ComponentValidationException {
ConfigurationValidationHelper.check(config)
.checkInt(MAX_CLIENTS_PROPERTY, true);

if (config.get(MAX_CLIENTS, DEFAULT_MAX_CLIENTS) < 0) {
throw new ComponentValidationException("''{0}'' should not be a negative number", MAX_CLIENTS_PROPERTY.getLabel());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.services.clientregistration.policy.impl;

import org.keycloak.component.ComponentModel;
import org.keycloak.component.ComponentValidationException;

import org.junit.Assert;
import org.junit.Test;

public class MaxClientsClientRegistrationPolicyTest {

private final MaxClientsClientRegistrationPolicyFactory factory = new MaxClientsClientRegistrationPolicyFactory();

@Test
public void acceptsNonNegativeMaxClients() {
factory.validateConfiguration(null, null, model("200"));
factory.validateConfiguration(null, null, model("0"));
}

@Test
public void rejectsNegativeMaxClients() {
ComponentValidationException ex = Assert.assertThrows(ComponentValidationException.class,
() -> factory.validateConfiguration(null, null, model("-200")));
Assert.assertTrue("validation error should mention the value is negative",
ex.getMessage().toLowerCase().contains("negative"));
}

@Test
public void rejectsNonIntegerMaxClients() {
Assert.assertThrows(ComponentValidationException.class,
() -> factory.validateConfiguration(null, null, model("not-a-number")));
}

private ComponentModel model(String maxClients) {
ComponentModel model = new ComponentModel();
model.put(MaxClientsClientRegistrationPolicyFactory.MAX_CLIENTS, maxClients);
return model;
}
}
0