Skip to content

Commit 7c2474f

Browse files
committed
Handle invalid regex pattern in EnvironmentEndpoint
Closes gh-49884 Signed-off-by: Lee JiWon <dlwldnjs1009@gmail.com>
1 parent c97a3f7 commit 7c2474f

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

module/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import java.util.Map;
2424
import java.util.function.Predicate;
2525
import java.util.regex.Pattern;
26+
import java.util.regex.PatternSyntaxException;
2627
import java.util.stream.Stream;
2728

2829
import com.fasterxml.jackson.annotation.JsonInclude;
2930
import org.jspecify.annotations.Nullable;
3031

32+
import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
3133
import org.springframework.boot.actuate.endpoint.OperationResponseBody;
3234
import org.springframework.boot.actuate.endpoint.SanitizableData;
3335
import org.springframework.boot.actuate.endpoint.Sanitizer;
@@ -87,11 +89,21 @@ public EnvironmentDescriptor environment(@Nullable String pattern) {
8789

8890
EnvironmentDescriptor getEnvironmentDescriptor(@Nullable String pattern, boolean showUnsanitized) {
8991
if (StringUtils.hasText(pattern)) {
90-
return getEnvironmentDescriptor(Pattern.compile(pattern).asPredicate(), showUnsanitized);
92+
return getEnvironmentDescriptor(getPatternPredicate(pattern), showUnsanitized);
9193
}
9294
return getEnvironmentDescriptor((name) -> true, showUnsanitized);
9395
}
9496

97+
private Predicate<String> getPatternPredicate(String pattern) {
98+
try {
99+
return Pattern.compile(pattern).asPredicate();
100+
}
101+
catch (PatternSyntaxException ex) {
102+
throw new InvalidEndpointRequestException("Pattern '" + pattern + "' is not a valid regular expression",
103+
ex.getMessage());
104+
}
105+
}
106+
95107
private EnvironmentDescriptor getEnvironmentDescriptor(Predicate<String> propertyNamePredicate,
96108
boolean showUnsanitized) {
97109
List<PropertySourceDescriptor> propertySources = new ArrayList<>();

module/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.junit.jupiter.api.AfterEach;
2929
import org.junit.jupiter.api.Test;
3030

31+
import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
3132
import org.springframework.boot.actuate.endpoint.Show;
3233
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor;
3334
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentEntryDescriptor;
@@ -50,6 +51,7 @@
5051
import org.springframework.mock.env.MockPropertySource;
5152

5253
import static org.assertj.core.api.Assertions.assertThat;
54+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5355

5456
/**
5557
* Tests for {@link EnvironmentEndpoint}.
@@ -71,6 +73,14 @@ void close() {
7173
System.clearProperty("VCAP_SERVICES");
7274
}
7375

76+
@Test
77+
void invalidPatternThrowsInvalidEndpointRequestException() {
78+
ConfigurableEnvironment environment = emptyEnvironment();
79+
EnvironmentEndpoint endpoint = new EnvironmentEndpoint(environment, Collections.emptyList(), Show.ALWAYS);
80+
assertThatExceptionOfType(InvalidEndpointRequestException.class).isThrownBy(() -> endpoint.environment("["))
81+
.withMessageContaining("Pattern '[' is not a valid regular expression");
82+
}
83+
7484
@Test
7585
void basicResponse() {
7686
ConfigurableEnvironment environment = emptyEnvironment();

0 commit comments

Comments
 (0)