[System.Net] Fix IPNetwork.Contains for mapped IPv4 addresses - #131862
Open
wfurt wants to merge 1 commit into
Open
[System.Net] Fix IPNetwork.Contains for mapped IPv4 addresses#131862wfurt wants to merge 1 commit into
wfurt wants to merge 1 commit into
Conversation
Select the comparison path based on the network address family so IPv4-mapped addresses are evaluated with IPv6 semantics for IPv6 networks. Fixes dotnet#131861
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts System.Net.IPNetwork.Contains so the comparison logic is selected based on the network’s address family (IPv4 vs IPv6), ensuring IPv6 networks apply full 128-bit prefix matching even when the queried address is an IPv4-mapped IPv6 address.
Changes:
- Update
IPNetwork.Containsto choose IPv4/IPv6 matching based onBaseAddress.AddressFamilyrather than the queried address. - Add functional test coverage for IPv4-mapped IPv6 addresses queried against IPv6 networks (both expected in-range and out-of-range cases).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Net.Primitives/src/System/Net/IPNetwork.cs | Switches Contains matching path selection to be driven by the network’s address family to avoid incorrect IPv4-path matching for IPv6 networks. |
| src/libraries/System.Net.Primitives/tests/FunctionalTests/IPNetworkTest.cs | Adds theory coverage validating Contains results for IPv4-mapped IPv6 addresses against IPv6 networks. |
rzikm
approved these changes
Aug 5, 2026
MihaZupan
approved these changes
Aug 5, 2026
MihaZupan
enabled auto-merge (squash)
August 5, 2026 11:22
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.
Fixes #131861
IPNetwork.Containscurrently selects its comparison path from the queried address. An IPv4-mapped address can therefore select the IPv4 path even when the network itself is IPv6.Select the comparison path from the network address family instead. This preserves support for querying IPv4 networks with IPv4-mapped addresses while ensuring IPv6 networks use 128-bit prefix semantics.
Adds coverage for mapped addresses both inside and outside IPv6 networks.
Testing
./dotnet.sh build src/libraries/System.Net.Primitives/src/System.Net.Primitives.csproj./dotnet.sh build src/libraries/System.Net.Primitives/tests/FunctionalTests/System.Net.Primitives.Functional.Tests.csproj /t:Test(6,311 passed)