Validate HttpVersionPolicy enum values on setters#129070
Open
Copilot wants to merge 2 commits into
Open
Conversation
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
MihaZupan
June 6, 2026 13:37
View session
MihaZupan
approved these changes
Jun 6, 2026
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
Contributor
There was a problem hiding this comment.
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.VersionPolicysetter, throwingArgumentExceptionfor out-of-rangeHttpVersionPolicyvalues. - Added the same validation to
HttpClient.DefaultVersionPolicysetter. - 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. |
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HttpRequestMessage.VersionPolicyandHttpClient.DefaultVersionPolicyaccepted arbitraryHttpVersionPolicyvalues, deferring failures to deep inside the request pipeline (or silently mis-behaving).Changes
HttpRequestMessage.VersionPolicysetter — reject values outsideRequestVersionOrLower..RequestVersionExactwithArgumentException(paramNamevalue).HttpClient.DefaultVersionPolicysetter — same validation, before the existingCheckDisposedOrStarted()call.SR.net_invalid_enumresource ("The specified value is not valid in the '{0}' enumeration."), matching the pattern already used inSystem.Net.Security(SslClientAuthenticationOptions,SslStream, etc.).-1,3, andint.MaxValuefor both setters.