Skip to content

Commit 419defa

Browse files
committed
[flutter_tools] Validate legacy-format plugin identifiers in validate()
#189156 restricts pluginClass/dartPluginClass/package to identifiers in each platform's validate(), but the legacy plugin format is parsed by Plugin._fromLegacyYaml, which builds AndroidPlugin/IOSPlugin through their plain constructors and never reaches that validation. A legacy-format pubspec whose androidPackage, pluginClass or iosPrefix contains injection characters therefore still lands verbatim in the generated GeneratedPluginRegistrant and is compiled into any app that (transitively) depends on the plugin. Follow the pattern #189156 established: check the identifiers in _validateLegacyYaml alongside the existing type checks, so every invalid field is reported at once through Plugin.fromYaml's error list rather than exiting on the first one. This only requires making the existing identifier predicate visible outside platform_plugins.dart, matching isValidPluginDartFileName. Adds five tests covering each field's injection payload, the combined multiple-error output and an unchanged valid legacy declaration.
1 parent d4e7f9a commit 419defa

3 files changed

Lines changed: 155 additions & 20 deletions

File tree

‎packages/flutter_tools/lib/src/platform_plugins.dart‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final RegExp _pluginIdentifierPattern = RegExp(
4040
/// Whether [value] is a valid native plugin class or dot-separated package
4141
/// identifier. Callers first confirm the value is a String via the schema type
4242
/// checks; absent fields are not validated here.
43-
bool _isValidPluginIdentifier(String value) => _pluginIdentifierPattern.hasMatch(value);
43+
bool isValidPluginIdentifier(String value) => _pluginIdentifierPattern.hasMatch(value);
4444

4545
/// Matches a safe relative Dart source path (e.g. `src/foo_web.dart`) ending in
4646
/// `.dart`. Plugin `fileName`/`dartFileName` values are interpolated into an
@@ -178,13 +178,13 @@ class AndroidPlugin extends PluginPlatform implements NativeOrDartPlugin {
178178
// Validate every identifier that is present, not just the ones that made
179179
// the declaration above valid, so a plugin cannot smuggle an unsafe
180180
// identifier in alongside an `ffiPlugin` or `default_package` entry.
181-
if (package is String && !_isValidPluginIdentifier(package)) {
181+
if (package is String && !isValidPluginIdentifier(package)) {
182182
return false;
183183
}
184-
if (pluginClass is String && !_isValidPluginIdentifier(pluginClass)) {
184+
if (pluginClass is String && !isValidPluginIdentifier(pluginClass)) {
185185
return false;
186186
}
187-
if (dartPluginClass is String && !_isValidPluginIdentifier(dartPluginClass)) {
187+
if (dartPluginClass is String && !isValidPluginIdentifier(dartPluginClass)) {
188188
return false;
189189
}
190190

@@ -358,10 +358,10 @@ class IOSPlugin extends PluginPlatform implements NativeOrDartPlugin, DarwinPlug
358358

359359
// Validate every identifier that is present, not just the ones that made
360360
// the declaration above valid.
361-
if (pluginClass is String && !_isValidPluginIdentifier(pluginClass)) {
361+
if (pluginClass is String && !isValidPluginIdentifier(pluginClass)) {
362362
return false;
363363
}
364-
if (dartPluginClass is String && !_isValidPluginIdentifier(dartPluginClass)) {
364+
if (dartPluginClass is String && !isValidPluginIdentifier(dartPluginClass)) {
365365
return false;
366366
}
367367

@@ -470,10 +470,10 @@ class MacOSPlugin extends PluginPlatform implements NativeOrDartPlugin, DarwinPl
470470

471471
// Validate every identifier that is present, not just the ones that made
472472
// the declaration above valid.
473-
if (pluginClass is String && !_isValidPluginIdentifier(pluginClass)) {
473+
if (pluginClass is String && !isValidPluginIdentifier(pluginClass)) {
474474
return false;
475475
}
476-
if (dartPluginClass is String && !_isValidPluginIdentifier(dartPluginClass)) {
476+
if (dartPluginClass is String && !isValidPluginIdentifier(dartPluginClass)) {
477477
return false;
478478
}
479479

@@ -595,10 +595,10 @@ class WindowsPlugin extends PluginPlatform implements NativeOrDartPlugin, Varian
595595

596596
// Validate every identifier that is present, not just the ones that made
597597
// the declaration above valid.
598-
if (pluginClass is String && !_isValidPluginIdentifier(pluginClass)) {
598+
if (pluginClass is String && !isValidPluginIdentifier(pluginClass)) {
599599
return false;
600600
}
601-
if (dartPluginClass is String && !_isValidPluginIdentifier(dartPluginClass)) {
601+
if (dartPluginClass is String && !isValidPluginIdentifier(dartPluginClass)) {
602602
return false;
603603
}
604604

@@ -701,10 +701,10 @@ class LinuxPlugin extends PluginPlatform implements NativeOrDartPlugin {
701701

702702
// Validate every identifier that is present, not just the ones that made
703703
// the declaration above valid.
704-
if (pluginClass is String && !_isValidPluginIdentifier(pluginClass)) {
704+
if (pluginClass is String && !isValidPluginIdentifier(pluginClass)) {
705705
return false;
706706
}
707-
if (dartPluginClass is String && !_isValidPluginIdentifier(dartPluginClass)) {
707+
if (dartPluginClass is String && !isValidPluginIdentifier(dartPluginClass)) {
708708
return false;
709709
}
710710

@@ -762,7 +762,7 @@ class WebPlugin extends PluginPlatform {
762762
if (fileName is! String) {
763763
throwToolExit('The plugin `$name` is missing the required field `fileName` in pubspec.yaml');
764764
}
765-
if (!_isValidPluginIdentifier(pluginClass)) {
765+
if (!isValidPluginIdentifier(pluginClass)) {
766766
throwToolExit(
767767
'The plugin `$name` has an invalid `pluginClass` in its web plugin declaration.',
768768
);

‎packages/flutter_tools/lib/src/plugins.dart‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class Plugin {
215215
bool isDirectDependency, {
216216
required bool isDevDependency,
217217
}) {
218+
// SAFETY: This constructor is only invoked from .fromYaml, which validates.
218219
final platforms = <String, PluginPlatform>{};
219220
final pluginClass = (pluginYaml as Map<dynamic, dynamic>)['pluginClass'] as String?;
220221
if (pluginClass != null) {
@@ -349,11 +350,31 @@ class Plugin {
349350
}
350351

351352
static List<String> _validateLegacyYaml(YamlMap yaml) {
353+
final Object? androidPackage = yaml['androidPackage'];
354+
final Object? iosPrefix = yaml['iosPrefix'];
355+
final Object? pluginClass = yaml['pluginClass'];
356+
357+
// As in the `platforms:` format, every identifier is checked here: they are
358+
// interpolated verbatim into the generated GeneratedPluginRegistrant source
359+
// files, so anything that is not a plain identifier would let a (possibly
360+
// transitive) dependency inject native code into the consuming app's build.
361+
const identifierRequirement =
362+
'must be a valid identifier, optionally with dot-separated segments';
363+
352364
return <String>[
353-
if (yaml['androidPackage'] is! String?)
354-
'The "androidPackage" must either be null or a string.',
355-
if (yaml['iosPrefix'] is! String?) 'The "iosPrefix" must either be null or a string.',
356-
if (yaml['pluginClass'] is! String?) 'The "pluginClass" must either be null or a string.',
365+
if (androidPackage is! String?)
366+
'The "androidPackage" must either be null or a string.'
367+
else if (androidPackage != null && !isValidPluginIdentifier(androidPackage))
368+
'The "androidPackage" $identifierRequirement.',
369+
if (iosPrefix is! String?)
370+
'The "iosPrefix" must either be null or a string.'
371+
// An empty prefix is the default for a legacy plugin that does not set one.
372+
else if (iosPrefix != null && iosPrefix.isNotEmpty && !isValidPluginIdentifier(iosPrefix))
373+
'The "iosPrefix" $identifierRequirement.',
374+
if (pluginClass is! String?)
375+
'The "pluginClass" must either be null or a string.'
376+
else if (pluginClass != null && !isValidPluginIdentifier(pluginClass))
377+
'The "pluginClass" $identifierRequirement.',
357378
];
358379
}
359380

‎packages/flutter_tools/test/general.shard/plugins_test.dart‎

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,9 @@ dependencies:
492492
'bad_plugin',
493493
]);
494494
// Write bytes that are not valid UTF-8, so readAsString throws a FileSystemException.
495-
pluginDirs[1].childFile('pubspec.yaml').writeAsBytesSync(
496-
Uint8List.fromList(<int>[0xff, 0xfe, 0xfd]),
497-
);
495+
pluginDirs[1]
496+
.childFile('pubspec.yaml')
497+
.writeAsBytesSync(Uint8List.fromList(<int>[0xff, 0xfe, 0xfd]));
498498

499499
// The tool must not crash when a plugin's pubspec.yaml cannot be read.
500500
final Future<List<Plugin>> pluginsFuture = findPlugins(flutterProject);
@@ -2095,6 +2095,120 @@ platforms:
20952095
},
20962096
);
20972097

2098+
testUsingContext(
2099+
'Plugin.fromYaml rejects legacy-format identifiers with code-injection characters',
2100+
() async {
2101+
// The legacy plugin format is parsed by Plugin._fromLegacyYaml, which
2102+
// builds AndroidPlugin/IOSPlugin through their plain constructors and
2103+
// so never reaches the platform `fromYaml` validation. Its fields are
2104+
// interpolated into the generated registrant just the same.
2105+
const maliciousYaml = '''
2106+
androidPackage: com.example.evil
2107+
pluginClass: "Evil(); evilInjectedCall(); //"
2108+
''';
2109+
expect(
2110+
() => Plugin.fromYaml(
2111+
'evil_legacy_plugin',
2112+
'',
2113+
loadYaml(maliciousYaml) as YamlMap,
2114+
null,
2115+
const <String>[],
2116+
fileSystem: globals.fs,
2117+
isDevDependency: false,
2118+
),
2119+
throwsToolExit(message: 'The "pluginClass" must be a valid identifier'),
2120+
);
2121+
},
2122+
);
2123+
2124+
testUsingContext(
2125+
'Plugin.fromYaml rejects a legacy-format androidPackage injection',
2126+
() async {
2127+
const maliciousYaml = '''
2128+
androidPackage: "com.example.evil.Payload.run(); //"
2129+
pluginClass: EvilPlugin
2130+
''';
2131+
expect(
2132+
() => Plugin.fromYaml(
2133+
'evil_legacy_plugin',
2134+
'',
2135+
loadYaml(maliciousYaml) as YamlMap,
2136+
null,
2137+
const <String>[],
2138+
fileSystem: globals.fs,
2139+
isDevDependency: false,
2140+
),
2141+
throwsToolExit(message: 'The "androidPackage" must be a valid identifier'),
2142+
);
2143+
},
2144+
);
2145+
2146+
testUsingContext('Plugin.fromYaml rejects a legacy-format iosPrefix injection', () async {
2147+
const maliciousYaml = '''
2148+
androidPackage: com.example.evil
2149+
pluginClass: EvilPlugin
2150+
iosPrefix: "FLT; evilInjectedCall(); //"
2151+
''';
2152+
expect(
2153+
() => Plugin.fromYaml(
2154+
'evil_legacy_plugin',
2155+
'',
2156+
loadYaml(maliciousYaml) as YamlMap,
2157+
null,
2158+
const <String>[],
2159+
fileSystem: globals.fs,
2160+
isDevDependency: false,
2161+
),
2162+
throwsToolExit(message: 'The "iosPrefix" must be a valid identifier'),
2163+
);
2164+
});
2165+
2166+
testUsingContext('Plugin.fromYaml reports every invalid legacy-format field at once', () async {
2167+
const maliciousYaml = '''
2168+
androidPackage: "com.example.evil.Payload.run(); //"
2169+
pluginClass: "Evil(); evilInjectedCall(); //"
2170+
iosPrefix: "FLT; evilInjectedCall(); //"
2171+
''';
2172+
expect(
2173+
() => Plugin.fromYaml(
2174+
'evil_legacy_plugin',
2175+
'',
2176+
loadYaml(maliciousYaml) as YamlMap,
2177+
null,
2178+
const <String>[],
2179+
fileSystem: globals.fs,
2180+
isDevDependency: false,
2181+
),
2182+
throwsToolExit(
2183+
message:
2184+
'Invalid plugin specification evil_legacy_plugin.\n'
2185+
'The "androidPackage" must be a valid identifier, optionally with dot-separated segments.\n'
2186+
'The "iosPrefix" must be a valid identifier, optionally with dot-separated segments.\n'
2187+
'The "pluginClass" must be a valid identifier, optionally with dot-separated segments.',
2188+
),
2189+
);
2190+
});
2191+
2192+
testUsingContext('Plugin.fromYaml accepts a legacy-format plugin declaration', () async {
2193+
const legacyYaml = '''
2194+
androidPackage: com.example.sample
2195+
pluginClass: SamplePlugin
2196+
iosPrefix: FLT
2197+
''';
2198+
final plugin = Plugin.fromYaml(
2199+
'sample_plugin',
2200+
'',
2201+
loadYaml(legacyYaml) as YamlMap,
2202+
null,
2203+
const <String>[],
2204+
fileSystem: globals.fs,
2205+
isDevDependency: false,
2206+
);
2207+
2208+
expect(plugin.platforms, contains(AndroidPlugin.kConfigKey));
2209+
expect(plugin.platforms, contains(IOSPlugin.kConfigKey));
2210+
});
2211+
20982212
testUsingContext('createPlatformsYamlMap should create the correct map', () async {
20992213
final YamlMap map = Plugin.createPlatformsYamlMap(
21002214
<String>['ios', 'android', 'linux'],

0 commit comments

Comments
 (0)