diff --git a/common/src/main/java/org/keycloak/common/util/StringPropertyReplacer.java b/common/src/main/java/org/keycloak/common/util/StringPropertyReplacer.java index a85e9255dd..8e44ba6f90 100755 --- a/common/src/main/java/org/keycloak/common/util/StringPropertyReplacer.java +++ b/common/src/main/java/org/keycloak/common/util/StringPropertyReplacer.java @@ -238,7 +238,11 @@ public final class StringPropertyReplacer buffer.append(string.substring(start, chars.length)); if (buffer.indexOf("${") != -1) { - return replaceProperties(buffer.toString(), resolver); + try { + return replaceProperties(buffer.toString(), resolver); + } catch (StackOverflowError ex) { + throw new IllegalStateException("Infinite recursion happening when replacing properties on '" + buffer + "'"); + } } // Done diff --git a/common/src/test/java/org/keycloak/common/util/StringPropertyReplacerTest.java b/common/src/test/java/org/keycloak/common/util/StringPropertyReplacerTest.java index e019e1e37d..5421ffe1ba 100644 --- a/common/src/test/java/org/keycloak/common/util/StringPropertyReplacerTest.java +++ b/common/src/test/java/org/keycloak/common/util/StringPropertyReplacerTest.java @@ -21,7 +21,6 @@ package org.keycloak.common.util; import java.security.NoSuchAlgorithmException; import java.util.Map; -import java.util.Set; import org.junit.Assert; import org.junit.Test; @@ -59,6 +58,13 @@ public class StringPropertyReplacerTest { Assert.assertEquals("foo-val6", StringPropertyReplacer.replaceProperties("foo-${prop6,prop7:def}")); } + @Test + public void testStackOverflow() { + System.setProperty("prop", "${prop}"); + IllegalStateException ise = Assert.assertThrows(IllegalStateException.class, () -> StringPropertyReplacer.replaceProperties("${prop}")); + Assert.assertEquals("Infinite recursion happening when replacing properties on '${prop}'", ise.getMessage()); + } + @Test public void testEnvironmentVariables() throws NoSuchAlgorithmException { Map env = System.getenv();