From 1952a6c8a19fb4a8c1fc80b50de6ae335ff40013 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Mon, 27 Nov 2023 12:50:24 -0800 Subject: [PATCH] Add type validation to non-template .arb file parsing logic (#139035) Resolves https://github.com/flutter/flutter/issues/138297. When reading from a .arb file (which contains localizations of strings in the form of a user-defined JSON string-string map), validate the type of values that we read. See [this comment of mine on #138297](https://github.com/flutter/flutter/issues/138297#issuecomment-1827043260) to see how I arrived at this fix. --- .../lib/src/localizations/gen_l10n_types.dart | 10 ++++++- .../generate_localizations_test.dart | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart b/packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart index d17988dd1e..d4c6f12c8d 100644 --- a/packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart +++ b/packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart @@ -4,6 +4,7 @@ import 'package:intl/locale.dart'; +import '../base/common.dart'; import '../base/file_system.dart'; import '../base/logger.dart'; import '../convert.dart'; @@ -646,7 +647,14 @@ class AppResourceBundle { final Map resources; final Iterable resourceIds; - String? translationFor(String resourceId) => resources[resourceId] as String?; + String? translationFor(String resourceId) { + final Object? result = resources[resourceId]; + if (result is! String?) { + throwToolExit('Localized message for key "$resourceId" in "${file.path}" ' + 'is not a string.'); + } + return result; + } @override String toString() { diff --git a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart index fa35cfd510..c69efb9b55 100644 --- a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart +++ b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart @@ -1241,6 +1241,34 @@ class AppLocalizationsEn extends AppLocalizations { )), ); }); + + testWithoutContext('AppResourceBundle throws if file contains non-string value', () { + const String inputPathString = 'lib/l10n'; + const String templateArbFileName = 'app_en.arb'; + const String outputFileString = 'app_localizations.dart'; + const String classNameString = 'AppLocalizations'; + + fs.file(fs.path.join(inputPathString, templateArbFileName)) + ..createSync(recursive: true) + ..writeAsStringSync('{ "helloWorld": "Hello World!" }'); + fs.file(fs.path.join(inputPathString, 'app_es.arb')) + ..createSync(recursive: true) + ..writeAsStringSync('{ "helloWorld": {} }'); + + final LocalizationsGenerator generator = LocalizationsGenerator( + fileSystem: fs, + inputPathString: inputPathString, + templateArbFileName: templateArbFileName, + outputFileString: outputFileString, + classNameString: classNameString, + logger: logger, + ); + expect( + () => generator.loadResources(), + throwsToolExit(message: 'Localized message for key "helloWorld" in ' + '"lib/l10n/app_es.arb" is not a string.'), + ); + }); }); group('writeOutputFiles', () {