Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 3, 2025

WinHttpHandler was passing headers to WinHTTP.dll without validating that header values contain only ASCII characters, unlike SocketsHttpHandler which performs this validation.

Changes

Added ASCII validation to WinHttpHandler.AddRequestHeaders():

  • Added IsAscii() helper method to check for ASCII characters (char <= 127)
  • Added ValidateHeadersForAscii() method that throws HttpRequestException for non-ASCII headers
  • Modified header serialization to validate:
    • Cookie headers from WinHttpCookieContainerAdapter.GetCookieHeader()
    • General request headers from requestMessage.Headers.ToString()
    • Content headers from requestMessage.Content.Headers.ToString()

Added comprehensive tests:

  • SendAsync_RequestWithNonAsciiHeaderValue_ThrowsHttpRequestException() - validates rejection of non-ASCII request headers
  • SendAsync_RequestWithAsciiHeaderValue_Succeeds() - validates ASCII headers work normally
  • SendAsync_RequestWithNonAsciiContentHeader_ThrowsHttpRequestException() - validates rejection of non-ASCII content headers

Behavior

Now throws HttpRequestException with message "Request headers must contain only ASCII characters." when header values contain characters > 127, matching the behavior and security posture of SocketsHttpHandler.

Testing

  • All 3 new tests pass ✅
  • All existing 175 unit tests continue to pass ✅
  • Validates RFC 9110 compliance for header values

Fixes #115112.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jun 3, 2025
@MihaZupan MihaZupan added area-System.Net.Http and removed linkable-framework Issues associated with delivering a linker friendly framework labels Jun 3, 2025
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jun 3, 2025
@MihaZupan MihaZupan removed the linkable-framework Issues associated with delivering a linker friendly framework label Jun 3, 2025
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jun 3, 2025
Co-authored-by: ManickaP <11718369+ManickaP@users.noreply.github.com>
Copilot AI changed the title [WIP] [WinHTTP] Validate header values for ASCII [WinHTTP] Validate header values for ASCII Jun 3, 2025
Copilot AI requested a review from ManickaP June 3, 2025 12:41
{
if (!IsAscii(headers))
{
throw new HttpRequestException("Request headers must contain only ASCII characters.");
Copy link
Member

Choose a reason for hiding this comment

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

The exception message should be in the resx.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the hardcoded exception message to the Strings.resx file and updated the code to use SR.net_http_headers_must_be_ascii. The message is now properly localized. 00a1e04

{
for (int i = 0; i < value.Length; i++)
{
if (value[i] > 127)
Copy link
Member

Choose a reason for hiding this comment

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

@ManickaP, is restricting to ASCII correct, or should it support Latin-1?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, for historical reasons it should allow Latin-1. On the other hand, it should not allow "dangerous" controls like CR LF and NUL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated header validation to allow Latin-1 characters (0-255) instead of just ASCII (0-127) for historical reasons, while still blocking dangerous control characters (CR, LF, NUL). Changed validation approach to check individual header values rather than the serialized header string to avoid issues with protocol formatting. Commit ab6c173.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…king dangerous controls

Co-authored-by: ManickaP <11718369+ManickaP@users.noreply.github.com>
@ManickaP
Copy link
Member

ManickaP commented Jun 5, 2025

After a discussion with @MihaZupan, closing this in favor of #116335.

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

Labels

area-System.Net.Http linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[WinHTTP] Validate header values for ASCII

4 participants