Skip to content

Commit d98ace8

Browse files
committed
Validate legacy-format plugin identifiers, allow hyphens in dartFileName
Plugin._fromLegacyYaml built AndroidPlugin/IOSPlugin directly through their plain constructors instead of their validating .fromYaml factories, so a plugin using the legacy pubspec.yaml format (androidPackage/pluginClass/iosPrefix directly under flutter.plugin, rather than flutter.plugin.platforms) could still smuggle an injection payload into androidPackage, pluginClass, or iosPrefix and have it compiled unescaped into GeneratedPluginRegistrant, same as the multi-platform format this PR already covered. Also allow hyphens in dartFileName: technically legal in a Dart source file name even though discouraged by the style guide.
1 parent a29df52 commit d98ace8

4 files changed

Lines changed: 102 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ const kSupportedVariants = 'supportedVariants';
3535
final RegExp _identifierSegment = RegExp(r'^[A-Za-z_][A-Za-z0-9_]*$');
3636

3737
/// Matches a safe relative Dart file path, as required for [kDartFileName].
38-
final RegExp _dartFileNamePattern = RegExp(r'^[A-Za-z0-9_]+(/[A-Za-z0-9_]+)*\.dart$');
38+
///
39+
/// Hyphens are discouraged by the Dart style guide but are still legal in
40+
/// a Dart source file name, so they're allowed here too.
41+
final RegExp _dartFileNamePattern = RegExp(r'^[A-Za-z0-9_-]+(/[A-Za-z0-9_-]+)*\.dart$');
3942

4043
/// Validates that [value] (an Android `package`, or a `pluginClass`/
4144
/// `dartPluginClass` for any platform) is safe to interpolate, unescaped,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ class Plugin {
217217
}) {
218218
final platforms = <String, PluginPlatform>{};
219219
final pluginClass = (pluginYaml as Map<dynamic, dynamic>)['pluginClass'] as String?;
220+
validatePluginIdentifier(name, kPluginClass, pluginClass);
220221
if (pluginClass != null) {
221222
final androidPackage = pluginYaml['androidPackage'] as String?;
223+
validatePluginIdentifier(name, 'androidPackage', androidPackage);
222224
if (androidPackage != null) {
223225
platforms[AndroidPlugin.kConfigKey] = AndroidPlugin(
224226
name: name,
@@ -230,6 +232,7 @@ class Plugin {
230232
}
231233

232234
final String iosPrefix = pluginYaml['iosPrefix'] as String? ?? '';
235+
validatePluginIdentifier(name, 'iosPrefix', iosPrefix);
233236
platforms[IOSPlugin.kConfigKey] = IOSPlugin(
234237
name: name,
235238
classPrefix: iosPrefix,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ void main() {
9595
);
9696
expect(plugin.dartFileName, 'src/android_class.dart');
9797
});
98+
99+
testWithoutContext('accepts a dartFileName containing hyphens', () {
100+
final plugin = AndroidPlugin.fromYaml(
101+
'plugin1',
102+
_yaml('dartPluginClass: Plugin1\ndartFileName: src/android-class.dart'),
103+
'.pub_cache/plugin1',
104+
fileSystem,
105+
);
106+
expect(plugin.dartFileName, 'src/android-class.dart');
107+
});
98108
});
99109

100110
testWithoutContext(

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

Lines changed: 85 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);
@@ -513,6 +513,88 @@ dependencies:
513513
},
514514
);
515515

516+
testUsingContext(
517+
'rejects a legacy-format plugin with an unsafe pluginClass',
518+
() async {
519+
final Directory pluginDirectory = fs.systemTempDirectory.createTempSync(
520+
'flutter_plugin_legacy_injection.',
521+
);
522+
pluginDirectory.childFile('pubspec.yaml').writeAsStringSync('''
523+
name: evil_plugin
524+
flutter:
525+
plugin:
526+
androidPackage: com.example.evil
527+
pluginClass: "Evil(); some.malicious.Payload.run(); //"
528+
''');
529+
addToPackageConfig('evil_plugin', pluginDirectory);
530+
531+
await expectLater(
532+
() => findPlugins(flutterProject),
533+
throwsToolExit(message: 'invalid "pluginClass"'),
534+
);
535+
},
536+
overrides: <Type, Generator>{
537+
FileSystem: () => fs,
538+
ProcessManager: () => FakeProcessManager.any(),
539+
Pub: ThrowingPub.new,
540+
},
541+
);
542+
543+
testUsingContext(
544+
'rejects a legacy-format plugin with an unsafe androidPackage',
545+
() async {
546+
final Directory pluginDirectory = fs.systemTempDirectory.createTempSync(
547+
'flutter_plugin_legacy_injection.',
548+
);
549+
pluginDirectory.childFile('pubspec.yaml').writeAsStringSync('''
550+
name: evil_plugin
551+
flutter:
552+
plugin:
553+
androidPackage: "com.example.evil.Payload.run(); //"
554+
pluginClass: EvilPlugin
555+
''');
556+
addToPackageConfig('evil_plugin', pluginDirectory);
557+
558+
await expectLater(
559+
() => findPlugins(flutterProject),
560+
throwsToolExit(message: 'invalid "androidPackage"'),
561+
);
562+
},
563+
overrides: <Type, Generator>{
564+
FileSystem: () => fs,
565+
ProcessManager: () => FakeProcessManager.any(),
566+
Pub: ThrowingPub.new,
567+
},
568+
);
569+
570+
testUsingContext(
571+
'rejects a legacy-format plugin with an unsafe iosPrefix',
572+
() async {
573+
final Directory pluginDirectory = fs.systemTempDirectory.createTempSync(
574+
'flutter_plugin_legacy_injection.',
575+
);
576+
pluginDirectory.childFile('pubspec.yaml').writeAsStringSync('''
577+
name: evil_plugin
578+
flutter:
579+
plugin:
580+
androidPackage: com.example.evil
581+
pluginClass: EvilPlugin
582+
iosPrefix: "FLT; some.malicious.Payload.run(); //"
583+
''');
584+
addToPackageConfig('evil_plugin', pluginDirectory);
585+
586+
await expectLater(
587+
() => findPlugins(flutterProject),
588+
throwsToolExit(message: 'invalid "iosPrefix"'),
589+
);
590+
},
591+
overrides: <Type, Generator>{
592+
FileSystem: () => fs,
593+
ProcessManager: () => FakeProcessManager.any(),
594+
Pub: ThrowingPub.new,
595+
},
596+
);
597+
516598
testUsingContext(
517599
'Refreshing the plugin list updates .flutter-plugins-dependencies if the plugins changed',
518600
() async {

0 commit comments

Comments
 (0)