Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions packages/devtools_shared/lib/src/server/server_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class ServerApi {
/// in [allowedOrigins].
static bool _isAllowedHost(String host, List<Uri> 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;
Expand All @@ -190,7 +190,7 @@ class ServerApi {
/// host, and port of an HTTP(S) URI in [allowedOrigins].
static bool _isAllowedOrigin(Uri originUri, List<Uri> 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;
Expand All @@ -211,14 +211,6 @@ class ServerApi {
return const ListEquality<int>().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].
Expand Down
37 changes: 27 additions & 10 deletions packages/devtools_shared/lib/src/service_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +51 to +54

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

[CONCERN] To improve readability, avoid magic numbers, and maintain consistency with how IP address bytes are compared elsewhere in the codebase (e.g., ServerApi._hostsMatch using ListEquality), we can compare the parsed IPv6 bytes directly to the expected loopback byte array using ListEquality from package:collection.

      const ipv6LoopbackBytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
      return const ListEquality<int>().equals(bytes, ipv6LoopbackBytes);
References
  1. Adhering to consistent style across all DevTools packages improves collaboration and reduces errors. Avoid using raw/magic strings or numbers. Extract them into descriptive named constants so the intent and meaning of the values are clear. (link)

} on FormatException {
return false;
}
}

try {
final bytes = Uri.parseIPv4Address(normalizedHost);
return bytes[0] == 127;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flagged by Gemini:

On Dart SDKs prior to 3.10, Uri.parseIPv4Address allows leading zeros in octets, so isLoopbackHost('0127.0.0.1') returns true (previously _ipv4LoopbackRegex rejected this because it matched ^127\.).

This creates a parser differential: OS resolvers (getaddrinfo / inet_aton) interpret leading-zero IPv4 octets as octal—resolving 0127.0.0.1 to 87.0.0.1 instead of loopback—while the WHATWG URL standard rejects non-zero leading-zero octets as invalid. As a result, ws://0127.0.0.1:8181/ws can pass normalizeVmServiceUri on older SDKs while pointing to a non-loopback address.

Requiring canonical decimal formatting avoids this across all supported Dart SDKs without needing to bump the minimum SDK constraint for devtools_shared on pub.dev:

return bytes[0] == 127 && bytes.join('.') == normalizedHost;

We should also add '0127.0.0.1' and '127.0.0.01' to the rejected list in service_utils_test.dart.

} on FormatException {
return false;
}
}

/// Returns a normalized vm service uri.
Expand Down
46 changes: 46 additions & 0 deletions packages/devtools_shared/test/service_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: consider adding 'localhost.' and '127.0.0.1 ' here to lock in the no-trim and no-trailing-dot behavior.

]) {
expect(
isLoopbackHost(host),
isFalse,
reason: 'Expected "$host" to be rejected as non-loopback',
);
}
});
});

group('normalizeVmServiceUri', () {
test('normalizes simple URIs', () {
expect(
Expand Down
Loading