From c23edf28386833f8980b446922e63e1912a26a07 Mon Sep 17 00:00:00 2001 From: iceweasel-oai Date: Wed, 29 Apr 2026 21:17:35 -0700 Subject: [PATCH] only use inner powershell command for rule matching --- codex-rs/core/src/exec_policy.rs | 14 ++++++++++-- codex-rs/core/src/exec_policy_tests.rs | 31 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/codex-rs/core/src/exec_policy.rs b/codex-rs/core/src/exec_policy.rs index c3abb7273f..6215a8231b 100644 --- a/codex-rs/core/src/exec_policy.rs +++ b/codex-rs/core/src/exec_policy.rs @@ -801,7 +801,10 @@ fn try_derive_execpolicy_amendment_for_prompt_rules( fn try_derive_execpolicy_amendment_for_allow_rules( matched_rules: &[RuleMatch], ) -> Option { - if matched_rules.iter().any(is_policy_match) { + if matched_rules + .iter() + .any(|rule_match| is_policy_match(rule_match) && rule_match.decision() != Decision::Allow) + { return None; } @@ -843,7 +846,14 @@ fn derive_requested_execpolicy_amendment_from_prefix_rule( } // if any policy rule already matches, don't suggest an additional rule that might conflict or not apply - if matched_rules.iter().any(is_policy_match) { + // PowerShell evaluation can combine outer wrapper matches with unmatched + // inner commands. Existing explicit allow rules should not suppress a + // requested inner-command prefix rule that would resolve the remaining + // prompt, but explicit prompt/forbidden matches still should. + if matched_rules + .iter() + .any(|rule_match| is_policy_match(rule_match) && rule_match.decision() != Decision::Allow) + { return None; } diff --git a/codex-rs/core/src/exec_policy_tests.rs b/codex-rs/core/src/exec_policy_tests.rs index 214d132356..a8e6efb03f 100644 --- a/codex-rs/core/src/exec_policy_tests.rs +++ b/codex-rs/core/src/exec_policy_tests.rs @@ -866,6 +866,37 @@ async fn powershell_outer_allow_does_not_override_unmatched_wrapper_prompt() { .await; } +#[cfg(windows)] +#[tokio::test] +async fn powershell_outer_allow_still_allows_inner_prefix_rule_suggestion() { + assert_exec_approval_requirement_for_command( + ExecApprovalRequirementScenario { + policy_src: Some( + r#"prefix_rule(pattern=["powershell.exe"], decision="allow")"#.to_string(), + ), + command: vec_str(&[ + "powershell.exe", + "-WindowStyle", + "Hidden", + "-Command", + "Get-Content Cargo.toml", + ]), + approval_policy: AskForApproval::OnRequest, + sandbox_policy: SandboxPolicy::new_read_only_policy(), + file_system_sandbox_policy: read_only_file_system_sandbox_policy(), + sandbox_permissions: SandboxPermissions::UseDefault, + prefix_rule: Some(vec_str(&["Get-Content"])), + }, + ExecApprovalRequirement::NeedsApproval { + reason: None, + proposed_execpolicy_amendment: Some(ExecPolicyAmendment::new(vec_str(&[ + "Get-Content", + ]))), + }, + ) + .await; +} + #[test] fn commands_for_exec_policy_falls_back_for_empty_shell_script() { let command = vec!["bash".to_string(), "-lc".to_string(), "".to_string()];