Skip to content

Commit 43d3abd

Browse files
committed
Fix SockJs CorsConfiguration for forbidden origins
After this commit, AbstractSockJsService uses the configured allowed origins when generating the CorsConfiguration instead of "*". As a consequence, forbidden origin requests still result in a 403 response but now with no CORS headers in order to improve consistency between the status code and the headers. Issue: SPR-16304
1 parent 3ae776b commit 43d3abd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.nio.charset.StandardCharsets;
21+
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Collection;
2324
import java.util.Collections;
@@ -492,7 +493,7 @@ protected boolean checkOrigin(ServerHttpRequest request, ServerHttpResponse resp
492493
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
493494
if (!this.suppressCors && CorsUtils.isCorsRequest(request)) {
494495
CorsConfiguration config = new CorsConfiguration();
495-
config.addAllowedOrigin("*");
496+
config.setAllowedOrigins(new ArrayList<>(this.allowedOrigins));
496497
config.addAllowedMethod("*");
497498
config.setAllowCredentials(true);
498499
config.setMaxAge(ONE_YEAR);

spring-websocket/src/test/java/org/springframework/web/socket/sockjs/support/SockJsServiceTests.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.http.server.ServletServerHttpResponse;
3333
import org.springframework.scheduling.TaskScheduler;
3434
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
35+
import org.springframework.web.cors.CorsConfiguration;
3536
import org.springframework.web.socket.AbstractHttpRequestTests;
3637
import org.springframework.web.socket.WebSocketHandler;
3738
import org.springframework.web.socket.sockjs.SockJsException;
@@ -176,7 +177,7 @@ public void handleInfoOptions() throws Exception {
176177
}
177178

178179
@Test // SPR-12226 and SPR-12660
179-
public void handleInfoOptionsWithOrigin() throws Exception {
180+
public void handleInfoOptionsWithAllowedOrigin() throws Exception {
180181
this.servletRequest.setServerName("mydomain2.com");
181182
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
182183
this.servletRequest.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
@@ -196,10 +197,22 @@ public void handleInfoOptionsWithOrigin() throws Exception {
196197
this.service.setAllowedOrigins(Arrays.asList("*"));
197198
resetResponseAndHandleRequest("OPTIONS", "/echo/info", HttpStatus.NO_CONTENT);
198199
assertNotNull(this.service.getCorsConfiguration(this.servletRequest));
200+
}
199201

202+
@Test // SPR-16304
203+
public void handleInfoOptionsWithForbiddenOrigin() throws Exception {
200204
this.servletRequest.setServerName("mydomain3.com");
205+
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
206+
this.servletRequest.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
207+
this.servletRequest.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Last-Modified");
208+
resetResponseAndHandleRequest("OPTIONS", "/echo/info", HttpStatus.FORBIDDEN);
209+
CorsConfiguration corsConfiguration = this.service.getCorsConfiguration(this.servletRequest);
210+
assertTrue(corsConfiguration.getAllowedOrigins().isEmpty());
211+
201212
this.service.setAllowedOrigins(Arrays.asList("http://mydomain1.com"));
202213
resetResponseAndHandleRequest("OPTIONS", "/echo/info", HttpStatus.FORBIDDEN);
214+
corsConfiguration = this.service.getCorsConfiguration(this.servletRequest);
215+
assertEquals(Arrays.asList("http://mydomain1.com"), corsConfiguration.getAllowedOrigins());
203216
}
204217

205218
@Test // SPR-12283

0 commit comments

Comments
 (0)