fix: prepare ExecPolicy in exec-server for execpolicy2 cutover

This commit is contained in:
Michael Bolin
2025-11-19 00:37:11 -08:00
parent a75321a64c
commit 4c82f9a609
7 changed files with 66 additions and 10 deletions
+18 -5
View File
@@ -59,11 +59,12 @@ use std::path::Path;
use clap::Parser;
use clap::Subcommand;
use codex_execpolicy2::Decision;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::{self};
use crate::posix::escalate_protocol::EscalateAction;
use crate::posix::escalate_server::EscalateServer;
use crate::posix::escalate_server::ExecPolicyOutcome;
mod escalate_client;
mod escalate_protocol;
@@ -71,16 +72,28 @@ mod escalate_server;
mod mcp;
mod socket;
fn dummy_exec_policy(file: &Path, argv: &[String], _workdir: &Path) -> EscalateAction {
fn dummy_exec_policy(file: &Path, argv: &[String], _workdir: &Path) -> ExecPolicyOutcome {
// TODO: execpolicy
if file == Path::new("/opt/homebrew/bin/gh")
if file.ends_with("/git") {
ExecPolicyOutcome {
decision: Decision::Prompt,
run_with_escalated_permissions: false,
}
} else if file == Path::new("/opt/homebrew/bin/gh")
&& let [_, arg1, arg2, ..] = argv
&& arg1 == "issue"
&& arg2 == "list"
{
return EscalateAction::Escalate;
ExecPolicyOutcome {
decision: Decision::Allow,
run_with_escalated_permissions: true,
}
} else {
ExecPolicyOutcome {
decision: Decision::Allow,
run_with_escalated_permissions: false,
}
}
EscalateAction::Run
}
#[derive(Parser)]
@@ -98,5 +98,8 @@ pub(crate) async fn run(file: String, argv: Vec<String>) -> anyhow::Result<i32>
Err(err.into())
}
EscalateAction::Deny => {
todo!("handle EscalateAction::Deny")
}
}
}
@@ -34,6 +34,8 @@ pub(super) enum EscalateAction {
Run,
/// The command should be escalated to the server for execution.
Escalate,
/// The command should be denied.
Deny,
}
/// The client sends this to the server to forward its open FDs.
@@ -12,6 +12,7 @@ use codex_core::exec::SandboxType;
use codex_core::exec::process_exec_tool_call;
use codex_core::get_platform_sandbox;
use codex_core::protocol::SandboxPolicy;
use codex_execpolicy2::Decision as ExecPolicyDecision;
use tokio::process::Command;
use crate::posix::escalate_protocol::BASH_EXEC_WRAPPER_ENV_VAR;
@@ -30,7 +31,13 @@ use crate::posix::socket::AsyncSocket;
/// `argv` is the argv, including the program name (`argv[0]`).
/// `workdir` is the absolute, canonical path to the working directory in which to execute the
/// command.
pub(crate) type ExecPolicy = fn(file: &Path, argv: &[String], workdir: &Path) -> EscalateAction;
pub(crate) type ExecPolicy = fn(file: &Path, argv: &[String], workdir: &Path) -> ExecPolicyOutcome;
#[derive(Debug)]
pub(crate) struct ExecPolicyOutcome {
pub(crate) decision: ExecPolicyDecision,
pub(crate) run_with_escalated_permissions: bool,
}
pub(crate) struct EscalateServer {
bash_path: PathBuf,
@@ -132,8 +139,27 @@ async fn handle_escalate_session_with_policy(
} = socket.receive::<EscalateRequest>().await?;
let file = PathBuf::from(&file).absolutize()?.into_owned();
let workdir = PathBuf::from(&workdir).absolutize()?.into_owned();
let action = policy(file.as_path(), &argv, &workdir);
tracing::debug!("decided {action:?} for {file:?} {argv:?} {workdir:?}");
let outcome = policy(file.as_path(), &argv, &workdir);
tracing::debug!("decided {outcome:?} for {file:?} {argv:?} {workdir:?}");
let ExecPolicyOutcome {
decision,
run_with_escalated_permissions,
} = outcome;
let action = match decision {
ExecPolicyDecision::Allow => {
if run_with_escalated_permissions {
EscalateAction::Escalate
} else {
EscalateAction::Run
}
}
ExecPolicyDecision::Prompt => {
todo!("prompting not yet implemented")
}
ExecPolicyDecision::Forbidden => EscalateAction::Deny,
};
match action {
EscalateAction::Run => {
socket
@@ -195,6 +221,9 @@ async fn handle_escalate_session_with_policy(
})
.await?;
}
EscalateAction::Deny => {
todo!("denying not yet implemented");
}
}
Ok(())
}
@@ -211,7 +240,10 @@ mod tests {
let (server, client) = AsyncSocket::pair()?;
let server_task = tokio::spawn(handle_escalate_session_with_policy(
server,
|_file, _argv, _workdir| EscalateAction::Run,
|_file, _argv, _workdir| ExecPolicyOutcome {
decision: ExecPolicyDecision::Allow,
run_with_escalated_permissions: false,
},
));
client
@@ -238,7 +270,10 @@ mod tests {
let (server, client) = AsyncSocket::pair()?;
let server_task = tokio::spawn(handle_escalate_session_with_policy(
server,
|_file, _argv, _workdir| EscalateAction::Escalate,
|_file, _argv, _workdir| ExecPolicyOutcome {
decision: ExecPolicyDecision::Allow,
run_with_escalated_permissions: true,
},
));
client