Skip to content

Commit 720d23a

Browse files
committed
Further restrict configuration wildcard patterns
Extend wildcard restrictions to the `spring.config.name` property. Also refine exception messages to include the property value. Closes gh-21217
1 parent 8611b2c commit 720d23a

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,16 +703,18 @@ private Set<String> getSearchLocations(String propertyName) {
703703
private void validateWildcardLocation(String path) {
704704
if (path.contains("*")) {
705705
Assert.state(StringUtils.countOccurrencesOf(path, "*") == 1,
706-
"Wildard pattern with multiple '*'s cannot be used as search location");
706+
() -> "Search location '" + path + "' cannot contain multiple wildcards");
707707
String directoryPath = path.substring(0, path.lastIndexOf("/") + 1);
708-
Assert.state(directoryPath.endsWith("*/"), "Wildcard patterns must end with '*/'");
708+
Assert.state(directoryPath.endsWith("*/"), () -> "Search location '" + path + "' must end with '*/'");
709709
}
710710
}
711711

712712
private Set<String> getSearchNames() {
713713
if (this.environment.containsProperty(CONFIG_NAME_PROPERTY)) {
714714
String property = this.environment.getProperty(CONFIG_NAME_PROPERTY);
715-
return asResolvedSet(property, null);
715+
Set<String> names = asResolvedSet(property, null);
716+
names.forEach(this::assertValidConfigName);
717+
return names;
716718
}
717719
return asResolvedSet(ConfigFileApplicationListener.this.names, DEFAULT_NAMES);
718720
}
@@ -724,6 +726,12 @@ private Set<String> asResolvedSet(String value, String fallback) {
724726
return new LinkedHashSet<>(list);
725727
}
726728

729+
private void assertValidConfigName(String name) {
730+
Assert.state(!name.contains("*"), () -> "Config name '" + name + "' cannot contain wildcards");
731+
Assert.state(!name.contains("/") && !name.contains("\\"),
732+
() -> "Config name '" + name + "' cannot contain slashes");
733+
}
734+
727735
private void addLoadedPropertySources() {
728736
MutablePropertySources destination = this.environment.getPropertySources();
729737
List<MutablePropertySources> loaded = new ArrayList<>(this.loaded.values());

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,16 @@ void directoryLocationsWithWildcardShouldHaveWildcardAsLastCharacterBeforeSlash(
10391039
"spring.config.location=" + location);
10401040
assertThatIllegalStateException()
10411041
.isThrownBy(() -> this.initializer.postProcessEnvironment(this.environment, this.application))
1042-
.withMessage("Wildcard patterns must end with '*/'");
1042+
.withMessageStartingWith("Search location '").withMessageEndingWith("' must end with '*/'");
1043+
}
1044+
1045+
@Test
1046+
void configNameCannotContainWildcard() {
1047+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
1048+
"spring.config.location=file:src/test/resources/", "spring.config.name=*/application");
1049+
assertThatIllegalStateException()
1050+
.isThrownBy(() -> this.initializer.postProcessEnvironment(this.environment, this.application))
1051+
.withMessage("Config name '*/application' cannot contain wildcards");
10431052
}
10441053

10451054
@Test
@@ -1049,7 +1058,8 @@ void directoryLocationsWithMultipleWildcardsShouldThrowException() {
10491058
"spring.config.location=" + location);
10501059
assertThatIllegalStateException()
10511060
.isThrownBy(() -> this.initializer.postProcessEnvironment(this.environment, this.application))
1052-
.withMessage("Wildard pattern with multiple '*'s cannot be used as search location");
1061+
.withMessageStartingWith("Search location '")
1062+
.withMessageEndingWith("' cannot contain multiple wildcards");
10531063
}
10541064

10551065
@Test

0 commit comments

Comments
 (0)