From 760bd7b814836fca58922151abe67207cc0db1a2 Mon Sep 17 00:00:00 2001
From: Ricardo Martin <rmartinc@redhat.com>
Date: Tue, 09 Jan 2024 07:20:14 +0000
Subject: [PATCH] Escape action in the form_post.jwt and only decode path in RedirectUtils (#93) (#25995)
---
src/main/java/org/keycloak/protocol/cas/utils/RedirectUtils.java | 45 ++++++++++++++++++++++++---------------------
1 files changed, 24 insertions(+), 21 deletions(-)
diff --git a/src/main/java/org/keycloak/protocol/cas/utils/RedirectUtils.java b/src/main/java/org/keycloak/protocol/cas/utils/RedirectUtils.java
index 81669f5..1cae012 100644
--- a/src/main/java/org/keycloak/protocol/cas/utils/RedirectUtils.java
+++ b/src/main/java/org/keycloak/protocol/cas/utils/RedirectUtils.java
@@ -195,7 +195,7 @@
return redirectUri;
}
- // Decode redirectUri. We don't decode query and fragment as those can be encoded in the original URL.
+ // Decode redirectUri. Only path is decoded as other elements can be encoded in the original URL or cannot be encoded at all.
// URL can be decoded multiple times (in case it was encoded multiple times, or some of it's parts were encoded multiple times)
private static String decodeRedirectUri(String redirectUri) {
if (redirectUri == null) return null;
@@ -203,28 +203,20 @@
try {
KeycloakUriBuilder uriBuilder = KeycloakUriBuilder.fromUri(redirectUri, false).preserveDefaultPort();
- String origQuery = uriBuilder.getQuery();
- String origFragment = uriBuilder.getFragment();
- String origUserInfo = uriBuilder.getUserInfo();
- String encodedRedirectUri = uriBuilder
- .replaceQuery(null)
- .fragment(null)
- .userInfo(null)
- .buildAsString();
- String decodedRedirectUri = null;
+ if (uriBuilder.getPath() == null) {
+ return redirectUri;
+ }
+ String encodedPath = uriBuilder.getPath();
+ String decodedPath;
for (int i = 0; i < MAX_DECODING_COUNT; i++) {
- decodedRedirectUri = Encode.decode(encodedRedirectUri);
- if (decodedRedirectUri.equals(encodedRedirectUri)) {
- // URL is decoded. We can return it (after attach original query and fragment)
- return KeycloakUriBuilder.fromUri(decodedRedirectUri, false).preserveDefaultPort()
- .replaceQuery(origQuery)
- .fragment(origFragment)
- .userInfo(origUserInfo)
- .buildAsString();
+ decodedPath = Encode.decode(encodedPath);
+ if (decodedPath.equals(encodedPath)) {
+ // URL path is decoded. We can return it in the original redirect URI
+ return uriBuilder.replacePath(decodedPath, false).buildAsString();
} else {
// Next attempt
- encodedRedirectUri = decodedRedirectUri;
+ encodedPath = decodedPath;
}
}
} catch (IllegalArgumentException iae) {
@@ -248,13 +240,24 @@
return sb.toString();
}
+ // removes the queryString, fragment and userInfo from the redirect
+ // to avoid comparing this when wildcards are used
+ private static String stripOffRedirectForWildcard(String redirect) {
+ return KeycloakUriBuilder.fromUri(redirect, false)
+ .preserveDefaultPort()
+ .userInfo(null)
+ .replaceQuery(null)
+ .fragment(null)
+ .buildAsString();
+ }
+
// return the String that matched the redirect or null if not matched
private static String matchesRedirects(Set<String> validRedirects, String redirect, boolean allowWildcards) {
logger.tracef("matchesRedirects: redirect URL to check: %s, allow wildcards: %b, Configured valid redirect URLs: %s", redirect, allowWildcards, validRedirects);
for (String validRedirect : validRedirects) {
if (validRedirect.endsWith("*") && !validRedirect.contains("?") && allowWildcards) {
- // strip off the query component - we don't check them when wildcards are effective
- String r = redirect.contains("?") ? redirect.substring(0, redirect.indexOf("?")) : redirect;
+ // strip off the userInfo, query or fragment components - we don't check them when wildcards are effective
+ String r = stripOffRedirectForWildcard(redirect);
// strip off *
int length = validRedirect.length() - 1;
validRedirect = validRedirect.substring(0, length);
--
Gitblit v1.10.0