Ignore invalid connect frame · spring-projects/spring-framework@dc2947c (original) (raw)

File tree

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1 1 /*
2 - * Copyright 2002-2019 the original author or authors.
2 + * Copyright 2002-2022 the original author or authors.
3 3 *
4 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 5 * you may not use this file except in compliance with the License.
@@ -306,6 +306,12 @@ protected void handleMessageInternal(Message<?> message) {
306 306 else if (SimpMessageType.CONNECT.equals(messageType)) {
307 307 logMessage(message);
308 308 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 + }
309 315 long[] heartbeatIn = SimpMessageHeaderAccessor.getHeartbeat(headers);
310 316 long[] heartbeatOut = getHeartbeatValue();
311 317 Principal user = SimpMessageHeaderAccessor.getUser(headers);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1 1 /*
2 - * Copyright 2002-2021 the original author or authors.
2 + * Copyright 2002-2022 the original author or authors.
3 3 *
4 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 5 * you may not use this file except in compliance with the License.
@@ -552,6 +552,12 @@ else if (accessor instanceof SimpMessageHeaderAccessor) {
552 552 }
553 553
554 554 if (StompCommand.CONNECT.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 + }
555 561 if (logger.isDebugEnabled()) {
556 562 logger.debug(stompAccessor.getShortLogMessage(EMPTY_PAYLOAD));
557 563 }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1 1 /*
2 - * Copyright 2002-2020 the original author or authors.
2 + * Copyright 2002-2022 the original author or authors.
3 3 *
4 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 5 * you may not use this file except in compliance with the License.
@@ -260,6 +260,30 @@ void systemSubscription() {
260 260 assertThat(captor.getValue()).isSameAs(message);
261 261 }
262 262
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 +
263 287 private Message<byte[]> connectMessage(String sessionId, String user) {
264 288 StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
265 289 headers.setSessionId(sessionId);