8000 Invalidating the rotated client secret returns 500 instead of 403 when the caller lacks permission · Issue #55391 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Invalidating the rotated client secret returns 500 instead of 403 when the caller lacks permission #55391

Description

@ksquare0608

Before reporting an issue

  • I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.

Area

admin/api

Bug Description

DELETE /admin/realms/{realm}/clients/{client-uuid}/client-secret/rotated returns 500 Internal Server Error when the caller does not have permission to configure the client. Other admin endpoints return 403 Forbidden for the same caller. The permission check itself works: I checked that an existing rotated secret is still there after the failed call. Only the response status is wrong, and the server logs a NullPointerException.

Version

26.8.2 (the same code is on main as of 994ee7e)

Regression

  • The issue is a regression

Expected behavior

403 Forbidden, as returned by the other admin endpoints for a caller without the required permission.

Actual behavior

500 Internal Server Error, with this in the server log:

ERROR [org.keycloak.services.error.KeycloakErrorHandler] Uncaught server error: java.lang.NullPointerException: Cannot invoke "java.lang.Throwable.getMessage()" because the return value of "java.lang.RuntimeException.getCause()" is null
	at org.keycloak.services.resources.admin.ClientResource.invalidateRotatedSecret(ClientResource.java:843)

How to Reproduce?

  1. Start Keycloak 26.8.2 with start-dev and an admin user.
  2. As admin, create a realm test, a confidential client in it, and a user noperm with a password and no admin roles.
  3. Get a token for noperm from the realm's admin-cli client:
curl -s -X POST http://localhost:8080/realms/test/protocol/openid-connect/token -d grant_type=password -d client_id=admin-cli -d username=noperm -d password=<password> | jq -r .access_token
  1. Call the endpoint with that token:
curl -s -o /dev/null -w '%{http_code}\n' -X DELETE -H "Authorization: Bearer <token>" http://localhost:8080/admin/realms/test/clients/<client-uuid>/client-secret/rotated

The response is 500. With the same token, other admin write endpoints I tried (for example PUT /client-policies/policies, POST /components, DELETE /identity-provider/instances/{alias}) return 403.

Anything else?

The permission check auth.clients().requireConfigure(client) runs inside a try whose catch (RuntimeException rte) builds the error with rte.getCause().getMessage(). ForbiddenException has no cause, so the catch block throws a NullPointerException:

public Response invalidateRotatedSecret() {
try{
auth.clients().requireConfigure(client);
logger.debug("delete rotated secret");
// Always remove rotated secret attributes even when the feature is disabled,
// so stale secrets cannot become valid again if the feature is re-enabled.
OIDCClientSecretConfigWrapper wrapper = OIDCClientSecretConfigWrapper.fromClientModel(client);
CredentialRepresentation rep = new CredentialRepresentation();
rep.setType(CredentialRepresentation.SECRET);
rep.setValue(wrapper.getClientRotatedSecret(session));
adminEvent.operation(OperationType.DELETE).resourcePath(session.getContext().getUri()).representation(rep).success();
wrapper.removeClientSecretRotated();
return Response.noContent().build();
} catch (RuntimeException rte) {
throw new ErrorResponseException(rte.getCause().getMessage(), rte.getMessage(),
Status.INTERNAL_SERVER_ERROR);
}

This pattern has been there since client secret rotation was added (#10603). regenerateSecret also calls requireConfigure inside a try, but only catches ClientPolicyException, and it returns 403 for the same caller as expected.

A possible fix is to call requireConfigure before the try, as most other methods in ClientResource do, and to stop relying on getCause() being non-null. I'd be happy to send a PR for this if that approach sounds right.

Activity

  1. atishay-kasliwal commented on Nov 20, 2026

    @atishay-kasliwal
    Contributor

    @ksquare0608 Looks like the catch block in invalidateRotatedSecret is the cause. requireConfigure throws a ForbiddenException with no cause, and rte.getCause().getMessage() then throws the NPE. Are you planning to send a fix? If not, I'd like to pick it up and let the forbidden exception pass through, with a test that expects 403.

  2. ksquare0608 commented on Nov 22, 2026

    @ksquare0608
    Author

    @atishay-kasliwal Thanks for confirming the cause! Yes, I'm planning to send a fix: moving requireConfigure before the try so the ForbiddenException passes through, and adding a test that expects 403. I'll open a PR in the next few days.

  3. added theissue type on Nov 25, 2026
  4. added
    kind/bug
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugstatus/triage

    Type

    Fields

    No fields configured for bug.

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0