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 — Port 5.0: Reject all control characters and DEL in HTTP response reas… · netty/netty@53854f3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http
      • HttpResponseStatus.java
    • test/java/io/netty/handler/codec/http
      • HttpResponseDecoderTest.java
      • HttpResponseStatusTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseStatus.java
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,13 @@ private HttpResponseStatus(int code, String reasonPhrase, boolean bytes) {
553553
for (int i = 0; i < reasonPhrase.length(); i ++) {
554554
char c = reasonPhrase.charAt(i);
555555
// Check prohibited characters.
556-
switch (c) {
557-
case '\n': case '\r':
558-
throw new IllegalArgumentException(
559-
"reasonPhrase contains one of the following prohibited characters: " +
560-
"\\r\\n: " + reasonPhrase);
556+
// RFC 9110 defines the reason-phrase as *( HTAB / SP / VCHAR / obs-text ).
557+
// All other control characters and DEL are rejected as lenient parsers may
558+
// treat them as line separators (response splitting / desync) and C-string
559+
// based parsers will truncate at NUL.
560+
if ((c <= 0x1F && c != '\t') || c == 0x7F) {
561+
throw new IllegalArgumentException(
562+
"reasonPhrase contains invalid character: " + reasonPhrase);
561563
}
562564
}
563565

Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,31 @@ public void testInvalidVersion(String version) throws Exception {
14911491
version + " 200 OK\r\nHost: whatever\r\n\r\n", CharsetUtil.US_ASCII));
14921492
}
14931493

1494+
@ParameterizedTest
1495+
@ValueSource(chars = { '\u0000', '\u000B', '\f', '\u001B', '\u007F' })
1496+
public void testInvalidReasonPhraseControlChars(char controlChar) throws Exception {
1497+
ByteBuf responseBuffer = Unpooled.buffer();
1498+
responseBuffer.writeCharSequence("HTTP/1.1 200 OK", CharsetUtil.US_ASCII);
1499+
responseBuffer.writeByte(controlChar);
1500+
responseBuffer.writeCharSequence("Injected\r\nHost: whatever\r\n\r\n", CharsetUtil.US_ASCII);
1501+
testInvalidHeaders0(responseBuffer);
1502+
}
1503+
1504+
@Test
1505+
public void testReasonPhraseWithHtabMustBeAccepted() throws Exception {
1506+
// HTAB is allowed by the RFC 9110 reason-phrase grammar.
1507+
EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseDecoder());
1508+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(
1509+
"HTTP/1.1 200 OK\tTabbed\r\nHost: whatever\r\nContent-Length: 0\r\n\r\n",
1510+
CharsetUtil.US_ASCII)));
1511+
HttpResponse response = channel.readInbound();
1512+
assertTrue(response.decoderResult().isSuccess());
1513+
assertEquals("OK\tTabbed", response.status().reasonPhrase());
1514+
ReferenceCountUtil.release(response);
1515+
assertTrue(channel.finish());
1516+
ReferenceCountUtil.release(channel.readInbound());
1517+
}
1518+
14941519
private static void testInvalidHeaders0(ByteBuf responseBuffer) throws Exception {
14951520
EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseDecoder());
14961521
assertTrue(channel.writeInbound(responseBuffer));
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseStatusTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseStatusTest.java
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.jupiter.api.function.Executable;
2121

2222
import static io.netty.handler.codec.http.HttpResponseStatus.parseLine;
23+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2324
import static org.junit.jupiter.api.Assertions.assertEquals;
2425
import static org.junit.jupiter.api.Assertions.assertNotSame;
2526
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -113,6 +114,43 @@ public void execute() {
113114
});
114115
}
115116

117+
@Test
118+
public void constructorMustRejectControlCharactersAndDelInReasonPhrase() {
119+
for (char c = 0; c <= 0x1F; c++) {
120+
if (c == '\t') {
121+
// HTAB is allowed by the RFC 9110 reason-phrase grammar.
122+
continue;
123+
}
124+
String phrase = "OK" + c + "Injected";
125+
assertThatThrownBy(() -> new HttpResponseStatus(200, phrase))
126+
.isInstanceOf(IllegalArgumentException.class)
127+
.hasMessageContaining("invalid character");
128+
}
129+
assertThatThrownBy(() -> new HttpResponseStatus(200, "OK\u007FInjected"))
130+
.isInstanceOf(IllegalArgumentException.class)
131+
.hasMessageContaining("invalid character");
132+
}
133+
134+
@Test
135+
public void parseLineMustRejectControlCharactersAndDelInReasonPhrase() {
136+
assertThrows(IllegalArgumentException.class, () -> parseLine("200 OK\u0000Injected"));
137+
assertThrows(IllegalArgumentException.class, () -> parseLine("200 OK\u000BInjected"));
138+
assertThrows(IllegalArgumentException.class, () -> parseLine("200 OK\fInjected"));
139+
assertThrows(IllegalArgumentException.class, () -> parseLine("200 OK\u007FInjected"));
140+
assertThrows(IllegalArgumentException.class, () -> parseLine(new AsciiString("200 OK\u0000Injected")));
141+
assertThrows(IllegalArgumentException.class, () -> parseLine(new AsciiString("200 OK\u007FInjected")));
142+
}
143+
144+
@Test
145+
public void constructorMustAcceptValidReasonPhrases() {
146+
assertEquals("OK", new HttpResponseStatus(200, "OK").reasonPhrase());
147+
assertEquals("Not Found", new HttpResponseStatus(404, "Not Found").reasonPhrase());
148+
// HTAB is allowed by the RFC 9110 reason-phrase grammar.
149+
assertEquals("OK\tTabbed", new HttpResponseStatus(200, "OK\tTabbed").reasonPhrase());
150+
// obs-text is allowed by the RFC 9110 reason-phrase grammar.
151+
assertEquals("OKé", new HttpResponseStatus(200, "OKé").reasonPhrase());
152+
}
153+
116154
@Test
117155
public void testHttpStatusClassValueOf() {
118156
// status scope: [100, 600).

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.