Record normalized sandbox violation events (#36207)

## Why

Filesystem denials and managed-network blocks did not share a structured event
shape, requiring downstream consumers to rediscover enforcement paths and parse
backend-specific output. See https://github.com/openai/codex/pull/17573.

## What changed

- Add normalized filesystem and network violation types in `codex-sandboxing`
  and emit them through a shared tracing seam.
- Classify filesystem denials by backend and reason, retaining an optional path
  and bounded output snippet, and preserve managed-network block context.
- Report the sandbox type through exec-server responses so unified exec can
  classify remote denials without guessing; omitted values remain compatible
  with older peers.
- Record violations from exec, apply-patch, shell-escalation, unified-exec, and
  managed-network enforcement paths without changing denial behavior.

## Testing

- Cover filesystem classification, path extraction, `SIGSYS`, network event
  conversion, protocol compatibility, and remote sandbox-type propagation.

GitOrigin-RevId: d673173b4fa6bdf0a24421194a8a61c81fab9c96
This commit is contained in:
viyatb-oai authored and copyberry committed 2026-07-30 19:23:33 +00:00
1 parent 789c72dcf6
commit 0042b00986
21 files changed
+724 -29

No files matched your search

@@ -304,6 +304,7 @@ async fn blocking_terminate_unified_process(
allow_terminate,
wake_tx,
}),
sandbox_type: Some(codex_sandboxing::SandboxType::None),
})
.await?,
))
+11 -7
View File
@@ -12,7 +12,6 @@ use tokio::task::JoinHandle;
use tokio::time::Duration;
use tokio_util::sync::CancellationToken;
use crate::exec::is_likely_sandbox_denied;
use codex_exec_server::ExecProcess;
use codex_exec_server::ExecProcessEvent;
use codex_exec_server::ProcessSignal as ExecServerProcessSignal;
@@ -23,6 +22,8 @@ use codex_protocol::exec_output::ExecToolCallOutput;
use codex_protocol::exec_output::StreamOutput;
use codex_protocol::protocol::TruncationPolicy;
use codex_sandboxing::SandboxType;
use codex_sandboxing::is_likely_sandbox_denied;
use codex_sandboxing::record_filesystem_sandbox_violation;
use codex_utils_output_truncation::formatted_truncate_text;
use codex_utils_pty::ExecCommandSession;
use codex_utils_pty::ProcessSignal as PtyProcessSignal;
@@ -315,7 +316,11 @@ impl UnifiedExecProcess {
aggregated_output: StreamOutput::new(text.to_string()),
..Default::default()
};
if executor_reported_denial || is_likely_sandbox_denied(sandbox_type, &exec_output) {
let likely_sandbox_denial = is_likely_sandbox_denied(sandbox_type, &exec_output);
if likely_sandbox_denial {
record_filesystem_sandbox_violation(sandbox_type, &exec_output);
}
if executor_reported_denial || likely_sandbox_denial {
let snippet = formatted_truncate_text(
text,
TruncationPolicy::Tokens(UNIFIED_EXEC_OUTPUT_MAX_TOKENS),
@@ -394,11 +399,10 @@ impl UnifiedExecProcess {
started: StartedExecProcess,
) -> Result<Self, UnifiedExecError> {
let process_handle = ProcessHandle::ExecServer(Arc::clone(&started.process));
let mut managed = Self::new(
process_handle,
SandboxType::None,
/*spawn_lifecycle*/ None,
);
// Older peers do not report this field. In that case, skip local
// classification rather than attributing a violation to a guessed backend.
let sandbox_type = started.sandbox_type.unwrap_or(SandboxType::None);
let mut managed = Self::new(process_handle, sandbox_type, /*spawn_lifecycle*/ None);
let output_handles = managed.output_handles();
managed.output_task = Some(Self::spawn_exec_server_output_task(
started,
@@ -512,6 +512,7 @@ async fn pruning_does_not_evict_live_process_while_exited_process_is_finalizing(
crate::unified_exec::process_tests::remote_process(
codex_exec_server::WriteStatus::Accepted,
/*terminate_error*/ None,
codex_sandboxing::SandboxType::None,
)
.await,
);
@@ -523,6 +524,7 @@ async fn pruning_does_not_evict_live_process_while_exited_process_is_finalizing(
crate::unified_exec::process_tests::remote_process(
codex_exec_server::WriteStatus::Accepted,
/*terminate_error*/ None,
codex_sandboxing::SandboxType::None,
)
.await,
);
@@ -88,6 +88,7 @@ impl ExecProcess for MockExecProcess {
pub(super) async fn remote_process(
write_status: WriteStatus,
terminate_error: Option<String>,
sandbox_type: codex_sandboxing::SandboxType,
) -> UnifiedExecProcess {
let (wake_tx, _wake_rx) = watch::channel(0);
let started = StartedExecProcess {
@@ -100,6 +101,7 @@ pub(super) async fn remote_process(
terminate_error,
wake_tx,
}),
sandbox_type: Some(sandbox_type),
};
UnifiedExecProcess::from_exec_server_started(started)
@@ -109,7 +111,12 @@ pub(super) async fn remote_process(
#[tokio::test]
async fn remote_write_unknown_process_marks_process_exited() {
let process = remote_process(WriteStatus::UnknownProcess, /*terminate_error*/ None).await;
let process = remote_process(
WriteStatus::UnknownProcess,
/*terminate_error*/ None,
codex_sandboxing::SandboxType::None,
)
.await;
let err = process
.write(b"hello")
@@ -122,7 +129,12 @@ async fn remote_write_unknown_process_marks_process_exited() {
#[tokio::test]
async fn remote_write_closed_stdin_marks_process_exited() {
let process = remote_process(WriteStatus::StdinClosed, /*terminate_error*/ None).await;
let process = remote_process(
WriteStatus::StdinClosed,
/*terminate_error*/ None,
codex_sandboxing::SandboxType::None,
)
.await;
let err = process
.write(b"hello")
@@ -135,7 +147,12 @@ async fn remote_write_closed_stdin_marks_process_exited() {
#[tokio::test]
async fn fail_and_terminate_preserves_failure_message() {
let process = remote_process(WriteStatus::Accepted, /*terminate_error*/ None).await;
let process = remote_process(
WriteStatus::Accepted,
/*terminate_error*/ None,
codex_sandboxing::SandboxType::None,
)
.await;
process.fail_and_terminate("network denied".to_string());
process.fail_and_terminate("second failure".to_string());
@@ -152,6 +169,7 @@ async fn remote_terminate_confirmed_updates_state_on_success_only() {
let process = remote_process(
WriteStatus::Accepted,
Some("terminate unavailable".to_string()),
codex_sandboxing::SandboxType::None,
)
.await;
@@ -163,7 +181,12 @@ async fn remote_terminate_confirmed_updates_state_on_success_only() {
assert!(matches!(err, UnifiedExecError::ProcessFailed { .. }));
assert!(!process.has_exited());
let process = remote_process(WriteStatus::Accepted, /*terminate_error*/ None).await;
let process = remote_process(
WriteStatus::Accepted,
/*terminate_error*/ None,
codex_sandboxing::SandboxType::None,
)
.await;
process
.terminate_confirmed()
@@ -172,3 +195,18 @@ async fn remote_terminate_confirmed_updates_state_on_success_only() {
assert!(process.has_exited());
}
#[tokio::test]
async fn remote_process_preserves_executor_sandbox_type() {
let process = remote_process(
WriteStatus::Accepted,
/*terminate_error*/ None,
codex_sandboxing::SandboxType::LinuxSeccomp,
)
.await;
assert_eq!(
process.sandbox_type(),
codex_sandboxing::SandboxType::LinuxSeccomp
);
}