Skip to content

Commit aa51ed1

Browse files
committed
Fix failing tests
This commit ensures that if an Origin is returned as it was provided, possibly with a trailing slash. See gh-26892
1 parent dc4e053 commit aa51ed1

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -549,31 +549,31 @@ private List<OriginPattern> combinePatterns(
549549

550550
/**
551551
* Check the origin of the request against the configured allowed origins.
552-
* @param requestOrigin the origin to check
552+
* @param origin the origin to check
553553
* @return the origin to use for the response, or {@code null} which
554554
* means the request origin is not allowed
555555
*/
556556
@Nullable
557-
public String checkOrigin(@Nullable String requestOrigin) {
558-
if (!StringUtils.hasText(requestOrigin)) {
557+
public String checkOrigin(@Nullable String origin) {
558+
if (!StringUtils.hasText(origin)) {
559559
return null;
560560
}
561-
requestOrigin = trimTrailingSlash(requestOrigin);
561+
String originToCheck = trimTrailingSlash(origin);
562562
if (!ObjectUtils.isEmpty(this.allowedOrigins)) {
563563
if (this.allowedOrigins.contains(ALL)) {
564564
validateAllowCredentials();
565565
return ALL;
566566
}
567567
for (String allowedOrigin : this.allowedOrigins) {
568-
if (requestOrigin.equalsIgnoreCase(allowedOrigin)) {
569-
return requestOrigin;
568+
if (originToCheck.equalsIgnoreCase(allowedOrigin)) {
569+
return origin;
570570
}
571571
}
572572
}
573573
if (!ObjectUtils.isEmpty(this.allowedOriginPatterns)) {
574574
for (OriginPattern p : this.allowedOriginPatterns) {
575-
if (p.getDeclaredPattern().equals(ALL) || p.getPattern().matcher(requestOrigin).matches()) {
576-
return requestOrigin;
575+
if (p.getDeclaredPattern().equals(ALL) || p.getPattern().matcher(originToCheck).matches()) {
576+
return origin;
577577
}
578578
}
579579
}

spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@ public void checkOriginAllowed() {
294294
// specific origin matches Origin header with or without trailing "/"
295295
config.setAllowedOrigins(Collections.singletonList("https://domain.com"));
296296
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com");
297-
assertThat(config.checkOrigin("https://domain.com/")).isEqualTo("https://domain.com");
297+
assertThat(config.checkOrigin("https://domain.com/")).isEqualTo("https://domain.com/");
298298

299299
// specific origin with trailing "/" matches Origin header with or without trailing "/"
300300
config.setAllowedOrigins(Collections.singletonList("https://domain.com/"));
301301
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com");
302-
assertThat(config.checkOrigin("https://domain.com/")).isEqualTo("https://domain.com");
302+
assertThat(config.checkOrigin("https://domain.com/")).isEqualTo("https://domain.com/");
303303

304304
config.setAllowCredentials(false);
305305
assertThat(config.checkOrigin("https://domain.com")).isEqualTo("https://domain.com");

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/CrossOriginTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ void classLevelComposedAnnotation(TestRequestMappingInfoHandlerMapping mapping)
284284
CorsConfiguration config = getCorsConfiguration(chain, false);
285285
assertThat(config).isNotNull();
286286
assertThat(config.getAllowedMethods()).containsExactly("GET");
287-
assertThat(config.getAllowedOrigins()).containsExactly("http://www.foo.example/");
287+
assertThat(config.getAllowedOrigins()).containsExactly("http://www.foo.example");
288288
assertThat(config.getAllowCredentials()).isTrue();
289289
}
290290

@@ -297,7 +297,7 @@ void methodLevelComposedAnnotation(TestRequestMappingInfoHandlerMapping mapping)
297297
CorsConfiguration config = getCorsConfiguration(chain, false);
298298
assertThat(config).isNotNull();
299299
assertThat(config.getAllowedMethods()).containsExactly("GET");
300-
assertThat(config.getAllowedOrigins()).containsExactly("http://www.foo.example/");
300+
assertThat(config.getAllowedOrigins()).containsExactly("http://www.foo.example");
301301
assertThat(config.getAllowCredentials()).isTrue();
302302
}
303303

0 commit comments

Comments
 (0)