Skip to content

Validate HttpVersionPolicy enum values on setters#129070

Open
Copilot wants to merge 2 commits into
mainfrom
copilot/add-validation-httprequestmessage-versionpolicy
Open

Validate HttpVersionPolicy enum values on setters#129070
Copilot wants to merge 2 commits into
mainfrom
copilot/add-validation-httprequestmessage-versionpolicy

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 6, 2026

HttpRequestMessage.VersionPolicy and HttpClient.DefaultVersionPolicy accepted arbitrary HttpVersionPolicy values, deferring failures to deep inside the request pipeline (or silently mis-behaving).

Changes

  • HttpRequestMessage.VersionPolicy setter — reject values outside RequestVersionOrLower..RequestVersionExact with ArgumentException (paramName value).
  • HttpClient.DefaultVersionPolicy setter — same validation, before the existing CheckDisposedOrStarted() call.
  • Error message — reuses the existing SR.net_invalid_enum resource ("The specified value is not valid in the '{0}' enumeration."), matching the pattern already used in System.Net.Security (SslClientAuthenticationOptions, SslStream, etc.).
  • Tests — added theory tests covering -1, 3, and int.MaxValue for both setters.
public HttpVersionPolicy VersionPolicy
{
    get => _versionPolicy;
    set
    {
        if ((uint)value > (uint)HttpVersionPolicy.RequestVersionExact)
        {
            throw new ArgumentException(SR.Format(SR.net_invalid_enum, nameof(HttpVersionPolicy)), nameof(value));
        }
        CheckDisposed();
        _versionPolicy = value;
    }
}

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 6, 2026 13:37
Copilot AI review requested due to automatic review settings June 6, 2026 13:37
Comment thread src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs
Comment thread src/libraries/System.Net.Http/tests/FunctionalTests/HttpRequestMessageTest.cs Outdated
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
Copilot AI review requested due to automatic review settings June 6, 2026 13:38
@MihaZupan MihaZupan added this to the 11.0.0 milestone Jun 6, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 tightens argument validation for HttpVersionPolicy setters in System.Net.Http so invalid enum values fail fast at the API boundary rather than later in the request pipeline.

Changes:

  • Added range validation to HttpRequestMessage.VersionPolicy setter, throwing ArgumentException for out-of-range HttpVersionPolicy values.
  • Added the same validation to HttpClient.DefaultVersionPolicy setter.
  • Added functional tests covering several invalid enum values for both setters.

Reviewed changes

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

File Description
src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestMessage.cs Validates VersionPolicy is within the defined HttpVersionPolicy range before assignment.
src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs Validates DefaultVersionPolicy is within the defined HttpVersionPolicy range before CheckDisposedOrStarted() and assignment.
src/libraries/System.Net.Http/tests/FunctionalTests/HttpRequestMessageTest.cs Adds theory coverage verifying invalid VersionPolicy values throw ArgumentException with paramName value.
src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs Adds theory coverage verifying invalid DefaultVersionPolicy values throw ArgumentException with paramName value, and client remains usable after.

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants