Skip to content

Commit 1c17305

Browse files
committed
Check for null in allowedOrigin list
Closes gh-26987
1 parent 491d1c4 commit 1c17305

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Collections;
2323
import java.util.LinkedHashSet;
2424
import java.util.List;
25+
import java.util.Objects;
2526
import java.util.Set;
2627
import java.util.regex.Matcher;
2728
import java.util.regex.Pattern;
@@ -138,13 +139,13 @@ public CorsConfiguration(CorsConfiguration other) {
138139
* However an instance of this class is often initialized further, e.g. for
139140
* {@code @CrossOrigin}, via {@link #applyPermitDefaultValues()}.
140141
*/
141-
public void setAllowedOrigins(@Nullable List<String> allowedOrigins) {
142-
this.allowedOrigins = (allowedOrigins != null ?
143-
allowedOrigins.stream().map(this::trimTrailingSlash).collect(Collectors.toList()) : null);
142+
public void setAllowedOrigins(@Nullable List<String> origins) {
143+
this.allowedOrigins = (origins == null ? null :
144+
origins.stream().filter(Objects::nonNull).map(this::trimTrailingSlash).collect(Collectors.toList()));
144145
}
145146

146147
private String trimTrailingSlash(String origin) {
147-
return origin.endsWith("/") ? origin.substring(0, origin.length() - 1) : origin;
148+
return (origin.endsWith("/") ? origin.substring(0, origin.length() - 1) : origin);
148149
}
149150

150151
/**
@@ -158,7 +159,11 @@ public List<String> getAllowedOrigins() {
158159
/**
159160
* Variant of {@link #setAllowedOrigins} for adding one origin at a time.
160161
*/
162+
@SuppressWarnings("ConstantConditions")
161163
public void addAllowedOrigin(String origin) {
164+
if (origin == null) {
165+
return;
166+
}
162167
if (this.allowedOrigins == null) {
163168
this.allowedOrigins = new ArrayList<>(4);
164169
}
@@ -220,7 +225,11 @@ public List<String> getAllowedOriginPatterns() {
220225
* Variant of {@link #setAllowedOriginPatterns} for adding one origin at a time.
221226
* @since 5.3
222227
*/
228+
@SuppressWarnings("ConstantConditions")
223229
public void addAllowedOriginPattern(String originPattern) {
230+
if (originPattern == null) {
231+
return;
232+
}
224233
if (this.allowedOriginPatterns == null) {
225234
this.allowedOriginPatterns = new ArrayList<>(4);
226235
}

0 commit comments

Comments
 (0)