Skip to content

[System.Net] Fix IPNetwork.Contains for mapped IPv4 addresses - #131862

Open
wfurt wants to merge 1 commit into
dotnet:mainfrom
wfurt:fix-ipnetwork-ipv4-mapped-address
Open

[System.Net] Fix IPNetwork.Contains for mapped IPv4 addresses#131862
wfurt wants to merge 1 commit into
dotnet:mainfrom
wfurt:fix-ipnetwork-ipv4-mapped-address

Conversation

@wfurt

@wfurt wfurt commented Aug 5, 2026

Copy link
Copy Markdown
Member

Fixes #131861

IPNetwork.Contains currently 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)

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
Copilot AI review requested due to automatic review settings August 5, 2026 09:06
@azure-pipelines

Copy link
Copy Markdown
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.

@wfurt wfurt self-assigned this Aug 5, 2026
@wfurt
wfurt requested a review from a team August 5, 2026 09:06
@wfurt wfurt added this to the 11.0.0 milestone Aug 5, 2026
@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.

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 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.Contains to choose IPv4/IPv6 matching based on BaseAddress.AddressFamily rather 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.

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.

IPNetwork.Contains handles IPv4-mapped addresses inconsistently for IPv6 networks

4 participants