Skip to content

Commit e2705b2

Browse files
committed
Throw exception if classpath*: is used
Update `ConfigFileApplicationListener` to throw a better exception if `classpath*:` is used as a location. Closes gh-21168
1 parent 16005c6 commit e2705b2

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.springframework.core.io.DefaultResourceLoader;
6363
import org.springframework.core.io.Resource;
6464
import org.springframework.core.io.ResourceLoader;
65+
import org.springframework.core.io.support.ResourcePatternResolver;
6566
import org.springframework.core.io.support.SpringFactoriesLoader;
6667
import org.springframework.util.Assert;
6768
import org.springframework.util.CollectionUtils;
@@ -618,6 +619,8 @@ private Set<String> getSearchLocations(String propertyName) {
618619
for (String path : asResolvedSet(this.environment.getProperty(propertyName), null)) {
619620
if (!path.contains("$")) {
620621
path = StringUtils.cleanPath(path);
622+
Assert.state(!path.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX),
623+
"Classpath wildard patterns cannot be used as a search location");
621624
if (!ResourceUtils.isUrl(path)) {
622625
path = ResourceUtils.FILE_URL_PREFIX + path;
623626
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.springframework.util.StringUtils;
6464

6565
import static org.assertj.core.api.Assertions.assertThat;
66+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
6667

6768
/**
6869
* Tests for {@link ConfigFileApplicationListener}.
@@ -648,6 +649,15 @@ public void absoluteResourceDefaultsToFile() {
648649
matchingPropertySource("applicationConfig: [file:" + location.replace(File.separatorChar, '/') + "]"));
649650
}
650651

652+
@Test
653+
public void classpathWildcardResourceThrowsException() {
654+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
655+
"spring.config.location=classpath*:override.properties");
656+
assertThatIllegalStateException()
657+
.isThrownBy(() -> this.initializer.postProcessEnvironment(this.environment, this.application))
658+
.withMessage("Classpath wildard patterns cannot be used as a search location");
659+
}
660+
651661
@Test
652662
public void propertySourceAnnotation() {
653663
SpringApplication application = new SpringApplication(WithPropertySource.class);

0 commit comments

Comments
 (0)