Skip to content

Commit a7c18d4

Browse files
authored
Detect missing colon when parsing http headers with no value (netty#9871)
Motivation: Technical speaking its valid to have http headers with no values so we should support it. That said we need to detect if these are "generated" because of an "invalid" fold. Modifications: - Detect if a colon is missing when parsing headers. - Add unit test Result: Fixes netty#9866
1 parent cf63bc1 commit a7c18d4

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,11 @@ private void splitHeader(AppendableCharSequence sb) {
747747
}
748748
}
749749

750+
if (nameEnd == length) {
751+
// There was no colon present at all.
752+
throw new IllegalArgumentException("No colon found");
753+
}
754+
750755
for (colonEnd = nameEnd; colonEnd < length; colonEnd ++) {
751756
if (sb.charAtUnsafe(colonEnd) == ':') {
752757
colonEnd ++;

codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,20 @@ public void testWhitespace() {
334334
assertTrue(request.decoderResult().cause() instanceof IllegalArgumentException);
335335
assertFalse(channel.finish());
336336
}
337+
338+
@Test
339+
public void testHeaderWithNoValueAndMissingColon() {
340+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
341+
String requestStr = "GET /some/path HTTP/1.1\r\n" +
342+
"Content-Length: 0\r\n" +
343+
"Host:\r\n" +
344+
"netty.io\r\n\r\n";
345+
346+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
347+
HttpRequest request = channel.readInbound();
348+
System.err.println(request.headers().names().toString());
349+
assertTrue(request.decoderResult().isFailure());
350+
assertTrue(request.decoderResult().cause() instanceof IllegalArgumentException);
351+
assertFalse(channel.finish());
352+
}
337353
}

0 commit comments

Comments
 (0)