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 — Auto-port 4.1: Reject all control characters and DEL in HTTP response… · netty/netty@2df8707

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) {
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) {
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() {
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) {
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
+56Lines changed: 56 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.assertThat;
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,61 @@ 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+
assertConstructorRejects("OK" + c + "Injected");
125+
}
126+
assertConstructorRejects("OK\u007FInjected");
127+
}
128+
129+
private static void assertConstructorRejects(final String phrase) {
130+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, new Executable() {
131+
@Override
132+
public void execute() {
133+
new HttpResponseStatus(200, phrase);
134+
}
135+
});
136+
assertThat(e).hasMessageContaining("invalid character");
137+
}
138+
139+
@Test
140+
public void parseLineMustRejectControlCharactersAndDelInReasonPhrase() {
141+
assertParseLineRejects("200 OK\u0000Injected");
142+
assertParseLineRejects("200 OK\u000BInjected");
143+
assertParseLineRejects("200 OK\fInjected");
144+
assertParseLineRejects("200 OK\u007FInjected");
145+
}
146+
147+
private static void assertParseLineRejects(final String line) {
148+
assertThrows(IllegalArgumentException.class, new Executable() {
149+
@Override
150+
public void execute() {
151+
parseLine(line);
152+
}
153+
});
154+
assertThrows(IllegalArgumentException.class, new Executable() {
155+
@Override
156+
public void execute() {
157+
parseLine(new AsciiString(line));
158+
}
159+
});
160+
}
161+
162+
@Test
163+
public void constructorMustAcceptValidReasonPhrases() {
164+
assertEquals("OK", new HttpResponseStatus(200, "OK").reasonPhrase());
165+
assertEquals("Not Found", new HttpResponseStatus(404, "Not Found").reasonPhrase());
166+
// HTAB is allowed by the RFC 9110 reason-phrase grammar.
167+
assertEquals("OK\tTabbed", new HttpResponseStatus(200, "OK\tTabbed").reasonPhrase());
168+
// obs-text is allowed by the RFC 9110 reason-phrase grammar.
169+
assertEquals("OKé", new HttpResponseStatus(200, "OKé").reasonPhrase());
170+
}
171+
116172
@Test
117173
public void testHttpStatusClassValueOf() {
118174
// 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.