Repository navigation
Fix IPAddress & Uri octal parsing inconsistencies - #121962
Conversation
There was a problem hiding this comment.
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
IsValidCanonicalto 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 |
Co-authored-by: xtqqczze <[email protected]>
stephentoub
left a comment
There was a problem hiding this comment.
Could anyone reasonably be relying on the current behavior?
|
Added When you commit this breaking change:
Tagging @dotnet/compat for awareness of the breaking change. |
📋 Breaking Change Documentation RequiredCreate a breaking change issue with AI-generated content Generated by Breaking Change Documentation Tool - 2026-01-10 00:32:40 |
Our IPv4 logic has two validation modes:
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 as0.0.0.123, but"custom://123/"is treated as a non-IPv4 host and we just use123.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.01was considered valid, but1.1.01.1was 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.08would 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 ascustom://255.255.255.255/(and triggers a debug assert).This PR fixes the strict validation to also forbid octal in the last segmnet.