Fix vulnerability in identifier for extension configuration (#10090) * PR https://github.com/flutter/devtools/pull/9981 added the `identifier` getter to `DevToolsExtensionConfig`, which built it based on the `packageName`, `name`, and `version`, separated by underscores. This makes it vulnerable if package names contain underscores. ``` String get identifier => packageName == displayName ? '${displayName}_$version' : '${packageName}_${displayName}_$version'; ``` If a legitimate extension belongs to package device_preview (with name: device_preview and version: 1.0.0), its `packageName == displayName` branch produces: `device_preview_1.0.0` An attacker can publish a package named device and set `name: preview`and `version: 1.0.0`in its config.yaml, and because `packageName != displayName`, it takes the second branch (`${packageName}_${displayName}_$version`), which also produces: device_preview_1.0.0. This change uses a hyphen between `packageName` and `displayName` and validates that the name and version are valid. * Address code review comments * Update NEXT_RELEASE_NOTES.md * release notes formatting
diff --git a/packages/devtools_app/lib/src/extensions/extension_service_helpers.dart b/packages/devtools_app/lib/src/extensions/extension_service_helpers.dart index 3133ac7..529b1dd 100644 --- a/packages/devtools_app/lib/src/extensions/extension_service_helpers.dart +++ b/packages/devtools_app/lib/src/extensions/extension_service_helpers.dart
@@ -62,29 +62,8 @@ DevToolsExtensionConfig a, DevToolsExtensionConfig b, ) { - bool exceptionParsingA = false; - bool exceptionParsingB = false; - SemanticVersion? versionA; - SemanticVersion? versionB; - try { - versionA = SemanticVersion.parse(a.version); - } catch (_) { - exceptionParsingA = true; - } - - try { - versionB = SemanticVersion.parse(b.version); - } catch (_) { - exceptionParsingB = true; - } - - if (exceptionParsingA || exceptionParsingB) { - if (exceptionParsingA) { - return b; - } - return a; - } - - final versionCompare = versionA!.compareTo(versionB!); + final versionA = SemanticVersion.parse(a.version); + final versionB = SemanticVersion.parse(b.version); + final versionCompare = versionA.compareTo(versionB); return versionCompare >= 0 ? a : b; }
diff --git a/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md b/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md index 3556e18..1dfdcb6 100644 --- a/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md +++ b/packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
@@ -111,6 +111,9 @@ extensions, rejected symlinked `devtools_options.yaml` files, and prevented creating missing parent directories. [#10088](https://github.com/flutter/devtools/pull/10088) +* Validated DevTools extension version strings and prevented extension + identifier collisions when serving extension assets. + [#10090](https://github.com/flutter/devtools/pull/10090) ## Advanced developer mode updates
diff --git a/packages/devtools_app/test/extensions/extension_service_helpers_test.dart b/packages/devtools_app/test/extensions/extension_service_helpers_test.dart index fbf0129..3c0f415 100644 --- a/packages/devtools_app/test/extensions/extension_service_helpers_test.dart +++ b/packages/devtools_app/test/extensions/extension_service_helpers_test.dart
@@ -18,94 +18,6 @@ StubDevToolsExtensions.newerBarExtension, ); }); - - test('handles parsing errors', () { - // Returns 'b' when 'a' has parsing errors. - var a = DevToolsExtensionConfig.parse({ - DevToolsExtensionConfig.nameKey: 'bar', - DevToolsExtensionConfig.packageNameKey: 'bar', - DevToolsExtensionConfig.issueTrackerKey: 'www.google.com', - DevToolsExtensionConfig.versionKey: 'this-will-not-parse', - DevToolsExtensionConfig.materialIconCodePointKey: 0xe638, - DevToolsExtensionConfig.requiresConnectionKey: 'false', - DevToolsExtensionConfig.extensionAssetsPathKey: '/absolute/path/to/bar', - DevToolsExtensionConfig.devtoolsOptionsUriKey: - 'file:///path/to/options/file', - DevToolsExtensionConfig.isPubliclyHostedKey: 'false', - DevToolsExtensionConfig.detectedFromStaticContextKey: 'true', - }); - var b = DevToolsExtensionConfig.parse({ - DevToolsExtensionConfig.nameKey: 'bar', - DevToolsExtensionConfig.packageNameKey: 'bar', - DevToolsExtensionConfig.issueTrackerKey: 'www.google.com', - DevToolsExtensionConfig.versionKey: '2.1.0', - DevToolsExtensionConfig.materialIconCodePointKey: 0xe638, - DevToolsExtensionConfig.requiresConnectionKey: 'false', - DevToolsExtensionConfig.extensionAssetsPathKey: '/absolute/path/to/bar', - DevToolsExtensionConfig.devtoolsOptionsUriKey: - 'file:///path/to/options/file', - DevToolsExtensionConfig.isPubliclyHostedKey: 'false', - DevToolsExtensionConfig.detectedFromStaticContextKey: 'true', - }); - expect(takeLatestExtension(a, b), b); - - // Returns 'a' when 'b' has parsing errors. - a = DevToolsExtensionConfig.parse({ - DevToolsExtensionConfig.nameKey: 'bar', - DevToolsExtensionConfig.packageNameKey: 'bar', - DevToolsExtensionConfig.issueTrackerKey: 'www.google.com', - DevToolsExtensionConfig.versionKey: '2.1.0', - DevToolsExtensionConfig.materialIconCodePointKey: 0xe638, - DevToolsExtensionConfig.requiresConnectionKey: 'false', - DevToolsExtensionConfig.extensionAssetsPathKey: '/absolute/path/to/bar', - DevToolsExtensionConfig.devtoolsOptionsUriKey: - 'file:///path/to/options/file', - DevToolsExtensionConfig.isPubliclyHostedKey: 'false', - DevToolsExtensionConfig.detectedFromStaticContextKey: 'true', - }); - b = DevToolsExtensionConfig.parse({ - DevToolsExtensionConfig.nameKey: 'bar', - DevToolsExtensionConfig.packageNameKey: 'bar', - DevToolsExtensionConfig.issueTrackerKey: 'www.google.com', - DevToolsExtensionConfig.versionKey: 'this-will-not-parse', - DevToolsExtensionConfig.materialIconCodePointKey: 0xe638, - DevToolsExtensionConfig.requiresConnectionKey: 'false', - DevToolsExtensionConfig.extensionAssetsPathKey: '/path/to/bar', - DevToolsExtensionConfig.devtoolsOptionsUriKey: '/path/to/options/file', - DevToolsExtensionConfig.isPubliclyHostedKey: 'false', - DevToolsExtensionConfig.detectedFromStaticContextKey: 'true', - }); - expect(takeLatestExtension(a, b), a); - - // Returns 'a' when both 'a' and 'b' have parsing errors. - a = DevToolsExtensionConfig.parse({ - DevToolsExtensionConfig.nameKey: 'bar', - DevToolsExtensionConfig.packageNameKey: 'bar', - DevToolsExtensionConfig.issueTrackerKey: 'www.google.com', - DevToolsExtensionConfig.versionKey: 'this-will-not-parse', - DevToolsExtensionConfig.materialIconCodePointKey: 0xe638, - DevToolsExtensionConfig.requiresConnectionKey: 'false', - DevToolsExtensionConfig.extensionAssetsPathKey: '/absolute/path/to/bar', - DevToolsExtensionConfig.devtoolsOptionsUriKey: - 'file:///path/to/options/file', - DevToolsExtensionConfig.isPubliclyHostedKey: 'false', - DevToolsExtensionConfig.detectedFromStaticContextKey: 'true', - }); - b = DevToolsExtensionConfig.parse({ - DevToolsExtensionConfig.nameKey: 'bar', - DevToolsExtensionConfig.packageNameKey: 'bar', - DevToolsExtensionConfig.issueTrackerKey: 'www.google.com', - DevToolsExtensionConfig.versionKey: 'this-will-not-parse', - DevToolsExtensionConfig.materialIconCodePointKey: 0xe638, - DevToolsExtensionConfig.requiresConnectionKey: 'false', - DevToolsExtensionConfig.extensionAssetsPathKey: '/absolute/path/to/bar', - DevToolsExtensionConfig.devtoolsOptionsUriKey: - 'file:///path/to/options/file', - DevToolsExtensionConfig.isPubliclyHostedKey: 'false', - DevToolsExtensionConfig.detectedFromStaticContextKey: 'true', - }); - expect(takeLatestExtension(a, b), a); - }); }); group('deduplicateExtensionsAndTakeLatest', () {
diff --git a/packages/devtools_extensions/bin/_validate.dart b/packages/devtools_extensions/bin/_validate.dart index 30f36cc..72707c5 100644 --- a/packages/devtools_extensions/bin/_validate.dart +++ b/packages/devtools_extensions/bin/_validate.dart
@@ -45,7 +45,7 @@ try { // TODO(kenz): try to use the the existing pub validator for this check. See // https://github.com/dart-lang/pub/blob/master/lib/src/validator/devtools_extension.dart. - _validateDirectoryContents(packagePath); + final packageName = _validateDirectoryContents(packagePath); // Try to parse the config.yaml file. This will throw an exception if there // are parsing errors. @@ -53,7 +53,7 @@ ..._configAsMap(packagePath), // These are generated on the DevTools server, so pass in stubbed // values for the sake of validation. - DevToolsExtensionConfig.packageNameKey: '', + DevToolsExtensionConfig.packageNameKey: packageName, DevToolsExtensionConfig.extensionAssetsPathKey: '', DevToolsExtensionConfig.devtoolsOptionsUriKey: '', DevToolsExtensionConfig.isPubliclyHostedKey: 'false', @@ -63,6 +63,8 @@ // If there are no exceptions at this point, the extension has successfully // been validated. stdout.writeln('Extension validation successful'); + } on ExtensionValidationException catch (e) { + _logError(e.message); } on StateError catch (e) { _logError(e.message); } on FileSystemException catch (e) { @@ -73,7 +75,18 @@ } } -void _validateDirectoryContents(String packagePath) { +/// Exception thrown when validating a DevTools extension fails. +class ExtensionValidationException implements Exception { + const ExtensionValidationException(this.message); + + /// The message describing why extension validation failed. + final String message; + + @override + String toString() => message; +} + +String _validateDirectoryContents(String packagePath) { final packageDirectory = Directory(packagePath); if (!packageDirectory.existsSync()) { throw FileSystemException('${packageDirectory.path} directory not found'); @@ -119,16 +132,15 @@ '''); } - // Ensure the extension's name is a valid identifier. - final configYaml = _configAsMap(packagePath); - final configName = configYaml['name']; - final underscoresAndLetters = RegExp(r'^[a-z0-9_]*$'); - if (configName is! String || !underscoresAndLetters.hasMatch(configName)) { - throw StateError( - 'The "name" field in config.yaml should only contain lowercase letters, ' - 'numbers, and underscores but instead was "$configName".', + // Ensure the pubspec.yaml contains a valid package name. + final pubspecYaml = loadYaml(pubspecFile.readAsStringSync()); + final packageName = pubspecYaml is YamlMap ? pubspecYaml['name'] : null; + if (packageName is! String || packageName.isEmpty) { + throw const ExtensionValidationException( + 'The pubspec.yaml file must contain a valid "name" field.', ); } + return packageName; } Map<String, Object?> _configAsMap(String packagePath) {
diff --git a/packages/devtools_extensions/test/validate_test.dart b/packages/devtools_extensions/test/validate_test.dart index 3616638..996636c 100644 --- a/packages/devtools_extensions/test/validate_test.dart +++ b/packages/devtools_extensions/test/validate_test.dart
@@ -84,8 +84,8 @@ expect( process.stderr, contains( - 'Validation error: The "name" field in config.yaml should only ' - 'contain lowercase letters, numbers, and underscores', + 'Validation error: The "name" field in the extension config.yaml ' + 'should only contain lowercase letters, numbers, and underscores', ), ); } finally {
diff --git a/packages/devtools_shared/CHANGELOG.md b/packages/devtools_shared/CHANGELOG.md index 7ec18db..50bc259 100644 --- a/packages/devtools_shared/CHANGELOG.md +++ b/packages/devtools_shared/CHANGELOG.md
@@ -7,6 +7,8 @@ * Track providing package name for DevTools extensions to isolate extension enablement, deduplication, and asset loading. +* Validated `DevToolsExtensionConfig.version` as a semantic version and used a collision-free + delimiter in `DevToolsExtensionConfig.identifier`. * Validate `rootPath`, `buildVariant`, `configuration`, and `target` parameters in `DeeplinkManager`. * Add `UniversalLinkSettings.fromErrorJson`, `UniversalLinkSettings.error`, and
diff --git a/packages/devtools_shared/lib/src/extensions/extension_model.dart b/packages/devtools_shared/lib/src/extensions/extension_model.dart index 1f14661..470a0e0 100644 --- a/packages/devtools_shared/lib/src/extensions/extension_model.dart +++ b/packages/devtools_shared/lib/src/extensions/extension_model.dart
@@ -51,8 +51,7 @@ // Note that the field [requiresConnectionKey] is not required for // this check because it is optional. }) { - final underscoresAndLetters = RegExp(r'^[a-z0-9_]*$'); - if (!underscoresAndLetters.hasMatch(name)) { + if (!_validPackageNameRegex.hasMatch(name)) { throw StateError( 'The "name" field in the extension config.yaml should only contain ' 'lowercase letters, numbers, and underscores but instead was ' @@ -60,6 +59,19 @@ 'package name this extension belongs to.', ); } + if (packageName.isNotEmpty && + !_validPackageNameRegex.hasMatch(packageName)) { + throw StateError( + 'The "packageName" field should only contain lowercase letters, ' + 'numbers, and underscores but instead was "$packageName".', + ); + } + if (!_validVersionRegex.hasMatch(version)) { + throw StateError( + 'The "version" field in the extension config.yaml must be a valid ' + 'semantic version (e.g. "1.0.0"), but instead was "$version".', + ); + } // Defaults to the code point for [Icons.extensions_outlined] if parsing // fails. @@ -114,6 +126,16 @@ } } + /// Regular expression matching valid Dart package names (lowercase letters, + /// digits, and underscores). + static final _validPackageNameRegex = RegExp(r'^[a-z0-9_]+$'); + + /// Regular expression matching valid semantic version strings (disallowing + /// underscores, slashes, and whitespace). + static final _validVersionRegex = RegExp( + r'^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$', + ); + // The following keys are required in the extension's config.yaml file. static const nameKey = 'name'; static const issueTrackerKey = 'issueTracker'; @@ -241,9 +263,16 @@ String get displayName => name.toLowerCase(); - String get identifier => packageName == displayName + /// The unique identifier for this extension configuration, used to look up + /// and serve extension assets. + /// + /// When [packageName] is non-empty and differs from [displayName], a hyphen + /// (`-`) separator is used between [packageName] and [displayName]. Because + /// neither [packageName] nor [displayName] may contain `-`, and [version] + /// may not contain `_`, this identifier is guaranteed to be collision-free. + String get identifier => packageName.isEmpty || packageName == displayName ? '${displayName}_$version' - : '${packageName}_${displayName}_$version'; + : '$packageName-${displayName}_$version'; String get analyticsSafeName => isPubliclyHosted ? name : 'private';
diff --git a/packages/devtools_shared/test/extensions/extension_model_test.dart b/packages/devtools_shared/test/extensions/extension_model_test.dart index 3a98e04..228904e 100644 --- a/packages/devtools_shared/test/extensions/extension_model_test.dart +++ b/packages/devtools_shared/test/extensions/extension_model_test.dart
@@ -126,7 +126,36 @@ 'detectedFromStaticContext': 'false', }); expect(configWithDistinctPackage.packageName, 'foo'); - expect(configWithDistinctPackage.identifier, 'foo_bar_1.0.0'); + expect(configWithDistinctPackage.identifier, 'foo-bar_1.0.0'); + + final devicePreviewConfig = DevToolsExtensionConfig.parse({ + 'name': 'device_preview', + 'packageName': 'device_preview', + 'issueTracker': 'www.google.com', + 'version': '1.0.0', + 'materialIconCodePoint': 0xf012, + 'extensionAssetsPath': '/absolute/path/to/device_preview/extension', + 'devtoolsOptionsUri': 'file:///path/to/package/devtools_options.yaml', + 'isPubliclyHosted': 'false', + 'detectedFromStaticContext': 'false', + }); + final spoofedDeviceConfig = DevToolsExtensionConfig.parse({ + 'name': 'preview', + 'packageName': 'device', + 'issueTracker': 'www.google.com', + 'version': '1.0.0', + 'materialIconCodePoint': 0xf012, + 'extensionAssetsPath': '/absolute/path/to/device/extension', + 'devtoolsOptionsUri': 'file:///path/to/package/devtools_options.yaml', + 'isPubliclyHosted': 'false', + 'detectedFromStaticContext': 'false', + }); + expect(devicePreviewConfig.identifier, 'device_preview_1.0.0'); + expect(spoofedDeviceConfig.identifier, 'device-preview_1.0.0'); + expect( + spoofedDeviceConfig.identifier, + isNot(equals(devicePreviewConfig.identifier)), + ); }); group('parse throws when missing required field', () { @@ -388,5 +417,69 @@ }); }, throwsInvalidNameError()); }); + + test('parse throws for invalid version', () { + Matcher throwsInvalidVersionError() { + return throwsA( + isA<StateError>().having( + (e) => e.message, + 'invalid version StateError', + startsWith( + 'The "version" field in the extension config.yaml must be a ' + 'valid semantic version', + ), + ), + ); + } + + for (final invalidVersion in [ + 'bar_2.0.0', + '1.0.0_1', + '1.0', + 'invalid', + '1.0.0/../../etc', + '1.0.0 beta', + '', + ]) { + expect( + () { + DevToolsExtensionConfig.parse({ + 'name': 'foo', + 'packageName': 'foo', + 'issueTracker': 'www.google.com', + 'version': invalidVersion, + 'materialIconCodePoint': 0xf012, + 'extensionAssetsPath': '/absolute/path/to/foo/extension', + 'devtoolsOptionsUri': + 'file:///path/to/package/devtools_options.yaml', + 'isPubliclyHosted': 'false', + 'detectedFromStaticContext': 'false', + }); + }, + throwsInvalidVersionError(), + reason: 'Expected version "$invalidVersion" to be rejected', + ); + } + + for (final validVersion in [ + '1.0.0', + '0.0.1-dev.1', + '2.1.0+build.42', + '1.0.0-alpha.1+exp.sha.5114f85', + ]) { + final config = DevToolsExtensionConfig.parse({ + 'name': 'foo', + 'packageName': 'foo', + 'issueTracker': 'www.google.com', + 'version': validVersion, + 'materialIconCodePoint': 0xf012, + 'extensionAssetsPath': '/absolute/path/to/foo/extension', + 'devtoolsOptionsUri': 'file:///path/to/package/devtools_options.yaml', + 'isPubliclyHosted': 'false', + 'detectedFromStaticContext': 'false', + }); + expect(config.version, validVersion); + } + }); }); }
diff --git a/packages/devtools_shared/test/server/devtools_extensions_api_test.dart b/packages/devtools_shared/test/server/devtools_extensions_api_test.dart index ec672bf..044d886 100644 --- a/packages/devtools_shared/test/server/devtools_extensions_api_test.dart +++ b/packages/devtools_shared/test/server/devtools_extensions_api_test.dart
@@ -137,7 +137,7 @@ expect(spoofedExtension, isNotNull); expect(spoofedExtension!.name, 'provider'); expect(spoofedExtension.version, '999.0.0'); - expect(spoofedExtension.identifier, 'bad_pkg_provider_999.0.0'); + expect(spoofedExtension.identifier, 'bad_pkg-provider_999.0.0'); // Verify lookupLocationFor returns the respective paths without collision. expect(