Skip to content

Commit eab2b45

Browse files
committed
Add validateProfiles
1 parent f77c3bb commit eab2b45

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @author Madhura Bhave
5555
* @author Phillip Webb
5656
* @author Scott Frederick
57+
* @author Sijun Yang
5758
* @since 2.4.0
5859
*/
5960
public class StandardConfigDataLocationResolver
@@ -154,6 +155,7 @@ public List<StandardConfigDataResource> resolveProfileSpecific(ConfigDataLocatio
154155
private Set<StandardConfigDataReference> getProfileSpecificReferences(ConfigDataLocationResolverContext context,
155156
ConfigDataLocation[] configDataLocations, Profiles profiles) {
156157
Set<StandardConfigDataReference> references = new LinkedHashSet<>();
158+
validateProfiles(profiles);
157159
for (String profile : profiles) {
158160
for (ConfigDataLocation configDataLocation : configDataLocations) {
159161
String resourceLocation = getResourceLocation(context, configDataLocation);
@@ -163,6 +165,21 @@ private Set<StandardConfigDataReference> getProfileSpecificReferences(ConfigData
163165
return references;
164166
}
165167

168+
private void validateProfiles(Profiles profiles) {
169+
Pattern validProfilePattern = Pattern.compile("^[a-zA-Z0-9_\\-]+$");
170+
Assert.notNull(profiles, "Profiles must not be null");
171+
for (String profile : profiles) {
172+
Assert.notNull(profile, "Profile must not be null");
173+
Assert.hasText(profile, "Profile must not be empty");
174+
Assert.state(!profile.startsWith("-") && !profile.startsWith("_"),
175+
() -> String.format("Invalid profile '%s': must not start with '-' or '_'", profile));
176+
Assert.state(!profile.endsWith("-") && !profile.endsWith("_"),
177+
() -> String.format("Invalid profile '%s': must not end with '-' or '_'", profile));
178+
Assert.state(validProfilePattern.matcher(profile).matches(), () -> String
179+
.format("Invalid profile '%s': must only contain alphanumeric characters, '-', or '_'", profile));
180+
}
181+
}
182+
166183
private String getResourceLocation(ConfigDataLocationResolverContext context,
167184
ConfigDataLocation configDataLocation) {
168185
String resourceLocation = configDataLocation.getNonPrefixedValue(PREFIX);

0 commit comments

Comments
 (0)