Skip to content

Fix IPAddress & Uri octal parsing inconsistencies - #121962

Merged
MihaZupan merged 3 commits into
dotnet:mainfrom
MihaZupan:uri-ipv4octal
Nov 26, 2025
Merged

MihaZupan merged 3 commits into
dotnet:mainfrom
MihaZupan:uri-ipv4octal

Conversation

@MihaZupan

Copy link
Copy Markdown
Member

Our IPv4 logic has two validation modes:

  • the default that handles things like hex/octal segments, or fewer than 4 segments
  • a stricter "Canonical" mode which requires 4 segments and disallows hex/octal

The stricter mode is used for IPv4 addresses embedded in IPv6, and by Uri when dealing with an unknown scheme.
E.g. "http://123/" is treated as 0.0.0.123, but "custom://123/" is treated as a non-IPv4 host and we just use 123.

The current "stricter" logic has broken logic around octal where we'll allow it in the last segment (we were only validating it when encountering a dot, but not at the end).
E.g. 1.1.1.01 was considered valid, but 1.1.01.1 was not.

That error further meant that while we allowed octal in the last segment, we weren't validating that the digit is in range, so an input like 1.1.1.08 would be valid in the strict mode, but not in the non-strict one.
This violated the expectation that anything valid under the strict mode is also valid under the non-strict mode.

As a result, an input such as custom://1.1.1.08/ is parsed as custom://255.255.255.255/ (and triggers a debug assert).

This PR fixes the strict validation to also forbid octal in the last segmnet.

@MihaZupan MihaZupan added this to the 11.0.0 milestone Nov 25, 2025
@MihaZupan
MihaZupan requested a review from a team November 25, 2025 16:58
@MihaZupan MihaZupan self-assigned this Nov 25, 2025
Copilot AI review requested due to automatic review settings November 25, 2025 16:58

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 fixes IPv4 address parsing inconsistencies related to octal notation in canonical/strict validation mode. The strict mode is used for IPv4 addresses embedded in IPv6 literals and by Uri when dealing with unknown schemes. Previously, the strict validator incorrectly allowed octal notation (leading zeros) in the last segment but not in earlier segments, and failed to validate digit ranges for octal-like patterns, leading to parsing errors.

Key Changes:

  • Simplified octal detection in IsValidCanonical to uniformly reject any segment starting with '0' followed by another digit
  • Removed buggy test cases that expected octal notation to work in embedded IPv4 addresses
  • Added new test cases to verify octal notation is properly rejected in all positions

Reviewed changes

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

File Description
src/libraries/Common/src/System/Net/IPv4AddressHelper.Common.cs Removed flawed firstCharIsZero tracking logic and replaced with simpler octal detection that rejects leading zeros followed by digits at the start of any segment
src/libraries/System.Private.Uri/tests/FunctionalTests/UriIpHostTest.cs Added test coverage for custom URI schemes and removed test case expecting octal to work in embedded IPv4
src/libraries/System.Net.Primitives/tests/FunctionalTests/IPEndPointParsing.cs Removed test case expecting octal notation to work in embedded IPv4 addresses
src/libraries/System.Net.Primitives/tests/FunctionalTests/IPAddressParsing.cs Added test cases for invalid octal notation in IPv4 addresses and embedded IPv4 in IPv6, removed test case expecting octal to work

@stephentoub stephentoub left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could anyone reasonably be relying on the current behavior?

@MihaZupan

Copy link
Copy Markdown
Member Author

Could, but I think it's unlikely given that using octal in other segments wouldn't have worked.

Given that we're collecting a couple such edge-case changes (#121392, #121083, #120908, this), I'm thinking of filing a single breaking change doc for Uri for .NET 11 that lists all of these.

@MihaZupan MihaZupan added breaking-change Issue or PR that represents a breaking API or functional change over a previous release. needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet labels Nov 26, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Added needs-breaking-change-doc-created label because this PR has the breaking-change label.

When you commit this breaking change:

  1. Create and link to this PR and the issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.
  2. Ask a committer to mail the .NET Breaking Change Notification DL.

Tagging @dotnet/compat for awareness of the breaking change.

@MihaZupan
MihaZupan merged commit 20901cb into dotnet:main Nov 26, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 27, 2025
@dotnet dotnet unlocked this conversation Jan 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📋 Breaking Change Documentation Required

Create a breaking change issue with AI-generated content

Generated by Breaking Change Documentation Tool - 2026-01-10 00:32:40

@github-actions github-actions Bot locked and limited conversation to collaborators Feb 9, 2026
@MihaZupan MihaZupan removed the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Aug 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Net breaking-change Issue or PR that represents a breaking API or functional change over a previous release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants