diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java index fe17f7a8477..dcdfa70aa0a 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java @@ -23,10 +23,12 @@ import java.util.List; import java.util.Map; import java.util.function.Predicate; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import java.util.stream.Stream; import com.fasterxml.jackson.annotation.JsonInclude; +import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException; import org.springframework.boot.actuate.endpoint.OperationResponseBody; import org.springframework.boot.actuate.endpoint.SanitizableData; import org.springframework.boot.actuate.endpoint.Sanitizer; @@ -87,11 +89,21 @@ public class EnvironmentEndpoint { EnvironmentDescriptor getEnvironmentDescriptor(String pattern, boolean showUnsanitized) { if (StringUtils.hasText(pattern)) { - return getEnvironmentDescriptor(Pattern.compile(pattern).asPredicate(), showUnsanitized); + return getEnvironmentDescriptor(parsePattern(pattern).asPredicate(), showUnsanitized); } return getEnvironmentDescriptor((name) -> true, showUnsanitized); } + private Pattern parsePattern(String pattern) { + try { + return Pattern.compile(pattern); + } + catch (PatternSyntaxException ex) { + throw new InvalidEndpointRequestException("Failed to parse regular expression: " + pattern, + "Invalid regular expression", ex); + } + } + private EnvironmentDescriptor getEnvironmentDescriptor(Predicate propertyNamePredicate, boolean showUnsanitized) { List propertySources = new ArrayList<>(); diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java index 484a58d101a..f3fa3ca0c3c 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java @@ -23,10 +23,12 @@ import java.math.BigInteger; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; +import java.util.regex.PatternSyntaxException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException; import org.springframework.boot.actuate.endpoint.Show; import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor; import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentEntryDescriptor; @@ -48,6 +50,7 @@ import org.springframework.core.io.InputStreamSource; import org.springframework.mock.env.MockPropertySource; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link EnvironmentEndpoint}. @@ -116,6 +119,15 @@ class EnvironmentEndpointTests { }); } + @Test + void responseWhenPatternIsInvalidThrowsInvalidEndpointRequestException() { + ConfigurableEnvironment environment = emptyEnvironment(); + EnvironmentEndpoint endpoint = new EnvironmentEndpoint(environment, Collections.emptyList(), Show.ALWAYS); + assertThatExceptionOfType(InvalidEndpointRequestException.class).isThrownBy(() -> endpoint.environment("[")) + .withMessageContaining("Failed to parse regular expression: [") + .withCauseInstanceOf(PatternSyntaxException.class); + } + @Test void compositeSourceIsHandledCorrectly() { ConfigurableEnvironment environment = emptyEnvironment();