FFFF Reject non-integer display order when creating or updating a client scope by kccrcodes · Pull Request #55824 · keycloak/keycloak · GitHub
[go: up one dir, main page]

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 @@ -141,6 +141,7 @@ public Response update(final ClientScopeRepresentation rep) {
rep.setProtocol(clientScope.getProtocol());
}
ClientScopeResource.validateClientScope(session, rep);
ClientScopeResource.validateClientScopeGuiOrder(rep);
validateParameterizedScopeUpdate(rep);
try {
LoginProtocolFactory loginProtocolFactory = //
Expand Down Expand Up @@ -251,6 +252,29 @@ public static void validateClientScopeName(String name) throws ErrorResponseExce
}
}

/**
* Validates that the display order of the client scope, when set, is an integer.
* A blank value is accepted because it means that the display order is not set.
*
* @param clientScope clientScope to validate
* @throws ErrorResponseException if the display order is not an integer
*/
public static void validateClientScopeGuiOrder(ClientScopeRepresentation clientScope) throws ErrorResponseException {
if (clientScope.getAttributes() == null) {
return;
}
String guiOrder = clientScope.getAttributes().get(ClientScopeModel.GUI_ORDER);
if (guiOrder == null || guiOrder.isBlank()) {
return;
}
try {
Integer.parseInt(guiOrder);
} catch (NumberFormatException e) {
String message = String.format("Unexpected value \"%s\" for ClientScope attribute \"%s\", expected an integer", guiOrder, ClientScopeModel.GUI_ORDER);
throw ErrorResponse.error(message, Response.Status.BAD_REQUEST);
}
}

public static void validateClientScopeProtocol(KeycloakSession session, String protocol)
throws ErrorResponseException {
KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public Response createClientScope(ClientScopeRepresentation rep) {
}
ClientScopeResource.validateClientScopeName(rep.getName());
ClientScopeResource.validateClientScopeProtocol(session, rep.getProtocol());
ClientScopeResource.validateClientScopeGuiOrder(rep);
ClientScopeResource.validateClientScope(session, rep);
ClientScopeResource.validateParameterizedClientScope(session, rep);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,66 @@ public void testAddFailureWithInvalidScopeName() {
Assertions.assertEquals("Unexpected name \"マルチバイト\" for ClientScope", error.getErrorMessage());
}

@ParameterizedTest
@ValueSource(strings = {"abc", "1.5", "1e3", " 5", "2147483648"})
public void testAddFailureWithInvalidGuiOrder(String guiOrder) {
ClientScopeRepresentation scopeRep = new ClientScopeRepresentation();
scopeRep.setName("scope-invalid-gui-order");
scopeRep.setProtocol("openid-connect");
scopeRep.setAttributes(new HashMap<>(Map.of(ClientScopeModel.GUI_ORDER, guiOrder)));

handleExpectedCreateFailure(scopeRep, 400,
"Unexpected value \"" + guiOrder + "\" for ClientScope attribute \"gui.order\", expected an integer");
Assertions.assertTrue(clientScopes().findAll().stream().noneMatch(s -> "scope-invalid-gui-order".equals(s.getName())));
}

@ParameterizedTest
@ValueSource(strings = {"abc", "1.5", "1e3", " 5", "2147483648"})
public void testUpdateFailureWithInvalidGuiOrder(String guiOrder) {
ClientScopeRepresentation scopeRep = new ClientScopeRepresentation();
scopeRep.setName("scope-update-invalid-gui-order");
scopeRep.setProtocol("openid-connect");
scopeRep.setAttributes(new HashMap<>(Map.of(ClientScopeModel.GUI_ORDER, "3")));
String scopeId = createClientScopeWithCleanup(scopeRep);

scopeRep = clientScopes().get(scopeId).toRepresentation();
scopeRep.getAttributes().put(ClientScopeModel.GUI_ORDER, guiOrder);

ErrorRepresentation error;
try (Response response = updateClientScopeRaw(scopeId, scopeRep)) {
Assertions.assertEquals(400, response.getStatus());
error = response.readEntity(ErrorRepresentation.class);
}
Assertions.assertEquals("Unexpected value \"" + guiOrder + "\" for ClientScope attribute \"gui.order\", expected an integer", error.getErrorMessage());
Assertions.assertEquals("3", clientScopes().get(scopeId).toRepresentation().getAttributes().get(ClientScopeModel.GUI_ORDER));
}

@Test
public void testGuiOrderIntegerOrBlankIsAccepted() {
ClientScopeRepresentation scopeRep = new ClientScopeRepresentation();
scopeRep.setName("scope-valid-gui-order");
scopeRep.setProtocol("openid-connect");
scopeRep.setAttributes(new HashMap<>(Map.of(ClientScopeModel.GUI_ORDER, "5")));
String scopeId = createClientScopeWithCleanup(scopeRep);
Assertions.assertEquals("5", clientScopes().get(scopeId).toRepresentation().getAttributes().get(ClientScopeModel.GUI_ORDER));

// a blank display order means that it is not set
scopeRep = clientScopes().get(scopeId).toRepresentation();
scopeRep.getAttributes().put(ClientScopeModel.GUI_ORDER, "");
clientScopes().get(scopeId).update(scopeRep);
AdminEventAssertion.assertEvent(adminEvents.poll(), OperationType.UPDATE, AdminEventPaths.clientScopeResourcePath(scopeId), scopeRep, ResourceType.CLIENT_SCOPE);
Assertions.assertNull(clientScopes().get(scopeId).toRepresentation().getAttributes().get(ClientScopeModel.GUI_ORDER));
}

private Response updateClientScopeRaw(String scopeId, ClientScopeRepresentation scopeRep) {
try {
clientScopes().get(scopeId).update(scopeRep);
return Response.noContent().build();
} catch (ClientErrorException e) {
return e.getResponse();
}
}

@Test
public void testUpdateFailureWithInvalidScopeName() {
// Creating first
Expand Down
0