Repository navigation
Consolidate loopback host checking in devtools_shared #10112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,18 +34,35 @@ const flutterEngineRasterCache = '_flutter.estimateRasterCacheMemory'; | |
| /// Allowed URI schemes for VM service connections. | ||
| const _allowedVmServiceSchemes = {'http', 'https', 'ws', 'wss', 'sse', 'sses'}; | ||
|
|
||
| /// Regular expression matching IPv4 loopback addresses (`127.0.0.0/8`). | ||
| final _ipv4LoopbackRegex = RegExp( | ||
| r'^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$', | ||
| ); | ||
|
|
||
| /// Returns whether [host] is a loopback host (`localhost`, `::1`, or | ||
| /// an IPv4 address in `127.0.0.0/8`). | ||
| /// Returns whether [host] is a loopback host (`localhost`, an IPv4 address in | ||
| /// `127.0.0.0/8`, or the IPv6 loopback address `::1`). | ||
| /// | ||
| /// This implementation is web-safe and uses [Uri.parseIPv4Address] and | ||
| /// [Uri.parseIPv6Address] so it can be shared across server, app, and extension | ||
| /// code without depending on `dart:io`. | ||
| bool isLoopbackHost(String host) { | ||
| final normalizedHost = host.toLowerCase(); | ||
| return normalizedHost == 'localhost' || | ||
| normalizedHost == '::1' || | ||
| _ipv4LoopbackRegex.hasMatch(normalizedHost); | ||
| if (normalizedHost == 'localhost') return true; | ||
| if (normalizedHost.isEmpty) return false; | ||
|
|
||
| if (normalizedHost.contains(':')) { | ||
| try { | ||
| final bytes = Uri.parseIPv6Address(normalizedHost); | ||
| for (var i = 0; i < 15; i++) { | ||
| if (bytes[i] != 0) return false; | ||
| } | ||
| return bytes[15] == 1; | ||
| } on FormatException { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| final bytes = Uri.parseIPv4Address(normalizedHost); | ||
| return bytes[0] == 127; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flagged by Gemini: On Dart SDKs prior to 3.10, This creates a parser differential: OS resolvers ( Requiring canonical decimal formatting avoids this across all supported Dart SDKs without needing to bump the minimum SDK constraint for return bytes[0] == 127 && bytes.join('.') == normalizedHost;We should also add |
||
| } on FormatException { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// Returns a normalized vm service uri. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,52 @@ import 'package:test/test.dart'; | |
| import 'package:vm_service/vm_service.dart'; | ||
|
|
||
| void main() { | ||
| group('isLoopbackHost', () { | ||
| test('accepts loopback hosts', () { | ||
| for (final host in const [ | ||
| 'localhost', | ||
| 'LOCALHOST', | ||
| 'LocalHost', | ||
| '127.0.0.1', | ||
| '127.0.0.2', | ||
| '127.255.255.254', | ||
| '::1', | ||
| '0:0:0:0:0:0:0:1', | ||
| '0000:0000:0000:0000:0000:0000:0000:0001', | ||
| ]) { | ||
| expect( | ||
| isLoopbackHost(host), | ||
| isTrue, | ||
| reason: 'Expected "$host" to be recognized as loopback', | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| test('rejects non-loopback and malformed hosts', () { | ||
| for (final host in const [ | ||
| '', | ||
| 'evil.com', | ||
| 'localhost.evil.com', | ||
| '127.0.0.1.evil.com', | ||
| '127.256.0.1', | ||
| '192.168.1.1', | ||
| '10.0.0.1', | ||
| '0.0.0.0', | ||
| '::', | ||
| '::2', | ||
| '::ffff:127.0.0.1', | ||
| '[::1]', | ||
| '::1%lo0', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: consider adding |
||
| ]) { | ||
| expect( | ||
| isLoopbackHost(host), | ||
| isFalse, | ||
| reason: 'Expected "$host" to be rejected as non-loopback', | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| group('normalizeVmServiceUri', () { | ||
| test('normalizes simple URIs', () { | ||
| expect( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[CONCERN] To improve readability, avoid magic numbers, and maintain consistency with how IP address bytes are compared elsewhere in the codebase (e.g.,
ServerApi._hostsMatchusingListEquality), we can compare the parsed IPv6 bytes directly to the expected loopback byte array usingListEqualityfrompackage:collection.References