Skip to content

Commit 22d51b8

Browse files
committed
[native_assets_builder] Don't pass in the whole environment
1 parent 3113f5e commit 22d51b8

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart

+37-2
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ class NativeAssetsBuildRunner {
433433
null,
434434
hookKernelFile,
435435
packageLayout!,
436+
_filteredEnvironment(_environmentVariablesFilter),
436437
),
437438
);
438439
if (buildOutput == null) return null;
@@ -450,7 +451,7 @@ class NativeAssetsBuildRunner {
450451
Uri? resources,
451452
PackageLayout packageLayout,
452453
) async {
453-
final environment = Platform.environment;
454+
final environment = _filteredEnvironment(_environmentVariablesFilter);
454455
final outDir = config.outputDirectory;
455456
return await runUnderDirectoriesLock(
456457
[
@@ -526,6 +527,7 @@ ${e.message}
526527
resources,
527528
hookKernelFile,
528529
packageLayout,
530+
environment,
529531
);
530532
if (result == null) {
531533
if (await dependenciesHashFile.exists()) {
@@ -550,6 +552,22 @@ ${e.message}
550552
);
551553
}
552554

555+
/// Limit the environment that hook invocations get to see.
556+
///
557+
/// This allowlist lists environment variables needed to run mainstream
558+
/// compilers.
559+
static const _environmentVariablesFilter = {
560+
'ANDROID_HOME', // Needed for the NDK.
561+
'HOME', // Needed to find tools in default install locations.
562+
'PATH', // Needed to invoke native tools.
563+
'PROGRAMDATA', // Needed for vswhere.exe.
564+
'SYSTEMROOT', // Needed for process invocations on Windows.
565+
'TEMP', // Needed for temp dirs in Dart process.
566+
'TMP', // Needed for temp dirs in Dart process.
567+
'TMPDIR', // Needed for temp dirs in Dart process.
568+
'USER_PROFILE', // Needed to find tools in default install locations.
569+
};
570+
553571
Future<HookOutput?> _runHookForPackage(
554572
Hook hook,
555573
HookConfig config,
@@ -559,6 +577,7 @@ ${e.message}
559577
Uri? resources,
560578
File hookKernelFile,
561579
PackageLayout packageLayout,
580+
Map<String, String> environment,
562581
) async {
563582
final configFile = config.outputDirectory.resolve('../config.json');
564583
final configFileContents =
@@ -583,6 +602,8 @@ ${e.message}
583602
executable: dartExecutable,
584603
arguments: arguments,
585604
logger: logger,
605+
includeParentEnvironment: false,
606+
environment: environment,
586607
);
587608

588609
var deleteOutputIfExists = false;
@@ -639,6 +660,12 @@ ${e.message}
639660
}
640661
}
641662

663+
Map<String, String> _filteredEnvironment(Set<String> allowList) => {
664+
for (final entry in Platform.environment.entries)
665+
if (allowList.contains(entry.key.toUpperCase()))
666+
entry.key: entry.value,
667+
};
668+
642669
/// Compiles the hook to kernel and caches the kernel.
643670
///
644671
/// If any of the Dart source files, or the package config changed after
@@ -666,7 +693,11 @@ ${e.message}
666693
Uri packageConfigUri,
667694
Uri workingDirectory,
668695
) async {
669-
final environment = Platform.environment;
696+
final environment = _filteredEnvironment({
697+
'HOME', // Needed for Dart.
698+
'PUB_CACHE', // Needed for Dart.
699+
'SYSTEMROOT', // Needed for process invocation on Windows.
700+
});
670701
final kernelFile = File.fromUri(
671702
outputDirectory.resolve('../hook.dill'),
672703
);
@@ -703,6 +734,7 @@ ${e.message}
703734
workingDirectory,
704735
kernelFile,
705736
depFile,
737+
environment,
706738
);
707739
if (!success) {
708740
await dependenciesHashFile.delete();
@@ -745,6 +777,7 @@ ${e.message}
745777
Uri workingDirectory,
746778
File kernelFile,
747779
File depFile,
780+
Map<String, String> environment,
748781
) async {
749782
final compileArguments = [
750783
'compile',
@@ -759,6 +792,8 @@ ${e.message}
759792
executable: dartExecutable,
760793
arguments: compileArguments,
761794
logger: logger,
795+
includeParentEnvironment: false,
796+
environment: environment,
762797
);
763798
var success = true;
764799
if (compileResult.exitCode != 0) {

0 commit comments

Comments
 (0)