From 3cdc69ddbb5c14c27653bc9b040330808fa879e7 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Thu, 11 Jan 2024 13:55:20 +0100 Subject: [PATCH] Have a more descriptive error message when infinite recusion happens (#26043) Closes #21151 Signed-off-by: Alexander Schwartz --- .../org/keycloak/common/util/StringPropertyReplacer.java | 6 +++++- .../keycloak/common/util/StringPropertyReplacerTest.java | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) 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();