Skip to content

[HTTP] Stricter header value validation#116634

Merged
ManickaP merged 14 commits into
dotnet:mainfrom
ManickaP:winhttp-headers
Jun 24, 2025
Merged

[HTTP] Stricter header value validation#116634
ManickaP merged 14 commits into
dotnet:mainfrom
ManickaP:winhttp-headers

Conversation

@ManickaP

@ManickaP ManickaP commented Jun 13, 2025

Copy link
Copy Markdown
Member

Based on RFC:

Field values containing CR, LF, or NUL characters are invalid and dangerous, due to the varying ways that implementations might parse and interpret those characters

  • Adding '\0' to forbidden characters in header value.
  • Making sure we follow the same rules in Cookie as well since that results in request headers.
  • Added negative / positive tests for all handlers and types of headers
  • WinHTTP already refuses bad characters, so no need to validate on our side, just improved the error from "Wrong parameter" --> "Request header must be valid Latin-1".

Changes here are up for discussion, this is just my opinionated solution to the problem.
cc @MihaZupan

Copilot AI review requested due to automatic review settings June 13, 2025 15:22
@ManickaP ManickaP requested a review from a team June 13, 2025 15:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements stricter validation for HTTP header values by rejecting header values containing CR, LF, or NUL characters, in accordance with RFC9110. It also improves related error messages and adds tests for the new validation rules.

  • Updated header validation logic throughout the HTTP and Cookie handlers.
  • Improved error messages with clearer, RFC-compliant text.
  • Added both negative and positive tests covering various header types and dangerous characters.

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs Added NUL character to reserved characters and updated value checks in cookies.
src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs Extended tests with additional dangerous header values.
src/libraries/System.Net.Http/src/System/Net/Http/HttpRuleParser.cs Updated new-line check to include NUL characters.
src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs Updated reason phrase validation to include NUL.
src/libraries/System.Net.Http/src/System/Net/Http/Headers/*.cs Replaced calls to CheckContainsNewLine with calls that also check for NUL.
src/libraries/System.Net.Http.WinHttpHandler/src/System/Net/Http/*.cs Adjusted WinHTTP header handling to throw more precise exceptions.
src/libraries/System.Net.Http.WinHttpHandler/src/Resources/Strings.resx Updated error message resource to reflect validation of NUL characters.
src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs Added extensive tests for forbidden and valid header characters.
Comments suppressed due to low confidence (1)

src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs:1130

  • [nitpick] The method name 'ContainsNewLineOrNull' may be misleading since it does not return a boolean value but instead validates the header value by throwing an exception when a forbidden character is found. Consider renaming it to 'EnsureNoNewLineOrNul' or a similar name that better reflects its behavior.
internal static void ContainsNewLineOrNull(string? value)

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Comment thread src/libraries/System.Net.Primitives/src/System/Net/Cookie.cs

@MihaZupan MihaZupan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in favour of blocking null as you suggest.

Can you please also update the fuzzer to look for nulls?
https://github.com/dotnet/runtime/blob/main/src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/HttpHeadersFuzzer.cs

Comment thread src/libraries/System.Net.Http/src/System/Net/Http/HttpRuleParser.cs
Comment thread src/libraries/System.Net.Http/src/Resources/Strings.resx
Comment thread src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs Outdated
Comment thread src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs Outdated
Comment thread src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs Outdated
@azure-pipelines

This comment was marked as off-topic.

@ManickaP

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@ManickaP

Copy link
Copy Markdown
Member Author

/ba-g unrelated and/or known issues

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants