mirror of
https://github.com/openai/codex.git
synced 2026-12-04 00:51:02 +00:00
Route blocked network requests to their owning calls (#32897)
## Why Policy-blocked proxy requests must terminate the corresponding active tool call and preserve the correct approval result, including when multiple calls are running concurrently. ## What changed - Resolve a blocked request's execution ID to its registered active network call before recording the policy denial. - Preserve an outcome already recorded for that call, and cancel the owning call after storing a new denial. ## Testing Expand network approval integration coverage for Guardian and user decisions, permission hooks, persisted policy amendments, concurrent calls, timeouts, cancellations, and unattributed proxy requests. GitOrigin-RevId: 961901bbc07cc498309453ec78cda1a56ab00a4b
This commit is contained in:
@@ -415,14 +415,6 @@ impl NetworkApprovalService {
|
||||
(created, true)
|
||||
}
|
||||
|
||||
async fn record_outcome_for_single_active_call(&self, outcome: NetworkApprovalOutcome) {
|
||||
let Some(owner_call) = self.resolve_single_active_call().await else {
|
||||
return;
|
||||
};
|
||||
self.record_call_outcome(&owner_call.registration_id, outcome)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
async fn take_call_outcome(&self, registration_id: &str) -> Option<NetworkApprovalOutcome> {
|
||||
let mut calls = self.calls.lock().await;
|
||||
@@ -467,12 +459,29 @@ impl NetworkApprovalService {
|
||||
return;
|
||||
};
|
||||
|
||||
let outcome = NetworkApprovalOutcome::DeniedByPolicy(message);
|
||||
if let Some(execution_id) = blocked.execution_id.as_deref() {
|
||||
self.record_call_outcome(execution_id, outcome).await;
|
||||
let owner_call = if let Some(execution_id) = blocked.execution_id.as_deref() {
|
||||
self.resolve_active_call_by_execution_id(execution_id).await
|
||||
} else {
|
||||
self.record_outcome_for_single_active_call(outcome).await;
|
||||
self.resolve_single_active_call().await
|
||||
};
|
||||
let Some(owner_call) = owner_call else {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut calls = self.calls.lock().await;
|
||||
if calls
|
||||
.call_outcomes
|
||||
.contains_key(&owner_call.registration_id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
calls.call_outcomes.insert(
|
||||
owner_call.registration_id.clone(),
|
||||
NetworkApprovalOutcome::DeniedByPolicy(message),
|
||||
);
|
||||
|
||||
drop(calls);
|
||||
owner_call.cancellation_token.cancel();
|
||||
}
|
||||
|
||||
async fn active_turn_context(
|
||||
|
||||
@@ -387,12 +387,15 @@ async fn record_blocked_request_sets_policy_outcome_for_owner_call() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn blocked_request_policy_does_not_override_user_denial_outcome() {
|
||||
async fn blocked_request_does_not_override_recorded_approval_outcome() {
|
||||
let service = NetworkApprovalService::default();
|
||||
register_call_with_default_shell_trigger(&service, "registration-1").await;
|
||||
|
||||
service
|
||||
.record_call_outcome("registration-1", NetworkApprovalOutcome::DeniedByUser)
|
||||
.record_call_outcome(
|
||||
"registration-1",
|
||||
NetworkApprovalOutcome::DeniedByPolicy("custom approval rejection".to_string()),
|
||||
)
|
||||
.await;
|
||||
service
|
||||
.record_blocked_request(denied_blocked_request("example.com"))
|
||||
@@ -400,7 +403,32 @@ async fn blocked_request_policy_does_not_override_user_denial_outcome() {
|
||||
|
||||
assert_eq!(
|
||||
service.take_call_outcome("registration-1").await,
|
||||
Some(NetworkApprovalOutcome::DeniedByUser)
|
||||
Some(NetworkApprovalOutcome::DeniedByPolicy(
|
||||
"custom approval rejection".to_string()
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn specific_approval_outcome_replaces_earlier_blocked_request() {
|
||||
let service = NetworkApprovalService::default();
|
||||
register_call_with_default_shell_trigger(&service, "registration-1").await;
|
||||
|
||||
service
|
||||
.record_blocked_request(denied_blocked_request("example.com"))
|
||||
.await;
|
||||
service
|
||||
.record_call_outcome(
|
||||
"registration-1",
|
||||
NetworkApprovalOutcome::DeniedByPolicy("specific approval rejection".to_string()),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
service.take_call_outcome("registration-1").await,
|
||||
Some(NetworkApprovalOutcome::DeniedByPolicy(
|
||||
"specific approval rejection".to_string()
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ regex-lite = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
similar = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
tokio = { workspace = true, features = ["net", "time"] }
|
||||
tokio = { workspace = true, features = ["net", "test-util", "time"] }
|
||||
tokio-tungstenite = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tracing-opentelemetry = { workspace = true }
|
||||
|
||||
@@ -3419,6 +3419,12 @@ allow_local_binding = true
|
||||
policy_contents.contains(&expected_rule),
|
||||
"unexpected policy contents: {policy_contents}"
|
||||
);
|
||||
assert!(first_results.requests().iter().any(|request| {
|
||||
request.body_contains_text(&format!(
|
||||
"Denied network rule saved in execpolicy (denylist): {}",
|
||||
deny_network_amendment.host
|
||||
))
|
||||
}));
|
||||
|
||||
let first_output = parse_result(
|
||||
&first_results
|
||||
|
||||
@@ -2316,6 +2316,38 @@ async fn permission_request_hook_sees_raw_exec_command_input() -> Result<()> {
|
||||
|
||||
#[tokio::test]
|
||||
async fn permission_request_hook_allows_network_approval_without_prompt() -> Result<()> {
|
||||
let command = r#"python3 -c "import urllib.request; opener = urllib.request.build_opener(urllib.request.ProxyHandler()); print('OK:' + opener.open('http://codex-network-test.invalid', timeout=2).read().decode(errors='replace'))""#;
|
||||
run_network_permission_hook_test(
|
||||
"allow",
|
||||
PERMISSION_REQUEST_ALLOW_REASON,
|
||||
"permissionrequest-network-approval",
|
||||
command,
|
||||
/*expected_denial*/ None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn permission_request_hook_denies_network_approval_with_custom_message() -> Result<()> {
|
||||
let denial = "network access denied by the integration-test hook";
|
||||
let command = r#"python3 -c "import urllib.request; opener = urllib.request.build_opener(urllib.request.ProxyHandler()); opener.open('http://codex-network-test.invalid', timeout=2).read()""#;
|
||||
run_network_permission_hook_test(
|
||||
"deny",
|
||||
denial,
|
||||
"permissionrequest-network-denied",
|
||||
command,
|
||||
Some(denial),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn run_network_permission_hook_test(
|
||||
hook_mode: &'static str,
|
||||
hook_reason: &'static str,
|
||||
call_id: &'static str,
|
||||
command: &'static str,
|
||||
expected_denial: Option<&'static str>,
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = start_mock_server().await;
|
||||
@@ -2333,21 +2365,19 @@ mode = "limited"
|
||||
allow_local_binding = true
|
||||
"#,
|
||||
)?;
|
||||
let call_id = "permissionrequest-network-approval";
|
||||
let command = r#"python3 -c "import urllib.request; opener = urllib.request.build_opener(urllib.request.ProxyHandler()); print('OK:' + opener.open('http://codex-network-test.invalid', timeout=2).read().decode(errors='replace'))""#;
|
||||
let args = serde_json::json!({ "command": command });
|
||||
let _responses = mount_sse_sequence(
|
||||
let responses = mount_sse_sequence(
|
||||
&server,
|
||||
vec![
|
||||
sse(vec![
|
||||
ev_response_created("resp-1"),
|
||||
ev_response_created("resp-network-hook-1"),
|
||||
ev_function_call(call_id, "shell_command", &serde_json::to_string(&args)?),
|
||||
ev_completed("resp-1"),
|
||||
ev_completed("resp-network-hook-1"),
|
||||
]),
|
||||
sse(vec![
|
||||
ev_response_created("resp-2"),
|
||||
ev_assistant_message("msg-1", "permission request hook allowed network access"),
|
||||
ev_completed("resp-2"),
|
||||
ev_response_created("resp-network-hook-2"),
|
||||
ev_assistant_message("msg-network-hook", "done"),
|
||||
ev_completed("resp-network-hook-2"),
|
||||
]),
|
||||
],
|
||||
)
|
||||
@@ -2358,13 +2388,19 @@ allow_local_binding = true
|
||||
let permission_profile_for_config = permission_profile.clone();
|
||||
let test = test_codex()
|
||||
.with_home(Arc::clone(&home))
|
||||
.with_pre_build_hook(|home| {
|
||||
install_allow_permission_request_hook(home)
|
||||
.expect("failed to write permission request hook test fixture");
|
||||
.with_pre_build_hook(move |home| {
|
||||
write_permission_request_hook(
|
||||
home,
|
||||
Some(PERMISSION_REQUEST_HOOK_MATCHER),
|
||||
hook_mode,
|
||||
hook_reason,
|
||||
)
|
||||
.expect("failed to write permission request hook test fixture");
|
||||
})
|
||||
.with_cloud_config_bundle(managed_network_requirements_loader())
|
||||
.with_config(move |config| {
|
||||
trust_discovered_hooks(config);
|
||||
config.approvals_reviewer = codex_config::types::ApprovalsReviewer::AutoReview;
|
||||
config.permissions.approval_policy = Constrained::allow_any(approval_policy);
|
||||
config
|
||||
.permissions
|
||||
@@ -2373,65 +2409,71 @@ allow_local_binding = true
|
||||
})
|
||||
.build(&server)
|
||||
.await?;
|
||||
assert!(
|
||||
test.config.managed_network_requirements_enabled(),
|
||||
"expected managed network requirements to be enabled"
|
||||
);
|
||||
assert!(
|
||||
test.config.permissions.network.is_some(),
|
||||
"expected managed network proxy config to be present"
|
||||
);
|
||||
test.session_configured
|
||||
.network_proxy
|
||||
.as_ref()
|
||||
.expect("expected runtime managed network proxy addresses");
|
||||
|
||||
test.submit_turn_with_approval_and_permission_profile(
|
||||
"run the shell command after network hook approval",
|
||||
"run the shell command after the network permission hook",
|
||||
approval_policy,
|
||||
permission_profile,
|
||||
)
|
||||
.await?;
|
||||
|
||||
timeout(Duration::from_secs(10), async {
|
||||
loop {
|
||||
if test
|
||||
.codex_home_path()
|
||||
.join("permission_request_hook_log.jsonl")
|
||||
.exists()
|
||||
{
|
||||
break;
|
||||
if expected_denial.is_none() {
|
||||
timeout(Duration::from_secs(10), async {
|
||||
loop {
|
||||
if test
|
||||
.codex_home_path()
|
||||
.join("permission_request_hook_log.jsonl")
|
||||
.exists()
|
||||
{
|
||||
break;
|
||||
}
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("expected network approval hook to run");
|
||||
|
||||
assert!(
|
||||
timeout(
|
||||
Duration::from_secs(2),
|
||||
wait_for_event(&test.codex, |event| matches!(
|
||||
event,
|
||||
EventMsg::ExecApprovalRequest(_)
|
||||
))
|
||||
)
|
||||
})
|
||||
.await
|
||||
.is_err(),
|
||||
"expected the network approval hook to bypass the approval prompt"
|
||||
);
|
||||
.expect("expected network approval hook to run");
|
||||
assert!(
|
||||
timeout(
|
||||
Duration::from_secs(2),
|
||||
wait_for_event(&test.codex, |event| matches!(
|
||||
event,
|
||||
EventMsg::ExecApprovalRequest(_)
|
||||
))
|
||||
)
|
||||
.await
|
||||
.is_err(),
|
||||
"expected the network approval hook to bypass the approval prompt"
|
||||
);
|
||||
}
|
||||
|
||||
assert_single_permission_request_hook_input(
|
||||
test.codex_home_path(),
|
||||
command,
|
||||
Some("network-access http://codex-network-test.invalid:80"),
|
||||
)?;
|
||||
|
||||
test.codex.submit(Op::Shutdown {}).await?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::ShutdownComplete)
|
||||
})
|
||||
.await;
|
||||
let requests = responses.requests();
|
||||
assert_eq!(
|
||||
requests
|
||||
.iter()
|
||||
.filter(|request| {
|
||||
request.body_json()["client_metadata"]["x-openai-subagent"].as_str()
|
||||
== Some("guardian")
|
||||
})
|
||||
.count(),
|
||||
0
|
||||
);
|
||||
if let Some(expected_denial) = expected_denial {
|
||||
let tool_output = requests
|
||||
.iter()
|
||||
.find_map(|request| request.function_call_output_text(call_id))
|
||||
.expect("expected denied tool output");
|
||||
assert!(tool_output.contains(expected_denial));
|
||||
} else {
|
||||
test.codex.submit(Op::Shutdown {}).await?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::ShutdownComplete)
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user