mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Preserve local path conventions in automatic approvals (#32261)
## Why Drive-shaped POSIX paths such as `/C:/workspace` can be mistaken for Windows paths when an automatic approval request converts them from `PathUri`. ## What changed - Keep approval paths as `PathUri` values together with their environment ID until the automatic review request is built. - Recover host-native paths for local working directories when strict conversion cannot determine the path convention. - Continue rejecting incompatible paths from remote environments. ## Testing Add coverage for drive-shaped local POSIX paths, foreign remote paths, and preserving apply-patch path URIs while building approval actions. GitOrigin-RevId: 3fd31cfb4e8b46f9968ff66d5e986ca777430185
This commit is contained in:
1 parent
9993fb838a
commit
b66c25c6a3
8 files changed
+166
-39
No files matched your search
@@ -1060,9 +1060,10 @@ async fn danger_full_access_tool_attempts_do_not_enforce_managed_network() -> an
|
|||||||
) -> std::io::Result<crate::tools::sandboxing::ApprovalAction> {
|
) -> std::io::Result<crate::tools::sandboxing::ApprovalAction> {
|
||||||
Ok(crate::tools::sandboxing::ApprovalAction::Shell {
|
Ok(crate::tools::sandboxing::ApprovalAction::Shell {
|
||||||
id: ctx.call_id.to_string(),
|
id: ctx.call_id.to_string(),
|
||||||
|
environment_id: codex_exec_server::LOCAL_ENVIRONMENT_ID.to_string(),
|
||||||
command: Vec::new(),
|
command: Vec::new(),
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
cwd: ctx.turn.cwd.clone(),
|
cwd: PathUri::from_abs_path(&ctx.turn.cwd),
|
||||||
sandbox_permissions: crate::sandboxing::SandboxPermissions::UseDefault,
|
sandbox_permissions: crate::sandboxing::SandboxPermissions::UseDefault,
|
||||||
additional_permissions: None,
|
additional_permissions: None,
|
||||||
justification: None,
|
justification: None,
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use crate::guardian::new_guardian_review_id;
|
|||||||
use crate::guardian::review_approval_request;
|
use crate::guardian::review_approval_request;
|
||||||
use crate::guardian::routes_approval_to_guardian_with_reviewer;
|
use crate::guardian::routes_approval_to_guardian_with_reviewer;
|
||||||
use crate::hook_runtime::run_permission_request_hooks;
|
use crate::hook_runtime::run_permission_request_hooks;
|
||||||
|
use crate::sandboxing::SandboxPermissions;
|
||||||
use crate::session::session::Session;
|
use crate::session::session::Session;
|
||||||
use crate::session::turn_context::TurnContext;
|
use crate::session::turn_context::TurnContext;
|
||||||
use crate::tools::flat_tool_name;
|
use crate::tools::flat_tool_name;
|
||||||
@@ -18,10 +19,119 @@ use crate::tools::sandboxing::ToolRuntime;
|
|||||||
use codex_config::types::ApprovalsReviewer;
|
use codex_config::types::ApprovalsReviewer;
|
||||||
use codex_hooks::PermissionRequestDecision;
|
use codex_hooks::PermissionRequestDecision;
|
||||||
use codex_otel::ToolDecisionSource;
|
use codex_otel::ToolDecisionSource;
|
||||||
|
use codex_protocol::models::AdditionalPermissionProfile;
|
||||||
use codex_protocol::protocol::NetworkPolicyRuleAction;
|
use codex_protocol::protocol::NetworkPolicyRuleAction;
|
||||||
use codex_protocol::protocol::ReviewDecision;
|
use codex_protocol::protocol::ReviewDecision;
|
||||||
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||||
|
use codex_utils_path_uri::PathUri;
|
||||||
|
|
||||||
pub(super) type ApprovalAction = crate::guardian::GuardianApprovalRequest;
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
|
pub(crate) enum ApprovalAction {
|
||||||
|
Shell {
|
||||||
|
id: String,
|
||||||
|
environment_id: String,
|
||||||
|
command: Vec<String>,
|
||||||
|
cwd: PathUri,
|
||||||
|
sandbox_permissions: SandboxPermissions,
|
||||||
|
additional_permissions: Option<AdditionalPermissionProfile>,
|
||||||
|
justification: Option<String>,
|
||||||
|
},
|
||||||
|
ExecCommand {
|
||||||
|
id: String,
|
||||||
|
environment_id: String,
|
||||||
|
command: Vec<String>,
|
||||||
|
cwd: PathUri,
|
||||||
|
sandbox_permissions: SandboxPermissions,
|
||||||
|
additional_permissions: Option<AdditionalPermissionProfile>,
|
||||||
|
justification: Option<String>,
|
||||||
|
tty: bool,
|
||||||
|
},
|
||||||
|
ApplyPatch {
|
||||||
|
id: String,
|
||||||
|
environment_id: String,
|
||||||
|
cwd: PathUri,
|
||||||
|
files: Vec<PathUri>,
|
||||||
|
patch: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ApprovalAction {
|
||||||
|
fn into_guardian_request(self) -> std::io::Result<crate::guardian::GuardianApprovalRequest> {
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
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::<std::io::Result<Vec<_>>>()?,
|
||||||
|
patch,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn guardian_cwd(environment_id: &str, cwd: PathUri) -> std::io::Result<AbsolutePathBuf> {
|
||||||
|
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)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub(super) enum ApprovalReviewer {
|
pub(super) enum ApprovalReviewer {
|
||||||
@@ -113,7 +223,10 @@ where
|
|||||||
let resolution = match reviewer {
|
let resolution = match reviewer {
|
||||||
ApprovalReviewer::Guardian => {
|
ApprovalReviewer::Guardian => {
|
||||||
let review_id = new_guardian_review_id();
|
let review_id = new_guardian_review_id();
|
||||||
let action = match tool.approval_action(req, &ctx) {
|
let action = match tool
|
||||||
|
.approval_action(req, &ctx)
|
||||||
|
.and_then(ApprovalAction::into_guardian_request)
|
||||||
|
{
|
||||||
Ok(action) => action,
|
Ok(action) => action,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
tracing::error!(%err, "failed to build automatic approval action");
|
tracing::error!(%err, "failed to build automatic approval action");
|
||||||
@@ -211,3 +324,7 @@ fn record_resolution(
|
|||||||
source,
|
source,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(all(test, unix))]
|
||||||
|
#[path = "approvals_tests.rs"]
|
||||||
|
mod tests;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
use super::*;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn guardian_cwd_preserves_drive_shaped_local_posix_path() {
|
||||||
|
let native_cwd = AbsolutePathBuf::try_from(std::path::PathBuf::from("/C:/workspace"))
|
||||||
|
.expect("drive-shaped POSIX path should be absolute");
|
||||||
|
let cwd = PathUri::from_abs_path(&native_cwd);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
guardian_cwd(codex_exec_server::LOCAL_ENVIRONMENT_ID, cwd)
|
||||||
|
.expect("local cwd should retain the host path convention"),
|
||||||
|
native_cwd
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn guardian_cwd_rejects_foreign_remote_path() {
|
||||||
|
let cwd = PathUri::parse("file:///C:/workspace").expect("valid Windows path URI");
|
||||||
|
|
||||||
|
assert!(guardian_cwd(codex_exec_server::REMOTE_ENVIRONMENT_ID, cwd).is_err());
|
||||||
|
}
|
||||||
@@ -73,23 +73,14 @@ impl ApplyPatchRuntime {
|
|||||||
&self.committed_delta
|
&self.committed_delta
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_guardian_review_request(
|
fn build_approval_action(req: &ApplyPatchRequest, call_id: &str) -> ApprovalAction {
|
||||||
req: &ApplyPatchRequest,
|
ApprovalAction::ApplyPatch {
|
||||||
call_id: &str,
|
|
||||||
) -> std::io::Result<ApprovalAction> {
|
|
||||||
// TODO(anp): Remove this conversion once the guardian API supports PathUri.
|
|
||||||
let cwd = req.action.cwd.to_abs_path()?;
|
|
||||||
let files = req
|
|
||||||
.file_paths
|
|
||||||
.iter()
|
|
||||||
.map(PathUri::to_abs_path)
|
|
||||||
.collect::<std::io::Result<Vec<_>>>()?;
|
|
||||||
Ok(ApprovalAction::ApplyPatch {
|
|
||||||
id: call_id.to_string(),
|
id: call_id.to_string(),
|
||||||
cwd,
|
environment_id: req.turn_environment.environment_id.clone(),
|
||||||
files,
|
cwd: req.action.cwd.clone(),
|
||||||
|
files: req.file_paths.clone(),
|
||||||
patch: req.action.patch.clone(),
|
patch: req.action.patch.clone(),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn file_system_sandbox_context_for_attempt(
|
fn file_system_sandbox_context_for_attempt(
|
||||||
@@ -188,7 +179,7 @@ impl Approvable<ApplyPatchRequest> for ApplyPatchRuntime {
|
|||||||
req: &ApplyPatchRequest,
|
req: &ApplyPatchRequest,
|
||||||
ctx: &ApprovalCtx<'_>,
|
ctx: &ApprovalCtx<'_>,
|
||||||
) -> std::io::Result<ApprovalAction> {
|
) -> std::io::Result<ApprovalAction> {
|
||||||
ApplyPatchRuntime::build_guardian_review_request(req, ctx.call_id)
|
Ok(ApplyPatchRuntime::build_approval_action(req, ctx.call_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wants_no_sandbox_approval(&self, policy: AskForApproval) -> bool {
|
fn wants_no_sandbox_approval(&self, policy: AskForApproval) -> bool {
|
||||||
|
|||||||
@@ -49,24 +49,17 @@ fn wants_no_sandbox_approval_granular_respects_sandbox_flag() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn guardian_review_request_includes_patch_context() {
|
async fn approval_action_preserves_patch_path_uris() {
|
||||||
let path = std::env::temp_dir()
|
let path = PathUri::parse("file:///C:/workspace/guardian-apply-patch-test.txt")
|
||||||
.join("guardian-apply-patch-test.txt")
|
.expect("valid foreign path URI");
|
||||||
.abs();
|
let action = ApplyPatchAction::new_add_for_test(&path, "hello".to_string());
|
||||||
let action =
|
let expected_cwd = action.cwd.clone();
|
||||||
ApplyPatchAction::new_add_for_test(&PathUri::from_abs_path(&path), "hello".to_string());
|
|
||||||
let expected_cwd = action.cwd.to_abs_path().expect("native patch cwd");
|
|
||||||
let expected_patch = action.patch.clone();
|
let expected_patch = action.patch.clone();
|
||||||
let request = ApplyPatchRequest {
|
let request = ApplyPatchRequest {
|
||||||
turn_environment: test_turn_environment(codex_exec_server::LOCAL_ENVIRONMENT_ID),
|
turn_environment: test_turn_environment(codex_exec_server::LOCAL_ENVIRONMENT_ID),
|
||||||
action,
|
action,
|
||||||
file_paths: vec![PathUri::from_abs_path(&path)],
|
file_paths: vec![path.clone()],
|
||||||
changes: HashMap::from([(
|
changes: HashMap::new(),
|
||||||
path.to_path_buf(),
|
|
||||||
FileChange::Add {
|
|
||||||
content: "hello".to_string(),
|
|
||||||
},
|
|
||||||
)]),
|
|
||||||
exec_approval_requirement: ExecApprovalRequirement::NeedsApproval {
|
exec_approval_requirement: ExecApprovalRequirement::NeedsApproval {
|
||||||
reason: None,
|
reason: None,
|
||||||
proposed_execpolicy_amendment: None,
|
proposed_execpolicy_amendment: None,
|
||||||
@@ -75,13 +68,13 @@ async fn guardian_review_request_includes_patch_context() {
|
|||||||
permissions_preapproved: false,
|
permissions_preapproved: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let guardian_request = ApplyPatchRuntime::build_guardian_review_request(&request, "call-1")
|
let approval_action = ApplyPatchRuntime::build_approval_action(&request, "call-1");
|
||||||
.expect("native guardian request cwd");
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
guardian_request,
|
approval_action,
|
||||||
ApprovalAction::ApplyPatch {
|
ApprovalAction::ApplyPatch {
|
||||||
id: "call-1".to_string(),
|
id: "call-1".to_string(),
|
||||||
|
environment_id: codex_exec_server::LOCAL_ENVIRONMENT_ID.to_string(),
|
||||||
cwd: expected_cwd,
|
cwd: expected_cwd,
|
||||||
files: vec![path],
|
files: vec![path],
|
||||||
patch: expected_patch,
|
patch: expected_patch,
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ use codex_protocol::protocol::ReviewDecision;
|
|||||||
use codex_sandboxing::SandboxablePreference;
|
use codex_sandboxing::SandboxablePreference;
|
||||||
use codex_shell_command::powershell::prefix_powershell_script_with_utf8;
|
use codex_shell_command::powershell::prefix_powershell_script_with_utf8;
|
||||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||||
|
use codex_utils_path_uri::PathUri;
|
||||||
use futures::future::BoxFuture;
|
use futures::future::BoxFuture;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
@@ -183,8 +184,9 @@ impl Approvable<ShellRequest> for ShellRuntime {
|
|||||||
) -> std::io::Result<ApprovalAction> {
|
) -> std::io::Result<ApprovalAction> {
|
||||||
Ok(ApprovalAction::Shell {
|
Ok(ApprovalAction::Shell {
|
||||||
id: ctx.call_id.to_string(),
|
id: ctx.call_id.to_string(),
|
||||||
|
environment_id: req.turn_environment.environment_id.clone(),
|
||||||
command: req.command.clone(),
|
command: req.command.clone(),
|
||||||
cwd: req.cwd.clone(),
|
cwd: PathUri::from_abs_path(&req.cwd),
|
||||||
sandbox_permissions: req.sandbox_permissions,
|
sandbox_permissions: req.sandbox_permissions,
|
||||||
additional_permissions: req.additional_permissions.clone(),
|
additional_permissions: req.additional_permissions.clone(),
|
||||||
justification: req.justification.clone(),
|
justification: req.justification.clone(),
|
||||||
|
|||||||
@@ -223,8 +223,9 @@ impl Approvable<UnifiedExecRequest> for UnifiedExecRuntime<'_> {
|
|||||||
) -> std::io::Result<ApprovalAction> {
|
) -> std::io::Result<ApprovalAction> {
|
||||||
Ok(ApprovalAction::ExecCommand {
|
Ok(ApprovalAction::ExecCommand {
|
||||||
id: ctx.call_id.to_string(),
|
id: ctx.call_id.to_string(),
|
||||||
|
environment_id: req.turn_environment.environment_id.clone(),
|
||||||
command: req.command.clone(),
|
command: req.command.clone(),
|
||||||
cwd: req.cwd.to_abs_path()?,
|
cwd: req.cwd.clone(),
|
||||||
sandbox_permissions: req.sandbox_permissions,
|
sandbox_permissions: req.sandbox_permissions,
|
||||||
additional_permissions: req.additional_permissions.clone(),
|
additional_permissions: req.additional_permissions.clone(),
|
||||||
justification: req.justification.clone(),
|
justification: req.justification.clone(),
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ pub(crate) struct ApprovalCtx<'a> {
|
|||||||
pub network_approval_context: Option<NetworkApprovalContext>,
|
pub network_approval_context: Option<NetworkApprovalContext>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) type ApprovalAction = super::approvals::ApprovalAction;
|
pub(crate) use super::approvals::ApprovalAction;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub(crate) struct PermissionRequestPayload {
|
pub(crate) struct PermissionRequestPayload {
|
||||||
|
|||||||
Reference in new issue
Block a user