Skip to content

Commit 2ef0256

Browse files
authored
Reject bare CR in managed HttpListener request parsing (dotnet#133665)
While reading the chunk-size change in dotnet#132747 I noticed that the managed HttpListener silently drops a CR that isn't followed by a LF, and glues together the text on either side of it. I replayed a few raw requests against the managed listener (Linux, .NET 8.0.30) and against http.sys (Windows 11, .NET 8.0.29): | request contains | managed HttpListener | http.sys | | --- | --- | --- | | `Content-Le\rngth: 5` + body `Hello` | context with `Content-Length: 5`, body read | 400 | | `X-Test: ab\rcd` | header value `abcd` | 400 | | `GET /c\rd HTTP/1.1` | `RawUrl` is `/cd` | 400 | | `Host: localhost\r\r\n` | accepted | 400 | | chunk-size line `1\r0\n` | read as a 0x10 byte chunk | 400 | Both line readers do the same thing. `HttpConnection.ReadLine` moves to `LineState.CR` on a CR but keeps appending the bytes that follow, and the next LF ends the line, so a CR anywhere in the request line or a header just disappears. `ChunkStream.GetChunkSize` behaves the same way. It does have a `Missing \n` check meant for this case, but it sits after the read loop, and the loop only exits without a `break` once `offset == size`, so `offset < size` is never true there. RFC 9112 section 2.2 says a recipient of a bare CR must treat the element as invalid or replace the CR with SP. Dropping it does neither, and `Content-Le\rngth` becoming `Content-Length` is the same kind of disagreement with an intermediary that dotnet#132163 and dotnet#132747 closed for whitespace. The change rejects a CR that isn't immediately followed by a LF in both places: `ReadLine` throws, which `ProcessInput` already turns into a 400, and the `Missing \n` check in `GetChunkSize` moves into the loop where it can fire. A bare LF is still accepted as a line terminator, and a CR and LF split across two reads still work, since the CR state carries over between calls as before. On the behavior change: the public API is untouched, and the only requests that now fail are ones that are invalid per the RFC and that http.sys already rejects with a 400 behind the same API, so this brings the managed implementation in line with Windows, as dotnet#130910 did for Content-Length. Tests: - four bare-CR entries in `InvalidClientRequestTests`, not gated on the implementation. I replayed exactly those four requests, built the way `GetContext_InvalidRequest_DoesNotGetContext` builds them, against http.sys on Windows 11 and each got `400 Bad Request`; against the shipped managed listener each one got a context. - `Read_ChunkSizeWithBareCR_ThrowsHttpListenerException`, managed only, next to the whitespace tests, because http.sys answers 400 before a context is handed out. I couldn't build the repo on the machines I had (no .NET 11 SDK), so I have not run the xunit tests themselves and am relying on CI for those. What I did run: a net8.0 harness that compiles `ChunkStream.cs` from the tree as-is and `HttpConnection.ReadLine` extracted verbatim. At main, 7 of its bare-CR checks fail; with this change they all pass, and the healthy-path checks (CR/LF split across reads, byte-by-byte input, bare LF endings, chunk extensions, several chunks, whitespace in the chunk size still rejected) pass on both sides. I also noticed that `ChunkStream.ReadTrailer` never appends the trailer characters themselves to `_saved`, so trailer fields are dropped, and that `0\r\n\r\r\n` leaves the stream waiting for more data. I left that alone here; I can open an issue if it's useful. AI tools used
1 parent ce202f8 commit 2ef0256

4 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎src/libraries/System.Net.HttpListener/src/System/Net/Managed/ChunkStream.cs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,14 @@ private State GetChunkSize(byte[] buffer, ref int offset, int size)
249249
continue;
250250
}
251251

252-
if (_sawCR && c == '\n')
252+
if (_sawCR)
253+
{
254+
// The CR must be immediately followed by a LF. Don't drop a bare CR and join the digits around it.
255+
if (c != '\n')
256+
ThrowProtocolViolation("Missing \\n");
257+
253258
break;
259+
}
254260

255261
_saved.Append(c);
256262

@@ -260,9 +266,6 @@ private State GetChunkSize(byte[] buffer, ref int offset, int size)
260266

261267
if (!_sawCR || c != '\n')
262268
{
263-
if (offset < size)
264-
ThrowProtocolViolation("Missing \\n");
265-
266269
try
267270
{
268271
if (_saved.Length > 0)

‎src/libraries/System.Net.HttpListener/src/System/Net/Managed/HttpConnection.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ private bool ProcessInput(MemoryStream ms)
423423
{
424424
used++;
425425
byte b = buffer[i];
426+
if (_lineState == LineState.CR && b != 10)
427+
{
428+
// A CR must be immediately followed by a LF. Reject a bare CR, as http.sys does,
429+
// rather than dropping it and joining the text on either side of it.
430+
throw new ProtocolViolationException("Missing \\n after \\r");
431+
}
432+
426433
if (b == 13)
427434
{
428435
_lineState = LineState.CR;

‎src/libraries/System.Net.HttpListener/tests/HttpRequestStreamTests.cs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,52 @@ public async Task Read_ChunkSizeWithWhitespace_ThrowsHttpListenerException(strin
736736
}
737737
}
738738

739+
// http.sys rejects this request with a 400 before a context is ever handed out, so there is
740+
// no request stream to read from; this asserts the behavior of the managed implementation.
741+
[ConditionalTheory(typeof(Helpers), nameof(Helpers.IsManagedImplementation))]
742+
[InlineData(false)]
743+
[InlineData(true)]
744+
public async Task Read_ChunkSizeWithBareCR_ThrowsHttpListenerException(bool sendCRSeparately)
745+
{
746+
using (Socket client = _factory.GetConnectedSocket())
747+
{
748+
Uri listeningUri = new Uri(_factory.ListeningUrl);
749+
750+
// The CR in "1\r0" isn't followed by a LF, so this must not be read as a 0x10 byte chunk.
751+
string beforeCR =
752+
$"POST {listeningUri.PathAndQuery} HTTP/1.1\r\n" +
753+
$"Host: {listeningUri.Host}\r\n" +
754+
"Transfer-Encoding: chunked\r\n" +
755+
"\r\n" +
756+
"1\r";
757+
string afterCR =
758+
"0\n" +
759+
"0123456789ABCDEF\r\n" +
760+
"0\r\n" +
761+
"\r\n";
762+
763+
byte[] buffer = new byte[16];
764+
Task<int> read;
765+
if (sendCRSeparately)
766+
{
767+
// Send the byte after the CR only once the read is pending, so the CR and that
768+
// byte reach the chunk decoder in separate writes.
769+
await client.SendAsync(Encoding.ASCII.GetBytes(beforeCR));
770+
HttpListenerContext context = await _listener.GetContextAsync();
771+
read = ReadLengthAsync(context.Request.InputStream, buffer, 0, buffer.Length);
772+
await client.SendAsync(Encoding.ASCII.GetBytes(afterCR));
773+
}
774+
else
775+
{
776+
await client.SendAsync(Encoding.ASCII.GetBytes(beforeCR + afterCR));
777+
HttpListenerContext context = await _listener.GetContextAsync();
778+
read = ReadLengthAsync(context.Request.InputStream, buffer, 0, buffer.Length);
779+
}
780+
781+
await Assert.ThrowsAsync<HttpListenerException>(() => read);
782+
}
783+
}
784+
739785
[ConditionalTheory(typeof(Helpers), nameof(Helpers.IsManagedImplementation))]
740786
[InlineData("80000000")]
741787
[InlineData("FFFFFFFF")]

‎src/libraries/System.Net.HttpListener/tests/InvalidClientRequestTests.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public static IEnumerable<object[]> InvalidRequest_TestData()
9797
yield return new object[] { "GET {path} HTTP/1.1", null, new string[] { "Header\t: value" }, null, "Bad Request" };
9898
}
9999

100+
// Bare CR, i.e. a CR that isn't immediately followed by a LF
101+
yield return new object[] { "GET {path}a\rb HTTP/1.1", null, null, null, "Bad Request" };
102+
yield return new object[] { "GET {path} HTTP/1.1", null, new string[] { "Content-Le\rngth: 0" }, "\r\n", "Bad Request" };
103+
yield return new object[] { "GET {path} HTTP/1.1", null, new string[] { "Header: val\rue" }, "\r\n", "Bad Request" };
104+
yield return new object[] { "GET {path} HTTP/1.1", null, new string[] { "Header: value\r" }, "\r\n", "Bad Request" };
105+
100106
yield return new object[] { "GET {path} HTTP/1.1", "", null, null, "Bad Request" };
101107
yield return new object[] { "GET {path} HTTP/1.1", "Host: \r\n", null, null, "Bad Request" };
102108

0 commit comments

Comments
 (0)