Skip to content

Commit dc2947c

Browse files
committed
Ignore invalid connect frame
Closes gh-28443
1 parent e4ec376 commit dc2947c

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -306,6 +306,12 @@ protected void handleMessageInternal(Message<?> message) {
306306
else if (SimpMessageType.CONNECT.equals(messageType)) {
307307
logMessage(message);
308308
if (sessionId != null) {
309+
if (this.sessions.get(sessionId) != null) {
310+
if (logger.isWarnEnabled()) {
311+
logger.warn("Ignoring CONNECT in session " + sessionId + ". Already connected.");
312+
}
313+
return;
314+
}
309315
long[] heartbeatIn = SimpMessageHeaderAccessor.getHeartbeat(headers);
310316
long[] heartbeatOut = getHeartbeatValue();
311317
Principal user = SimpMessageHeaderAccessor.getUser(headers);

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -552,6 +552,12 @@ else if (accessor instanceof SimpMessageHeaderAccessor) {
552552
}
553553

554554
if (StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) {
555+
if (this.connectionHandlers.get(sessionId) != null) {
556+
if (logger.isWarnEnabled()) {
557+
logger.warn("Ignoring CONNECT in session " + sessionId + ". Already connected.");
558+
}
559+
return;
560+
}
555561
if (logger.isDebugEnabled()) {
556562
logger.debug(stompAccessor.getShortLogMessage(EMPTY_PAYLOAD));
557563
}

spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerTests.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -260,6 +260,30 @@ void systemSubscription() {
260260
assertThat(captor.getValue()).isSameAs(message);
261261
}
262262

263+
@Test
264+
void alreadyConnected() {
265+
266+
this.brokerRelay.start();
267+
268+
Message<byte[]> connect = connectMessage("sess1", "joe");
269+
this.brokerRelay.handleMessage(connect);
270+
271+
assertThat(this.tcpClient.getSentMessages().size()).isEqualTo(2);
272+
273+
StompHeaderAccessor headers1 = this.tcpClient.getSentHeaders(0);
274+
assertThat(headers1.getCommand()).isEqualTo(StompCommand.CONNECT);
275+
assertThat(headers1.getSessionId()).isEqualTo(StompBrokerRelayMessageHandler.SYSTEM_SESSION_ID);
276+
277+
StompHeaderAccessor headers2 = this.tcpClient.getSentHeaders(1);
278+
assertThat(headers2.getCommand()).isEqualTo(StompCommand.CONNECT);
279+
assertThat(headers2.getSessionId()).isEqualTo("sess1");
280+
281+
this.brokerRelay.handleMessage(connect);
282+
283+
assertThat(this.tcpClient.getSentMessages().size()).isEqualTo(2);
284+
assertThat(this.outboundChannel.getMessages()).isEmpty();
285+
}
286+
263287
private Message<byte[]> connectMessage(String sessionId, String user) {
264288
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
265289
headers.setSessionId(sessionId);

0 commit comments

Comments
 (0)