@@ -166,13 +166,20 @@ fn strip_supervisor_only_env(cmd: &mut Command) {
166166 }
167167}
168168
169- fn inject_provider_env ( cmd : & mut Command , provider_env : & HashMap < String , String > ) {
169+ fn inject_provider_env (
170+ cmd : & mut Command ,
171+ provider_env : & HashMap < String , String > ,
172+ repair_standard_sbin : bool ,
173+ ) {
170174 for ( key, value) in provider_env {
171175 if is_supervisor_only_env_var ( key) {
172176 continue ;
173177 }
174178 if key == "PATH" {
175- cmd. env ( key, child_env:: path_with_standard_sbin_paths ( value) ) ;
179+ cmd. env (
180+ key,
181+ child_env:: maybe_path_with_standard_sbin_paths ( value, repair_standard_sbin) ,
182+ ) ;
176183 } else {
177184 cmd. env ( key, value) ;
178185 }
@@ -563,8 +570,16 @@ fn shell_command_arg_index(args: &[String]) -> Option<usize> {
563570 None
564571}
565572
566- fn process_args_with_standard_sbin_paths ( program : & str , args : & [ String ] ) -> Vec < String > {
573+ fn process_args_with_standard_sbin_paths (
574+ program : & str ,
575+ args : & [ String ] ,
576+ repair_standard_sbin : bool ,
577+ ) -> Vec < String > {
567578 let mut args = args. to_vec ( ) ;
579+ if !repair_standard_sbin {
580+ return args;
581+ }
582+
568583 let basename = program. rsplit ( '/' ) . next ( ) . unwrap_or ( program) ;
569584 if matches ! ( basename, "bash" | "sh" )
570585 && let Some ( command_index) = shell_command_arg_index ( & args)
@@ -654,23 +669,24 @@ impl ProcessHandle {
654669 ca_paths : Option < & ( PathBuf , PathBuf ) > ,
655670 provider_env : & HashMap < String , String > ,
656671 ) -> Result < Self > {
657- let args = process_args_with_standard_sbin_paths ( program, args) ;
672+ let repair_standard_sbin = child_env:: standard_sbin_path_repair_enabled ( policy) ;
673+ let args = process_args_with_standard_sbin_paths ( program, args, repair_standard_sbin) ;
658674 let mut cmd = Command :: new ( program) ;
659675 cmd. args ( & args)
660676 . stdin ( Stdio :: inherit ( ) )
661677 . stdout ( Stdio :: inherit ( ) )
662678 . stderr ( Stdio :: inherit ( ) )
663679 . kill_on_drop ( true )
664680 . env ( openshell_core:: sandbox_env:: SANDBOX , "1" )
665- . env ( "PATH" , child_env:: child_path_from_env ( ) ) ;
681+ . env ( "PATH" , child_env:: child_path_from_env ( repair_standard_sbin ) ) ;
666682
667683 // Strip supervisor-only identity material from the entrypoint's
668684 // inherited environment. The entrypoint drops to the sandbox user
669685 // before `exec`; without this strip, sandbox code could recover
670686 // supervisor credentials from its inherited environment.
671687 strip_supervisor_only_env ( & mut cmd) ;
672688
673- inject_provider_env ( & mut cmd, provider_env) ;
689+ inject_provider_env ( & mut cmd, provider_env, repair_standard_sbin ) ;
674690
675691 if let Some ( dir) = workspace. root ( ) {
676692 cmd. current_dir ( dir) ;
@@ -813,21 +829,22 @@ impl ProcessHandle {
813829 ca_paths : Option < & ( PathBuf , PathBuf ) > ,
814830 provider_env : & HashMap < String , String > ,
815831 ) -> Result < Self > {
816- let args = process_args_with_standard_sbin_paths ( program, args) ;
832+ let repair_standard_sbin = child_env:: standard_sbin_path_repair_enabled ( policy) ;
833+ let args = process_args_with_standard_sbin_paths ( program, args, repair_standard_sbin) ;
817834 let mut cmd = Command :: new ( program) ;
818835 cmd. args ( & args)
819836 . stdin ( Stdio :: inherit ( ) )
820837 . stdout ( Stdio :: inherit ( ) )
821838 . stderr ( Stdio :: inherit ( ) )
822839 . kill_on_drop ( true )
823840 . env ( openshell_core:: sandbox_env:: SANDBOX , "1" )
824- . env ( "PATH" , child_env:: child_path_from_env ( ) ) ;
841+ . env ( "PATH" , child_env:: child_path_from_env ( repair_standard_sbin ) ) ;
825842
826843 // Strip supervisor-only identity material from the entrypoint's
827844 // inherited environment.
828845 strip_supervisor_only_env ( & mut cmd) ;
829846
830- inject_provider_env ( & mut cmd, provider_env) ;
847+ inject_provider_env ( & mut cmd, provider_env, repair_standard_sbin ) ;
831848
832849 if let Some ( dir) = workspace. root ( ) {
833850 cmd. current_dir ( dir) ;
@@ -2252,7 +2269,7 @@ mod tests {
22522269 fn process_args_wrap_shell_c_command_with_standard_sbin_paths ( ) {
22532270 let args = vec ! [ "-lc" . to_string( ) , "nvidia-smi -L" . to_string( ) ] ;
22542271
2255- let wrapped = process_args_with_standard_sbin_paths ( "sh" , & args) ;
2272+ let wrapped = process_args_with_standard_sbin_paths ( "sh" , & args, true ) ;
22562273
22572274 assert_eq ! ( wrapped[ 0 ] , "-lc" ) ;
22582275 assert ! ( wrapped[ 1 ] . contains( "/usr/sbin" ) ) ;
@@ -2263,7 +2280,16 @@ mod tests {
22632280 fn process_args_leave_non_shell_commands_unchanged ( ) {
22642281 let args = vec ! [ "nvidia-smi -L" . to_string( ) ] ;
22652282
2266- let wrapped = process_args_with_standard_sbin_paths ( "python" , & args) ;
2283+ let wrapped = process_args_with_standard_sbin_paths ( "python" , & args, true ) ;
2284+
2285+ assert_eq ! ( wrapped, args) ;
2286+ }
2287+
2288+ #[ test]
2289+ fn process_args_do_not_wrap_when_standard_sbin_repair_disabled ( ) {
2290+ let args = vec ! [ "-lc" . to_string( ) , "nvidia-smi -L" . to_string( ) ] ;
2291+
2292+ let wrapped = process_args_with_standard_sbin_paths ( "sh" , & args, false ) ;
22672293
22682294 assert_eq ! ( wrapped, args) ;
22692295 }
@@ -2786,15 +2812,15 @@ mod tests {
27862812 ) )
27872813 . collect ( ) ;
27882814
2789- inject_provider_env ( & mut cmd, & provider_env) ;
2815+ inject_provider_env ( & mut cmd, & provider_env, false ) ;
27902816
27912817 let output = cmd. output ( ) . await . expect ( "spawn env" ) ;
27922818 let stdout = String :: from_utf8 ( output. stdout ) . expect ( "utf8" ) ;
27932819 assert ! ( stdout. contains( "ANTHROPIC_API_KEY=openshell:resolve:env:ANTHROPIC_API_KEY" ) ) ;
27942820 }
27952821
27962822 #[ tokio:: test]
2797- async fn inject_provider_env_appends_standard_sbin_to_path ( ) {
2823+ async fn inject_provider_env_appends_standard_sbin_to_path_when_enabled ( ) {
27982824 let mut cmd = Command :: new ( "/usr/bin/env" ) ;
27992825 cmd. env_clear ( )
28002826 . stdin ( StdStdio :: null ( ) )
@@ -2806,7 +2832,7 @@ mod tests {
28062832 "/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin" . to_string ( ) ,
28072833 ) ] ) ;
28082834
2809- inject_provider_env ( & mut cmd, & provider_env) ;
2835+ inject_provider_env ( & mut cmd, & provider_env, true ) ;
28102836
28112837 let output = cmd. output ( ) . await . expect ( "spawn env" ) ;
28122838 assert ! ( output. status. success( ) ) ;
@@ -2816,6 +2842,31 @@ mod tests {
28162842 } ) ) ;
28172843 }
28182844
2845+ #[ tokio:: test]
2846+ async fn inject_provider_env_leaves_path_unchanged_when_standard_sbin_repair_disabled ( ) {
2847+ let mut cmd = Command :: new ( "/usr/bin/env" ) ;
2848+ cmd. env_clear ( )
2849+ . stdin ( StdStdio :: null ( ) )
2850+ . stdout ( StdStdio :: piped ( ) )
2851+ . stderr ( StdStdio :: null ( ) ) ;
2852+
2853+ let provider_env = HashMap :: from ( [ (
2854+ "PATH" . to_string ( ) ,
2855+ "/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin" . to_string ( ) ,
2856+ ) ] ) ;
2857+
2858+ inject_provider_env ( & mut cmd, & provider_env, false ) ;
2859+
2860+ let output = cmd. output ( ) . await . expect ( "spawn env" ) ;
2861+ assert ! ( output. status. success( ) ) ;
2862+ let stdout = String :: from_utf8 ( output. stdout ) . expect ( "utf8" ) ;
2863+ assert ! (
2864+ stdout
2865+ . lines( )
2866+ . any( |line| { line == "PATH=/sandbox/.venv/bin:/usr/local/bin:/usr/bin:/bin" } )
2867+ ) ;
2868+ }
2869+
28192870 #[ cfg( unix) ]
28202871 fn sandbox_policy_with_read_write (
28212872 path : PathBuf ,
@@ -3693,7 +3744,7 @@ mod tests {
36933744 ) ,
36943745 ] ) ;
36953746
3696- inject_provider_env ( & mut cmd, & provider_env) ;
3747+ inject_provider_env ( & mut cmd, & provider_env, false ) ;
36973748
36983749 let output = cmd. output ( ) . await . expect ( "spawn env" ) ;
36993750 assert ! ( output. status. success( ) ) ;
0 commit comments