@@ -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 (! dependenciesOutdated && ! environmentChanged) {
517
519
logger.info (
518
520
[
@@ -535,14 +537,15 @@ ${e.message}
535
537
resources,
536
538
hookKernelFile,
537
539
packageLayout,
540
+ environment,
538
541
);
539
542
if (result == null ) {
540
543
if (await dependenciesHashFile.exists ()) {
541
544
await dependenciesHashFile.delete ();
542
545
}
543
546
} else {
544
547
await environmentFile.writeAsString (
545
- json.encode (Platform . environment),
548
+ json.encode (environment),
546
549
);
547
550
final modifiedDuringBuild = await dependenciesHashes.hashFiles (
548
551
[
@@ -561,6 +564,22 @@ ${e.message}
561
564
);
562
565
}
563
566
567
+ /// Limit the environment that hook invocations get to see.
568
+ ///
569
+ /// This allowlist lists environment variables needed to run mainstream
570
+ /// compilers.
571
+ static const _environmentVariablesFilter = {
572
+ 'ANDROID_HOME' ,
573
+ 'HOME' ,
574
+ 'PATH' ,
575
+ 'PROGRAMDATA' ,
576
+ 'SYSTEMROOT' ,
577
+ 'TEMP' ,
578
+ 'TMP' ,
579
+ 'TMPDIR' ,
580
+ 'USER_PROFILE' ,
581
+ };
582
+
564
583
Future <HookOutput ?> _runHookForPackage (
565
584
Hook hook,
566
585
HookConfig config,
@@ -570,6 +589,7 @@ ${e.message}
570
589
Uri ? resources,
571
590
File hookKernelFile,
572
591
PackageLayout packageLayout,
592
+ Map <String , String > environment,
573
593
) async {
574
594
final configFile = config.outputDirectory.resolve ('../config.json' );
575
595
final configFileContents =
@@ -594,6 +614,8 @@ ${e.message}
594
614
executable: dartExecutable,
595
615
arguments: arguments,
596
616
logger: logger,
617
+ includeParentEnvironment: false ,
618
+ environment: environment,
597
619
);
598
620
599
621
var deleteOutputIfExists = false ;
@@ -650,6 +672,12 @@ ${e.message}
650
672
}
651
673
}
652
674
675
+ Map <String , String > _filteredEnvironment (Set <String > allowList) => {
676
+ for (final entry in Platform .environment.entries)
677
+ if (allowList.contains (entry.key.toUpperCase ()))
678
+ entry.key: entry.value,
679
+ };
680
+
653
681
/// Compiles the hook to kernel and caches the kernel.
654
682
///
655
683
/// If any of the Dart source files, or the package config changed after
@@ -752,6 +780,8 @@ ${e.message}
752
780
executable: dartExecutable,
753
781
arguments: compileArguments,
754
782
logger: logger,
783
+ includeParentEnvironment: false ,
784
+ environment: _filteredEnvironment ({'HOME' , 'PUB_CACHE' , 'SYSTEMROOT' }),
755
785
);
756
786
var success = true ;
757
787
if (compileResult.exitCode != 0 ) {
0 commit comments