@@ -434,6 +434,7 @@ class NativeAssetsBuildRunner {
434
434
null ,
435
435
hookKernelFile,
436
436
packageLayout! ,
437
+ _filteredEnvironment (_environmentVariablesFilter),
437
438
),
438
439
);
439
440
if (buildOutput == null ) return null ;
@@ -489,6 +490,7 @@ class NativeAssetsBuildRunner {
489
490
final environmentFile = File .fromUri (
490
491
config.outputDirectory.resolve ('../environment.json' ),
491
492
);
493
+ final environment = _filteredEnvironment (_environmentVariablesFilter);
492
494
if (buildOutputFile.existsSync () &&
493
495
dependenciesHashFile.existsSync () &&
494
496
environmentFile.existsSync ()) {
@@ -512,7 +514,7 @@ ${e.message}
512
514
! const MapEquality <String , String >().equals (
513
515
(json.decode (await environmentFile.readAsString ()) as Map )
514
516
.cast <String , String >(),
515
- Platform . environment);
517
+ environment);
516
518
if (outdatedFile == null && ! environmentChanged) {
517
519
logger.info (
518
520
'Skipping ${hook .name } for ${config .packageName }'
@@ -547,14 +549,15 @@ ${e.message}
547
549
resources,
548
550
hookKernelFile,
549
551
packageLayout,
552
+ environment,
550
553
);
551
554
if (result == null ) {
552
555
if (await dependenciesHashFile.exists ()) {
553
556
await dependenciesHashFile.delete ();
554
557
}
555
558
} else {
556
559
await environmentFile.writeAsString (
557
- json.encode (Platform . environment),
560
+ json.encode (environment),
558
561
);
559
562
final modifiedDuringBuild =
560
563
await dependenciesHashes.hashFilesAndDirectories (
@@ -574,6 +577,22 @@ ${e.message}
574
577
);
575
578
}
576
579
580
+ /// Limit the environment that hook invocations get to see.
581
+ ///
582
+ /// This allowlist lists environment variables needed to run mainstream
583
+ /// compilers.
584
+ static const _environmentVariablesFilter = {
585
+ 'ANDROID_HOME' ,
586
+ 'HOME' ,
587
+ 'PATH' ,
588
+ 'PROGRAMDATA' ,
589
+ 'SYSTEMROOT' ,
590
+ 'TEMP' ,
591
+ 'TMP' ,
592
+ 'TMPDIR' ,
593
+ 'USER_PROFILE' ,
594
+ };
595
+
577
596
Future <HookOutput ?> _runHookForPackage (
578
597
Hook hook,
579
598
HookConfig config,
@@ -583,6 +602,7 @@ ${e.message}
583
602
Uri ? resources,
584
603
File hookKernelFile,
585
604
PackageLayout packageLayout,
605
+ Map <String , String > environment,
586
606
) async {
587
607
final configFile = config.outputDirectory.resolve ('../config.json' );
588
608
final configFileContents =
@@ -607,6 +627,8 @@ ${e.message}
607
627
executable: dartExecutable,
608
628
arguments: arguments,
609
629
logger: logger,
630
+ includeParentEnvironment: false ,
631
+ environment: environment,
610
632
);
611
633
612
634
var deleteOutputIfExists = false ;
@@ -663,6 +685,12 @@ ${e.message}
663
685
}
664
686
}
665
687
688
+ Map <String , String > _filteredEnvironment (Set <String > allowList) => {
689
+ for (final entry in Platform .environment.entries)
690
+ if (allowList.contains (entry.key.toUpperCase ()))
691
+ entry.key: entry.value,
692
+ };
693
+
666
694
/// Compiles the hook to kernel and caches the kernel.
667
695
///
668
696
/// If any of the Dart source files, or the package config changed after
@@ -783,6 +811,8 @@ ${e.message}
783
811
executable: dartExecutable,
784
812
arguments: compileArguments,
785
813
logger: logger,
814
+ includeParentEnvironment: false ,
815
+ environment: _filteredEnvironment ({'HOME' , 'PUB_CACHE' , 'SYSTEMROOT' }),
786
816
);
787
817
var success = true ;
788
818
if (compileResult.exitCode != 0 ) {
0 commit comments