diff --git a/packages/devtools_app/lib/src/framework/framework_core.dart b/packages/devtools_app/lib/src/framework/framework_core.dart index 08f50bf419c..e1013ca5aac 100644 --- a/packages/devtools_app/lib/src/framework/framework_core.dart +++ b/packages/devtools_app/lib/src/framework/framework_core.dart @@ -134,7 +134,9 @@ extension FrameworkCore on Never { return true; } - final uri = normalizeVmServiceUri(serviceUriAsString); + final uri = + parseMessagePortUri(serviceUriAsString) ?? + normalizeVmServiceUri(serviceUriAsString); if (uri != null) { vmServiceInitializationInProgress = true; _lastServiceUriAsString = serviceUriAsString; diff --git a/packages/devtools_app/lib/src/framework/home_screen.dart b/packages/devtools_app/lib/src/framework/home_screen.dart index 08b99ab55ca..2c1848154f8 100644 --- a/packages/devtools_app/lib/src/framework/home_screen.dart +++ b/packages/devtools_app/lib/src/framework/home_screen.dart @@ -12,6 +12,7 @@ import 'package:material_ui/material_ui.dart'; import 'package:provider/provider.dart'; import '../service/connected_app/connection_info.dart'; +import '../service/message_port_connection.dart'; import '../shared/analytics/analytics.dart' as ga; import '../shared/analytics/constants.dart' as gac; import '../shared/config_specific/import_export/import_export.dart'; @@ -274,7 +275,8 @@ class _ConnectInputState extends State with BlockingActionMixin { await routerDelegate.updateArgsIfChanged({'uri': '$connectedUri'}); final shortUri = connectedUri.replace(path: ''); notificationService.push('Successfully connected to $shortUri.'); - } else if (normalizeVmServiceUri(uri) == null) { + } else if (parseMessagePortUri(uri) == null && + normalizeVmServiceUri(uri) == null) { notificationService.push( 'Failed to connect to the VM Service at "${connectDialogController.text}".\n' 'The link was not valid.', diff --git a/packages/devtools_app/lib/src/service/message_port_connection.dart b/packages/devtools_app/lib/src/service/message_port_connection.dart index cfa7e452ada..b3101023d93 100644 --- a/packages/devtools_app/lib/src/service/message_port_connection.dart +++ b/packages/devtools_app/lib/src/service/message_port_connection.dart @@ -30,3 +30,13 @@ const messagePortUriScheme = 'messageport'; /// Whether [uri] has the [messagePortUriScheme]. bool isMessagePortUri(Uri uri) => uri.isScheme(messagePortUriScheme); + +/// Parses [value] as a [messagePortUriScheme] URI, or returns `null` if [value] +/// does not use the [messagePortUriScheme] or has an empty target origin path. +Uri? parseMessagePortUri(String value) { + final uri = Uri.tryParse(value.trim())?.removeFragment(); + if (uri == null || !isMessagePortUri(uri) || uri.path.isEmpty) { + return null; + } + return uri; +} diff --git a/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md b/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md index 1aadb2c50a5..3556e18497f 100644 --- a/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md +++ b/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md @@ -33,6 +33,8 @@ such as black or white, are now adjusted to remain legible. and `Sec-Fetch-Site` headers to protect against cross-site request forgery (CSRF), DNS rebinding, and cross-origin attacks. [#10082](https://github.com/flutter/devtools/pull/10082) +* Restricted VM service connection URIs to allowed schemes and loopback hosts. + [#10091](https://github.com/flutter/devtools/pull/10091) ## Inspector updates diff --git a/packages/devtools_app/test/service/message_port_connection_test.dart b/packages/devtools_app/test/service/message_port_connection_test.dart index 843fcfc7ffc..97942e6d9de 100644 --- a/packages/devtools_app/test/service/message_port_connection_test.dart +++ b/packages/devtools_app/test/service/message_port_connection_test.dart @@ -26,16 +26,20 @@ void main() { expect(isMessagePortUri(Uri.parse('messageport')), isFalse); }); - test('survives normalizeVmServiceUri', () { - // `FrameworkCore.initVmService` normalizes the `uri` query parameter - // before checking the scheme, so the target origin must survive it. - for (final targetOrigin in ['https://example.com', '*']) { - final uri = normalizeVmServiceUri('messageport:$targetOrigin')!; - expect(isMessagePortUri(uri), isTrue); - expect(uri.path, targetOrigin); - expect(uri.toString(), 'messageport:$targetOrigin'); - } - }); + test( + 'parseMessagePortUri preserves target origin and rejects other URIs', + () { + for (final targetOrigin in ['https://example.com', '*']) { + expect(normalizeVmServiceUri('messageport:$targetOrigin'), isNull); + final uri = parseMessagePortUri(' messageport:$targetOrigin#/vm ')!; + expect(isMessagePortUri(uri), isTrue); + expect(uri.path, targetOrigin); + expect(uri.toString(), 'messageport:$targetOrigin'); + } + expect(parseMessagePortUri('messageport:'), isNull); + expect(parseMessagePortUri('ws://127.0.0.1:8181/ws'), isNull); + }, + ); }); test('connectWithMessagePort is not supported outside the web', () { diff --git a/packages/devtools_shared/CHANGELOG.md b/packages/devtools_shared/CHANGELOG.md index e8257206ff3..7ec18dbf67d 100644 --- a/packages/devtools_shared/CHANGELOG.md +++ b/packages/devtools_shared/CHANGELOG.md @@ -31,6 +31,8 @@ found in the LICENSE file or at https://developers.google.com/open-source/licens * Confine `devtoolsOptionsUri` in `api/extensionEnabledState` to currently served extensions, reject symlinked `devtools_options.yaml` files, and prevent recursive directory creation when looking up `devtools_options.yaml`. +* Restrict `normalizeVmServiceUri` to allowed VM service schemes (`http`, `https`, + `ws`, `wss`, `sse`, `sses`) and loopback hosts (`localhost`, `127.0.0.0/8`, `::1`). # 14.0.0 diff --git a/packages/devtools_shared/lib/src/service_utils.dart b/packages/devtools_shared/lib/src/service_utils.dart index 5abe740b0f2..83ddbc1e2af 100644 --- a/packages/devtools_shared/lib/src/service_utils.dart +++ b/packages/devtools_shared/lib/src/service_utils.dart @@ -24,6 +24,23 @@ const flutterListViews = '_flutter.listViews'; /// raster cache entries in bytes. 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`). +bool isLoopbackHost(String host) { + final normalizedHost = host.toLowerCase(); + return normalizedHost == 'localhost' || + normalizedHost == '::1' || + _ipv4LoopbackRegex.hasMatch(normalizedHost); +} + /// Returns a normalized vm service uri. /// /// Removes trailing characters, trailing url fragments, and decodes urls that @@ -32,8 +49,10 @@ const flutterEngineRasterCache = '_flutter.estimateRasterCacheMemory'; /// For example, given a [value] of http://127.0.0.1:60667/72K34Xmq0X0=/#/vm, /// this method will return the URI http://127.0.0.1:60667/72K34Xmq0X0=/. /// -/// Returns null if the [Uri] parsed from [value] is not [Uri.isAbsolute] -/// (i.e., it has no scheme or it has a fragment). +/// Returns `null` if the [Uri] parsed from [value] is not [Uri.isAbsolute] +/// (i.e., it has no scheme or it has a fragment), does not use an allowed +/// VM service scheme (`http`, `https`, `ws`, `wss`, `sse`, `sses`), or does +/// not target a loopback host (`localhost`, `127.x.x.x`, `::1`, or `[::1]`). Uri? normalizeVmServiceUri(String value) { value = value.trim(); @@ -49,10 +68,18 @@ Uri? normalizeVmServiceUri(String value) { // Cleanup encoded urls likely copied from the uri of an existing running // DevTools app. if (value.contains('%3A%2F%2F')) { - value = Uri.decodeFull(value); + try { + value = Uri.decodeFull(value); + } on ArgumentError { + return null; + } + } + final uri = Uri.tryParse(value.trim())?.removeFragment(); + if (uri == null || !uri.isAbsolute) { + return null; } - final uri = Uri.parse(value.trim()).removeFragment(); - if (!uri.isAbsolute) { + const allowedSchemes = _allowedVmServiceSchemes; + if (!allowedSchemes.contains(uri.scheme) || !isLoopbackHost(uri.host)) { return null; } if (uri.path.endsWith('/')) return uri; diff --git a/packages/devtools_shared/test/service_utils_test.dart b/packages/devtools_shared/test/service_utils_test.dart index 351a6fbd304..8222163f676 100644 --- a/packages/devtools_shared/test/service_utils_test.dart +++ b/packages/devtools_shared/test/service_utils_test.dart @@ -78,5 +78,50 @@ void main() { test('Returns null when given a non-absolute url', () { expect(normalizeVmServiceUri('my/page'), null); }); + + test('allows valid loopback URIs with allowed schemes', () { + expect( + normalizeVmServiceUri('http://127.0.0.1:8181').toString(), + equals('http://127.0.0.1:8181'), + ); + expect( + normalizeVmServiceUri('ws://localhost:8181/ws').toString(), + equals('ws://localhost:8181/ws'), + ); + expect( + normalizeVmServiceUri('http://[::1]:8181').toString(), + equals('http://[::1]:8181'), + ); + expect( + normalizeVmServiceUri('https://127.0.0.1:8181').toString(), + equals('https://127.0.0.1:8181'), + ); + expect( + normalizeVmServiceUri('sse://127.0.0.1:8181').toString(), + equals('sse://127.0.0.1:8181'), + ); + expect( + normalizeVmServiceUri('wss://localhost:8181/ws').toString(), + equals('wss://localhost:8181/ws'), + ); + expect( + normalizeVmServiceUri('sses://127.0.0.2:8181/sse').toString(), + equals('sses://127.0.0.2:8181/sse'), + ); + }); + + test('returns null for non-loopback hosts', () { + expect(normalizeVmServiceUri('ws://attacker.evil/fake'), isNull); + expect(normalizeVmServiceUri('http://192.168.1.1:8181'), isNull); + expect(normalizeVmServiceUri('http://10.0.0.1:8181'), isNull); + expect(normalizeVmServiceUri('http://127.256.0.1:8181'), isNull); + expect(normalizeVmServiceUri('http://[::2]:8181'), isNull); + }); + + test('returns null for disallowed schemes', () { + expect(normalizeVmServiceUri('file:///etc/passwd'), isNull); + expect(normalizeVmServiceUri('javascript:alert(1)'), isNull); + expect(normalizeVmServiceUri('ftp://127.0.0.1:8181/'), isNull); + }); }); }