Skip to content

Commit 30d68f2

Browse files
committed
Reject user names with "%2F" in STOMP
Closes gh-23836
1 parent 08669cc commit 30d68f2

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessagingTemplate.java

Lines changed: 2 additions & 1 deletion
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-2019 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.
@@ -224,6 +224,7 @@ public void convertAndSendToUser(String user, String destination, Object payload
224224
throws MessagingException {
225225

226226
Assert.notNull(user, "User must not be null");
227+
Assert.isTrue(!user.contains("%2F"), "Invalid sequence \"%2F\" in user name: " + user);
227228
user = StringUtils.replace(user, "/", "%2F");
228229
destination = destination.startsWith("/") ? destination : "/" + destination;
229230
super.convertAndSend(this.destinationPrefix + user + destination, payload, headers, postProcessor);

spring-messaging/src/main/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolver.java

Lines changed: 2 additions & 1 deletion
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-2019 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.
@@ -203,6 +203,7 @@ private ParseResult parseSubscriptionMessage(Message<?> message, String sourceDe
203203
}
204204
Principal principal = SimpMessageHeaderAccessor.getUser(headers);
205205
String user = (principal != null ? principal.getName() : null);
206+
Assert.isTrue(user == null || !user.contains("%2F"), "Invalid sequence \"%2F\" in user name: " + user);
206207
Set<String> sessionIds = Collections.singleton(sessionId);
207208
return new ParseResult(sourceDestination, actualDestination, sourceDestination, sessionIds, user);
208209
}

spring-messaging/src/test/java/org/springframework/messaging/simp/SimpMessagingTemplateTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.util.LinkedMultiValueMap;
3737

3838
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3940

4041
/**
4142
* Unit tests for {@link org.springframework.messaging.simp.SimpMessagingTemplate}.
@@ -86,6 +87,12 @@ public void convertAndSendToUserWithEncoding() {
8687
assertThat(headerAccessor.getDestination()).isEqualTo("/user/https:%2F%2Fjoe.openid.example.org%2F/queue/foo");
8788
}
8889

90+
@Test // gh-23836
91+
public void convertAndSendToUserWithInvalidSequence() {
92+
assertThatIllegalArgumentException().isThrownBy(() ->
93+
this.messagingTemplate.convertAndSendToUser("joe%2F", "/queue/foo", "data"));
94+
}
95+
8996
@Test
9097
public void convertAndSendWithCustomHeader() {
9198
Map<String, Object> headers = Collections.<String, Object>singletonMap("key", "value");

spring-messaging/src/test/java/org/springframework/messaging/simp/user/DefaultUserDestinationResolverTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.util.StringUtils;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3233
import static org.mockito.BDDMockito.given;
3334
import static org.mockito.Mockito.mock;
3435

@@ -113,6 +114,15 @@ public void handleSubscribeNoUser() {
113114
assertThat(actual.getUser()).isNull();
114115
}
115116

117+
@Test // gh-23836
118+
public void handleSubscribeInvalidUserName() {
119+
TestPrincipal user = new TestPrincipal("joe%2F");
120+
String sourceDestination = "/user/queue/foo";
121+
122+
Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, user, "123", sourceDestination);
123+
assertThatIllegalArgumentException().isThrownBy(() -> this.resolver.resolveDestination(message));
124+
}
125+
116126
@Test
117127
public void handleUnsubscribe() {
118128
TestPrincipal user = new TestPrincipal("joe");

0 commit comments

Comments
 (0)