Skip to content

Commit 2b7ec43

Browse files
committed
Merge branch '7.0.x'
2 parents 787f9e1 + 0044c4c commit 2b7ec43

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ else if (transportType.supportsCors()) {
321321
return;
322322
}
323323
InetSocketAddress remoteAddress = session.getRemoteAddress();
324-
if (remoteAddress != null && !remoteAddress.equals(request.getRemoteAddress())) {
324+
if (remoteAddress != null && !isSameAddress(remoteAddress, request.getRemoteAddress())) {
325325
logger.debug("The remote address for the session and the request do not match.");
326326
response.setStatusCode(HttpStatus.NOT_FOUND);
327327
return;
@@ -366,6 +366,19 @@ else if (transportType.supportsCors()) {
366366
}
367367
}
368368

369+
private boolean isSameAddress(InetSocketAddress address, InetSocketAddress that) {
370+
// InetSocketAddress#equals minus port checks, which can vary by requests
371+
if (address.getAddress() != null) {
372+
return address.getAddress().equals(that.getAddress());
373+
}
374+
else if (address.getHostName() != null) {
375+
return (that.getAddress() == null && address.getHostName().equalsIgnoreCase(that.getHostName()));
376+
}
377+
else {
378+
return (that.getAddress() == null) && (that.getHostName() == null);
379+
}
380+
}
381+
369382
@Override
370383
protected boolean validateRequest(String serverId, String sessionId, String transport) {
371384
if (!super.validateRequest(serverId, sessionId, transport)) {

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,9 @@ void handleTransportRequestXhrSendWithDifferentRemoteAddress() {
291291
assertThat(this.servletResponse.getStatus()).isEqualTo(200);
292292
verify(this.xhrHandler).handleRequest(this.request, this.response, this.wsHandler, this.session);
293293

294-
this.session.setRemoteAddress(new InetSocketAddress("127.0.0.1:8080", 8080));
295-
this.servletRequest.setRemoteAddr("127.0.0.1:9090");
294+
this.session.setRemoteAddress(new InetSocketAddress("0.0.0.0.1", 54001));
295+
this.servletRequest.setRemoteAddr("0.0.0.0.2");
296+
this.servletRequest.setRemotePort(54001);
296297

297298
resetResponse();
298299
reset(this.xhrSendHandler);
@@ -304,6 +305,32 @@ void handleTransportRequestXhrSendWithDifferentRemoteAddress() {
304305
verifyNoMoreInteractions(this.xhrSendHandler);
305306
}
306307

308+
@Test
309+
void handleTransportRequestXhrSendWithSameRemoteAddress() {
310+
String sockJsPath = sessionUrlPrefix + "xhr";
311+
setRequest("POST", sockJsPrefix + sockJsPath);
312+
this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
313+
314+
// session created
315+
assertThat(this.servletResponse.getStatus()).isEqualTo(200);
316+
verify(this.xhrHandler).handleRequest(this.request, this.response, this.wsHandler, this.session);
317+
318+
this.session.setRemoteAddress(new InetSocketAddress("0.0.0.0.1", 54001));
319+
this.servletRequest.setRemoteAddr("0.0.0.0.1");
320+
this.servletRequest.setRemotePort(54002); // port can vary
321+
322+
resetResponse();
323+
reset(this.xhrSendHandler);
324+
given(this.xhrSendHandler.checkSessionType(this.session)).willReturn(true);
325+
326+
sockJsPath = sessionUrlPrefix + "xhr_send";
327+
setRequest("POST", sockJsPrefix + sockJsPath);
328+
this.service.handleRequest(this.request, this.response, sockJsPath, this.wsHandler);
329+
330+
assertThat(this.servletResponse.getStatus()).isEqualTo(200);
331+
verify(this.xhrSendHandler).handleRequest(this.request, this.response, this.wsHandler, this.session);
332+
}
333+
307334
@Test
308335
void handleTransportRequestWebsocket() {
309336
TransportHandlingSockJsService wsService = new TransportHandlingSockJsService(

0 commit comments

Comments
 (0)