From 35375146dedfa14ff1c117d22b840b1ab3cc8f73 Mon Sep 17 00:00:00 2001 From: eva Date: Tue, 15 Feb 2022 17:07:28 +0100 Subject: [PATCH] #10227: RelayState max length not respected (fix) --- .../provider/util/IdentityBrokerState.java | 50 ++++++++++++- .../util/IdentityBrokerStateTest.java | 74 +++++++++++++++++++ 2 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 server-spi-private/src/test/java/org/keycloak/broker/provider/util/IdentityBrokerStateTest.java diff --git a/server-spi-private/src/main/java/org/keycloak/broker/provider/util/IdentityBrokerState.java b/server-spi-private/src/main/java/org/keycloak/broker/provider/util/IdentityBrokerState.java index 1783b1a46b7..2aa4dcd476a 100644 --- a/server-spi-private/src/main/java/org/keycloak/broker/provider/util/IdentityBrokerState.java +++ b/server-spi-private/src/main/java/org/keycloak/broker/provider/util/IdentityBrokerState.java @@ -18,6 +18,8 @@ package org.keycloak.broker.provider.util; import java.util.regex.Pattern; +import java.util.HashMap; +import java.util.Map; /** * Encapsulates parsing logic related to state passed to identity provider in "state" (or RelayState) parameter @@ -29,11 +31,20 @@ public class IdentityBrokerState { private static final Pattern DOT = Pattern.compile("\\."); + private static final int RELAY_STATE_MAX_LENGTH = 80; + private static final Map clientIdDigests = new HashMap<>(); public static IdentityBrokerState decoded(String state, String clientId, String tabId) { - String encodedState = state + "." + tabId + "." + clientId; + String clientIdDigest = clientId; + String encodedState = buildEncodedState(state, clientIdDigest, tabId); - return new IdentityBrokerState(state, clientId, tabId, encodedState); + if(encodedState.length() > RELAY_STATE_MAX_LENGTH) { + // Try to compress the clientId + clientIdDigest = getClientIdDigest(clientIdDigest); + encodedState = buildEncodedState(state, clientIdDigest, tabId); + } + + return new IdentityBrokerState(state, clientIdDigest, tabId, encodedState); } @@ -42,12 +53,45 @@ public static IdentityBrokerState encoded(String encodedState) { String state =(decoded.length > 0) ? decoded[0] : null; String tabId = (decoded.length > 1) ? decoded[1] : null; - String clientId = (decoded.length > 2) ? decoded[2] : null; + + // The clientId may have been digested previously. In that case, replace it with the original clientId. + String clientIdFromEncodedState = (decoded.length > 2) ? decoded[2] : null; + String clientId = tryLookupClientIdFromDigest(clientIdFromEncodedState); return new IdentityBrokerState(state, clientId, tabId, encodedState); } + private static String buildEncodedState(String state, String clientId, String tabId) { + return state + "." + tabId + "." + clientId; + } + + private static String getClientIdDigest(String clientId) { + // Note: The map may be accessed concurrently, but a key always gets the same value (since String.hashCode() is deterministic), + // so we don't need to worry about race conditions. + clientIdDigests.put(clientId, Integer.toString(clientId.hashCode())); + return clientIdDigests.get(clientId); + } + + private static String tryLookupClientIdFromDigest(String clientIdDigest) { + String foundClientId = null; + + for(String clientId : clientIdDigests.keySet()) { + String digest = clientIdDigests.get(clientId); + if(digest == null) { + throw new IllegalStateException(String.format("Digest for clientId %s was unexpectedly null!", clientId)); + } + + if(digest.equals(clientIdDigest)) { + if(foundClientId != null) { + throw new IllegalStateException(String.format("The clientId digest %s has multiple client id's mapped to it, impossible to reverse!", clientIdDigest)); + } + foundClientId = clientId; + } + } + + return foundClientId != null ? foundClientId : clientIdDigest; + } private final String decodedState; private final String clientId; diff --git a/server-spi-private/src/test/java/org/keycloak/broker/provider/util/IdentityBrokerStateTest.java b/server-spi-private/src/test/java/org/keycloak/broker/provider/util/IdentityBrokerStateTest.java new file mode 100644 index 00000000000..2a318c18884 --- /dev/null +++ b/server-spi-private/src/test/java/org/keycloak/broker/provider/util/IdentityBrokerStateTest.java @@ -0,0 +1,74 @@ +package org.keycloak.broker.provider.util; + +import org.junit.Test; +import org.keycloak.authorization.identity.Identity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class IdentityBrokerStateTest { + private static final int RELAY_STATE_MAX_LENGTH = 80; + + @Test + public void decoded_withinBounds_preservesClientId() { + // Arrange + // The length of the concatenated strings is below 80 characters, the RelayState limit as per the SAML specification. + String state = "gNrGamIDGKpKSI9yOrcFzYTKoFGH779_WNCacAelkhk"; + String tabId = "vpISZLVDAc0"; + String clientId = "foo"; + + // Act + IdentityBrokerState ibs = IdentityBrokerState.decoded(state, clientId, tabId); + + // Assert + assertEquals(clientId, ibs.getClientId()); + assertTrue(ibs.getEncoded().length() < RELAY_STATE_MAX_LENGTH); + } + + @Test + public void decoded_outOfBounds_compressesClientId() { + // Arrange + // The length of the concatenated strings is above 80 characters, the RelayState limit as per the SAML specification. + String state = "gNrGamIDGKpKSI9yOrcFzYTKoFGH779_WNCacAelkhk"; + String tabId = "vpISZLVDAc0"; + String clientId = "https://login.blablabla.com/auth/realms/broker"; + + // Act + IdentityBrokerState ibs = IdentityBrokerState.decoded(state, clientId, tabId); + + // Assert + assertTrue(ibs.getClientId().length() < clientId.length()); + assertTrue(ibs.getEncoded().length() < RELAY_STATE_MAX_LENGTH); + } + + @Test + public void encoded_compressedClientId_decompressesClientId() { + // Arrange + String state = "gNrGamIDGKpKSI9yOrcFzYTKoFGH779_WNCacAelkhk"; + String tabId = "vpISZLVDAc0"; + String clientId = "https://login.blablabla.com/auth/realms/broker"; + String encodedState = IdentityBrokerState.decoded(state, clientId, tabId).getEncoded(); + + // Act + IdentityBrokerState ibs = IdentityBrokerState.encoded(encodedState); + + // Assert + assertEquals(clientId, ibs.getClientId()); + } + + @Test + public void encoded_uncompressedClientId_preservesClientId() { + // Arrange + String state = "gNrGamIDGKpKSI9yOrcFzYTKoFGH779_WNCacAelkhk"; + String tabId = "vpISZLVDAc0"; + String clientId = "foo"; + String encodedState = IdentityBrokerState.decoded(state, clientId, tabId).getEncoded(); + + // Act + IdentityBrokerState ibs = IdentityBrokerState.encoded(encodedState); + + // Assert + assertEquals(clientId, ibs.getClientId()); + } +} +