mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
## Why Removing the selected working directory can prevent filesystem sandbox helpers from launching, even when the requested absolute paths remain accessible. Permission rules must stay anchored to the selected directory while those operations continue. ## What changed - Require a policy `cwd` in `FileSystemSandboxContext` and launch filesystem helpers from the filesystem root while preserving the policy directory and workspace roots. - Carry explicit `policyContext` in filesystem RPCs, preserving legacy wire fields and resolving omitted directories from older clients at executor ingress. - Keep permission paths as executor file URIs and validate host compatibility where they are enforced. - Bind Windows relative denial globs to the policy directory before changing the helper's launch directory, preserving home-relative patterns. ## Testing Add regression coverage for `apply_patch` after working-directory removal, legacy RPC directory fallbacks, cross-platform permission URI transport, and Windows relative read denials. The patch regression verifies that an allowed file is updated while an explicitly denied file remains unreadable and unchanged. GitOrigin-RevId: b0f4db722b27cb72ec129fc297c85732afac11f7
122 lines
3.9 KiB
Rust
122 lines
3.9 KiB
Rust
use bytes::Bytes;
|
|
use codex_utils_path_uri::PathUri;
|
|
use tokio::io;
|
|
use uuid::Uuid;
|
|
|
|
use super::map_remote_error;
|
|
use crate::ExecServerClient;
|
|
use crate::FILE_READ_CHUNK_SIZE;
|
|
use crate::FileSystemReadStream;
|
|
use crate::FileSystemResult;
|
|
use crate::FileSystemSandboxContext;
|
|
use crate::protocol::FS_READ_BLOCK_METHOD;
|
|
use crate::protocol::FsCloseParams;
|
|
use crate::protocol::FsOpenParams;
|
|
use crate::protocol::FsReadBlockParams;
|
|
|
|
struct FileReadRegistration {
|
|
client: ExecServerClient,
|
|
handle_id: String,
|
|
runtime: Option<tokio::runtime::Handle>,
|
|
active: bool,
|
|
}
|
|
|
|
pub(super) async fn open(
|
|
client: ExecServerClient,
|
|
path: PathUri,
|
|
sandbox: Option<&FileSystemSandboxContext>,
|
|
) -> FileSystemResult<FileSystemReadStream> {
|
|
let registration = FileReadRegistration {
|
|
client,
|
|
handle_id: Uuid::new_v4().simple().to_string(),
|
|
runtime: tokio::runtime::Handle::try_current().ok(),
|
|
active: true,
|
|
};
|
|
registration
|
|
.client
|
|
.fs_open(FsOpenParams {
|
|
handle_id: registration.handle_id.clone(),
|
|
path,
|
|
sandbox: sandbox.cloned(),
|
|
})
|
|
.await
|
|
.map_err(map_remote_error)?;
|
|
Ok(FileSystemReadStream::new(futures::stream::try_unfold(
|
|
Some((registration, 0_u64)),
|
|
|state| async move {
|
|
let Some((mut registration, offset)) = state else {
|
|
return Ok(None);
|
|
};
|
|
let response = registration
|
|
.client
|
|
.fs_read_block(FsReadBlockParams {
|
|
handle_id: registration.handle_id.clone(),
|
|
offset,
|
|
len: FILE_READ_CHUNK_SIZE,
|
|
})
|
|
.await
|
|
.map_err(map_remote_error)?;
|
|
let chunk = Bytes::from(response.chunk.into_inner());
|
|
if chunk.len() > FILE_READ_CHUNK_SIZE {
|
|
return Err(io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
format!(
|
|
"{FS_READ_BLOCK_METHOD} returned {} bytes, maximum is {}",
|
|
chunk.len(),
|
|
FILE_READ_CHUNK_SIZE
|
|
),
|
|
));
|
|
}
|
|
if response.eof {
|
|
if registration
|
|
.client
|
|
.fs_close(FsCloseParams {
|
|
handle_id: registration.handle_id.clone(),
|
|
})
|
|
.await
|
|
.is_ok()
|
|
{
|
|
registration.active = false;
|
|
}
|
|
return if chunk.is_empty() {
|
|
Ok(None)
|
|
} else {
|
|
Ok(Some((chunk, None)))
|
|
};
|
|
}
|
|
if chunk.is_empty() {
|
|
return Err(io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
format!("{FS_READ_BLOCK_METHOD} returned an empty non-terminal block"),
|
|
));
|
|
}
|
|
let next_offset = offset.checked_add(chunk.len() as u64).ok_or_else(|| {
|
|
io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
format!("{FS_READ_BLOCK_METHOD} offset overflowed after {offset} bytes"),
|
|
)
|
|
})?;
|
|
Ok(Some((chunk, Some((registration, next_offset)))))
|
|
},
|
|
)))
|
|
}
|
|
|
|
impl Drop for FileReadRegistration {
|
|
fn drop(&mut self) {
|
|
if !self.active {
|
|
return;
|
|
}
|
|
let client = self.client.clone();
|
|
let handle_id = self.handle_id.clone();
|
|
let runtime = self
|
|
.runtime
|
|
.clone()
|
|
.or_else(|| tokio::runtime::Handle::try_current().ok());
|
|
if let Some(runtime) = runtime {
|
|
runtime.spawn(async move {
|
|
let _ = client.fs_close(FsCloseParams { handle_id }).await;
|
|
});
|
|
}
|
|
}
|
|
}
|