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 — Merge commit from fork · lltopk/netty@0399e4a

File tree

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

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpDecoderConfig.java
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,16 @@ public boolean isStrictLineParsing() {
232232
* security vulnerabilities, when multiple systems disagree on the meaning of leniently parsed messages.
233233
* <p>
234234
* When <em>strict line parsing</em> is enabled ({@code true}), then Netty will enforce that start- and header
235-
* field-lines MUST be separated by a CR LF octet pair, and will produce messagas with failed
235+
* field-lines MUST be separated by a CR LF octet pair, and will produce messages with failed
236236
* {@link io.netty.handler.codec.DecoderResult}s.
237+
* Additionally, Netty will enforce that only CR LF characters precede the initial line, if any.
237238
* <p>
238239
* When <em>strict line parsing</em> is disabled ({@code false}), then Netty will accept lone LF octets as line
239-
* seperators for the start- and header field-lines.
240+
* separators for the start- and header field-lines.
241+
* Additionally, Netty will ignore any ISO control and line separator characters prior to the initial line.
240242
* <p>
241-
* See <a href="https://datatracker.ietf.org/doc/html/rfc9112#name-message-format">RFC 9112 Section 2.1</a>.
243+
* See <a href="https://datatracker.ietf.org/doc/html/rfc9112#name-message-format">RFC 9112 Section 2.1</a> and
244+
* <a href="https://datatracker.ietf.org/doc/html/rfc9112#section-2.2-6">RFC 9112 Section 2.2</a>.
242245
* @param strictLineParsing Whether <em>strict line parsing</em> should be enabled ({@code true}),
243246
* or not ({@code false}).
244247
* @return This decoder config.
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java
+19-11Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception {
224224
* <em>Internal use only</em>.
225225
*/
226226
private enum State {
227+
SKIP_INITIAL_LINE_CHARS,
227228
SKIP_CONTROL_CHARS,
228229
READ_INITIAL,
229230
READ_HEADER,
@@ -237,7 +238,7 @@ private enum State {
237238
UPGRADED
238239
}
239240

240-
private State currentState = State.SKIP_CONTROL_CHARS;
241+
private State currentState = State.SKIP_INITIAL_LINE_CHARS;
241242

242243
/**
243244
* Creates a new instance with the default
@@ -374,7 +375,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> ou
374375
}
375376

376377
switch (currentState) {
377-
case SKIP_CONTROL_CHARS:
378+
case SKIP_INITIAL_LINE_CHARS:
378379
// Fall-through
379380
case READ_INITIAL: try {
380381
ByteBuf line = lineParser.parse(buffer, defaultStrictCRLFCheck);
@@ -618,6 +619,7 @@ protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> ou
618619
resetNow();
619620
return;
620621
case SKIP_CONTROL_CHARS: // fall-trough
622+
case SKIP_INITIAL_LINE_CHARS: // fall-trough
621623
case READ_INITIAL:// fall-trough
622624
case BAD_MESSAGE: // fall-trough
623625
case UPGRADED: // fall-trough
@@ -717,7 +719,7 @@ private void resetNow() {
717719
}
718720

719721
resetRequested.lazySet(false);
720-
currentState = State.SKIP_CONTROL_CHARS;
722+
currentState = State.SKIP_INITIAL_LINE_CHARS;
721723
}
722724

723725
private HttpMessage invalidMessage(HttpMessage current, ByteBuf in, Exception cause) {
@@ -1338,26 +1340,33 @@ public ByteBuf parse(ByteBuf buffer, Runnable strictCRLFCheck) {
13381340
if (readableBytes == 0) {
13391341
return null;
13401342
}
1341-
if (currentState == State.SKIP_CONTROL_CHARS &&
1342-
skipControlChars(buffer, readableBytes, buffer.readerIndex())) {
1343+
if (currentState == State.SKIP_INITIAL_LINE_CHARS &&
1344+
skipLineChars(buffer, readableBytes, buffer.readerIndex(), strictCRLFCheck)) {
13431345
return null;
13441346
}
13451347
return super.parse(buffer, strictCRLFCheck);
13461348
}
13471349

1348-
private boolean skipControlChars(ByteBuf buffer, int readableBytes, int readerIndex) {
1349-
assert currentState == State.SKIP_CONTROL_CHARS;
1350+
private boolean skipLineChars(ByteBuf buffer, int readableBytes, int readerIndex, Runnable strictCRLFCheck) {
1351+
assert currentState == State.SKIP_INITIAL_LINE_CHARS;
13501352
final int maxToSkip = Math.min(maxLength, readableBytes);
1351-
final int firstNonControlIndex = buffer.forEachByte(readerIndex, maxToSkip, SKIP_CONTROL_CHARS_BYTES);
1352-
if (firstNonControlIndex == -1) {
1353+
final int firstNonLineIndex = buffer.forEachByte(readerIndex, maxToSkip,
1354+
strictCRLFCheck == null ? SKIP_CONTROL_CHARS_BYTES : ByteProcessor.FIND_NON_CRLF);
1355+
if (firstNonLineIndex == -1) {
13531356
buffer.skipBytes(maxToSkip);
13541357
if (readableBytes > maxLength) {
13551358
throw newException(maxLength);
13561359
}
13571360
return true;
13581361
}
1362+
if (strictCRLFCheck != null) {
1363+
final int b = buffer.getByte(firstNonLineIndex) & 0xFF;
1364+
if (Character.isISOControl(b)) {
1365+
strictCRLFCheck.run();
1366+
}
1367+
}
13591368
// from now on we don't care about control chars
1360-
buffer.readerIndex(firstNonControlIndex);
1369+
buffer.readerIndex(firstNonLineIndex);
13611370
currentState = State.READ_INITIAL;
13621371
return false;
13631372
}
@@ -1378,7 +1387,6 @@ protected TooLongFrameException newException(int maxLength) {
13781387
}
13791388

13801389
private static final ByteProcessor SKIP_CONTROL_CHARS_BYTES = new ByteProcessor() {
1381-
13821390
@Override
13831391
public boolean process(byte value) {
13841392
return ISO_CONTROL_OR_WHITESPACE[128 + value];
Collapse file

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

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,31 @@ public void testInitialLineWithLeadingControlChars() {
489489
assertTrue(channel.finishAndReleaseAll());
490490
}
491491

492+
@Test
493+
public void testNonCrlfControlBytesPrecedingRequestLineAreRejected() {
494+
// RFC 9112 §2.2: servers SHOULD ignore "at least one empty line (CRLF)" before the
495+
// request-line. Non-CRLF control bytes are not part of this robustness allowance
496+
// and must not be silently swallowed.
497+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
498+
499+
ByteBuf buf = Unpooled.buffer();
500+
buf.writeByte(0x00); // NUL — not an empty CRLF line
501+
buf.writeByte(0x01); // SOH — not an empty CRLF line
502+
buf.writeCharSequence(
503+
"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n",
504+
CharsetUtil.US_ASCII);
505+
506+
channel.writeInbound(buf);
507+
HttpRequest req = channel.readInbound();
508+
509+
DecoderResult decoderResult = req.decoderResult();
510+
assertTrue(decoderResult.isFailure(),
511+
"Non-CRLF control bytes before the request-line must not be silently skipped");
512+
assertThat(decoderResult.cause()).isInstanceOf(InvalidLineSeparatorException.class);
513+
514+
assertFalse(channel.finish());
515+
}
516+
492517
@Test
493518
public void testTooLargeHeaders() {
494519
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder(1024, 10, 1024));

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.