Have a more descriptive error message when infinite recusion happens (#26043)

Closes #21151

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz 2024-01-11 13:55:20 +01:00 committed by GitHub
parent 8e8ff33b1f
commit 3cdc69ddbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View file

@ -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

View file

@ -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<String, String> env = System.getenv();