@@ -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,8 @@ ${e.message}
666
693
Uri packageConfigUri,
667
694
Uri workingDirectory,
668
695
) async {
669
- final environment = Platform .environment;
696
+ // Don't invalidate cache with environment changes.
697
+ final environmentForCaching = < String , String > {};
670
698
final kernelFile = File .fromUri (
671
699
outputDirectory.resolve ('../hook.dill' ),
672
700
);
@@ -682,8 +710,8 @@ ${e.message}
682
710
if (! await dependenciesHashFile.exists ()) {
683
711
mustCompile = true ;
684
712
} else {
685
- final outdatedDependency =
686
- await dependenciesHashes .findOutdatedDependency (environment );
713
+ final outdatedDependency = await dependenciesHashes
714
+ .findOutdatedDependency (environmentForCaching );
687
715
if (outdatedDependency != null ) {
688
716
mustCompile = true ;
689
717
logger.info (
@@ -717,7 +745,7 @@ ${e.message}
717
745
dartExecutable.resolve ('../version' ),
718
746
],
719
747
lastModifiedCutoffTime,
720
- environment ,
748
+ environmentForCaching ,
721
749
);
722
750
if (modifiedDuringBuild != null ) {
723
751
logger.severe ('File modified during build. Build must be rerun.' );
@@ -759,6 +787,7 @@ ${e.message}
759
787
executable: dartExecutable,
760
788
arguments: compileArguments,
761
789
logger: logger,
790
+ includeParentEnvironment: true ,
762
791
);
763
792
var success = true ;
764
793
if (compileResult.exitCode != 0 ) {
0 commit comments