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
4 changes: 3 additions & 1 deletion .github/actions/update-hosts/hosts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
127.0.0.1 localtest.me admin.localtest.me localhost-myapp.localtest.me localhost-sso.localtest.me realmFrontend.localtest.me proxy.kc.localtest.me
::1 localtest.me admin.localtest.me localhost-myapp.localtest.me localhost-sso.localtest.me realmFrontend.localtest.me proxy.kc.localtest.me
::1 localtest.me admin.localtest.me localhost-myapp.localtest.me localhost-sso.localtest.me realmFrontend.localtest.me proxy.kc.localtest.me
127.0.0.1 redirect-loopback-v4.kc.test
::1 redirect-loopback-v6.kc.test
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ Include patterns for these literal values when your policy allows them. Selected

The sector identifier URL stored in a pairwise protocol mapper is validated separately and is outside this catalog.

The *Allow IPv4 loopback address* and *Allow IPv6 loopback address* options of *Secure Redirect URIs Enforcer* apply only to literal IP addresses.
Hostnames, including `localhost`, must match *Allow permitted domains* when it is configured, regardless of the addresses they resolve to.
The executor continues to reject `localhost` when *OAuth 2.1 Compliant* is enabled.
If the executor rejects a redirect URI during authorization, {project_name} displays an error page instead of redirecting to that URI.
Review previously registered redirect URIs when upgrading: this validation does not remove existing client configuration.

[[_full_scope_disabled_policy]]
== Enforcing Full Scope Allowed to be disabled

Expand Down
12 changes: 12 additions & 0 deletions docs/documentation/upgrading/topics/changes/changes-27_0_0.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
Breaking changes are identified as those that might require changes for existing users to their configurations or applications.
In minor or patch releases, {project_name} will only introduce breaking changes to fix bugs.

=== Redirect URI loopback exceptions require literal IP addresses
// https://github.com/keycloak/keycloak/issues/54413

The *Allow IPv4 loopback address* and *Allow IPv6 loopback address* options of the *Secure Redirect URIs Enforcer* client policy executor now apply only to literal IP addresses.
Hostnames, including `localhost`, are evaluated using the ordinary scheme and permitted-domain rules, regardless of the addresses they resolve to.
The executor continues to reject `localhost` when *OAuth 2.1 Compliant* is enabled.

The executor also validates the effective redirect URI during authorization, so previously registered callbacks can now be rejected.
When a redirect URI is rejected, {project_name} displays an error page instead of redirecting to that URI.
Review affected client configurations and use literal loopback addresses where appropriate, or ensure that legitimate hostnames satisfy the configured scheme and permitted-domain rules.
Existing registrations are not automatically rewritten or removed.

=== Client registration policies restrict default roles and full scope (CVE-2026-109081)
// https://github.com/keycloak/keycloak/issues/54702

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
* @version $Revision: 1 $
*/
public final class Constants {
/** Client policy exception detail controlling whether an authorization error may redirect to the client. */
public static final String ERROR_REDIRECT_ALLOWED = "error_redirect_allowed";

public static final String ADMIN_CONSOLE_CLIENT_ID = "security-admin-console";
public static final String ADMIN_CLI_CLIENT_ID = "admin-cli";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ public enum ClientPolicyEvent {
REGISTER_PROTOCOL_MAPPER,
UPDATE_PROTOCOL_MAPPER,
UNREGISTER_PROTOCOL_MAPPER,

/**
* Validate the effective authorization redirect URI before any response can redirect to it.
* Conditions and executors receive this event normally. Executors unrelated to redirect URI
* validation should ignore it and perform their checks during the regular authorization event.
*/
AUTHORIZATION_REQUEST_REDIRECT_URI,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.keycloak.services.clientpolicy;

import java.util.HashMap;
import java.util.Map;

import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;

Expand All @@ -30,6 +33,7 @@ public class ClientPolicyException extends Exception {
private String error = OAuthErrorException.INVALID_REQUEST;
private String errorDetail ="NA";
private Status errorStatus = Response.Status.BAD_REQUEST;
private final Map<String, Object> details = new HashMap<>();

public ClientPolicyException(String error) {
super(error);
Expand Down Expand Up @@ -86,6 +90,10 @@ public void setErrorStatus(Status errorStatus) {
this.errorStatus = errorStatus;
}

public Map<String, Object> getDetails() {
return details;
}

/**
* If {@link ClientPolicyException} is used to notify the event so that it needs not to have stack trace.
* @return always null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
import org.keycloak.services.clientpolicy.ClientPolicyException;
import org.keycloak.services.clientpolicy.ClientPolicyVote;
import org.keycloak.services.clientpolicy.condition.AbstractClientPolicyConditionProvider;
import org.keycloak.services.clientpolicy.context.AuthorizationRequestRedirectUriContext;
import org.keycloak.services.clientpolicy.context.PreAuthorizationRequestContext;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.jboss.logging.Logger;

/**
* The class is a condition of client policies. On {@code PRE_AUTHORIZATION_REQUEST} event,
* it checks if the value of {@code client_id} parameter is URI and
* The class is a condition of client policies. On {@code PRE_AUTHORIZATION_REQUEST} and
* {@code AUTHORIZATION_REQUEST_REDIRECT_URI} events, it checks if the client ID is a URI and
* the scheme part of the URI is the one defined in its configuration.
*
* @author <a href="mailto:takashi.norimatsu.ws@hitachi.com">Takashi Norimatsu</a>
Expand Down Expand Up @@ -69,26 +70,30 @@ public String getProviderId() {

@Override
public ClientPolicyVote applyPolicy(ClientPolicyContext context) throws ClientPolicyException {
String clientId;
switch (context.getEvent()) {
case PRE_AUTHORIZATION_REQUEST:
PreAuthorizationRequestContext paContext = (PreAuthorizationRequestContext) context;
String clientId = paContext.getRequestParameters().getFirst(OAuth2Constants.CLIENT_ID);
if (clientId == null || configuration.getClientIdUriSchemes() == null || configuration.getClientIdUriSchemes().isEmpty()) {
return ClientPolicyVote.NO;
}
final URI uri;
try {
uri = new URI(clientId);
} catch (URISyntaxException e) {
logger.debugv("not URL: clientId = {0}", clientId);
return ClientPolicyVote.NO;
}
if (isUriSchemeMatched(uri) && isTrustedDomainMatched(uri)) return ClientPolicyVote.YES;

return ClientPolicyVote.NO;
clientId = ((PreAuthorizationRequestContext) context).getRequestParameters().getFirst(OAuth2Constants.CLIENT_ID);
break;
case AUTHORIZATION_REQUEST_REDIRECT_URI:
clientId = ((AuthorizationRequestRedirectUriContext) context).getClient().getClientId();
break;
default:
return ClientPolicyVote.ABSTAIN;
}
if (clientId == null || configuration.getClientIdUriSchemes() == null || configuration.getClientIdUriSchemes().isEmpty()) {
return ClientPolicyVote.NO;
}
final URI uri;
try {
uri = new URI(clientId);
} catch (URISyntaxException e) {
logger.debugv("not URL: clientId = {0}", clientId);
return ClientPolicyVote.NO;
}
if (isUriSchemeMatched(uri) && isTrustedDomainMatched(uri)) return ClientPolicyVote.YES;

return ClientPolicyVote.NO;
}

private boolean isUriSchemeMatched(URI uri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.keycloak.services.Urls;
import org.keycloak.services.clientpolicy.ClientPolicyException;
import org.keycloak.services.clientpolicy.context.AuthorizationRequestContext;
import org.keycloak.services.clientpolicy.context.AuthorizationRequestRedirectUriContext;
import org.keycloak.services.clientpolicy.context.PreAuthorizationRequestContext;
import org.keycloak.services.managers.AuthenticationSessionManager;
import org.keycloak.services.messages.Messages;
Expand Down Expand Up @@ -170,6 +171,16 @@ private Response process(final MultivaluedMap<String, String> params) {
checker.throwAsErrorPageException(authenticationSession, ex);
}

try {
session.clientPolicy().triggerOnEvent(new AuthorizationRequestRedirectUriContext(client, request, redirectUri));
} catch (ClientPolicyException cpe) {
event.detail(Details.REASON, Details.CLIENT_POLICY_ERROR);
event.detail(Details.CLIENT_POLICY_ERROR, cpe.getError());
event.detail(Details.CLIENT_POLICY_ERROR_DETAIL, cpe.getErrorDetail());
event.error(cpe.getError());
throw ErrorPageException.oauthError(session, null, cpe.getErrorStatus(), cpe.getError(), cpe.getErrorDetail());
}

try {
checker.checkResponseType();
this.parsedResponseType = checker.getParsedResponseType();
Expand Down Expand Up @@ -218,6 +229,9 @@ private Response process(final MultivaluedMap<String, String> params) {
event.detail(Details.CLIENT_POLICY_ERROR_DETAIL, cpe.getErrorDetail());
event.error(cpe.getError());
new AuthenticationSessionManager(session).removeAuthenticationSession(realm, authenticationSession, false);
if (Boolean.FALSE.equals(cpe.getDetails().get(Constants.ERROR_REDIRECT_ALLOWED))) {
throw ErrorPageException.oauthError(session, null, cpe.getErrorStatus(), cpe.getError(), cpe.getErrorDetail());
}
return redirectErrorToClient(parsedResponseMode, cpe.getError(), cpe.getErrorDetail());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.keycloak.protocol.oidc.utils.AcrUtils;
import org.keycloak.representations.idm.ClientPolicyConditionConfigurationRepresentation;
import org.keycloak.services.clientpolicy.ClientPolicyContext;
import org.keycloak.services.clientpolicy.ClientPolicyEvent;
import org.keycloak.services.clientpolicy.ClientPolicyException;
import org.keycloak.services.clientpolicy.ClientPolicyVote;
import org.keycloak.services.clientpolicy.context.AuthorizationRequestContext;
import org.keycloak.services.clientpolicy.context.AuthorizationRequestParametersContext;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down Expand Up @@ -67,10 +67,11 @@ public String getProviderId() {

@Override
public ClientPolicyVote applyPolicy(ClientPolicyContext context) throws ClientPolicyException {
if (context.getEvent() == ClientPolicyEvent.AUTHORIZATION_REQUEST) {
AuthorizationRequestContext authorizationRequestContext = ((AuthorizationRequestContext) context);
if (context instanceof AuthorizationRequestParametersContext authorizationRequestContext) {
if (containsAcr(authorizationRequestContext)) {
authorizationRequestContext.getAuthenticationSession().setAuthNote(Constants.CLIENT_POLICY_REQUESTED_ACR, configuration.getAcrProperty());
if (context instanceof AuthorizationRequestContext authenticatedRequestContext) {
authenticatedRequestContext.getAuthenticationSession().setAuthNote(Constants.CLIENT_POLICY_REQUESTED_ACR, configuration.getAcrProperty());
}
return ClientPolicyVote.YES;
}
else {
Expand All @@ -80,7 +81,7 @@ public ClientPolicyVote applyPolicy(ClientPolicyContext context) throws ClientPo
return ClientPolicyVote.ABSTAIN;
}

private boolean containsAcr(AuthorizationRequestContext context) {
private boolean containsAcr(AuthorizationRequestParametersContext context) {
List<String> acrValues = AcrUtils.getAcrValues(context.getAuthorizationEndpointRequest().getClaims(), context.getAuthorizationEndpointRequest().getAcr(), session.getContext().getClient());
return acrValues != null && !acrValues.isEmpty() && acrValues.contains(configuration.getAcrProperty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.keycloak.services.clientpolicy.ClientPolicyContext;
import org.keycloak.services.clientpolicy.ClientPolicyException;
import org.keycloak.services.clientpolicy.ClientPolicyVote;
import org.keycloak.services.clientpolicy.context.AuthorizationRequestContext;
import org.keycloak.services.clientpolicy.context.AuthorizationRequestParametersContext;
import org.keycloak.services.clientpolicy.context.ImplicitHybridTokenResponse;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -72,8 +72,9 @@ public String getProviderId() {
public ClientPolicyVote applyPolicy(ClientPolicyContext context) throws ClientPolicyException {

switch (context.getEvent()) {
case AUTHORIZATION_REQUEST_REDIRECT_URI:
case AUTHORIZATION_REQUEST:
if (isGrantMatching((AuthorizationRequestContext)context)) return ClientPolicyVote.YES;
if (isGrantMatching((AuthorizationRequestParametersContext)context)) return ClientPolicyVote.YES;
return ClientPolicyVote.NO;
case TOKEN_REFRESH:
case TOKEN_REFRESH_RESPONSE:
Expand Down Expand Up @@ -115,7 +116,7 @@ public ClientPolicyVote applyPolicy(ClientPolicyContext context) throws ClientPo
}
}

private boolean isGrantMatching(AuthorizationRequestContext request) {
private boolean isGrantMatching(AuthorizationRequestParametersContext request) {
if (request == null) return false;
return isGrantMatchingResponseType(request.getAuthorizationEndpointRequest().getResponseType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.protocol.oidc.endpoints.request.AuthorizationEndpointRequest;
import org.keycloak.protocol.oidc.utils.OIDCResponseType;
import org.keycloak.services.clientpolicy.ClientPolicyContext;
import org.keycloak.services.clientpolicy.ClientPolicyEvent;
import org.keycloak.sessions.AuthenticationSessionModel;

/**
* @author <a href="mailto:takashi.norimatsu.ws@hitachi.com">Takashi Norimatsu</a>
*/
public class AuthorizationRequestContext implements ClientPolicyContext, ClientModelContext, ScopeParameterContext {
public class AuthorizationRequestContext implements AuthorizationRequestParametersContext {
Comment thread
lhanusov marked this conversation as resolved.
Resolved

private final OIDCResponseType parsedResponseType;
private final AuthorizationEndpointRequest request;
Expand Down Expand Up @@ -60,10 +59,12 @@ public OIDCResponseType getParsedResponseType() {
return parsedResponseType;
}

@Override
public AuthorizationEndpointRequest getAuthorizationEndpointRequest() {
return request;
}

@Override
public String getRedirectUri() {
return redirectUri;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.clientpolicy.context;

import org.keycloak.protocol.oidc.endpoints.request.AuthorizationEndpointRequest;

/**
* Resolved authorization request data available both before and after authentication session creation.
*/
public interface AuthorizationRequestParametersContext extends ClientModelContext, ScopeParameterContext {

AuthorizationEndpointRequest getAuthorizationEndpointRequest();

String getRedirectUri();

@Override
default String getScopeParameter() {
return getAuthorizationEndpointRequest().getScope();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.clientpolicy.context;

import org.keycloak.models.ClientModel;
import org.keycloak.protocol.oidc.endpoints.request.AuthorizationEndpointRequest;
import org.keycloak.services.clientpolicy.ClientPolicyEvent;

/**
* Resolved authorization parameters for redirect URI validation. Response type validation and
* authentication session creation have not happened yet.
*/
public class AuthorizationRequestRedirectUriContext implements AuthorizationRequestParametersContext {

private final ClientModel client;
private final AuthorizationEndpointRequest request;
private final String redirectUri;

public AuthorizationRequestRedirectUriContext(ClientModel client, AuthorizationEndpointRequest request,
String redirectUri) {
this.client = client;
this.request = request;
this.redirectUri = redirectUri;
}

@Override
public ClientPolicyEvent getEvent() {
return ClientPolicyEvent.AUTHORIZATION_REQUEST_REDIRECT_URI;
}

@Override
public AuthorizationEndpointRequest getAuthorizationEndpointRequest() {
return request;
}

@Override
public String getRedirectUri() {
return redirectUri;
}

@Override
public ClientModel getClient() {
return client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.keycloak.models.KeycloakSession;
import org.keycloak.representations.idm.ClientPolicyExecutorConfigurationRepresentation;
import org.keycloak.services.clientpolicy.ClientPolicyContext;
import org.keycloak.services.clientpolicy.ClientPolicyEvent;
import org.keycloak.services.clientpolicy.ClientPolicyException;

/**
Expand All @@ -38,6 +39,10 @@ public String getProviderId() {

@Override
public void executeOnEvent(ClientPolicyContext context) throws ClientPolicyException {
// Validate the callback before rejecting the request during the regular authorization event.
if (context.getEvent() == ClientPolicyEvent.AUTHORIZATION_REQUEST_REDIRECT_URI) {
return;
}
throw new ClientPolicyException(OAuthErrorException.INVALID_REQUEST, "Request not allowed");
}
}
Loading
Loading