Skip to content

Commit e935018

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 0747cd6 commit e935018

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

Lines changed: 3 additions & 2 deletions
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.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.nio.charset.Charset;
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<String>(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

Lines changed: 15 additions & 2 deletions
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.
@@ -31,6 +31,7 @@
3131
import org.springframework.http.server.ServletServerHttpResponse;
3232
import org.springframework.scheduling.TaskScheduler;
3333
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
34+
import org.springframework.web.cors.CorsConfiguration;
3435
import org.springframework.web.socket.AbstractHttpRequestTests;
3536
import org.springframework.web.socket.WebSocketHandler;
3637
import org.springframework.web.socket.sockjs.SockJsException;
@@ -172,7 +173,7 @@ public void handleInfoOptions() throws Exception {
172173
}
173174

174175
@Test // SPR-12226 and SPR-12660
175-
public void handleInfoOptionsWithOrigin() throws Exception {
176+
public void handleInfoOptionsWithAllowedOrigin() throws Exception {
176177
this.servletRequest.setServerName("mydomain2.com");
177178
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
178179
this.servletRequest.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
@@ -191,10 +192,22 @@ public void handleInfoOptionsWithOrigin() throws Exception {
191192
this.service.setAllowedOrigins(Arrays.asList("*"));
192193
resetResponseAndHandleRequest("OPTIONS", "/echo/info", HttpStatus.NO_CONTENT);
193194
assertNotNull(this.service.getCorsConfiguration(this.servletRequest));
195+
}
194196

197+
@Test // SPR-16304
198+
public void handleInfoOptionsWithForbiddenOrigin() throws Exception {
195199
this.servletRequest.setServerName("mydomain3.com");
200+
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
201+
this.servletRequest.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
202+
this.servletRequest.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Last-Modified");
203+
resetResponseAndHandleRequest("OPTIONS", "/echo/info", HttpStatus.FORBIDDEN);
204+
CorsConfiguration corsConfiguration = this.service.getCorsConfiguration(this.servletRequest);
205+
assertTrue(corsConfiguration.getAllowedOrigins().isEmpty());
206+
196207
this.service.setAllowedOrigins(Arrays.asList("http://mydomain1.com"));
197208
resetResponseAndHandleRequest("OPTIONS", "/echo/info", HttpStatus.FORBIDDEN);
209+
corsConfiguration = this.service.getCorsConfiguration(this.servletRequest);
210+
assertEquals(Arrays.asList("http://mydomain1.com"), corsConfiguration.getAllowedOrigins());
198211
}
199212

200213
@Test // SPR-12283

0 commit comments

Comments
 (0)