Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
Open
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 @@ -25,6 +25,7 @@
import org.keycloak.services.resources.admin.ClientResource;
import org.keycloak.services.resources.admin.ClientsResource;
import org.keycloak.services.resources.admin.RealmAdminResource;
import org.keycloak.validation.ValidationUtil;
import org.keycloak.validation.jakarta.HibernateValidatorProvider;
import org.keycloak.validation.jakarta.JakartaValidatorProvider;

Expand Down Expand Up @@ -98,6 +99,12 @@ public CreateOrUpdateResult createOrUpdate(RealmModel realm, BaseClientRepresent
clientResource = clientsResource.getClient(model.getId());

mapper.toModel(client, model);

// Validate the fully populated model (createClientModel only validates the basic model)
ValidationUtil.validateClient(session, model, true, r -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed also for the update... See here: https://github.com/keycloak/keycloak/blob/main/services/src/main/java/org/keycloak/services/resources/admin/ClientResource.java#L162

Would it be possible to move this validation at the end of the method, where you set the third boolean param to mark if it's create or update op?

Copy link
Contributor Author

@edewit edewit Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the code you show there is executed already form DefaultClientService

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edewit Ahh, sorry, you're right. So, please prepare the missing tests for the update. Thanks!

session.getTransactionManager().setRollbackOnly();
throw new ServiceException(r.getAllErrorsAsString(), Response.Status.BAD_REQUEST);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the v1, we use localized errors, but I think it does not have to be here for now as we don't have propagated the Auth context in the service.

So, +1 to keep this instead of the localized messages for now.

});
}

handleRoles(client.getRoles());
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned, we lack of the validation for the update. Thus, we're missing some test cases for update.

Bonus: It'd be good to abstract/generalize a little bit these test cases to avoid the duplication.

Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,77 @@ public void preflight() throws Exception {
}
}

@Test
public void createClientWithInvalidRedirectUriFragment() throws Exception {
OIDCClientRepresentation rep = new OIDCClientRepresentation();
rep.setEnabled(true);
rep.setClientId("client-invalid-fragment");
rep.setRedirectUris(Set.of("http://localhost:3000#fragment"));
assertClientCreationFailsWithError(rep, "Redirect URIs must not contain an URI fragment");
}

@Test
public void createClientWithInvalidRedirectUriScheme() throws Exception {
OIDCClientRepresentation rep = new OIDCClientRepresentation();
rep.setEnabled(true);
rep.setClientId("client-invalid-scheme");
rep.setRedirectUris(Set.of("javascript:alert(1)"));
assertClientCreationFailsWithError(rep, "Each redirect URL must be valid");
}

@Test
public void createClientWithInvalidRootUrl() throws Exception {
OIDCClientRepresentation rep = new OIDCClientRepresentation();
rep.setEnabled(true);
rep.setClientId("client-invalid-root-url");
rep.setAppUrl("http://localhost:3000#fragment");
assertClientCreationFailsWithError(rep, "Root URL must not contain an URL fragment");
}

@Test
public void createSamlClientWithInvalidRedirectUriFragment() throws Exception {
SAMLClientRepresentation rep = new SAMLClientRepresentation();
rep.setEnabled(true);
rep.setClientId("saml-client-invalid-fragment");
rep.setRedirectUris(Set.of("http://localhost:3000#fragment"));
assertClientCreationFailsWithError(rep, "Redirect URIs must not contain an URI fragment");
}

@Test
public void createSamlClientWithInvalidRedirectUriScheme() throws Exception {
SAMLClientRepresentation rep = new SAMLClientRepresentation();
rep.setEnabled(true);
rep.setClientId("saml-client-invalid-scheme");
rep.setRedirectUris(Set.of("javascript:alert(1)"));
assertClientCreationFailsWithError(rep, "Each redirect URL must be valid");
}

@Test
public void createSamlClientWithInvalidRootUrl() throws Exception {
SAMLClientRepresentation rep = new SAMLClientRepresentation();
rep.setEnabled(true);
rep.setClientId("saml-client-invalid-root-url");
rep.setAppUrl("http://localhost:3000#fragment");
assertClientCreationFailsWithError(rep, "Root URL must not contain an URL fragment");
}

/**
* Helper method to verify that client creation fails with the expected validation error.
* This verifies that ValidationUtil.validateClient is called after the full model is populated.
*/
private void assertClientCreationFailsWithError(BaseClientRepresentation rep, String expectedErrorMessage) throws Exception {
HttpPost request = new HttpPost(getClientsApiUrl());
setAuthHeader(request);
request.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
request.setEntity(new StringEntity(mapper.writeValueAsString(rep)));

try (var response = client.execute(request)) {
assertThat(response.getStatusLine().getStatusCode(), is(400));
String body = EntityUtils.toString(response.getEntity());
assertThat(body, containsString(expectedErrorMessage));
}
}

private OIDCClientRepresentation getTestingFullClientRep() {
var rep = new OIDCClientRepresentation();
rep.setClientId("my-client");
Expand Down