Skip to content

Commit 8ade083

Browse files
committed
Filter out null WebSocketSession attributes
Closes gh-29315
1 parent db9e89f commit 8ade083

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/AbstractWebSocketSession.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -73,9 +73,12 @@ protected AbstractWebSocketSession(T delegate, String id, HandshakeInfo info, Da
7373
this.id = id;
7474
this.handshakeInfo = info;
7575
this.bufferFactory = bufferFactory;
76-
this.attributes.putAll(info.getAttributes());
7776
this.logPrefix = initLogPrefix(info, id);
7877

78+
info.getAttributes().entrySet().stream()
79+
.filter(entry -> (entry.getKey() != null && entry.getValue() != null))
80+
.forEach(entry -> this.attributes.put(entry.getKey(), entry.getValue()));
81+
7982
if (logger.isDebugEnabled()) {
8083
logger.debug(getLogPrefix() + "Session id \"" + getId() + "\" for " + getHandshakeInfo().getUri());
8184
}

spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -62,7 +62,9 @@ public abstract class AbstractWebSocketSession<T> implements NativeWebSocketSess
6262
*/
6363
public AbstractWebSocketSession(@Nullable Map<String, Object> attributes) {
6464
if (attributes != null) {
65-
this.attributes.putAll(attributes);
65+
attributes.entrySet().stream()
66+
.filter(entry -> (entry.getKey() != null && entry.getValue() != null))
67+
.forEach(entry -> this.attributes.put(entry.getKey(), entry.getValue()));
6668
}
6769
}
6870

spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java

Lines changed: 12 additions & 4 deletions
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.
@@ -37,6 +37,7 @@
3737
*
3838
* @author Rossen Stoyanchev
3939
*/
40+
@SuppressWarnings("resource")
4041
public class StandardWebSocketSessionTests {
4142

4243
private final HttpHeaders headers = new HttpHeaders();
@@ -54,7 +55,6 @@ public void getPrincipalWithConstructorArg() {
5455
}
5556

5657
@Test
57-
@SuppressWarnings("resource")
5858
public void getPrincipalWithNativeSession() {
5959
TestPrincipal user = new TestPrincipal("joe");
6060

@@ -68,7 +68,6 @@ public void getPrincipalWithNativeSession() {
6868
}
6969

7070
@Test
71-
@SuppressWarnings("resource")
7271
public void getPrincipalNone() {
7372
Session nativeSession = Mockito.mock(Session.class);
7473
given(nativeSession.getUserPrincipal()).willReturn(null);
@@ -83,7 +82,6 @@ public void getPrincipalNone() {
8382
}
8483

8584
@Test
86-
@SuppressWarnings("resource")
8785
public void getAcceptedProtocol() {
8886
String protocol = "foo";
8987

@@ -99,4 +97,14 @@ public void getAcceptedProtocol() {
9997
verifyNoMoreInteractions(nativeSession);
10098
}
10199

100+
@Test // gh-29315
101+
public void addAttributesWithNullKeyOrValue() {
102+
this.attributes.put(null, "value");
103+
this.attributes.put("key", null);
104+
this.attributes.put("foo", "bar");
105+
106+
assertThat(new StandardWebSocketSession(this.headers, this.attributes, null, null).getAttributes())
107+
.hasSize(1).containsEntry("foo", "bar");
108+
}
109+
102110
}

0 commit comments

Comments
 (0)