Skip to content

Commit 4428c34

Browse files
committed
StompDecoder handles partial headers correctly
Issue: SPR-13416
1 parent ad16706 commit 4428c34

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,15 @@ private String readCommand(ByteBuffer buffer) {
208208
private void readHeaders(ByteBuffer buffer, StompHeaderAccessor headerAccessor) {
209209
while (true) {
210210
ByteArrayOutputStream headerStream = new ByteArrayOutputStream(256);
211-
while (buffer.remaining() > 0 && !tryConsumeEndOfLine(buffer)) {
211+
boolean headerComplete = false;
212+
while (buffer.hasRemaining()) {
213+
if (tryConsumeEndOfLine(buffer)) {
214+
headerComplete = true;
215+
break;
216+
}
212217
headerStream.write(buffer.get());
213218
}
214-
if (headerStream.size() > 0) {
219+
if (headerStream.size() > 0 && headerComplete) {
215220
String header = new String(headerStream.toByteArray(), UTF8_CHARSET);
216221
int colonIndex = header.indexOf(':');
217222
if (colonIndex <= 0 || colonIndex == header.length() - 1) {

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -25,7 +25,9 @@
2525

2626
import org.springframework.messaging.Message;
2727

28-
import static org.junit.Assert.*;
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertNull;
30+
import static org.junit.Assert.fail;
2931

3032
/**
3133
* Unit tests for {@link BufferingStompDecoder}.
@@ -177,13 +179,30 @@ public void incompleteCommand() {
177179
assertEquals(0, messages.size());
178180
}
179181

180-
@Test(expected = StompConversionException.class) // SPR-12418
181-
public void endingBackslashHeaderValueCheck() {
182+
// SPR-13416
183+
184+
@Test
185+
public void incompleteHeaderWithPartialEscapeSequence() throws Exception {
182186
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
183-
String payload = "SEND\na:alpha\\\n\nMessage body\0";
187+
String chunk = "SEND\na:long\\";
188+
189+
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk));
190+
assertEquals(0, messages.size());
191+
}
192+
193+
@Test(expected = StompConversionException.class)
194+
public void invalidEscapeSequence() {
195+
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
196+
String payload = "SEND\na:alpha\\x\\n\nMessage body\0";
184197
stompDecoder.decode(toByteBuffer(payload));
185198
}
186199

200+
@Test(expected = StompConversionException.class)
201+
public void invalidEscapeSequenceWithSingleSlashAtEndOfHeaderValue() {
202+
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
203+
String payload = "SEND\na:alpha\\\n\nMessage body\0";
204+
stompDecoder.decode(toByteBuffer(payload));
205+
}
187206

188207
private ByteBuffer toByteBuffer(String chunk) {
189208
return ByteBuffer.wrap(chunk.getBytes(Charset.forName("UTF-8")));

0 commit comments

Comments
 (0)