//! Central approval policy-stage execution and reviewer routing. use crate::command_canonicalization::canonicalize_command_for_approval; use crate::guardian::GuardianReviewContext; use crate::guardian::guardian_timeout_message; use crate::guardian::new_guardian_review_id; use crate::guardian::review_approval_request; use crate::guardian::routes_approval_to_guardian_with_reviewer; use crate::hook_runtime::run_permission_request_hooks; use crate::sandboxing::SandboxPermissions; use crate::session::session::Session; use crate::session::turn_context::TurnContext; use crate::tools::flat_tool_name; use crate::tools::hook_names::HookToolName; use crate::tools::runtimes::apply_patch::ApplyPatchApprovalKey; use crate::tools::runtimes::shell::ApprovalKey; use crate::tools::runtimes::unified_exec::UnifiedExecApprovalKey; use crate::tools::sandboxing::ApprovalRequestReasons; use crate::tools::sandboxing::PermissionRequestPayload; use crate::tools::sandboxing::ToolError; use crate::tools::sandboxing::with_cached_approval; use codex_hooks::PermissionRequestDecision; use codex_otel::ToolDecisionSource; use codex_protocol::approvals::ExecPolicyAmendment; #[cfg(unix)] use codex_protocol::approvals::GuardianCommandSource; use codex_protocol::approvals::NetworkApprovalContext; use codex_protocol::error::CodexErr; use codex_protocol::models::AdditionalPermissionProfile; use codex_protocol::protocol::FileChange; use codex_protocol::protocol::NetworkPolicyRuleAction; use codex_protocol::protocol::ReviewDecision; use codex_tools::ToolName; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_path_uri::PathUri; use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; use tracing::error; #[derive(Clone)] pub(crate) struct ApprovalContext { pub(crate) review_context: GuardianReviewContext, pub(crate) call_id: String, pub(crate) tool_name: ToolName, pub(crate) strict_auto_review: bool, pub(crate) approval_reason: Option, pub(crate) retry_reason: Option, pub(crate) network_approval_context: Option, } #[derive(Clone, Debug, PartialEq)] pub(crate) enum ApprovalAction { Shell { id: String, environment_id: String, command: Vec, hook_command: String, cwd: PathUri, sandbox_permissions: SandboxPermissions, additional_permissions: Option, justification: Option, proposed_execpolicy_amendment: Option, }, ExecCommand { id: String, environment_id: String, command: Vec, hook_command: String, cwd: PathUri, sandbox_permissions: SandboxPermissions, additional_permissions: Option, justification: Option, tty: bool, proposed_execpolicy_amendment: Option, }, #[cfg(unix)] Execve { id: String, approval_id: String, environment_id: String, source: GuardianCommandSource, program: AbsolutePathBuf, argv: Vec, command: Vec, cwd: AbsolutePathBuf, additional_permissions: Option, }, ApplyPatch { id: String, environment_id: String, cwd: PathUri, files: Vec, patch: String, changes: Arc>, permissions_preapproved: bool, }, } #[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize)] #[serde(untagged)] pub(crate) enum ApprovalCacheKey { Shell(ApprovalKey), ExecCommand(UnifiedExecApprovalKey), ApplyPatch(ApplyPatchApprovalKey), } impl ApprovalAction { pub(crate) fn permission_request_payload(&self) -> PermissionRequestPayload { match self { Self::Shell { hook_command, justification, .. } | Self::ExecCommand { hook_command, justification, .. } => PermissionRequestPayload::bash(hook_command.clone(), justification.clone()), #[cfg(unix)] Self::Execve { command, .. } => PermissionRequestPayload::bash( codex_shell_command::parse_command::shlex_join(command), /*description*/ None, ), Self::ApplyPatch { patch, .. } => PermissionRequestPayload { tool_name: HookToolName::apply_patch(), tool_input: serde_json::json!({ "command": patch }), }, } } pub(crate) fn cache_keys(&self) -> Vec { match self { Self::Shell { environment_id, command, cwd, sandbox_permissions, additional_permissions, .. } => vec![ApprovalCacheKey::Shell(ApprovalKey { environment_id: environment_id.clone(), command: canonicalize_command_for_approval(command), cwd: cwd.clone(), sandbox_permissions: *sandbox_permissions, additional_permissions: additional_permissions.clone(), })], Self::ExecCommand { environment_id, command, cwd, tty, sandbox_permissions, additional_permissions, .. } => vec![ApprovalCacheKey::ExecCommand(UnifiedExecApprovalKey { environment_id: environment_id.clone(), command: canonicalize_command_for_approval(command), cwd: cwd.clone(), tty: *tty, sandbox_permissions: *sandbox_permissions, additional_permissions: additional_permissions.clone(), })], #[cfg(unix)] Self::Execve { .. } => Vec::new(), Self::ApplyPatch { environment_id, files, .. } => files .iter() .cloned() .map(|path| { ApprovalCacheKey::ApplyPatch(ApplyPatchApprovalKey { environment_id: environment_id.clone(), path, }) }) .collect(), } } fn into_guardian_request(self) -> std::io::Result { Ok(match self { Self::Shell { id, environment_id, command, cwd, sandbox_permissions, additional_permissions, justification, .. } => crate::guardian::GuardianApprovalRequest::Shell { id, command, cwd: guardian_cwd(&environment_id, cwd)?, sandbox_permissions, additional_permissions, justification, }, Self::ExecCommand { id, environment_id, command, cwd, sandbox_permissions, additional_permissions, justification, tty, .. } => crate::guardian::GuardianApprovalRequest::ExecCommand { id, command, cwd: guardian_cwd(&environment_id, cwd)?, sandbox_permissions, additional_permissions, justification, tty, }, #[cfg(unix)] Self::Execve { id, source, program, argv, cwd, additional_permissions, .. } => crate::guardian::GuardianApprovalRequest::Execve { id, source, program: program.to_string_lossy().into_owned(), argv, cwd, additional_permissions, }, Self::ApplyPatch { id, environment_id, cwd, files, patch, .. } => crate::guardian::GuardianApprovalRequest::ApplyPatch { id, cwd: guardian_cwd(&environment_id, cwd)?, files: files .into_iter() .map(|path| path.to_abs_path()) .collect::>>()?, patch, }, }) } } fn guardian_cwd(environment_id: &str, cwd: PathUri) -> std::io::Result { match cwd.to_abs_path() { Ok(cwd) => Ok(cwd), Err(err) if environment_id != codex_exec_server::LOCAL_ENVIRONMENT_ID => Err(err), Err(_) => { let cwd_display = cwd.to_string(); let path = cwd.to_url().to_file_path().map_err(|()| { std::io::Error::new( std::io::ErrorKind::InvalidInput, format!("local cwd URI `{cwd_display}` is not a host-native path"), ) })?; AbsolutePathBuf::from_absolute_path_checked(path).map_err(|err| { std::io::Error::new( std::io::ErrorKind::InvalidInput, format!("local cwd URI `{cwd_display}` is not absolute: {err}"), ) }) } } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum ApprovalReviewer { Guardian, User, } impl ApprovalReviewer { fn for_turn(turn: &TurnContext) -> Self { if routes_approval_to_guardian_with_reviewer(turn, turn.config.approvals_reviewer) { Self::Guardian } else { Self::User } } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum ApprovalResolutionSource { Hook, Guardian, User, } #[derive(Clone, Debug, PartialEq, Eq)] struct ApprovalResolution { decision: ReviewDecision, source: ApprovalResolutionSource, } impl ApprovalResolution { fn into_tool_result(self) -> Result { let source = self.source; match self.decision { ReviewDecision::ApprovedMcpPolicyAmendment => { error!("Tool approval received ApprovedMcpPolicyAmendment"); Err(ToolError::Rejected( "Error while requesting approval".to_string(), )) } ReviewDecision::NetworkPolicyAmendment { network_policy_amendment, } if network_policy_amendment.action == NetworkPolicyRuleAction::Deny => { let rejection = match source { ApprovalResolutionSource::Hook => "rejected by configuration", ApprovalResolutionSource::Guardian => { "automatic approval review denied the action" } ApprovalResolutionSource::User => "rejected by user", }; Err(ToolError::Rejected(rejection.to_string())) } ReviewDecision::Denied { rejection } => Err(ToolError::Rejected(rejection)), ReviewDecision::TimedOut => Err(ToolError::Rejected(guardian_timeout_message())), ReviewDecision::Abort => Err(ToolError::Codex(CodexErr::TurnAborted)), decision => Ok(decision), } } } impl Session { pub(crate) async fn request_approval( self: &Arc, action: ApprovalAction, ctx: ApprovalContext, ) -> Result { let permission_request_run_id = match &action { #[cfg(unix)] ApprovalAction::Execve { approval_id, .. } => approval_id.clone(), _ if ctx.retry_reason.is_some() => format!("{}:retry", ctx.call_id), _ => ctx.call_id.clone(), }; // Approval precedence is: // 1. Hooks // 2. If StrictAutoReview || Guardian enabled, then Guardian. Else, user. let resolution = match run_permission_request_hooks( self, ctx.review_context.turn(), &permission_request_run_id, action.permission_request_payload(), ) .await { Some(PermissionRequestDecision::Allow) => ApprovalResolution { decision: ReviewDecision::Approved, source: ApprovalResolutionSource::Hook, }, Some(PermissionRequestDecision::Deny { message }) => ApprovalResolution { decision: ReviewDecision::denied(message), source: ApprovalResolutionSource::Hook, }, None => self.request_reviewer_approval(action, &ctx).await, }; record_resolution(&ctx, &resolution); resolution.into_tool_result() } async fn request_reviewer_approval( self: &Arc, action: ApprovalAction, ctx: &ApprovalContext, ) -> ApprovalResolution { let reviewer = if ctx.strict_auto_review { ApprovalReviewer::Guardian } else { ApprovalReviewer::for_turn(ctx.review_context.turn()) }; let decision = match reviewer { ApprovalReviewer::Guardian => self.request_guardian_approval(action, ctx).await, ApprovalReviewer::User => self.request_user_approval(&action, ctx).await, }; let source = match reviewer { ApprovalReviewer::Guardian => ApprovalResolutionSource::Guardian, ApprovalReviewer::User => ApprovalResolutionSource::User, }; ApprovalResolution { decision, source } } async fn request_guardian_approval( self: &Arc, action: ApprovalAction, ctx: &ApprovalContext, ) -> ReviewDecision { let review_id = new_guardian_review_id(); let action = match action.into_guardian_request() { Ok(action) => action, Err(err) => { tracing::error!(%err, "failed to build automatic approval action"); return ReviewDecision::denied( "automatic approval review could not prepare the action", ); } }; review_approval_request( self, ctx.review_context.clone(), review_id, action, ApprovalRequestReasons { approval: ctx.approval_reason.clone(), retry: ctx.retry_reason.clone(), }, ) .await } async fn request_user_approval( &self, action: &ApprovalAction, ctx: &ApprovalContext, ) -> ReviewDecision { match action { ApprovalAction::Shell { environment_id, command, cwd, additional_permissions, justification, proposed_execpolicy_amendment, .. } | ApprovalAction::ExecCommand { environment_id, command, cwd, additional_permissions, justification, proposed_execpolicy_amendment, .. } => { let cwd = match guardian_cwd(environment_id, cwd.clone()) { Ok(cwd) => cwd, Err(err) => { tracing::error!(%err, "failed to resolve approval command cwd"); return ReviewDecision::denied(format!( "failed to resolve approval command cwd: {err}" )); } }; let tool_name = match action { ApprovalAction::Shell { .. } => "shell", ApprovalAction::ExecCommand { .. } => "unified_exec", #[cfg(unix)] ApprovalAction::Execve { .. } => unreachable!("matched command approval"), ApprovalAction::ApplyPatch { .. } => unreachable!("matched command approval"), }; let reason = ctx .retry_reason .clone() .or_else(|| ctx.approval_reason.clone()) .or_else(|| justification.clone()); with_cached_approval(&self.services, tool_name, action.cache_keys(), || async { self.request_command_approval( ctx.review_context.turn(), ctx.call_id.clone(), /*approval_id*/ None, Some(environment_id.clone()), command.clone(), cwd, reason, ctx.network_approval_context.clone(), proposed_execpolicy_amendment.clone(), additional_permissions.clone(), /*available_decisions*/ None, /*plugin_attribution_override*/ None, ) .await }) .await } #[cfg(unix)] ApprovalAction::Execve { approval_id, environment_id, command, cwd, additional_permissions, .. } => { self.request_command_approval( ctx.review_context.turn(), ctx.call_id.clone(), Some(approval_id.clone()), Some(environment_id.clone()), command.clone(), cwd.clone(), /*reason*/ None, /*network_approval_context*/ None, /*proposed_execpolicy_amendment*/ None, additional_permissions.clone(), Some(vec![ReviewDecision::Approved, ReviewDecision::Abort]), /*plugin_attribution_override*/ None, ) .await } ApprovalAction::ApplyPatch { changes, permissions_preapproved, .. } => { let reason = ctx .retry_reason .clone() .or_else(|| ctx.approval_reason.clone()); if *permissions_preapproved && reason.is_none() { return ReviewDecision::Approved; } if reason.is_some() { return self .request_patch_approval( ctx.review_context.turn(), ctx.call_id.clone(), changes.as_ref().clone(), reason, /*grant_root*/ None, ) .await; } with_cached_approval( &self.services, "apply_patch", action.cache_keys(), || async { self.request_patch_approval( ctx.review_context.turn(), ctx.call_id.clone(), changes.as_ref().clone(), /*reason*/ None, /*grant_root*/ None, ) .await }, ) .await } } } } fn record_resolution(ctx: &ApprovalContext, resolution: &ApprovalResolution) { let source = match resolution.source { ApprovalResolutionSource::Hook => ToolDecisionSource::Config, ApprovalResolutionSource::Guardian => ToolDecisionSource::AutomatedReviewer, ApprovalResolutionSource::User => ToolDecisionSource::User, }; let tool_name = flat_tool_name(&ctx.tool_name); ctx.review_context.turn().session_telemetry.tool_decision( tool_name.as_ref(), &ctx.call_id, &resolution.decision, source, ); } #[cfg(all(test, unix))] #[path = "approvals_tests.rs"] mod tests;