runtime icon indicating copy to clipboard operation
runtime copied to clipboard

HttpClient Silently Omits Message Body If Header Has Trailing Newline

Open bradwestness opened this issue 5 years ago • 3 comments

Description

In .NET 5, if you construct an HttpRequestMessage and set a header value that has a trailing newline, there is no exception thrown, but when the request is sent, the message body will silently be omitted.

Example:

var message = new HttpRequestMessage(HttpMethod.Post, "http://example.com/some/endpoint");
message.Content = new StringContent("{ \"someKey\": \"someValue\" }", Encoding.UTF8, "application/json");
// note trailing newline in header value
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "myBearerToken\n");
var response = await httpClient.SendAsync(request);

The message will still be sent and if you inspect it, the Content appears to be there, but the message body is not included with the payload that gets sent over the wire.

It seems like adding a header value with newlines should either cause an exception or else cause the message's Content to be removed so it doesn't appear to be including the content when it doesn't ultimately actually get sent.

Calling .Trim() on the header value prevents this issue, however it's not obvious to diagnose especially if you're dealing with encrypted tokens that are only decrypted at runtime (in my case, the newline was actually encrypted into the string, so it wasn't obvious when looking at it that there was a newline included).

The relevant section of the HTTP spec does say that a sequence of CRLF is meant to cause the rest of the content to be ignored, however it seems like this is probably not ever intended behavior when calling HttpClient methods to add headers that are intended to validate the header (in contrast to TryAddWithoutValidation()).

Configuration

.NET 5, reproduced on Windows and Ubuntu

bradwestness avatar Sep 07 '21 21:09 bradwestness

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

Issue Details

Description

In .NET 5, if you construct an HttpRequestMessage and set a header value that has a trailing newline, there is no exception thrown, but when the request is sent, the message body will silently be omitted.

Example:

var message = new HttpRequestMessage(HttpMethod.Post, "http://example.com/some/endpoint");
message.Content = new StringContent("{ \"someKey\": \"someValue\" }", Encoding.UTF8, "application/json");
// note trailing newline in header value
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "myBearerToken\n");
var response = await httpClient.SendAsync(request);

The message will still be sent and if you inspect it, the Content appears to be there, but the message body is not included with the payload that gets sent over the wire.

It seems like adding a header value with newlines should either cause an exception or else cause the message's Content to be removed so it doesn't appear to be including the content when it doesn't ultimately actually get sent.

Calling .Trim() on the header value prevents this issue, however it's not obvious to diagnose especially if you're dealing with encrypted tokens that are only decrypted at runtime (in my case, the newline was actually encrypted into the string, so it wasn't obvious when looking at it that there was a newline included).

The relevant section of the HTTP spec does say that a sequence of CRLF is meant to cause the rest of the content to be ignored, however it seems like this is probably not ever intended behavior when calling HttpClient methods to add headers that are intended to validate the header (in contrast to TryAddWithoutValidation()).

Configuration

.NET 5, reproduced on Windows and Ubuntu

Author: bradwestness
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

ghost avatar Sep 07 '21 21:09 ghost

the message body will silently be omitted

The body will be sent, but the server may fail to interpret it / reject the request as the request is corrupting the HTTP flow. image

See the underlined bytes in the screenshot above - the server may interpret the 0A 0D 0A (\n\r\n) as two new lines separating the headers from the body. As it never sees a Content-Length or Transfer-Encoding header, it doesn't know there is any content.

From 6.0 onwards, you will see

System.FormatException: 'New-line characters are not allowed in header values.'

if you try to add a header value with new line characters. (https://github.com/dotnet/runtime/pull/53505#issuecomment-880538401)

MihaZupan avatar Sep 07 '21 22:09 MihaZupan

Ah, ok. I was looking at the messages in Fiddler and it doesn't show any content (and the remote server was also evidently just silently interpreting it as no content) so I assumed it wasn't getting sent. But evidently Fiddler was also just interpreting the CLRF as an end of message delimiter and not showing the rest.

Sounds like the .NET 6 behavior is already addressing this and would've saved me some headaches figuring out what was going on by failing earlier. 😅

bradwestness avatar Sep 07 '21 22:09 bradwestness