@@ -433,6 +433,7 @@ class NativeAssetsBuildRunner {
433
433
null ,
434
434
hookKernelFile,
435
435
packageLayout! ,
436
+ _filteredEnvironment (_environmentVariablesFilter),
436
437
),
437
438
);
438
439
if (buildOutput == null ) return null ;
@@ -450,7 +451,7 @@ class NativeAssetsBuildRunner {
450
451
Uri ? resources,
451
452
PackageLayout packageLayout,
452
453
) async {
453
- final environment = Platform .environment ;
454
+ final environment = _filteredEnvironment (_environmentVariablesFilter) ;
454
455
final outDir = config.outputDirectory;
455
456
return await runUnderDirectoriesLock (
456
457
[
@@ -526,6 +527,7 @@ ${e.message}
526
527
resources,
527
528
hookKernelFile,
528
529
packageLayout,
530
+ environment,
529
531
);
530
532
if (result == null ) {
531
533
if (await dependenciesHashFile.exists ()) {
@@ -550,6 +552,22 @@ ${e.message}
550
552
);
551
553
}
552
554
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
+
553
571
Future <HookOutput ?> _runHookForPackage (
554
572
Hook hook,
555
573
HookConfig config,
@@ -559,6 +577,7 @@ ${e.message}
559
577
Uri ? resources,
560
578
File hookKernelFile,
561
579
PackageLayout packageLayout,
580
+ Map <String , String > environment,
562
581
) async {
563
582
final configFile = config.outputDirectory.resolve ('../config.json' );
564
583
final configFileContents =
@@ -583,6 +602,8 @@ ${e.message}
583
602
executable: dartExecutable,
584
603
arguments: arguments,
585
604
logger: logger,
605
+ includeParentEnvironment: false ,
606
+ environment: environment,
586
607
);
587
608
588
609
var deleteOutputIfExists = false ;
@@ -639,6 +660,12 @@ ${e.message}
639
660
}
640
661
}
641
662
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
+
642
669
/// Compiles the hook to kernel and caches the kernel.
643
670
///
644
671
/// If any of the Dart source files, or the package config changed after
@@ -666,7 +693,11 @@ ${e.message}
666
693
Uri packageConfigUri,
667
694
Uri workingDirectory,
668
695
) 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
+ });
670
701
final kernelFile = File .fromUri (
671
702
outputDirectory.resolve ('../hook.dill' ),
672
703
);
@@ -703,6 +734,7 @@ ${e.message}
703
734
workingDirectory,
704
735
kernelFile,
705
736
depFile,
737
+ environment,
706
738
);
707
739
if (! success) {
708
740
await dependenciesHashFile.delete ();
@@ -745,6 +777,7 @@ ${e.message}
745
777
Uri workingDirectory,
746
778
File kernelFile,
747
779
File depFile,
780
+ Map <String , String > environment,
748
781
) async {
749
782
final compileArguments = [
750
783
'compile' ,
@@ -759,6 +792,8 @@ ${e.message}
759
792
executable: dartExecutable,
760
793
arguments: compileArguments,
761
794
logger: logger,
795
+ includeParentEnvironment: false ,
796
+ environment: environment,
762
797
);
763
798
var success = true ;
764
799
if (compileResult.exitCode != 0 ) {
0 commit comments