fix: NumberParseException instead of ArgumentOutOfRangeException on malformed RFC3966 phone-context ordering - #406
Merged
Conversation
…n on malformed RFC3966 phone-context ordering BuildNationalNumberForParsing assumed the "tel:" prefix, if present, always occurs before the ";phone-context=" marker. When it occurs after (e.g. ";phone-context=example.com;tel:1234"), indexOfPhoneContext - indexOfNationalNumber went negative and leaked an unchecked ArgumentOutOfRangeException from StringBuilder.Append(string, int, int) out of the public Parse API, instead of the documented NumberParseException. Found via code review of a separate change that narrowed a catch-all in PhoneNumbers.Extensions.PhoneNumber.TryParse to catch (NumberParseException) specifically — that narrowing was correct, but it stopped silently swallowing this pre-existing bug.
This was referenced Aug 24, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #406 +/- ##
==========================================
+ Coverage 77.26% 77.27% +0.01%
==========================================
Files 39 39
Lines 4548 4551 +3
Branches 1129 1130 +1
==========================================
+ Hits 3514 3517 +3
Misses 783 783
Partials 251 251 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
📊 Benchmark Results
PR branch
PR base
|
This was referenced Aug 28, 2026
Closed
Closed
Merged
This was referenced Sep 7, 2026
Closed
Open
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.
Summary
BuildNationalNumberForParsingassumed the"tel:"prefix, if present anywhere in the input, always occurs before the";phone-context="marker. When it occurs after — e.g.";phone-context=example.com;tel:1234"—indexOfPhoneContext - indexOfNationalNumbergoes negative, andStringBuilder.Append(string, int, int)throws an uncheckedArgumentOutOfRangeExceptionstraight out of the publicPhoneNumberUtil.ParseAPI, instead of the documentedNumberParseException.Found via code review of #401 (narrowing
PhoneNumbers.Extensions.PhoneNumber.TryParse'scatchtocatch (NumberParseException)) — that narrowing is correct, but it stopped silently swallowing this pre-existing bug. It's reachable directly throughPhoneNumberUtil.Parsewith no dependency on #401, and is worse combined with #405's new[PhoneNumber]ValidationAttribute: aValidationAttribute.IsValidis conventionally expected to never throw on arbitrary/untrusted input, so this could crash model validation on a crafted string.Fix: guard the ordering assumption and throw
NumberParseException(NOT_A_NUMBER, ...)instead, matching the error this method already throws just above for other malformed phone-context input.Recommend merging this before/alongside #401 and #405.
Test plan
PhoneNumberUtil.GetInstance().Parse(";phone-context=example.com;tel:1234", null)threwArgumentOutOfRangeExceptionbefore this fix.TestFailedParseOnInvalidNumbers(VerifyFailure(";phone-context=example.com;tel:1234", RegionCode.ZZ, ErrorType.NOT_A_NUMBER)), alongside the other phone-context failure cases.dotnet build csharp --no-restore— 0 warnings/errorsdotnet test csharp/PhoneNumbers.slnx— 864 tests passed (net8.0 + net10.0)