Skip to content

Handling of "invalid" HTTP header values results in values being incorrectly reordered #63833

@geoffkizer

Description

@geoffkizer

When we parse known multi-valued headers like Accept, we store any "invalid" values (i.e. values we couldn't parse) separately so that they will still be written to the wire and can still be enumerated via NonValidated. However, these invalid values are reordered relative to the rest of the values so that they always appear after valid values.

Repro code:

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://microsoft.com");

request.Headers.TryAddWithoutValidation("Accept", "text/bar");
request.Headers.TryAddWithoutValidation("Accept", "invalid");
request.Headers.TryAddWithoutValidation("Accept", "text/baz");

Console.WriteLine("NonValidated before parsing (correct):");
PrintHeadersNonValidated(request);

// Force parsing
_ = request.Headers.Accept.Count;

Console.WriteLine("NonValidated after parsing (incorrect):");
PrintHeadersNonValidated(request);

static void PrintHeadersNonValidated(HttpRequestMessage request)
{
    foreach (var header in request.Headers.NonValidated)
    {
        Console.WriteLine($"  {header.Key}:");
        foreach (var value in header.Value)
        {
            Console.WriteLine($"    {value}");
        }
    }

    Console.WriteLine();
} 

Output:

NonValidated before parsing (correct):
  Accept:
    text/bar
    invalid
    text/baz

NonValidated after parsing (incorrect):
  Accept:
    text/bar
    text/baz
    invalid

Note that this behavior is not limited to NonValidated -- it affects how the request headers are written to the wire when the request is sent.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions