Repository navigation
Commit 2ef0256
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 used1 parent ce202f8 commit 2ef0256
4 files changed
Lines changed: 66 additions & 4 deletions
File tree
- src/libraries/System.Net.HttpListener
- src/System/Net/Managed
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
249 | 249 | | |
250 | 250 | | |
251 | 251 | | |
252 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
253 | 258 | | |
| 259 | + | |
254 | 260 | | |
255 | 261 | | |
256 | 262 | | |
| |||
260 | 266 | | |
261 | 267 | | |
262 | 268 | | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | 269 | | |
267 | 270 | | |
268 | 271 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
423 | 423 | | |
424 | 424 | | |
425 | 425 | | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
426 | 433 | | |
427 | 434 | | |
428 | 435 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
736 | 736 | | |
737 | 737 | | |
738 | 738 | | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
739 | 785 | | |
740 | 786 | | |
741 | 787 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
100 | 106 | | |
101 | 107 | | |
102 | 108 | | |
| |||
0 commit comments