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
This commit is contained in:
parent
8611b2c585
commit
720d23af41
|
|
@ -703,16 +703,18 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
|||
private void validateWildcardLocation(String path) {
|
||||
if (path.contains("*")) {
|
||||
Assert.state(StringUtils.countOccurrencesOf(path, "*") == 1,
|
||||
"Wildard pattern with multiple '*'s cannot be used as search location");
|
||||
() -> "Search location '" + path + "' cannot contain multiple wildcards");
|
||||
String directoryPath = path.substring(0, path.lastIndexOf("/") + 1);
|
||||
Assert.state(directoryPath.endsWith("*/"), "Wildcard patterns must end with '*/'");
|
||||
Assert.state(directoryPath.endsWith("*/"), () -> "Search location '" + path + "' must end with '*/'");
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> getSearchNames() {
|
||||
if (this.environment.containsProperty(CONFIG_NAME_PROPERTY)) {
|
||||
String property = this.environment.getProperty(CONFIG_NAME_PROPERTY);
|
||||
return asResolvedSet(property, null);
|
||||
Set<String> names = asResolvedSet(property, null);
|
||||
names.forEach(this::assertValidConfigName);
|
||||
return names;
|
||||
}
|
||||
return asResolvedSet(ConfigFileApplicationListener.this.names, DEFAULT_NAMES);
|
||||
}
|
||||
|
|
@ -724,6 +726,12 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
|||
return new LinkedHashSet<>(list);
|
||||
}
|
||||
|
||||
private void assertValidConfigName(String name) {
|
||||
Assert.state(!name.contains("*"), () -> "Config name '" + name + "' cannot contain wildcards");
|
||||
Assert.state(!name.contains("/") && !name.contains("\\"),
|
||||
() -> "Config name '" + name + "' cannot contain slashes");
|
||||
}
|
||||
|
||||
private void addLoadedPropertySources() {
|
||||
MutablePropertySources destination = this.environment.getPropertySources();
|
||||
List<MutablePropertySources> loaded = new ArrayList<>(this.loaded.values());
|
||||
|
|
|
|||
|
|
@ -1039,7 +1039,16 @@ class ConfigFileApplicationListenerTests {
|
|||
"spring.config.location=" + location);
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> this.initializer.postProcessEnvironment(this.environment, this.application))
|
||||
.withMessage("Wildcard patterns must end with '*/'");
|
||||
.withMessageStartingWith("Search location '").withMessageEndingWith("' must end with '*/'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void configNameCannotContainWildcard() {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
|
||||
"spring.config.location=file:src/test/resources/", "spring.config.name=*/application");
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> this.initializer.postProcessEnvironment(this.environment, this.application))
|
||||
.withMessage("Config name '*/application' cannot contain wildcards");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -1049,7 +1058,8 @@ class ConfigFileApplicationListenerTests {
|
|||
"spring.config.location=" + location);
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> this.initializer.postProcessEnvironment(this.environment, this.application))
|
||||
.withMessage("Wildard pattern with multiple '*'s cannot be used as search location");
|
||||
.withMessageStartingWith("Search location '")
|
||||
.withMessageEndingWith("' cannot contain multiple wildcards");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue