Skip to content

Commit d7a475f

Browse files
committed
Merge branch '3.5.x' into 4.0.x
Closes gh-49946
2 parents 5c506b1 + a35de55 commit d7a475f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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(parsePattern(pattern).asPredicate(), showUnsanitized);
9193
}
9294
return getEnvironmentDescriptor((name) -> true, showUnsanitized);
9395
}
9496

97+
private Pattern parsePattern(String pattern) {
98+
try {
99+
return Pattern.compile(pattern);
100+
}
101+
catch (PatternSyntaxException ex) {
102+
throw new InvalidEndpointRequestException("Failed to parse regular expression: " + pattern,
103+
"Invalid regular expression", ex);
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import java.util.Collections;
2424
import java.util.LinkedHashMap;
2525
import java.util.Map;
26+
import java.util.regex.PatternSyntaxException;
2627

2728
import org.jspecify.annotations.Nullable;
2829
import org.junit.jupiter.api.AfterEach;
2930
import org.junit.jupiter.api.Test;
3031

32+
import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
3133
import org.springframework.boot.actuate.endpoint.Show;
3234
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor;
3335
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentEntryDescriptor;
@@ -50,6 +52,7 @@
5052
import org.springframework.mock.env.MockPropertySource;
5153

5254
import static org.assertj.core.api.Assertions.assertThat;
55+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5356

5457
/**
5558
* Tests for {@link EnvironmentEndpoint}.
@@ -134,6 +137,15 @@ void responseWhenShowWhenAuthorized() {
134137
});
135138
}
136139

140+
@Test
141+
void responseWhenPatternIsInvalidThrowsInvalidEndpointRequestException() {
142+
ConfigurableEnvironment environment = emptyEnvironment();
143+
EnvironmentEndpoint endpoint = new EnvironmentEndpoint(environment, Collections.emptyList(), Show.ALWAYS);
144+
assertThatExceptionOfType(InvalidEndpointRequestException.class).isThrownBy(() -> endpoint.environment("["))
145+
.withMessageContaining("Failed to parse regular expression: [")
146+
.withCauseInstanceOf(PatternSyntaxException.class);
147+
}
148+
137149
@Test
138150
void compositeSourceIsHandledCorrectly() {
139151
ConfigurableEnvironment environment = emptyEnvironment();

0 commit comments

Comments
 (0)