diff --git a/packages/devtools_shared/lib/src/server/server_api.dart b/packages/devtools_shared/lib/src/server/server_api.dart index f284d7f281f..efbe434a6ee 100644 --- a/packages/devtools_shared/lib/src/server/server_api.dart +++ b/packages/devtools_shared/lib/src/server/server_api.dart @@ -177,7 +177,7 @@ class ServerApi { /// in [allowedOrigins]. static bool _isAllowedHost(String host, List allowedOrigins) { if (host.isEmpty) return false; - if (_isLoopbackHost(host)) return true; + if (isLoopbackHost(host)) return true; for (final allowedOrigin in allowedOrigins) { if (allowedOrigin.host.isEmpty) continue; @@ -190,7 +190,7 @@ class ServerApi { /// host, and port of an HTTP(S) URI in [allowedOrigins]. static bool _isAllowedOrigin(Uri originUri, List allowedOrigins) { if (originUri.host.isEmpty) return false; - if (_isLoopbackHost(originUri.host)) return true; + if (isLoopbackHost(originUri.host)) return true; for (final allowedOrigin in allowedOrigins) { if (!_isHttpOrHttps(allowedOrigin)) continue; @@ -211,14 +211,6 @@ class ServerApi { return const ListEquality().equals(addrA.rawAddress, addrB.rawAddress); } - /// Returns whether [host] is a loopback host - /// (`localhost`, `127.x.x.x`, or `::1`). - static bool _isLoopbackHost(String host) { - if (host.toLowerCase() == 'localhost') return true; - final address = InternetAddress.tryParse(host); - return address?.isLoopback ?? false; - } - /// Handles all requests. /// /// To override an API call, pass in a subclass of [ServerApi]. diff --git a/packages/devtools_shared/lib/src/service_utils.dart b/packages/devtools_shared/lib/src/service_utils.dart index a148a93a9d9..c73d271daeb 100644 --- a/packages/devtools_shared/lib/src/service_utils.dart +++ b/packages/devtools_shared/lib/src/service_utils.dart @@ -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; + } on FormatException { + return false; + } } /// Returns a normalized vm service uri. diff --git a/packages/devtools_shared/test/service_utils_test.dart b/packages/devtools_shared/test/service_utils_test.dart index 84f8b811f93..e181fee0a91 100644 --- a/packages/devtools_shared/test/service_utils_test.dart +++ b/packages/devtools_shared/test/service_utils_test.dart @@ -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', + ]) { + expect( + isLoopbackHost(host), + isFalse, + reason: 'Expected "$host" to be rejected as non-loopback', + ); + } + }); + }); + group('normalizeVmServiceUri', () { test('normalizes simple URIs', () { expect(