Skip to content

Commit dc5b5ca

Browse files
committed
Check the user of a SockJS request
Issue: SPR-12497
1 parent 6e54fc9 commit dc5b5ca

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.web.socket.sockjs.transport;
1818

1919
import java.io.IOException;
20+
import java.net.InetSocketAddress;
21+
import java.security.Principal;
2022
import java.util.ArrayList;
2123
import java.util.Arrays;
2224
import java.util.Collection;
@@ -245,6 +247,15 @@ else if (transportType.supportsCors()) {
245247
return;
246248
}
247249
}
250+
else {
251+
if (session.getPrincipal() != null) {
252+
if (!session.getPrincipal().equals(request.getPrincipal())) {
253+
logger.debug("The user for the session does not match the user for the request.");
254+
response.setStatusCode(HttpStatus.NOT_FOUND);
255+
return;
256+
}
257+
}
258+
}
248259

249260
if (transportType.sendsNoCacheInstruction()) {
250261
addNoCacheHeaders(response);

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

+23
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.scheduling.TaskScheduler;
3030
import org.springframework.web.socket.AbstractHttpRequestTests;
3131
import org.springframework.web.socket.WebSocketHandler;
32+
import org.springframework.web.socket.handler.TestPrincipal;
3233
import org.springframework.web.socket.server.HandshakeHandler;
3334
import org.springframework.web.socket.server.support.OriginHandshakeInterceptor;
3435
import org.springframework.web.socket.sockjs.transport.SockJsSessionFactory;
@@ -243,6 +244,28 @@ public void handleTransportRequestXhrSend() throws Exception {
243244
verify(this.xhrSendHandler).handleRequest(this.request, this.response, this.wsHandler, this.session);
244245
}
245246

247+
@Test
248+
public void handleTransportRequestXhrSendWithDifferentUser() throws Exception {
249+
String sockJsPath = sessionUrlPrefix + "xhr";
250+
setRequest("POST", sockJsPrefix + sockJsPath);
251+
this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
252+
253+
assertEquals(200, this.servletResponse.getStatus()); // session created
254+
verify(this.xhrHandler).handleRequest(this.request, this.response, this.wsHandler, this.session);
255+
256+
this.session.setPrincipal(new TestPrincipal("little red riding hood"));
257+
this.servletRequest.setUserPrincipal(new TestPrincipal("wolf"));
258+
259+
resetResponse();
260+
reset(this.xhrSendHandler);
261+
sockJsPath = sessionUrlPrefix + "xhr_send";
262+
setRequest("POST", sockJsPrefix + sockJsPath);
263+
this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
264+
265+
assertEquals(404, this.servletResponse.getStatus());
266+
verifyNoMoreInteractions(this.xhrSendHandler);
267+
}
268+
246269
@Test
247270
public void handleTransportRequestJsonp() throws Exception {
248271
TransportHandlingSockJsService jsonpService = new TransportHandlingSockJsService(this.taskScheduler, this.jsonpHandler, this.jsonpSendHandler);

0 commit comments

Comments
 (0)