From 71aa94e99fd15e67ca200820c722f7ec5a3a97ad Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Tue, 03 Nov 2026 21:04:42 +0000
Subject: [PATCH] Require HTTPS for proxy granting ticket callback by default

---
 README.md                                                                       |    3 +++
 src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java |   17 ++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 3c48f4b..e0f0c1c 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,9 @@
 In such case append `/login` to the CAS URL to get the "login URL".
 Similarly append `/serviceValidate` to get the "validate URL".
 
+Proxy callback URLs (`pgtUrl`) must match the client's redirect URIs and use HTTPS.
+Plain HTTP is accepted only if the realm's "Require SSL" setting allows it.
+
 ## Disclaimer
 
 This plugin was implemented from scratch to comply to the official CAS protocol specification,
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 f510ebe..422c760 100644
--- a/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
+++ b/src/main/java/org/keycloak/protocol/cas/endpoints/AbstractValidateEndpoint.java
@@ -18,6 +18,7 @@
 import org.keycloak.services.managers.UserSessionCrossDCManager;
 import org.keycloak.services.util.DefaultClientSessionContext;
 
+import java.net.URI;
 import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.HexFormat;
@@ -228,16 +229,26 @@
     }
 
     protected void createProxyGrant(String pgtUrl) {
-        if ( RedirectUtils.verifyRedirectUri(session, pgtUrl, client) == null ) {
+        String verifiedPgtUrl = RedirectUtils.verifyRedirectUri(session, pgtUrl, client);
+        if (verifiedPgtUrl == null) {
             event.error(Errors.INVALID_REQUEST);
             throw new CASValidationException(CASErrorCode.INVALID_PROXY_CALLBACK, "Proxy callback is invalid", Response.Status.BAD_REQUEST);
         }
 
+        // The callback receives a PGT, so require HTTPS unless the realm's "Require SSL" setting allows plain HTTP for the host
+        URI pgtUri = URI.create(verifiedPgtUrl);
+        boolean isHttps = "https".equalsIgnoreCase(pgtUri.getScheme());
+        boolean isHttp = "http".equalsIgnoreCase(pgtUri.getScheme());
+        if (pgtUri.getHost() == null || !(isHttps || (isHttp && !realm.getSslRequired().isRequired(pgtUri.getHost())))) {
+            event.error(Errors.INVALID_REQUEST);
+            throw new CASValidationException(CASErrorCode.INVALID_PROXY_CALLBACK, "Proxy callback must use HTTPS", Response.Status.BAD_REQUEST);
+        }
+
         String pgtIou = getPGTIOU();
-        String pgtId  = getPGT(session, clientSession, pgtUrl);
+        String pgtId  = getPGT(session, clientSession, verifiedPgtUrl);
 
         try {
-            HttpGet request = new HttpGet(new URIBuilder(pgtUrl).setParameter("pgtIou",pgtIou).setParameter("pgtId",pgtId).build());
+            HttpGet request = new HttpGet(new URIBuilder(verifiedPgtUrl).setParameter("pgtIou",pgtIou).setParameter("pgtId",pgtId).build());
             request.setConfig(PGT_CALLBACK_REQUEST_CONFIG);
 
             HttpClient httpClient = session.getProvider(HttpClientProvider.class).getHttpClient();

--
Gitblit v1.10.0