Skip to content

Commit ac5c361

Browse files
committed
Check the user of a SockJS request
Issue: SPR-12497 (backport of commit dc5b5c)
1 parent 328ba7b commit ac5c361

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ else if (transportType.supportsCors()) {
242242
return;
243243
}
244244
}
245+
else {
246+
if (session.getPrincipal() != null) {
247+
if (!session.getPrincipal().equals(request.getPrincipal())) {
248+
logger.debug("The user for the session does not match the user for the request.");
249+
response.setStatusCode(HttpStatus.NOT_FOUND);
250+
return;
251+
}
252+
}
253+
}
245254

246255
if (transportType.sendsNoCacheInstruction()) {
247256
addNoCacheHeaders(response);

spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsServiceTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.scheduling.TaskScheduler;
2828
import org.springframework.web.socket.AbstractHttpRequestTests;
2929
import org.springframework.web.socket.WebSocketHandler;
30+
import org.springframework.web.socket.handler.TestPrincipal;
3031
import org.springframework.web.socket.sockjs.transport.SockJsSessionFactory;
3132
import org.springframework.web.socket.sockjs.transport.TransportHandler;
3233
import org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService;
@@ -178,6 +179,28 @@ public void handleTransportRequestXhrSend() throws Exception {
178179
verify(this.xhrSendHandler).handleRequest(this.request, this.response, this.wsHandler, this.session);
179180
}
180181

182+
@Test
183+
public void handleTransportRequestXhrSendWithDifferentUser() throws Exception {
184+
String sockJsPath = sessionUrlPrefix + "xhr";
185+
setRequest("POST", sockJsPrefix + sockJsPath);
186+
this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
187+
188+
assertEquals(200, this.servletResponse.getStatus()); // session created
189+
verify(this.xhrHandler).handleRequest(this.request, this.response, this.wsHandler, this.session);
190+
191+
this.session.setPrincipal(new TestPrincipal("little red riding hood"));
192+
this.servletRequest.setUserPrincipal(new TestPrincipal("wolf"));
193+
194+
resetResponse();
195+
reset(this.xhrSendHandler);
196+
sockJsPath = sessionUrlPrefix + "xhr_send";
197+
setRequest("POST", sockJsPrefix + sockJsPath);
198+
this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
199+
200+
assertEquals(404, this.servletResponse.getStatus());
201+
verifyNoMoreInteractions(this.xhrSendHandler);
202+
}
203+
181204

182205
interface SessionCreatingTransportHandler extends TransportHandler, SockJsSessionFactory {
183206
}

0 commit comments

Comments
 (0)