GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Validate STOMP CONNECT/CONNECTED command headers · netty/netty@536d64a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-stomp/src
    • main/java/io/netty/handler/codec/stomp
      • StompSubframeEncoder.java
    • test/java/io/netty/handler/codec/stomp
      • StompSubframeEncoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-stomp/src/main/java/io/netty/handler/codec/stomp/StompSubframeEncoder.java‎

Copy file name to clipboardExpand all lines: codec-stomp/src/main/java/io/netty/handler/codec/stomp/StompSubframeEncoder.java
+36-8Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.netty.handler.codec.MessageToMessageEncoder;
2222
import io.netty.util.concurrent.FastThreadLocal;
2323
import io.netty.util.internal.AppendableCharSequence;
24+
import io.netty.util.internal.PlatformDependent;
2425

2526
import java.util.LinkedHashMap;
2627
import java.util.List;
@@ -180,19 +181,29 @@ private static void encodeHeaders(StompHeadersSubframe frame, ByteBuf buf) {
180181
LinkedHashMap<CharSequence, CharSequence> cache = ESCAPE_HEADER_KEY_CACHE.get();
181182
for (Entry<CharSequence, CharSequence> entry : frame.headers()) {
182183
CharSequence headerKey = entry.getKey();
183-
if (shouldEscape) {
184-
CharSequence cachedHeaderKey = cache.get(headerKey);
185-
if (cachedHeaderKey == null) {
186-
cachedHeaderKey = escape(headerKey);
187-
cache.put(headerKey, cachedHeaderKey);
184+
CharSequence headerValue = entry.getValue();
185+
try {
186+
if (shouldEscape) {
187+
CharSequence cachedHeaderKey = cache.get(headerKey);
188+
if (cachedHeaderKey == null) {
189+
cachedHeaderKey = escape(command, "header name", headerKey);
190+
cache.put(headerKey, cachedHeaderKey);
191+
}
192+
headerKey = cachedHeaderKey;
193+
headerValue = escape(command, "header value", headerValue);
194+
} else {
195+
// For CONNECT/CONNECTED: don't escape but REJECT newlines
196+
validateNoIllegalCharacters(command, headerKey, "header name");
197+
validateNoIllegalCharacters(command, headerValue, "header value");
188198
}
189-
headerKey = cachedHeaderKey;
199+
} catch (Exception e) {
200+
buf.release();
201+
PlatformDependent.throwException(e);
190202
}
191203

192204
ByteBufUtil.writeUtf8(buf, headerKey);
193205
buf.writeByte(StompConstants.COLON);
194206

195-
CharSequence headerValue = shouldEscape? escape(entry.getValue()) : entry.getValue();
196207
ByteBufUtil.writeUtf8(buf, headerValue);
197208
buf.writeByte(StompConstants.LF);
198209
}
@@ -215,7 +226,21 @@ private static boolean shouldEscape(StompCommand command) {
215226
return command != StompCommand.CONNECT && command != StompCommand.CONNECTED;
216227
}
217228

218-
private static CharSequence escape(CharSequence input) {
229+
private static void validateNoIllegalCharacters(StompCommand command, CharSequence value, String type) {
230+
for (int i = 0; i < value.length(); i++) {
231+
char c = value.charAt(i);
232+
if (c == '\n' || c == '\r' || c == ':' || c == '\0') {
233+
throw newIllegalCharacterException(command, type, i);
234+
}
235+
}
236+
}
237+
238+
private static IllegalArgumentException newIllegalCharacterException(StompCommand command, String type, int index) {
239+
return new IllegalArgumentException(
240+
"STOMP " + command + " " + type + " contains illegal character at index " + index);
241+
}
242+
243+
private static CharSequence escape(StompCommand command, String type, CharSequence input) {
219244
AppendableCharSequence builder = null;
220245
for (int i = 0; i < input.length(); i++) {
221246
char chr = input.charAt(i);
@@ -231,6 +256,9 @@ private static CharSequence escape(CharSequence input) {
231256
} else if (chr == '\r') {
232257
builder = escapeBuilder(builder, input, i);
233258
builder.append("\\r");
259+
} else if (chr == '\0') {
260+
// The NUL character has no escape and is always illegal.
261+
throw newIllegalCharacterException(command, type, i);
234262
} else if (builder != null) {
235263
builder.append(chr);
236264
}
Collapse file

‎codec-stomp/src/test/java/io/netty/handler/codec/stomp/StompSubframeEncoderTest.java‎

Copy file name to clipboardExpand all lines: codec-stomp/src/test/java/io/netty/handler/codec/stomp/StompSubframeEncoderTest.java
+48-4Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
import io.netty.buffer.ByteBuf;
1919
import io.netty.buffer.Unpooled;
2020
import io.netty.channel.embedded.EmbeddedChannel;
21+
import io.netty.handler.codec.EncoderException;
2122
import io.netty.util.AsciiString;
2223
import io.netty.util.CharsetUtil;
2324
import org.junit.jupiter.api.AfterEach;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
2629

2730
import java.nio.charset.StandardCharsets;
2831

2932
import static io.netty.handler.codec.stomp.StompTestConstants.SEND_FRAME_UTF8;
33+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3034
import static org.junit.jupiter.api.Assertions.assertEquals;
3135
import static org.junit.jupiter.api.Assertions.assertFalse;
3236
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -126,11 +130,11 @@ void testEscapeStompHeaders() {
126130
@Test
127131
void testNotEscapeStompHeadersForConnectCommand() {
128132
String expectedStompFrame = "CONNECT\n"
129-
+ "colonHeaderName-::colonHeaderValue-:\n"
133+
+ "backslashHeaderName-\\:backslashHeaderValue-\\\n"
130134
+ '\n' + '\0';
131135
StompFrame connectFrame = new DefaultStompFrame(StompCommand.CONNECT);
132136
connectFrame.headers()
133-
.add("colonHeaderName-:", "colonHeaderValue-:");
137+
.add("backslashHeaderName-\\", "backslashHeaderValue-\\");
134138

135139
assertTrue(channel.writeOutbound(connectFrame));
136140

@@ -142,14 +146,34 @@ void testNotEscapeStompHeadersForConnectCommand() {
142146
assertTrue(stompBuffer.release());
143147
}
144148

149+
@ParameterizedTest
150+
@ValueSource(chars = {'\r', '\n', '\0', ':'})
151+
void mustRejectIllegalCharsInConnectCommandHeaders(char illegalChar) {
152+
StompFrame connectFrame1 = new DefaultStompFrame(StompCommand.CONNECT);
153+
connectFrame1.headers()
154+
.add("header" + illegalChar, "value");
155+
assertThatThrownBy(() -> channel.writeOutbound(connectFrame1))
156+
.isInstanceOf(EncoderException.class)
157+
.hasRootCauseInstanceOf(IllegalArgumentException.class)
158+
.hasMessageContaining("illegal character");
159+
160+
StompFrame connectFrame2 = new DefaultStompFrame(StompCommand.CONNECT);
161+
connectFrame2.headers()
162+
.add("header", "value" + illegalChar);
163+
assertThatThrownBy(() -> channel.writeOutbound(connectFrame2))
164+
.isInstanceOf(EncoderException.class)
165+
.hasRootCauseInstanceOf(IllegalArgumentException.class)
166+
.hasMessageContaining("illegal character");
167+
}
168+
145169
@Test
146170
void testNotEscapeStompHeadersForConnectedCommand() {
147171
String expectedStompFrame = "CONNECTED\n"
148-
+ "colonHeaderName-::colonHeaderValue-:\n"
172+
+ "backslashHeaderName-\\:backslashHeaderValue-\\\n"
149173
+ '\n' + '\0';
150174
StompFrame connectedFrame = new DefaultStompFrame(StompCommand.CONNECTED);
151175
connectedFrame.headers()
152-
.add("colonHeaderName-:", "colonHeaderValue-:");
176+
.add("backslashHeaderName-\\", "backslashHeaderValue-\\");
153177

154178
assertTrue(channel.writeOutbound(connectedFrame));
155179

@@ -160,4 +184,24 @@ void testNotEscapeStompHeadersForConnectedCommand() {
160184
assertEquals(expectedStompFrame, stompBuffer.toString(StandardCharsets.UTF_8));
161185
assertTrue(stompBuffer.release());
162186
}
187+
188+
@ParameterizedTest
189+
@ValueSource(chars = {'\r', '\n', '\0', ':'})
190+
void mustRejectIllegalCharsInConnectedCommandHeaders(char illegalChar) {
191+
StompFrame connectedFrame1 = new DefaultStompFrame(StompCommand.CONNECTED);
192+
connectedFrame1.headers()
193+
.add("header" + illegalChar + ":", "name");
194+
assertThatThrownBy(() -> channel.writeOutbound(connectedFrame1))
195+
.isInstanceOf(EncoderException.class)
196+
.hasRootCauseInstanceOf(IllegalArgumentException.class)
197+
.hasMessageContaining("illegal character");
198+
199+
StompFrame connectedFrame2 = new DefaultStompFrame(StompCommand.CONNECTED);
200+
connectedFrame2.headers()
201+
.add("header", "name" + illegalChar + ":");
202+
assertThatThrownBy(() -> channel.writeOutbound(connectedFrame2))
203+
.isInstanceOf(EncoderException.class)
204+
.hasRootCauseInstanceOf(IllegalArgumentException.class)
205+
.hasMessageContaining("illegal character");
206+
}
163207
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.