You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Uri.parseIPv4Address accepts IPv4 addresses with arbitrary number of leading zeroes while InternetAddress.tryParse does not. #61392
Passing a IPv4 string with a large number of leading zeroes in any of the octets to Uri.parseIPv4Address would lead to long parsing time and high memory usage. This may be a security concern. Uri.parseIPv6Address is also affected (for the trailing IPv4 part of the IPv6 address).
InternetAddress.tryParse however rejects leading zeroes and is not affected.
Should Uri.parseIPv4Address be changed to match the behavior of InternetAddress.tryParse? It may affect users who use it to parse IPv4 addresses in octal form.
So we have two parsers. That seems redundant. The one in Uri is for URIs and should follow the appropriate spec, and the one in InternetAddress may follow a different spec, so it might make sense to have two.
Except ... the spec followed by the Dart Uri class is RFC 3986, and it does not allow leading zeros in IPv4address:
I looked at the existing code, and it's somewhat inefficient (fx it creates substrings to pass to int.parse).
I've rewritten it and parseIPv6Address to avoid extra allocations and most unnecessary copying
(https://dart-review.googlesource.com/c/sdk/+/447260).
Activity
lrhn commentedon Aug 25, 2025
So we have two parsers. That seems redundant. The one in
Uriis for URIs and should follow the appropriate spec, and the one inInternetAddressmay follow a different spec, so it might make sense to have two.Except ... the spec followed by the Dart
Uriclass is RFC 3986, and it does not allow leading zeros in IPv4address:It's a breaking change to fix it, but hopefully not something many people depend on. It was never a valid RFC 3986 URI.
The WhatWG URL spec also don't allow leading zeros:
So it wouldn't work anywhere else.
elliotwutingfeng commentedon Aug 25, 2025
InternetAddressis part of thedart:iolibrary too, which is not available for web apps, so the only option for web apps isUri.elliotwutingfeng commentedon Aug 26, 2025
I've opened a merge request to patch this. This would however, be a breaking change for users who need to parse IPv4 addresses with leading zeros.
lrhn commentedon Aug 27, 2025
I looked at the existing code, and it's somewhat inefficient (fx it creates substrings to pass to
int.parse).I've rewritten it and
parseIPv6Addressto avoid extra allocations and most unnecessary copying(https://dart-review.googlesource.com/c/sdk/+/447260).
BREAKING CHANGE: Fix `parseIPv4Address` to not allow leading zeros.
elliotwutingfeng commentedon Sep 11, 2025
@lrhn I would like to point out that this patch has also fixed the following bug:
used to erroneously throw the following exception
fix: update IPv6 tests to handle changes made in Dart's IPv4/6 IP par…