Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion packages/devtools_app/lib/src/framework/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -274,7 +275,8 @@ class _ConnectInputState extends State<ConnectInput> 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.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down
2 changes: 2 additions & 0 deletions packages/devtools_shared/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 32 additions & 5 deletions packages/devtools_shared/lib/src/service_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions packages/devtools_shared/test/service_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
Loading