Preserve unparsed shell wrappers in exec policy (#39588)

## Why

Reducing a heredoc shell script to its inner executable lets a prefix rule for
that executable apply to the entire wrapper, even though the full script was
not parsed as a plain command.

## What changed

- Fall back to evaluating the complete shell wrapper when plain-command parsing
  fails, including for heredoc scripts.
- Keep these commands sandboxed when only the inner executable is allowed.
- Propose the full wrapper as the exec policy amendment when approval is needed.

## Testing

Added exec policy, Unix escalation, and approval scenario coverage for unparsed
and heredoc shell wrappers.

GitOrigin-RevId: 8f65133acb6b7c638263917e1d9137e45990772c
This commit is contained in:
Dylan Hurd
2026-08-20 05:07:17 +00:00
committed by copyberry
parent 3bebaea8f2
commit 4b450d2f1b
8 changed files with 90 additions and 307 deletions
@@ -45,7 +45,6 @@ use codex_sandboxing::SandboxablePreference;
use codex_sandboxing::policy_transforms::merge_permission_profiles;
use codex_sandboxing::record_filesystem_sandbox_violation;
use codex_shell_command::bash::parse_shell_lc_plain_commands;
use codex_shell_command::bash::parse_shell_lc_single_command_prefix;
use codex_shell_escalation::EscalateServer;
use codex_shell_escalation::EscalationDecision;
use codex_shell_escalation::EscalationExecution;
@@ -681,10 +680,7 @@ fn evaluate_intercepted_exec_policy(
sandbox_permissions,
enable_shell_wrapper_parsing,
} = context;
let CandidateCommands {
commands,
used_complex_parsing,
} = if enable_shell_wrapper_parsing {
let commands = if enable_shell_wrapper_parsing {
// In this codepath, the first argument in `commands` could be a bare
// name like `find` instead of an absolute path like `/usr/bin/find`.
// It could also be a shell built-in like `echo`.
@@ -692,10 +688,7 @@ fn evaluate_intercepted_exec_policy(
} else {
// In this codepath, `commands` has a single entry where the program
// is always an absolute path.
CandidateCommands {
commands: vec![join_program_and_argv(program, argv)],
used_complex_parsing: false,
}
vec![join_program_and_argv(program, argv)]
};
let fallback = |cmd: &[String]| {
@@ -706,7 +699,6 @@ fn evaluate_intercepted_exec_policy(
permission_profile: &permission_profile,
windows_sandbox_level,
sandbox_permissions,
used_complex_parsing,
command_origin: crate::exec_policy::ExecPolicyCommandOrigin::Generic,
},
)
@@ -730,39 +722,24 @@ struct InterceptedExecPolicyContext {
enable_shell_wrapper_parsing: bool,
}
struct CandidateCommands {
commands: Vec<Vec<String>>,
used_complex_parsing: bool,
}
fn commands_for_intercepted_exec_policy(
program: &AbsolutePathBuf,
argv: &[String],
) -> CandidateCommands {
) -> Vec<Vec<String>> {
if let [_, flag, script] = argv {
let shell_command = [
program.to_string_lossy().to_string(),
flag.clone(),
script.clone(),
];
if let Some(commands) = parse_shell_lc_plain_commands(&shell_command) {
return CandidateCommands {
commands,
used_complex_parsing: false,
};
}
if let Some(single_command) = parse_shell_lc_single_command_prefix(&shell_command) {
return CandidateCommands {
commands: vec![single_command],
used_complex_parsing: true,
};
if let Some(commands) = parse_shell_lc_plain_commands(&shell_command)
&& !commands.is_empty()
{
return commands;
}
}
CandidateCommands {
commands: vec![join_program_and_argv(program, argv)],
used_complex_parsing: false,
}
vec![join_program_and_argv(program, argv)]
}
struct CoreShellCommandExecutor {
@@ -257,13 +257,24 @@ fn commands_for_intercepted_exec_policy_parses_plain_shell_wrappers() {
);
assert_eq!(
candidate_commands.commands,
candidate_commands,
vec![
vec!["git".to_string(), "status".to_string()],
vec!["pwd".to_string()],
]
);
assert!(!candidate_commands.used_complex_parsing);
}
#[test]
fn commands_for_intercepted_exec_policy_preserves_unparsed_shell_wrappers() {
let program = AbsolutePathBuf::try_from(host_absolute_path(&["bin", "bash"])).unwrap();
for script in ["", " \n\t", "cat <<'EOF'\nhello\nEOF"] {
let argv = ["not-bash".into(), "-lc".into(), script.into()];
assert_eq!(
commands_for_intercepted_exec_policy(&program, &argv),
vec![join_program_and_argv(&program, &argv)]
);
}
}
#[test]