From 7b839951b7d2cb088a557c642c2dec65bc5953ce Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Tue, 03 Nov 2026 20:58:44 +0000
Subject: [PATCH] Bind service and proxy tickets to the service they were issued for

---
 src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java b/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
index 9e87a07..33f5ac0 100644
--- a/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
+++ b/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
@@ -12,8 +12,8 @@
 import org.keycloak.protocol.cas.mappers.CASAttributeMapper;
 import org.keycloak.protocol.cas.representations.CASErrorCode;
 import org.keycloak.protocol.cas.utils.CASValidationException;
+import org.keycloak.protocol.cas.utils.RedirectUtils;
 import org.keycloak.protocol.oidc.utils.OAuth2Code;
-import org.keycloak.protocol.oidc.utils.RedirectUtils;
 import org.keycloak.services.managers.AuthenticationManager;
 import org.keycloak.services.managers.UserSessionCrossDCManager;
 import org.keycloak.services.util.DefaultClientSessionContext;
@@ -22,6 +22,7 @@
 import java.util.HashMap;
 import java.util.HexFormat;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import java.util.UUID;
 import java.util.regex.Pattern;
@@ -39,6 +40,7 @@
     protected RealmModel realm;
     protected EventBuilder event;
     protected ClientModel client;
+    protected String service;
     protected AuthenticatedClientSessionModel clientSession;
     protected String pgtIou;
 
@@ -82,6 +84,8 @@
             throw new CASValidationException(CASErrorCode.INVALID_SERVICE, "Client disabled", Response.Status.BAD_REQUEST);
         }
 
+        this.service = RedirectUtils.verifyRedirectUri(session, service, client);
+
         event.client(client.getClientId());
 
         session.getContext().setClient(client);
@@ -98,7 +102,7 @@
             throw new CASValidationException(CASErrorCode.INVALID_TICKET_SPEC, "Malformed service ticket", Response.Status.BAD_REQUEST);
         }
 
-        String ticketValue = ticket.substring(prefix.length()); 
+        String ticketValue = ticket.substring(prefix.length());
 
         boolean isReusable = ticket.startsWith(CASLoginProtocol.PROXY_GRANTING_TICKET_PREFIX);
         // Check if the ticket is hex encoded or using old method
@@ -161,6 +165,12 @@
             throw new CASValidationException(CASErrorCode.INVALID_TICKET, "Code is expired", Response.Status.BAD_REQUEST);
         }
 
+        // Service and proxy tickets are valid only for the service they were issued for
+        if (!isReusable && (service == null || !service.equals(codeData.getRedirectUriParam()))) {
+            event.error(Errors.INVALID_REDIRECT_URI);
+            throw new CASValidationException(CASErrorCode.INVALID_SERVICE, "Ticket was not issued for this service", Response.Status.BAD_REQUEST);
+        }
+
         clientSession.setNote(CASLoginProtocol.SESSION_TICKET, ticket);
 
         if (requireReauth && AuthenticationManager.isSSOAuthentication(clientSession)) {
@@ -183,13 +193,19 @@
 
         if (client == null) {
             client = clientSession.getClient();
-        } else {
+        } else if (!prefix.equals(CASLoginProtocol.PROXY_TICKET_PREFIX)) {
+            // Proxy tickets are issued from the proxying client's session for a (possibly) different target service
             if (!client.getClientId().equals(clientSession.getClient().getClientId())) {
                 event.error(Errors.INVALID_CODE);
                 throw new CASValidationException(CASErrorCode.INVALID_SERVICE, "Invalid service", Response.Status.BAD_REQUEST);
             }
         }
 
+        if (codeData.getClientUUID() != null && !codeData.getClientUUID().equals(clientUUID)) {
+            event.error(Errors.INVALID_CODE);
+            throw new CASValidationException(CASErrorCode.INVALID_SERVICE, "Invalid client", Response.Status.BAD_REQUEST);
+        }
+
         if (!AuthenticationManager.isSessionValid(realm, userSession)) {
             event.error(Errors.USER_SESSION_NOT_FOUND);
             throw new CASValidationException(CASErrorCode.INVALID_TICKET, "Session not active", Response.Status.BAD_REQUEST);
@@ -270,7 +286,7 @@
     {
         String key = UUID.randomUUID().toString();
         UserSessionModel userSession = clientSession.getUserSession();
-        OAuth2Code codeData = new OAuth2Code(key, Time.currentTime() + userSession.getRealm().getAccessCodeLifespan(), null, null, redirectUriParam, null, null, null, userSession.getId());
+        OAuth2Code codeData = new OAuth2Code(key, clientSession.getClient().getId(), Time.currentTime() + userSession.getRealm().getAccessCodeLifespan(), null, null, null, redirectUriParam, null, null, null, userSession.getId());
         session.singleUseObjects().put(prefix + key, clientSession.getUserSession().getRealm().getAccessCodeLifespan(), codeData.serializeCode());
         return prefix + key + "--" + hexEncode(clientSession.getUserSession().getId()) + "--" + clientSession.getClient().getId();
     }

--
Gitblit v1.10.0