Skip to content

Commit 159a99b

Browse files
committed
Ignore invalid STOMP frame
Closes gh-28444
1 parent 41e158c commit 159a99b

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Diff for: 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);

Diff for: 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-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.
@@ -532,6 +532,12 @@ else if (accessor instanceof SimpMessageHeaderAccessor) {
532532
}
533533

534534
if (StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) {
535+
if (this.connectionHandlers.get(sessionId) != null) {
536+
if (logger.isWarnEnabled()) {
537+
logger.warn("Ignoring CONNECT in session " + sessionId + ". Already connected.");
538+
}
539+
return;
540+
}
535541
if (logger.isDebugEnabled()) {
536542
logger.debug(stompAccessor.getShortLogMessage(EMPTY_PAYLOAD));
537543
}

Diff for: 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-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.
@@ -223,6 +223,30 @@ void systemSubscription() {
223223
assertThat(captor.getValue()).isSameAs(message);
224224
}
225225

226+
@Test
227+
void alreadyConnected() {
228+
229+
this.brokerRelay.start();
230+
231+
Message<byte[]> connect = connectMessage("sess1", "joe");
232+
this.brokerRelay.handleMessage(connect);
233+
234+
assertThat(this.tcpClient.getSentMessages().size()).isEqualTo(2);
235+
236+
StompHeaderAccessor headers1 = this.tcpClient.getSentHeaders(0);
237+
assertThat(headers1.getCommand()).isEqualTo(StompCommand.CONNECT);
238+
assertThat(headers1.getSessionId()).isEqualTo(StompBrokerRelayMessageHandler.SYSTEM_SESSION_ID);
239+
240+
StompHeaderAccessor headers2 = this.tcpClient.getSentHeaders(1);
241+
assertThat(headers2.getCommand()).isEqualTo(StompCommand.CONNECT);
242+
assertThat(headers2.getSessionId()).isEqualTo("sess1");
243+
244+
this.brokerRelay.handleMessage(connect);
245+
246+
assertThat(this.tcpClient.getSentMessages().size()).isEqualTo(2);
247+
assertThat(this.outboundChannel.getMessages()).isEmpty();
248+
}
249+
226250
private Message<byte[]> connectMessage(String sessionId, String user) {
227251
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
228252
headers.setSessionId(sessionId);

0 commit comments

Comments
 (0)