diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 1b85a6622b..05880791fd 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -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 getSearchNames() { if (this.environment.containsProperty(CONFIG_NAME_PROPERTY)) { String property = this.environment.getProperty(CONFIG_NAME_PROPERTY); - return asResolvedSet(property, null); + Set 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 loaded = new ArrayList<>(this.loaded.values()); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 911c3b77e7..b6aa1d1077 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -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