Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ceb3692804 | ||
|
|
46abd7900d | ||
|
|
5fc174076a | ||
|
|
8bcc5e4957 |
No files matched your search
Generated
+2
@@ -2900,6 +2900,7 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serial_test",
|
||||
"sha2 0.10.9",
|
||||
"tempfile",
|
||||
"test-case",
|
||||
"thiserror 2.0.18",
|
||||
@@ -2911,6 +2912,7 @@ dependencies = [
|
||||
"uuid",
|
||||
"windows-sys 0.52.0",
|
||||
"wiremock",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -33,6 +33,8 @@ reqwest = { workspace = true, features = ["json", "rustls-tls", "stream"] }
|
||||
prost = "0.14.3"
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
zeroize = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
toml = { workspace = true }
|
||||
tokio = { workspace = true, features = [
|
||||
|
||||
@@ -48,6 +48,10 @@ use crate::protocol::ExecExitedNotification;
|
||||
use crate::protocol::ExecOutputDeltaNotification;
|
||||
use crate::protocol::ExecParams;
|
||||
use crate::protocol::ExecResponse;
|
||||
use crate::protocol::FILE_TRANSFER_CANCEL_METHOD;
|
||||
use crate::protocol::FILE_TRANSFER_PREPARE_UPLOAD_METHOD;
|
||||
use crate::protocol::FILE_TRANSFER_START_UPLOAD_METHOD;
|
||||
use crate::protocol::FILE_TRANSFER_STATUS_METHOD;
|
||||
use crate::protocol::FS_CANONICALIZE_METHOD;
|
||||
use crate::protocol::FS_CLOSE_METHOD;
|
||||
use crate::protocol::FS_COPY_METHOD;
|
||||
@@ -59,6 +63,14 @@ use crate::protocol::FS_READ_DIRECTORY_METHOD;
|
||||
use crate::protocol::FS_READ_FILE_METHOD;
|
||||
use crate::protocol::FS_REMOVE_METHOD;
|
||||
use crate::protocol::FS_WRITE_FILE_METHOD;
|
||||
use crate::protocol::FileTransferCancelParams;
|
||||
use crate::protocol::FileTransferCancelResponse;
|
||||
use crate::protocol::FileTransferPrepareUploadParams;
|
||||
use crate::protocol::FileTransferPrepareUploadResponse;
|
||||
use crate::protocol::FileTransferStartUploadParams;
|
||||
use crate::protocol::FileTransferStartUploadResponse;
|
||||
use crate::protocol::FileTransferStatusParams;
|
||||
use crate::protocol::FileTransferStatusResponse;
|
||||
use crate::protocol::FsCanonicalizeParams;
|
||||
use crate::protocol::FsCanonicalizeResponse;
|
||||
use crate::protocol::FsCloseParams;
|
||||
@@ -406,6 +418,34 @@ impl LazyRemoteExecServerClient {
|
||||
pub(crate) async fn environment_info(&self) -> Result<EnvironmentInfo, ExecServerError> {
|
||||
self.get().await?.environment_info().await
|
||||
}
|
||||
|
||||
pub(crate) async fn file_transfer_prepare_upload(
|
||||
&self,
|
||||
params: FileTransferPrepareUploadParams,
|
||||
) -> Result<FileTransferPrepareUploadResponse, ExecServerError> {
|
||||
self.get().await?.file_transfer_prepare_upload(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn file_transfer_start_upload(
|
||||
&self,
|
||||
params: FileTransferStartUploadParams,
|
||||
) -> Result<FileTransferStartUploadResponse, ExecServerError> {
|
||||
self.get().await?.file_transfer_start_upload(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn file_transfer_status(
|
||||
&self,
|
||||
transfer_id: String,
|
||||
) -> Result<FileTransferStatusResponse, ExecServerError> {
|
||||
self.get().await?.file_transfer_status(transfer_id).await
|
||||
}
|
||||
|
||||
pub(crate) async fn file_transfer_cancel(
|
||||
&self,
|
||||
transfer_id: String,
|
||||
) -> Result<FileTransferCancelResponse, ExecServerError> {
|
||||
self.get().await?.file_transfer_cancel(transfer_id).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
@@ -509,6 +549,43 @@ impl ExecServerClient {
|
||||
self.call(ENVIRONMENT_INFO_METHOD, &()).await
|
||||
}
|
||||
|
||||
pub async fn file_transfer_prepare_upload(
|
||||
&self,
|
||||
params: FileTransferPrepareUploadParams,
|
||||
) -> Result<FileTransferPrepareUploadResponse, ExecServerError> {
|
||||
self.call(FILE_TRANSFER_PREPARE_UPLOAD_METHOD, ¶ms)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn file_transfer_start_upload(
|
||||
&self,
|
||||
params: FileTransferStartUploadParams,
|
||||
) -> Result<FileTransferStartUploadResponse, ExecServerError> {
|
||||
self.call(FILE_TRANSFER_START_UPLOAD_METHOD, ¶ms).await
|
||||
}
|
||||
|
||||
pub async fn file_transfer_status(
|
||||
&self,
|
||||
transfer_id: String,
|
||||
) -> Result<FileTransferStatusResponse, ExecServerError> {
|
||||
self.call(
|
||||
FILE_TRANSFER_STATUS_METHOD,
|
||||
&FileTransferStatusParams { transfer_id },
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn file_transfer_cancel(
|
||||
&self,
|
||||
transfer_id: String,
|
||||
) -> Result<FileTransferCancelResponse, ExecServerError> {
|
||||
self.call(
|
||||
FILE_TRANSFER_CANCEL_METHOD,
|
||||
&FileTransferCancelParams { transfer_id },
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn read(&self, params: ReadParams) -> Result<ReadResponse, ExecServerError> {
|
||||
self.call(EXEC_READ_METHOD, ¶ms).await
|
||||
}
|
||||
|
||||
@@ -22,7 +22,15 @@ use crate::environment_toml::environment_provider_from_codex_home;
|
||||
use crate::local_file_system::LocalFileSystem;
|
||||
use crate::local_process::LocalProcess;
|
||||
use crate::process::ExecBackend;
|
||||
use crate::protocol::EnvironmentCapabilities;
|
||||
use crate::protocol::EnvironmentInfo;
|
||||
use crate::protocol::FileTransferCancelResponse;
|
||||
use crate::protocol::FileTransferPrepareUploadParams;
|
||||
use crate::protocol::FileTransferPrepareUploadResponse;
|
||||
use crate::protocol::FileTransferStartUploadParams;
|
||||
use crate::protocol::FileTransferStartUploadResponse;
|
||||
use crate::protocol::FileTransferStatusResponse;
|
||||
use crate::protocol::PreparedFileUploadCapability;
|
||||
use crate::protocol::ShellInfo;
|
||||
use crate::remote::NoiseRendezvousEnvironmentConfig;
|
||||
use crate::remote_file_system::RemoteFileSystem;
|
||||
@@ -561,6 +569,54 @@ impl Environment {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn file_transfer_prepare_upload(
|
||||
&self,
|
||||
params: FileTransferPrepareUploadParams,
|
||||
) -> Result<FileTransferPrepareUploadResponse, ExecServerError> {
|
||||
let client = self.remote_client.as_ref().ok_or_else(|| {
|
||||
ExecServerError::Protocol(
|
||||
"executor-owned file transfer requires a remote environment".to_string(),
|
||||
)
|
||||
})?;
|
||||
client.file_transfer_prepare_upload(params).await
|
||||
}
|
||||
|
||||
pub async fn file_transfer_start_upload(
|
||||
&self,
|
||||
params: FileTransferStartUploadParams,
|
||||
) -> Result<FileTransferStartUploadResponse, ExecServerError> {
|
||||
let client = self.remote_client.as_ref().ok_or_else(|| {
|
||||
ExecServerError::Protocol(
|
||||
"executor-owned file transfer requires a remote environment".to_string(),
|
||||
)
|
||||
})?;
|
||||
client.file_transfer_start_upload(params).await
|
||||
}
|
||||
|
||||
pub async fn file_transfer_status(
|
||||
&self,
|
||||
transfer_id: String,
|
||||
) -> Result<FileTransferStatusResponse, ExecServerError> {
|
||||
let client = self.remote_client.as_ref().ok_or_else(|| {
|
||||
ExecServerError::Protocol(
|
||||
"executor-owned file transfer requires a remote environment".to_string(),
|
||||
)
|
||||
})?;
|
||||
client.file_transfer_status(transfer_id).await
|
||||
}
|
||||
|
||||
pub async fn file_transfer_cancel(
|
||||
&self,
|
||||
transfer_id: String,
|
||||
) -> Result<FileTransferCancelResponse, ExecServerError> {
|
||||
let client = self.remote_client.as_ref().ok_or_else(|| {
|
||||
ExecServerError::Protocol(
|
||||
"executor-owned file transfer requires a remote environment".to_string(),
|
||||
)
|
||||
})?;
|
||||
client.file_transfer_cancel(transfer_id).await
|
||||
}
|
||||
|
||||
/// Starts connecting a remote environment without waiting for it.
|
||||
/// Requires an active Tokio runtime when background startup is supported.
|
||||
pub fn start_connecting(&self) {
|
||||
@@ -608,6 +664,16 @@ impl EnvironmentInfo {
|
||||
pub(crate) fn local() -> Self {
|
||||
Self {
|
||||
shell: codex_shell_command::shell_detect::default_user_shell().into(),
|
||||
capabilities: EnvironmentCapabilities::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn executor(prepared_file_upload: Option<PreparedFileUploadCapability>) -> Self {
|
||||
Self {
|
||||
shell: codex_shell_command::shell_detect::default_user_shell().into(),
|
||||
capabilities: EnvironmentCapabilities {
|
||||
prepared_file_upload,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +190,11 @@ pub(crate) async fn run_direct_request(
|
||||
match request {
|
||||
FsHelperRequest::ReadFile(params) => {
|
||||
let data = file_system
|
||||
.read_file(¶ms.path, /*sandbox*/ None)
|
||||
.read_file_with_limit(
|
||||
¶ms.path,
|
||||
/*sandbox*/ None,
|
||||
params.max_bytes.unwrap_or(512 * 1024 * 1024),
|
||||
)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsHelperPayload::ReadFile(FsReadFileResponse {
|
||||
|
||||
@@ -80,6 +80,7 @@ pub use process::ExecProcessFuture;
|
||||
pub use process::StartedExecProcess;
|
||||
pub use process_id::ProcessId;
|
||||
pub use protocol::ByteChunk;
|
||||
pub use protocol::EnvironmentCapabilities;
|
||||
pub use protocol::EnvironmentInfo;
|
||||
pub use protocol::ExecClosedNotification;
|
||||
pub use protocol::ExecEnvPolicy;
|
||||
@@ -88,6 +89,20 @@ pub use protocol::ExecOutputDeltaNotification;
|
||||
pub use protocol::ExecOutputStream;
|
||||
pub use protocol::ExecParams;
|
||||
pub use protocol::ExecResponse;
|
||||
pub use protocol::FileTransferCancelParams;
|
||||
pub use protocol::FileTransferCancelResponse;
|
||||
pub use protocol::FileTransferDigest;
|
||||
pub use protocol::FileTransferDigestAlgorithm;
|
||||
pub use protocol::FileTransferHeader;
|
||||
pub use protocol::FileTransferOperationState;
|
||||
pub use protocol::FileTransferPrepareUploadParams;
|
||||
pub use protocol::FileTransferPrepareUploadResponse;
|
||||
pub use protocol::FileTransferStartUploadParams;
|
||||
pub use protocol::FileTransferStartUploadResponse;
|
||||
pub use protocol::FileTransferStatusParams;
|
||||
pub use protocol::FileTransferStatusResponse;
|
||||
pub use protocol::FileTransferUploadDescriptor;
|
||||
pub use protocol::FileTransferUploadDescriptorKind;
|
||||
pub use protocol::FsCanonicalizeParams;
|
||||
pub use protocol::FsCanonicalizeResponse;
|
||||
pub use protocol::FsCloseParams;
|
||||
@@ -117,6 +132,9 @@ pub use protocol::HttpRequestParams;
|
||||
pub use protocol::HttpRequestResponse;
|
||||
pub use protocol::InitializeParams;
|
||||
pub use protocol::InitializeResponse;
|
||||
pub use protocol::MAX_PREPARED_FILE_UPLOAD_BYTES;
|
||||
pub use protocol::PREPARED_FILE_UPLOAD_PROTOCOL_VERSION;
|
||||
pub use protocol::PreparedFileUploadCapability;
|
||||
pub use protocol::ProcessOutputChunk;
|
||||
pub use protocol::ProcessSignal;
|
||||
pub use protocol::ReadParams;
|
||||
@@ -131,6 +149,7 @@ pub use protocol::WriteResponse;
|
||||
pub use protocol::WriteStatus;
|
||||
pub use remote::RemoteEnvironmentConfig;
|
||||
pub use remote::run_remote_environment;
|
||||
pub use rpc::FILE_TRANSFER_SESSION_LOST_ERROR_CODE;
|
||||
pub use runtime_paths::ExecServerRuntimePaths;
|
||||
pub use server::DEFAULT_LISTEN_URL;
|
||||
pub use server::ExecServerListenUrlParseError;
|
||||
|
||||
@@ -27,10 +27,10 @@ use crate::sandboxed_file_system::SandboxedFileSystem;
|
||||
|
||||
const MAX_READ_FILE_BYTES: u64 = 512 * 1024 * 1024;
|
||||
|
||||
fn file_too_large_error() -> io::Error {
|
||||
fn file_too_large_error(max_bytes: u64) -> io::Error {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
format!("file is too large to read: limit is {MAX_READ_FILE_BYTES} bytes"),
|
||||
format!("file is too large to read: limit is {max_bytes} bytes"),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -88,6 +88,24 @@ impl LocalFileSystem {
|
||||
Ok((&self.unsandboxed, sandbox))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn read_file_with_limit(
|
||||
&self,
|
||||
path: &PathUri,
|
||||
sandbox: Option<&FileSystemSandboxContext>,
|
||||
max_bytes: u64,
|
||||
) -> FileSystemResult<Vec<u8>> {
|
||||
let max_bytes = max_bytes.min(MAX_READ_FILE_BYTES);
|
||||
if sandbox.is_some_and(FileSystemSandboxContext::should_run_in_sandbox) {
|
||||
self.sandboxed()?
|
||||
.read_file_with_limit(path, sandbox, max_bytes)
|
||||
.await
|
||||
} else {
|
||||
self.unsandboxed
|
||||
.read_file_with_limit(path, sandbox, max_bytes)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LocalFileSystem {
|
||||
@@ -119,8 +137,8 @@ impl LocalFileSystem {
|
||||
path: &PathUri,
|
||||
sandbox: Option<&FileSystemSandboxContext>,
|
||||
) -> FileSystemResult<Vec<u8>> {
|
||||
let (file_system, sandbox) = self.file_system_for(sandbox)?;
|
||||
file_system.read_file(path, sandbox).await
|
||||
self.read_file_with_limit(path, sandbox, MAX_READ_FILE_BYTES)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn read_file_stream(
|
||||
@@ -306,9 +324,21 @@ impl UnsandboxedFileSystem {
|
||||
&self,
|
||||
path: &PathUri,
|
||||
sandbox: Option<&FileSystemSandboxContext>,
|
||||
) -> FileSystemResult<Vec<u8>> {
|
||||
self.read_file_with_limit(path, sandbox, MAX_READ_FILE_BYTES)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn read_file_with_limit(
|
||||
&self,
|
||||
path: &PathUri,
|
||||
sandbox: Option<&FileSystemSandboxContext>,
|
||||
max_bytes: u64,
|
||||
) -> FileSystemResult<Vec<u8>> {
|
||||
reject_platform_sandbox_context(sandbox)?;
|
||||
self.file_system.read_file(path, /*sandbox*/ None).await
|
||||
self.file_system
|
||||
.read_file_with_limit(path, /*sandbox*/ None, max_bytes)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn read_file_stream(
|
||||
@@ -514,17 +544,26 @@ impl DirectFileSystem {
|
||||
path: &PathUri,
|
||||
sandbox: Option<&FileSystemSandboxContext>,
|
||||
) -> FileSystemResult<Vec<u8>> {
|
||||
self.read_file_with_limit(path, sandbox, MAX_READ_FILE_BYTES)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn read_file_with_limit(
|
||||
&self,
|
||||
path: &PathUri,
|
||||
sandbox: Option<&FileSystemSandboxContext>,
|
||||
max_bytes: u64,
|
||||
) -> FileSystemResult<Vec<u8>> {
|
||||
let max_bytes = max_bytes.min(MAX_READ_FILE_BYTES);
|
||||
let file = self.open_file_for_read(path, sandbox).await?;
|
||||
let metadata = file.metadata().await?;
|
||||
if metadata.len() > MAX_READ_FILE_BYTES {
|
||||
return Err(file_too_large_error());
|
||||
if metadata.len() > max_bytes {
|
||||
return Err(file_too_large_error(max_bytes));
|
||||
}
|
||||
let mut bytes = Vec::with_capacity(metadata.len() as usize);
|
||||
file.take(MAX_READ_FILE_BYTES + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.await?;
|
||||
if bytes.len() as u64 > MAX_READ_FILE_BYTES {
|
||||
return Err(file_too_large_error());
|
||||
file.take(max_bytes + 1).read_to_end(&mut bytes).await?;
|
||||
if bytes.len() as u64 > max_bytes {
|
||||
return Err(file_too_large_error(max_bytes));
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
@@ -897,6 +936,33 @@ fn system_time_to_unix_ms(time: SystemTime) -> i64 {
|
||||
#[path = "local_file_system_path_uri_tests.rs"]
|
||||
mod path_uri_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod bounded_read_tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn direct_read_file_with_limit_rejects_bytes_past_the_boundary() -> io::Result<()> {
|
||||
let temp_dir = tempfile::TempDir::new()?;
|
||||
let path = temp_dir.path().join("bounded.txt");
|
||||
tokio::fs::write(&path, b"four").await?;
|
||||
let path = PathUri::from_path(&path).expect("temporary path should be absolute");
|
||||
let file_system = DirectFileSystem;
|
||||
|
||||
let error = file_system
|
||||
.read_file_with_limit(&path, /*sandbox*/ None, /*max_bytes*/ 3)
|
||||
.await
|
||||
.expect_err("file should exceed the requested byte boundary");
|
||||
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
|
||||
assert_eq!(
|
||||
file_system
|
||||
.read_file_with_limit(&path, /*sandbox*/ None, /*max_bytes*/ 4)
|
||||
.await?,
|
||||
b"four"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -35,6 +35,10 @@ pub const FS_COPY_METHOD: &str = "fs/copy";
|
||||
pub const HTTP_REQUEST_METHOD: &str = "http/request";
|
||||
/// JSON-RPC notification method for streamed executor HTTP response bodies.
|
||||
pub const HTTP_REQUEST_BODY_DELTA_METHOD: &str = "http/request/bodyDelta";
|
||||
pub const FILE_TRANSFER_PREPARE_UPLOAD_METHOD: &str = "fileTransfer/prepareUpload";
|
||||
pub const FILE_TRANSFER_START_UPLOAD_METHOD: &str = "fileTransfer/startUpload";
|
||||
pub const FILE_TRANSFER_STATUS_METHOD: &str = "fileTransfer/status";
|
||||
pub const FILE_TRANSFER_CANCEL_METHOD: &str = "fileTransfer/cancel";
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
@@ -71,6 +75,166 @@ pub struct InitializeResponse {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EnvironmentInfo {
|
||||
pub shell: ShellInfo,
|
||||
#[serde(default)]
|
||||
pub capabilities: EnvironmentCapabilities,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct EnvironmentCapabilities {
|
||||
/// Executor-owned upload support. This primitive is intended for trusted
|
||||
/// orchestration code that performs approval before preparing a source and
|
||||
/// again after presenting the exact name, size, and digest to the user.
|
||||
#[serde(default)]
|
||||
pub prepared_file_upload: Option<PreparedFileUploadCapability>,
|
||||
}
|
||||
|
||||
pub const PREPARED_FILE_UPLOAD_PROTOCOL_VERSION: u32 = 1;
|
||||
pub const MAX_PREPARED_FILE_UPLOAD_BYTES: u64 = 8 * 1024 * 1024;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PreparedFileUploadCapability {
|
||||
pub protocol_version: u32,
|
||||
pub max_upload_bytes: u64,
|
||||
pub descriptor_kinds: Vec<FileTransferUploadDescriptorKind>,
|
||||
pub supports_status_reconciliation: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum FileTransferUploadDescriptorKind {
|
||||
HttpsPut,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum FileTransferDigestAlgorithm {
|
||||
Sha256,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferDigest {
|
||||
pub algorithm: FileTransferDigestAlgorithm,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferPrepareUploadParams {
|
||||
pub path: PathUri,
|
||||
/// Authoritative permission context selected by the trusted caller. This
|
||||
/// may intentionally be full access after the caller's read approval.
|
||||
pub sandbox: FileSystemSandboxContext,
|
||||
pub max_bytes: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferPrepareUploadResponse {
|
||||
/// Opaque, session-bound identifier used for the entire lifecycle.
|
||||
pub transfer_id: String,
|
||||
pub name: String,
|
||||
pub size: u64,
|
||||
pub digest: FileTransferDigest,
|
||||
/// The snapshot's upper-bound wall-clock expiry. The session may expire
|
||||
/// sooner when its transport remains detached.
|
||||
pub expires_at_unix_seconds: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferHeader {
|
||||
pub name: String,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for FileTransferHeader {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter
|
||||
.debug_struct("FileTransferHeader")
|
||||
.field("name", &self.name)
|
||||
.field("value", &"[REDACTED]")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", rename_all = "camelCase")]
|
||||
pub enum FileTransferUploadDescriptor {
|
||||
HttpsPut {
|
||||
url: String,
|
||||
#[serde(default)]
|
||||
headers: Vec<FileTransferHeader>,
|
||||
expires_at_unix_seconds: i64,
|
||||
},
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for FileTransferUploadDescriptor {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::HttpsPut { headers, .. } => formatter
|
||||
.debug_struct("HttpsPut")
|
||||
.field("url", &"[REDACTED]")
|
||||
.field("header_count", &headers.len())
|
||||
.field("expires_at_unix_seconds", &"[REDACTED]")
|
||||
.finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferStartUploadParams {
|
||||
pub transfer_id: String,
|
||||
pub descriptor: FileTransferUploadDescriptor,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferStartUploadResponse {
|
||||
pub transfer_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum FileTransferOperationState {
|
||||
Prepared,
|
||||
Uploading,
|
||||
CancelRequested,
|
||||
Succeeded,
|
||||
Failed,
|
||||
Canceled,
|
||||
CompletionUnknown,
|
||||
Expired,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferStatusParams {
|
||||
pub transfer_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferStatusResponse {
|
||||
pub transfer_id: String,
|
||||
pub state: FileTransferOperationState,
|
||||
#[serde(default)]
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferCancelParams {
|
||||
pub transfer_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FileTransferCancelResponse {
|
||||
pub state: FileTransferOperationState,
|
||||
}
|
||||
|
||||
/// Shell detected for an execution/filesystem environment.
|
||||
@@ -212,6 +376,8 @@ pub struct TerminateResponse {
|
||||
pub struct FsReadFileParams {
|
||||
pub path: PathUri,
|
||||
pub sandbox: Option<FileSystemSandboxContext>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub max_bytes: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
@@ -501,6 +667,10 @@ mod base64_bytes {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::EnvironmentCapabilities;
|
||||
use super::EnvironmentInfo;
|
||||
use super::FileTransferHeader;
|
||||
use super::FileTransferUploadDescriptor;
|
||||
use super::FsReadFileParams;
|
||||
use super::HttpRequestParams;
|
||||
use codex_file_system::FileSystemSandboxContext;
|
||||
@@ -508,6 +678,33 @@ mod tests {
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn environment_info_accepts_legacy_payload_without_capabilities() {
|
||||
let info: EnvironmentInfo = serde_json::from_value(serde_json::json!({
|
||||
"shell": {"name": "sh", "path": "/bin/sh"},
|
||||
}))
|
||||
.expect("legacy environment info should deserialize");
|
||||
|
||||
assert_eq!(info.capabilities, EnvironmentCapabilities::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upload_descriptor_debug_redacts_secrets() {
|
||||
let descriptor = FileTransferUploadDescriptor::HttpsPut {
|
||||
url: "https://account.blob.core.windows.net/object?sig=secret".to_string(),
|
||||
headers: vec![FileTransferHeader {
|
||||
name: "x-ms-meta-token".to_string(),
|
||||
value: "secret-header".to_string(),
|
||||
}],
|
||||
expires_at_unix_seconds: 123,
|
||||
};
|
||||
|
||||
let debug = format!("{descriptor:?}");
|
||||
assert!(!debug.contains("sig=secret"));
|
||||
assert!(!debug.contains("secret-header"));
|
||||
assert!(debug.contains("header_count"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesystem_protocol_accepts_legacy_absolute_paths_and_serializes_path_uris() {
|
||||
let legacy_path = std::env::current_dir()
|
||||
@@ -530,6 +727,7 @@ mod tests {
|
||||
let expected = FsReadFileParams {
|
||||
path: PathUri::from_path(legacy_path).expect("path URI"),
|
||||
sandbox: Some(expected_sandbox.clone()),
|
||||
max_bytes: None,
|
||||
};
|
||||
|
||||
assert_eq!(params, expected);
|
||||
|
||||
@@ -69,6 +69,7 @@ impl RemoteFileSystem {
|
||||
.fs_read_file(FsReadFileParams {
|
||||
path: path.clone(),
|
||||
sandbox: remote_sandbox_context(sandbox),
|
||||
max_bytes: None,
|
||||
})
|
||||
.await
|
||||
.map_err(map_remote_error)?;
|
||||
|
||||
@@ -73,6 +73,7 @@ async fn remote_file_system_sends_path_and_sandbox_cwd_uris_without_native_conve
|
||||
.map(|path| FsReadFileParams {
|
||||
path,
|
||||
sandbox: Some(sandbox.clone()),
|
||||
max_bytes: None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
|
||||
@@ -27,6 +27,7 @@ use crate::connection::JsonRpcConnectionEvent;
|
||||
use crate::connection::JsonRpcTransport;
|
||||
|
||||
pub(crate) const SESSION_ALREADY_ATTACHED_ERROR_CODE: i64 = -32010;
|
||||
pub const FILE_TRANSFER_SESSION_LOST_ERROR_CODE: i64 = -32011;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum RpcCallError {
|
||||
@@ -440,6 +441,14 @@ pub(crate) fn session_already_attached(message: String) -> JSONRPCErrorError {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn file_transfer_session_lost(message: String) -> JSONRPCErrorError {
|
||||
JSONRPCErrorError {
|
||||
code: FILE_TRANSFER_SESSION_LOST_ERROR_CODE,
|
||||
data: None,
|
||||
message,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn method_not_found(message: String) -> JSONRPCErrorError {
|
||||
JSONRPCErrorError {
|
||||
code: -32601,
|
||||
|
||||
@@ -77,6 +77,16 @@ impl SandboxedFileSystem {
|
||||
&self,
|
||||
path: &PathUri,
|
||||
sandbox: Option<&FileSystemSandboxContext>,
|
||||
) -> FileSystemResult<Vec<u8>> {
|
||||
self.read_file_with_limit(path, sandbox, 512 * 1024 * 1024)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn read_file_with_limit(
|
||||
&self,
|
||||
path: &PathUri,
|
||||
sandbox: Option<&FileSystemSandboxContext>,
|
||||
max_bytes: u64,
|
||||
) -> FileSystemResult<Vec<u8>> {
|
||||
let sandbox = require_platform_sandbox(sandbox)?;
|
||||
validate_native_path(path)?;
|
||||
@@ -86,6 +96,7 @@ impl SandboxedFileSystem {
|
||||
FsHelperRequest::ReadFile(FsReadFileParams {
|
||||
path: path.clone(),
|
||||
sandbox: None,
|
||||
max_bytes: Some(max_bytes),
|
||||
}),
|
||||
)
|
||||
.await?
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
mod file_system_handler;
|
||||
mod file_transfer_handler;
|
||||
mod file_transfer_http;
|
||||
mod handler;
|
||||
mod process_handler;
|
||||
mod processor;
|
||||
|
||||
@@ -108,7 +108,11 @@ impl FileSystemHandler {
|
||||
) -> Result<FsReadFileResponse, JSONRPCErrorError> {
|
||||
let bytes = self
|
||||
.file_system
|
||||
.read_file(¶ms.path, params.sandbox.as_ref())
|
||||
.read_file_with_limit(
|
||||
¶ms.path,
|
||||
params.sandbox.as_ref(),
|
||||
params.max_bytes.unwrap_or(512 * 1024 * 1024),
|
||||
)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
Ok(FsReadFileResponse {
|
||||
@@ -322,6 +326,7 @@ mod tests {
|
||||
.read_file(FsReadFileParams {
|
||||
path,
|
||||
sandbox: Some(sandbox_context(sandbox_policy)),
|
||||
max_bytes: None,
|
||||
})
|
||||
.await
|
||||
.expect("read file");
|
||||
|
||||
@@ -0,0 +1,526 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::time::SystemTime;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
|
||||
use codex_app_server_protocol::JSONRPCErrorError;
|
||||
use sha2::Digest;
|
||||
use sha2::Sha256;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::Notify;
|
||||
use tokio::time::Instant;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tokio_util::task::TaskTracker;
|
||||
use uuid::Uuid;
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
use crate::ExecServerRuntimePaths;
|
||||
use crate::local_file_system::LocalFileSystem;
|
||||
use crate::protocol::FileTransferCancelParams;
|
||||
use crate::protocol::FileTransferCancelResponse;
|
||||
use crate::protocol::FileTransferDigest;
|
||||
use crate::protocol::FileTransferDigestAlgorithm;
|
||||
use crate::protocol::FileTransferOperationState;
|
||||
use crate::protocol::FileTransferPrepareUploadParams;
|
||||
use crate::protocol::FileTransferPrepareUploadResponse;
|
||||
use crate::protocol::FileTransferStartUploadParams;
|
||||
use crate::protocol::FileTransferStartUploadResponse;
|
||||
use crate::protocol::FileTransferStatusParams;
|
||||
use crate::protocol::FileTransferStatusResponse;
|
||||
use crate::protocol::FileTransferUploadDescriptorKind;
|
||||
use crate::protocol::MAX_PREPARED_FILE_UPLOAD_BYTES;
|
||||
use crate::protocol::PREPARED_FILE_UPLOAD_PROTOCOL_VERSION;
|
||||
use crate::protocol::PreparedFileUploadCapability;
|
||||
use crate::rpc::file_transfer_session_lost;
|
||||
use crate::rpc::internal_error;
|
||||
use crate::rpc::invalid_params;
|
||||
use crate::rpc::invalid_request;
|
||||
use crate::rpc::not_found;
|
||||
use crate::server::file_transfer_http::UploadOutcome;
|
||||
use crate::server::file_transfer_http::upload_bytes;
|
||||
use crate::server::file_transfer_http::validate_upload_descriptor;
|
||||
|
||||
pub(crate) const FILE_TRANSFER_ENABLED_ENV_VAR: &str =
|
||||
"CODEX_EXEC_SERVER_PREPARED_FILE_UPLOAD_ENABLED";
|
||||
const MAX_PREPARED_BYTES_PER_SESSION: u64 = 32 * 1024 * 1024;
|
||||
const MAX_OPERATIONS_PER_SESSION: usize = 32;
|
||||
const MAX_ACTIVE_UPLOADS_PER_SESSION: usize = 2;
|
||||
#[cfg(test)]
|
||||
const PREPARED_UPLOAD_TTL: Duration = Duration::from_millis(100);
|
||||
#[cfg(not(test))]
|
||||
const PREPARED_UPLOAD_TTL: Duration = Duration::from_secs(10 * 60);
|
||||
const TERMINAL_RESULT_TTL: Duration = Duration::from_secs(10 * 60);
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum PreparedFileUploadAvailability {
|
||||
Disabled,
|
||||
EnabledForDevelopment,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct FileTransferHandler {
|
||||
inner: Arc<Inner>,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
session_generation_id: String,
|
||||
availability: PreparedFileUploadAvailability,
|
||||
file_system: LocalFileSystem,
|
||||
operations: Mutex<HashMap<String, UploadOperation>>,
|
||||
tasks: TaskTracker,
|
||||
shutdown: CancellationToken,
|
||||
expiry_changed: Notify,
|
||||
}
|
||||
|
||||
struct UploadOperation {
|
||||
deadline: Instant,
|
||||
bytes: Option<Zeroizing<Vec<u8>>>,
|
||||
state: FileTransferOperationState,
|
||||
error: Option<String>,
|
||||
terminal_at: Option<Instant>,
|
||||
cancellation: CancellationToken,
|
||||
}
|
||||
|
||||
impl FileTransferHandler {
|
||||
pub(crate) fn new(
|
||||
runtime_paths: ExecServerRuntimePaths,
|
||||
availability: PreparedFileUploadAvailability,
|
||||
) -> Self {
|
||||
let handler = Self {
|
||||
inner: Arc::new(Inner {
|
||||
// This tag distinguishes logical session generations without
|
||||
// exposing the resume session ID, which is a bearer secret.
|
||||
session_generation_id: Uuid::new_v4().to_string(),
|
||||
availability,
|
||||
file_system: LocalFileSystem::with_runtime_paths(runtime_paths),
|
||||
operations: Mutex::new(HashMap::new()),
|
||||
tasks: TaskTracker::new(),
|
||||
shutdown: CancellationToken::new(),
|
||||
expiry_changed: Notify::new(),
|
||||
}),
|
||||
};
|
||||
handler.start_expiry_sweeper();
|
||||
handler
|
||||
}
|
||||
|
||||
pub(crate) fn capability(&self) -> Option<PreparedFileUploadCapability> {
|
||||
matches!(
|
||||
self.inner.availability,
|
||||
PreparedFileUploadAvailability::EnabledForDevelopment
|
||||
)
|
||||
.then_some(PreparedFileUploadCapability {
|
||||
protocol_version: PREPARED_FILE_UPLOAD_PROTOCOL_VERSION,
|
||||
max_upload_bytes: MAX_PREPARED_FILE_UPLOAD_BYTES,
|
||||
descriptor_kinds: vec![FileTransferUploadDescriptorKind::HttpsPut],
|
||||
supports_status_reconciliation: true,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn prepare_upload(
|
||||
&self,
|
||||
params: FileTransferPrepareUploadParams,
|
||||
) -> Result<FileTransferPrepareUploadResponse, JSONRPCErrorError> {
|
||||
self.require_enabled()?;
|
||||
if params.max_bytes == 0 || params.max_bytes > MAX_PREPARED_FILE_UPLOAD_BYTES {
|
||||
return Err(invalid_params(format!(
|
||||
"file transfer maxBytes must be between 1 and {MAX_PREPARED_FILE_UPLOAD_BYTES}"
|
||||
)));
|
||||
}
|
||||
self.prune_operations().await;
|
||||
{
|
||||
let operations = self.inner.operations.lock().await;
|
||||
ensure_prepare_quota(&operations, /*additional_bytes*/ 0)?;
|
||||
}
|
||||
|
||||
let bytes = self
|
||||
.inner
|
||||
.file_system
|
||||
.read_file_with_limit(¶ms.path, Some(¶ms.sandbox), params.max_bytes)
|
||||
.await
|
||||
.map_err(map_prepare_error)?;
|
||||
let size = bytes.len() as u64;
|
||||
let name = params
|
||||
.path
|
||||
.basename()
|
||||
.filter(|name| !name.is_empty())
|
||||
.unwrap_or_else(|| "upload".to_string());
|
||||
let digest = FileTransferDigest {
|
||||
algorithm: FileTransferDigestAlgorithm::Sha256,
|
||||
value: URL_SAFE_NO_PAD.encode(Sha256::digest(&bytes)),
|
||||
};
|
||||
let transfer_id = format!("{}:{}", self.inner.session_generation_id, Uuid::new_v4());
|
||||
let deadline = Instant::now() + PREPARED_UPLOAD_TTL;
|
||||
let expires_at_unix_seconds = unix_seconds(SystemTime::now() + PREPARED_UPLOAD_TTL);
|
||||
let operation = UploadOperation {
|
||||
deadline,
|
||||
bytes: Some(Zeroizing::new(bytes)),
|
||||
state: FileTransferOperationState::Prepared,
|
||||
error: None,
|
||||
terminal_at: None,
|
||||
cancellation: CancellationToken::new(),
|
||||
};
|
||||
|
||||
let mut operations = self.inner.operations.lock().await;
|
||||
ensure_prepare_quota(&operations, size)?;
|
||||
operations.insert(transfer_id.clone(), operation);
|
||||
drop(operations);
|
||||
self.inner.expiry_changed.notify_one();
|
||||
|
||||
Ok(FileTransferPrepareUploadResponse {
|
||||
transfer_id,
|
||||
name,
|
||||
size,
|
||||
digest,
|
||||
expires_at_unix_seconds,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn start_upload(
|
||||
&self,
|
||||
params: FileTransferStartUploadParams,
|
||||
) -> Result<FileTransferStartUploadResponse, JSONRPCErrorError> {
|
||||
self.require_enabled()?;
|
||||
self.validate_transfer_id(¶ms.transfer_id)?;
|
||||
{
|
||||
let mut operations = self.inner.operations.lock().await;
|
||||
let active_upload_count = active_uploads(&operations);
|
||||
let operation = operations
|
||||
.get_mut(¶ms.transfer_id)
|
||||
.ok_or_else(|| not_found("unknown prepared upload".to_string()))?;
|
||||
expire_operation(operation, Instant::now());
|
||||
if operation.state != FileTransferOperationState::Prepared {
|
||||
return Err(invalid_request(format!(
|
||||
"prepared upload is {}",
|
||||
state_name(operation.state)
|
||||
)));
|
||||
}
|
||||
if active_upload_count >= MAX_ACTIVE_UPLOADS_PER_SESSION {
|
||||
return Err(invalid_request(
|
||||
"active file upload quota exceeded".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
let descriptor = validate_upload_descriptor(params.descriptor).await?;
|
||||
let (bytes, cancellation) = {
|
||||
let mut operations = self.inner.operations.lock().await;
|
||||
let active_upload_count = active_uploads(&operations);
|
||||
let operation = operations
|
||||
.get_mut(¶ms.transfer_id)
|
||||
.ok_or_else(|| not_found("unknown prepared upload".to_string()))?;
|
||||
expire_operation(operation, Instant::now());
|
||||
if operation.state != FileTransferOperationState::Prepared {
|
||||
return Err(invalid_request(format!(
|
||||
"prepared upload is {}",
|
||||
state_name(operation.state)
|
||||
)));
|
||||
}
|
||||
if active_upload_count >= MAX_ACTIVE_UPLOADS_PER_SESSION {
|
||||
return Err(invalid_request(
|
||||
"active file upload quota exceeded".to_string(),
|
||||
));
|
||||
}
|
||||
let bytes = operation
|
||||
.bytes
|
||||
.take()
|
||||
.ok_or_else(|| internal_error("prepared upload bytes are missing".to_string()))?;
|
||||
operation.state = FileTransferOperationState::Uploading;
|
||||
(bytes, operation.cancellation.clone())
|
||||
};
|
||||
self.inner.expiry_changed.notify_one();
|
||||
|
||||
let transfer_id = params.transfer_id;
|
||||
let handler = self.clone();
|
||||
let task_transfer_id = transfer_id.clone();
|
||||
let _task = self.inner.tasks.spawn(async move {
|
||||
let outcome = upload_bytes(bytes, descriptor, cancellation).await;
|
||||
handler.finish_upload(&task_transfer_id, outcome).await;
|
||||
});
|
||||
|
||||
Ok(FileTransferStartUploadResponse { transfer_id })
|
||||
}
|
||||
|
||||
pub(crate) async fn status(
|
||||
&self,
|
||||
params: FileTransferStatusParams,
|
||||
) -> Result<FileTransferStatusResponse, JSONRPCErrorError> {
|
||||
self.require_enabled()?;
|
||||
self.validate_transfer_id(¶ms.transfer_id)?;
|
||||
let mut operations = self.inner.operations.lock().await;
|
||||
let operation = operations
|
||||
.get_mut(¶ms.transfer_id)
|
||||
.ok_or_else(|| not_found("unknown file transfer operation".to_string()))?;
|
||||
expire_operation(operation, Instant::now());
|
||||
Ok(status_response(¶ms.transfer_id, operation))
|
||||
}
|
||||
|
||||
pub(crate) async fn cancel(
|
||||
&self,
|
||||
params: FileTransferCancelParams,
|
||||
) -> Result<FileTransferCancelResponse, JSONRPCErrorError> {
|
||||
self.require_enabled()?;
|
||||
self.validate_transfer_id(¶ms.transfer_id)?;
|
||||
let mut operations = self.inner.operations.lock().await;
|
||||
let operation = operations
|
||||
.get_mut(¶ms.transfer_id)
|
||||
.ok_or_else(|| not_found("unknown file transfer operation".to_string()))?;
|
||||
expire_operation(operation, Instant::now());
|
||||
match operation.state {
|
||||
FileTransferOperationState::Prepared => {
|
||||
operation.bytes = None;
|
||||
set_terminal(
|
||||
operation,
|
||||
FileTransferOperationState::Canceled,
|
||||
/*error*/ None,
|
||||
);
|
||||
self.inner.expiry_changed.notify_one();
|
||||
}
|
||||
FileTransferOperationState::Uploading => {
|
||||
operation.cancellation.cancel();
|
||||
operation.state = FileTransferOperationState::CancelRequested;
|
||||
operation.error = None;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(FileTransferCancelResponse {
|
||||
state: operation.state,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn shutdown(&self) {
|
||||
self.inner.shutdown.cancel();
|
||||
self.inner.tasks.close();
|
||||
{
|
||||
let mut operations = self.inner.operations.lock().await;
|
||||
for operation in operations.values_mut() {
|
||||
operation.cancellation.cancel();
|
||||
operation.bytes = None;
|
||||
if matches!(
|
||||
operation.state,
|
||||
FileTransferOperationState::Uploading
|
||||
| FileTransferOperationState::CancelRequested
|
||||
) {
|
||||
set_terminal(
|
||||
operation,
|
||||
FileTransferOperationState::CompletionUnknown,
|
||||
Some("upload completion was lost with the executor session".to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.inner.tasks.wait().await;
|
||||
}
|
||||
|
||||
async fn finish_upload(&self, transfer_id: &str, outcome: UploadOutcome) {
|
||||
let mut operations = self.inner.operations.lock().await;
|
||||
let Some(operation) = operations.get_mut(transfer_id) else {
|
||||
return;
|
||||
};
|
||||
match outcome {
|
||||
UploadOutcome::Succeeded => set_terminal(
|
||||
operation,
|
||||
FileTransferOperationState::Succeeded,
|
||||
/*error*/ None,
|
||||
),
|
||||
UploadOutcome::Failed(error) => {
|
||||
set_terminal(operation, FileTransferOperationState::Failed, Some(error))
|
||||
}
|
||||
UploadOutcome::CompletionUnknown(error) => set_terminal(
|
||||
operation,
|
||||
FileTransferOperationState::CompletionUnknown,
|
||||
Some(error),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn require_enabled(&self) -> Result<(), JSONRPCErrorError> {
|
||||
if matches!(
|
||||
self.inner.availability,
|
||||
PreparedFileUploadAvailability::EnabledForDevelopment
|
||||
) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(invalid_request(
|
||||
"prepared file upload is disabled on this executor".to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_transfer_id(&self, transfer_id: &str) -> Result<(), JSONRPCErrorError> {
|
||||
let Some((session_generation_id, opaque_id)) = transfer_id.split_once(':') else {
|
||||
return Err(invalid_params("invalid file transfer id".to_string()));
|
||||
};
|
||||
if session_generation_id != self.inner.session_generation_id {
|
||||
return Err(file_transfer_session_lost(
|
||||
"file transfer belongs to an expired executor session".to_string(),
|
||||
));
|
||||
}
|
||||
if Uuid::parse_str(opaque_id).is_err() {
|
||||
return Err(invalid_params("invalid file transfer id".to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn start_expiry_sweeper(&self) {
|
||||
let inner = Arc::clone(&self.inner);
|
||||
let tasks = self.inner.tasks.clone();
|
||||
let _task = tasks.spawn(async move {
|
||||
loop {
|
||||
let notified = inner.expiry_changed.notified();
|
||||
tokio::pin!(notified);
|
||||
notified.as_mut().enable();
|
||||
let next_deadline = inner
|
||||
.operations
|
||||
.lock()
|
||||
.await
|
||||
.values()
|
||||
.filter(|operation| operation.state == FileTransferOperationState::Prepared)
|
||||
.map(|operation| operation.deadline)
|
||||
.min();
|
||||
match next_deadline {
|
||||
Some(deadline) => tokio::select! {
|
||||
_ = tokio::time::sleep_until(deadline) => {
|
||||
let now = Instant::now();
|
||||
let mut operations = inner.operations.lock().await;
|
||||
for operation in operations.values_mut() {
|
||||
expire_operation(operation, now);
|
||||
}
|
||||
}
|
||||
_ = &mut notified => {}
|
||||
_ = inner.shutdown.cancelled() => break,
|
||||
},
|
||||
None => tokio::select! {
|
||||
_ = &mut notified => {}
|
||||
_ = inner.shutdown.cancelled() => break,
|
||||
},
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async fn prune_operations(&self) {
|
||||
let now = Instant::now();
|
||||
let mut operations = self.inner.operations.lock().await;
|
||||
for operation in operations.values_mut() {
|
||||
expire_operation(operation, now);
|
||||
}
|
||||
operations.retain(|_, operation| {
|
||||
operation
|
||||
.terminal_at
|
||||
.is_none_or(|terminal_at| now.duration_since(terminal_at) < TERMINAL_RESULT_TTL)
|
||||
});
|
||||
while operations.len() >= MAX_OPERATIONS_PER_SESSION {
|
||||
let oldest_terminal = operations
|
||||
.iter()
|
||||
.filter_map(|(id, operation)| operation.terminal_at.map(|at| (id.clone(), at)))
|
||||
.min_by_key(|(_, at)| *at)
|
||||
.map(|(id, _)| id);
|
||||
let Some(oldest_terminal) = oldest_terminal else {
|
||||
break;
|
||||
};
|
||||
operations.remove(&oldest_terminal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ensure_prepare_quota(
|
||||
operations: &HashMap<String, UploadOperation>,
|
||||
additional_bytes: u64,
|
||||
) -> Result<(), JSONRPCErrorError> {
|
||||
if operations.len() >= MAX_OPERATIONS_PER_SESSION {
|
||||
return Err(invalid_request(
|
||||
"file transfer operation quota exceeded".to_string(),
|
||||
));
|
||||
}
|
||||
if prepared_bytes(operations).saturating_add(additional_bytes) > MAX_PREPARED_BYTES_PER_SESSION
|
||||
{
|
||||
return Err(invalid_request(
|
||||
"prepared upload session quota exceeded".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prepared_bytes(operations: &HashMap<String, UploadOperation>) -> u64 {
|
||||
operations
|
||||
.values()
|
||||
.filter_map(|operation| operation.bytes.as_ref())
|
||||
.map(|bytes| bytes.len() as u64)
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn active_uploads(operations: &HashMap<String, UploadOperation>) -> usize {
|
||||
operations
|
||||
.values()
|
||||
.filter(|operation| {
|
||||
matches!(
|
||||
operation.state,
|
||||
FileTransferOperationState::Uploading | FileTransferOperationState::CancelRequested
|
||||
)
|
||||
})
|
||||
.count()
|
||||
}
|
||||
|
||||
fn status_response(transfer_id: &str, operation: &UploadOperation) -> FileTransferStatusResponse {
|
||||
FileTransferStatusResponse {
|
||||
transfer_id: transfer_id.to_string(),
|
||||
state: operation.state,
|
||||
error: operation.error.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn expire_operation(operation: &mut UploadOperation, now: Instant) {
|
||||
if operation.state == FileTransferOperationState::Prepared && now >= operation.deadline {
|
||||
operation.bytes = None;
|
||||
set_terminal(
|
||||
operation,
|
||||
FileTransferOperationState::Expired,
|
||||
/*error*/ None,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_terminal(
|
||||
operation: &mut UploadOperation,
|
||||
state: FileTransferOperationState,
|
||||
error: Option<String>,
|
||||
) {
|
||||
operation.state = state;
|
||||
operation.error = error;
|
||||
operation.terminal_at = Some(Instant::now());
|
||||
}
|
||||
|
||||
fn state_name(state: FileTransferOperationState) -> &'static str {
|
||||
match state {
|
||||
FileTransferOperationState::Prepared => "prepared",
|
||||
FileTransferOperationState::Uploading => "uploading",
|
||||
FileTransferOperationState::CancelRequested => "cancel requested",
|
||||
FileTransferOperationState::Succeeded => "succeeded",
|
||||
FileTransferOperationState::Failed => "failed",
|
||||
FileTransferOperationState::Canceled => "canceled",
|
||||
FileTransferOperationState::CompletionUnknown => "completion unknown",
|
||||
FileTransferOperationState::Expired => "expired",
|
||||
}
|
||||
}
|
||||
|
||||
fn unix_seconds(time: SystemTime) -> i64 {
|
||||
time.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_secs() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn map_prepare_error(error: std::io::Error) -> JSONRPCErrorError {
|
||||
match error.kind() {
|
||||
std::io::ErrorKind::NotFound => not_found("upload source was not found".to_string()),
|
||||
std::io::ErrorKind::InvalidInput | std::io::ErrorKind::PermissionDenied => {
|
||||
invalid_params("upload source is unavailable or exceeds the byte limit".to_string())
|
||||
}
|
||||
_ => internal_error("failed to prepare upload source".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "file_transfer_handler_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,370 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_file_system::FileSystemSandboxContext;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tokio::time::timeout;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
use wiremock::ResponseTemplate;
|
||||
use wiremock::matchers::body_bytes;
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
use super::*;
|
||||
use crate::protocol::FileTransferHeader;
|
||||
use crate::protocol::FileTransferUploadDescriptor;
|
||||
use crate::rpc::FILE_TRANSFER_SESSION_LOST_ERROR_CODE;
|
||||
use crate::server::file_transfer_http::validate_upload_descriptor;
|
||||
|
||||
fn test_runtime_paths() -> ExecServerRuntimePaths {
|
||||
ExecServerRuntimePaths::new(
|
||||
std::env::current_exe().expect("current exe"),
|
||||
/*codex_linux_sandbox_exe*/ None,
|
||||
)
|
||||
.expect("runtime paths")
|
||||
}
|
||||
|
||||
fn test_handler() -> FileTransferHandler {
|
||||
FileTransferHandler::new(
|
||||
test_runtime_paths(),
|
||||
PreparedFileUploadAvailability::EnabledForDevelopment,
|
||||
)
|
||||
}
|
||||
|
||||
fn prepare_params(path: &std::path::Path, max_bytes: u64) -> FileTransferPrepareUploadParams {
|
||||
FileTransferPrepareUploadParams {
|
||||
path: PathUri::from_path(path).expect("path URI"),
|
||||
sandbox: full_access_context(),
|
||||
max_bytes,
|
||||
}
|
||||
}
|
||||
|
||||
fn full_access_context() -> FileSystemSandboxContext {
|
||||
FileSystemSandboxContext::from_permission_profile(PermissionProfile::Disabled)
|
||||
}
|
||||
|
||||
fn upload_descriptor(url: String) -> FileTransferUploadDescriptor {
|
||||
FileTransferUploadDescriptor::HttpsPut {
|
||||
url,
|
||||
headers: Vec::new(),
|
||||
expires_at_unix_seconds: unix_seconds(SystemTime::now() + Duration::from_secs(60)),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn prepared_upload_uses_stable_executor_owned_bytes() {
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("PUT"))
|
||||
.and(path("/upload"))
|
||||
.and(body_bytes(b"prepared bytes".as_slice()))
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let source_dir = tempfile::tempdir().expect("source tempdir");
|
||||
let source = source_dir.path().join("report.txt");
|
||||
tokio::fs::write(&source, b"prepared bytes")
|
||||
.await
|
||||
.expect("write source");
|
||||
let handler = test_handler();
|
||||
let prepared = handler
|
||||
.prepare_upload(prepare_params(&source, /*max_bytes*/ 1024))
|
||||
.await
|
||||
.expect("prepare upload");
|
||||
assert_eq!(prepared.name, "report.txt");
|
||||
assert_eq!(prepared.size, 14);
|
||||
assert_eq!(
|
||||
prepared.digest.algorithm,
|
||||
FileTransferDigestAlgorithm::Sha256
|
||||
);
|
||||
|
||||
tokio::fs::write(&source, b"different bytes")
|
||||
.await
|
||||
.expect("mutate source");
|
||||
let started = handler
|
||||
.start_upload(FileTransferStartUploadParams {
|
||||
transfer_id: prepared.transfer_id.clone(),
|
||||
descriptor: upload_descriptor(format!("{}/upload", server.uri())),
|
||||
})
|
||||
.await
|
||||
.expect("start upload");
|
||||
assert_eq!(started.transfer_id, prepared.transfer_id);
|
||||
|
||||
let status = timeout(Duration::from_secs(2), async {
|
||||
loop {
|
||||
let status = handler
|
||||
.status(FileTransferStatusParams {
|
||||
transfer_id: started.transfer_id.clone(),
|
||||
})
|
||||
.await
|
||||
.expect("upload status");
|
||||
if status.state != FileTransferOperationState::Uploading {
|
||||
break status;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("upload should finish");
|
||||
assert_eq!(
|
||||
status,
|
||||
FileTransferStatusResponse {
|
||||
transfer_id: started.transfer_id.clone(),
|
||||
state: FileTransferOperationState::Succeeded,
|
||||
error: None,
|
||||
}
|
||||
);
|
||||
|
||||
let replay = handler
|
||||
.start_upload(FileTransferStartUploadParams {
|
||||
transfer_id: started.transfer_id,
|
||||
descriptor: upload_descriptor(format!("{}/replay", server.uri())),
|
||||
})
|
||||
.await
|
||||
.expect_err("prepared upload is single use");
|
||||
assert_eq!(replay.code, -32600);
|
||||
handler.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn prepare_enforces_byte_limit_and_cancel_drops_snapshot() {
|
||||
let source_dir = tempfile::tempdir().expect("source tempdir");
|
||||
let source = source_dir.path().join("report.txt");
|
||||
tokio::fs::write(&source, b"nine-byte")
|
||||
.await
|
||||
.expect("write source");
|
||||
let handler = test_handler();
|
||||
|
||||
let oversized = handler
|
||||
.prepare_upload(prepare_params(&source, /*max_bytes*/ 8))
|
||||
.await
|
||||
.expect_err("source exceeds requested limit");
|
||||
assert_eq!(oversized.code, -32602);
|
||||
|
||||
let prepared = handler
|
||||
.prepare_upload(prepare_params(&source, /*max_bytes*/ 1024))
|
||||
.await
|
||||
.expect("prepare upload");
|
||||
let canceled = handler
|
||||
.cancel(FileTransferCancelParams {
|
||||
transfer_id: prepared.transfer_id.clone(),
|
||||
})
|
||||
.await
|
||||
.expect("cancel prepared upload");
|
||||
assert_eq!(canceled.state, FileTransferOperationState::Canceled);
|
||||
let status = handler
|
||||
.status(FileTransferStatusParams {
|
||||
transfer_id: prepared.transfer_id.clone(),
|
||||
})
|
||||
.await
|
||||
.expect("canceled status");
|
||||
assert_eq!(status.state, FileTransferOperationState::Canceled);
|
||||
{
|
||||
let operations = handler.inner.operations.lock().await;
|
||||
assert_eq!(prepared_bytes(&operations), 0);
|
||||
}
|
||||
|
||||
let start = handler
|
||||
.start_upload(FileTransferStartUploadParams {
|
||||
transfer_id: prepared.transfer_id,
|
||||
descriptor: upload_descriptor("https://safe.blob.core.windows.net/object".to_string()),
|
||||
})
|
||||
.await
|
||||
.expect_err("canceled snapshot cannot be uploaded");
|
||||
assert_eq!(start.code, -32600);
|
||||
handler.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancel_after_remote_receives_body_reports_unknown_completion() {
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("PUT"))
|
||||
.and(path("/delayed"))
|
||||
.and(body_bytes(b"committable bytes".as_slice()))
|
||||
.respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(2)))
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let source_dir = tempfile::tempdir().expect("source tempdir");
|
||||
let source = source_dir.path().join("report.txt");
|
||||
tokio::fs::write(&source, b"committable bytes")
|
||||
.await
|
||||
.expect("write source");
|
||||
let handler = test_handler();
|
||||
let prepared = handler
|
||||
.prepare_upload(prepare_params(&source, /*max_bytes*/ 1024))
|
||||
.await
|
||||
.expect("prepare upload");
|
||||
handler
|
||||
.start_upload(FileTransferStartUploadParams {
|
||||
transfer_id: prepared.transfer_id.clone(),
|
||||
descriptor: upload_descriptor(format!("{}/delayed", server.uri())),
|
||||
})
|
||||
.await
|
||||
.expect("start upload");
|
||||
timeout(Duration::from_secs(1), async {
|
||||
loop {
|
||||
if server
|
||||
.received_requests()
|
||||
.await
|
||||
.is_some_and(|requests| !requests.is_empty())
|
||||
{
|
||||
break;
|
||||
}
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("server should receive the complete request before cancellation");
|
||||
|
||||
let canceled = handler
|
||||
.cancel(FileTransferCancelParams {
|
||||
transfer_id: prepared.transfer_id.clone(),
|
||||
})
|
||||
.await
|
||||
.expect("request cancellation");
|
||||
assert_eq!(canceled.state, FileTransferOperationState::CancelRequested);
|
||||
let status = timeout(Duration::from_secs(1), async {
|
||||
loop {
|
||||
let status = handler
|
||||
.status(FileTransferStatusParams {
|
||||
transfer_id: prepared.transfer_id.clone(),
|
||||
})
|
||||
.await
|
||||
.expect("upload status");
|
||||
if status.state != FileTransferOperationState::CancelRequested {
|
||||
break status;
|
||||
}
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("cancellation should settle");
|
||||
assert_eq!(status.state, FileTransferOperationState::CompletionUnknown);
|
||||
handler.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn prepared_snapshot_expires_without_a_follow_up_rpc() {
|
||||
let source_dir = tempfile::tempdir().expect("source tempdir");
|
||||
let source = source_dir.path().join("report.txt");
|
||||
tokio::fs::write(&source, b"sensitive bytes")
|
||||
.await
|
||||
.expect("write source");
|
||||
let handler = test_handler();
|
||||
let prepared = handler
|
||||
.prepare_upload(prepare_params(&source, /*max_bytes*/ 1024))
|
||||
.await
|
||||
.expect("prepare upload");
|
||||
|
||||
timeout(PREPARED_UPLOAD_TTL + Duration::from_secs(1), async {
|
||||
loop {
|
||||
let expired = handler
|
||||
.inner
|
||||
.operations
|
||||
.lock()
|
||||
.await
|
||||
.get(&prepared.transfer_id)
|
||||
.is_some_and(|operation| operation.state == FileTransferOperationState::Expired);
|
||||
if expired {
|
||||
break;
|
||||
}
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("sweeper should expire the prepared snapshot");
|
||||
{
|
||||
let operations = handler.inner.operations.lock().await;
|
||||
let operation = operations
|
||||
.get(&prepared.transfer_id)
|
||||
.expect("terminal status should be retained");
|
||||
assert_eq!(operation.state, FileTransferOperationState::Expired);
|
||||
assert!(operation.bytes.is_none());
|
||||
}
|
||||
handler.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn terminal_records_do_not_poison_the_session_quota() {
|
||||
let source_dir = tempfile::tempdir().expect("source tempdir");
|
||||
let source = source_dir.path().join("report.txt");
|
||||
tokio::fs::write(&source, b"bytes")
|
||||
.await
|
||||
.expect("write source");
|
||||
let handler = test_handler();
|
||||
for _ in 0..(MAX_OPERATIONS_PER_SESSION + 8) {
|
||||
let prepared = handler
|
||||
.prepare_upload(prepare_params(&source, /*max_bytes*/ 1024))
|
||||
.await
|
||||
.expect("terminal records should be pruned under pressure");
|
||||
handler
|
||||
.cancel(FileTransferCancelParams {
|
||||
transfer_id: prepared.transfer_id,
|
||||
})
|
||||
.await
|
||||
.expect("cancel upload");
|
||||
}
|
||||
assert_eq!(handler.inner.tasks.len(), 1);
|
||||
handler.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn disabled_handler_rejects_before_reading_and_does_not_accept_old_session_ids() {
|
||||
let disabled = FileTransferHandler::new(
|
||||
test_runtime_paths(),
|
||||
PreparedFileUploadAvailability::Disabled,
|
||||
);
|
||||
let temp_dir = tempfile::tempdir().expect("create temp directory");
|
||||
let missing = temp_dir.path().join("missing");
|
||||
let error = disabled
|
||||
.prepare_upload(prepare_params(&missing, /*max_bytes*/ 1024))
|
||||
.await
|
||||
.expect_err("disabled handler must reject before filesystem access");
|
||||
assert_eq!(error.code, -32600);
|
||||
|
||||
let handler = test_handler();
|
||||
let error = handler
|
||||
.status(FileTransferStatusParams {
|
||||
transfer_id: format!("old-session:{}", Uuid::new_v4()),
|
||||
})
|
||||
.await
|
||||
.expect_err("old session ID should be distinguishable from an unknown operation");
|
||||
assert_eq!(error.code, FILE_TRANSFER_SESSION_LOST_ERROR_CODE);
|
||||
disabled.shutdown().await;
|
||||
handler.shutdown().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn descriptor_policy_rejects_unsafe_transport_and_headers() {
|
||||
let insecure =
|
||||
validate_upload_descriptor(upload_descriptor("http://example.com/upload".to_string()))
|
||||
.await
|
||||
.expect_err("non-local HTTP URL must be rejected");
|
||||
assert_eq!(insecure.code, -32602);
|
||||
|
||||
let server = MockServer::start().await;
|
||||
let forbidden_header = validate_upload_descriptor(FileTransferUploadDescriptor::HttpsPut {
|
||||
url: format!("{}/upload", server.uri()),
|
||||
headers: vec![FileTransferHeader {
|
||||
name: "Authorization".to_string(),
|
||||
value: "secret".to_string(),
|
||||
}],
|
||||
expires_at_unix_seconds: unix_seconds(SystemTime::now() + Duration::from_secs(60)),
|
||||
})
|
||||
.await
|
||||
.expect_err("authorization header must be rejected");
|
||||
assert_eq!(forbidden_header.code, -32602);
|
||||
|
||||
let missing_expiry = validate_upload_descriptor(FileTransferUploadDescriptor::HttpsPut {
|
||||
url: format!("{}/upload", server.uri()),
|
||||
headers: Vec::new(),
|
||||
expires_at_unix_seconds: 0,
|
||||
})
|
||||
.await
|
||||
.expect_err("expired descriptor must be rejected");
|
||||
assert_eq!(missing_expiry.code, -32602);
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
use std::net::IpAddr;
|
||||
use std::time::Duration;
|
||||
use std::time::SystemTime;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
use codex_app_server_protocol::JSONRPCErrorError;
|
||||
use codex_client::build_reqwest_client_with_custom_ca;
|
||||
use futures::stream;
|
||||
use reqwest::Method;
|
||||
use reqwest::Url;
|
||||
use reqwest::header::CONTENT_LENGTH;
|
||||
use reqwest::header::HeaderMap;
|
||||
use reqwest::header::HeaderName;
|
||||
use reqwest::header::HeaderValue;
|
||||
use reqwest::redirect::Policy;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
use crate::protocol::FileTransferHeader;
|
||||
use crate::protocol::FileTransferUploadDescriptor;
|
||||
use crate::rpc::invalid_params;
|
||||
|
||||
const MAX_DESCRIPTOR_URL_BYTES: usize = 8 * 1024;
|
||||
const MAX_DESCRIPTOR_HEADERS: usize = 16;
|
||||
const MAX_DESCRIPTOR_HEADER_BYTES: usize = 16 * 1024;
|
||||
const MIN_DESCRIPTOR_LIFETIME: Duration = Duration::from_secs(30);
|
||||
const MAX_DESCRIPTOR_LIFETIME: Duration = Duration::from_secs(10 * 60);
|
||||
const DNS_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
const TRANSFER_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
const TRANSFER_TIMEOUT: Duration = Duration::from_secs(5 * 60);
|
||||
|
||||
pub(super) enum UploadOutcome {
|
||||
Succeeded,
|
||||
Failed(String),
|
||||
CompletionUnknown(String),
|
||||
}
|
||||
|
||||
pub(super) struct ValidatedUploadDescriptor {
|
||||
url: Url,
|
||||
addresses: Vec<std::net::SocketAddr>,
|
||||
headers: HeaderMap,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ValidatedUploadDescriptor {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter
|
||||
.debug_struct("ValidatedUploadDescriptor")
|
||||
.field("url", &"[REDACTED]")
|
||||
.field("address_count", &self.addresses.len())
|
||||
.field("header_count", &self.headers.len())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn validate_upload_descriptor(
|
||||
descriptor: FileTransferUploadDescriptor,
|
||||
) -> Result<ValidatedUploadDescriptor, JSONRPCErrorError> {
|
||||
let FileTransferUploadDescriptor::HttpsPut {
|
||||
url,
|
||||
headers,
|
||||
expires_at_unix_seconds,
|
||||
} = descriptor;
|
||||
validate_expiry(expires_at_unix_seconds)?;
|
||||
if url.len() > MAX_DESCRIPTOR_URL_BYTES {
|
||||
return Err(invalid_params(
|
||||
"file transfer URL exceeds the size limit".to_string(),
|
||||
));
|
||||
}
|
||||
let url =
|
||||
Url::parse(&url).map_err(|_| invalid_params("file transfer URL is invalid".to_string()))?;
|
||||
let local_development_url = is_local_development_url(&url);
|
||||
if url.scheme() != "https" && !local_development_url {
|
||||
return Err(invalid_params(
|
||||
"file transfer URL must use HTTPS".to_string(),
|
||||
));
|
||||
}
|
||||
if !url.username().is_empty() || url.password().is_some() || url.fragment().is_some() {
|
||||
return Err(invalid_params(
|
||||
"file transfer URL must not contain credentials or a fragment".to_string(),
|
||||
));
|
||||
}
|
||||
let host = url
|
||||
.host_str()
|
||||
.ok_or_else(|| invalid_params("file transfer URL must contain a host".to_string()))?;
|
||||
if !local_development_url && !is_trusted_transfer_host(host) {
|
||||
return Err(invalid_params(
|
||||
"file transfer URL host is not trusted".to_string(),
|
||||
));
|
||||
}
|
||||
let port = url
|
||||
.port_or_known_default()
|
||||
.ok_or_else(|| invalid_params("file transfer URL has no valid port".to_string()))?;
|
||||
if !local_development_url && port != 443 {
|
||||
return Err(invalid_params(
|
||||
"file transfer URL must use port 443".to_string(),
|
||||
));
|
||||
}
|
||||
let addresses = tokio::time::timeout(DNS_TIMEOUT, tokio::net::lookup_host((host, port)))
|
||||
.await
|
||||
.map_err(|_| invalid_params("file transfer host resolution timed out".to_string()))?
|
||||
.map_err(|_| invalid_params("file transfer host resolution failed".to_string()))?
|
||||
.collect::<Vec<_>>();
|
||||
if addresses.is_empty()
|
||||
|| addresses
|
||||
.iter()
|
||||
.any(|address| is_disallowed_transfer_address(address.ip()) && !local_development_url)
|
||||
{
|
||||
return Err(invalid_params(
|
||||
"file transfer URL resolved to a disallowed address".to_string(),
|
||||
));
|
||||
}
|
||||
let azure_blob = host
|
||||
.trim_end_matches('.')
|
||||
.to_ascii_lowercase()
|
||||
.ends_with(".blob.core.windows.net");
|
||||
let headers = validate_headers(headers, azure_blob)?;
|
||||
Ok(ValidatedUploadDescriptor {
|
||||
url,
|
||||
addresses,
|
||||
headers,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) async fn upload_bytes(
|
||||
bytes: Zeroizing<Vec<u8>>,
|
||||
descriptor: ValidatedUploadDescriptor,
|
||||
cancellation: CancellationToken,
|
||||
) -> UploadOutcome {
|
||||
let Some(host) = descriptor.url.host_str() else {
|
||||
return UploadOutcome::Failed("transfer URL omitted host".to_string());
|
||||
};
|
||||
let mut builder = reqwest::Client::builder()
|
||||
.connect_timeout(TRANSFER_CONNECT_TIMEOUT)
|
||||
.timeout(TRANSFER_TIMEOUT)
|
||||
.no_proxy()
|
||||
.redirect(Policy::none());
|
||||
builder = builder.resolve_to_addrs(host, &descriptor.addresses);
|
||||
let client = match build_reqwest_client_with_custom_ca(builder) {
|
||||
Ok(client) => client,
|
||||
Err(_) => return UploadOutcome::Failed("failed to build transfer client".to_string()),
|
||||
};
|
||||
let size = bytes.len() as u64;
|
||||
let body_stream = stream::unfold((bytes, 0usize), |(bytes, offset)| async move {
|
||||
if offset >= bytes.len() {
|
||||
return None;
|
||||
}
|
||||
let end = (offset + 64 * 1024).min(bytes.len());
|
||||
let chunk = bytes::Bytes::copy_from_slice(&bytes[offset..end]);
|
||||
Some((Ok::<_, std::convert::Infallible>(chunk), (bytes, end)))
|
||||
});
|
||||
let request = match client
|
||||
.request(Method::PUT, descriptor.url)
|
||||
.headers(descriptor.headers)
|
||||
.header(CONTENT_LENGTH, size)
|
||||
.body(reqwest::Body::wrap_stream(body_stream))
|
||||
.build()
|
||||
{
|
||||
Ok(request) => request,
|
||||
Err(_) => return UploadOutcome::Failed("failed to build transfer request".to_string()),
|
||||
};
|
||||
let response = tokio::select! {
|
||||
_ = cancellation.cancelled() => {
|
||||
return UploadOutcome::CompletionUnknown(
|
||||
"upload cancellation was requested after dispatch".to_string(),
|
||||
);
|
||||
}
|
||||
response = client.execute(request) => response,
|
||||
};
|
||||
let response = match response {
|
||||
Ok(response) => response,
|
||||
Err(_) => {
|
||||
return UploadOutcome::CompletionUnknown(
|
||||
"upload completion could not be confirmed".to_string(),
|
||||
);
|
||||
}
|
||||
};
|
||||
if response.status().is_success() {
|
||||
UploadOutcome::Succeeded
|
||||
} else {
|
||||
UploadOutcome::CompletionUnknown(format!(
|
||||
"transfer returned HTTP {}",
|
||||
response.status().as_u16()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_expiry(expires_at_unix_seconds: i64) -> Result<(), JSONRPCErrorError> {
|
||||
let now = unix_seconds(SystemTime::now());
|
||||
let minimum = now.saturating_add(MIN_DESCRIPTOR_LIFETIME.as_secs() as i64);
|
||||
let maximum = now.saturating_add(MAX_DESCRIPTOR_LIFETIME.as_secs() as i64);
|
||||
if expires_at_unix_seconds < minimum || expires_at_unix_seconds > maximum {
|
||||
return Err(invalid_params(
|
||||
"file transfer descriptor expiry must be between 30 seconds and 10 minutes".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_headers(
|
||||
headers: Vec<FileTransferHeader>,
|
||||
azure_blob: bool,
|
||||
) -> Result<HeaderMap, JSONRPCErrorError> {
|
||||
if headers.len() > MAX_DESCRIPTOR_HEADERS {
|
||||
return Err(invalid_params(
|
||||
"file transfer descriptor has too many headers".to_string(),
|
||||
));
|
||||
}
|
||||
let mut header_bytes = 0usize;
|
||||
let mut result = HeaderMap::new();
|
||||
for header in headers {
|
||||
header_bytes = header_bytes.saturating_add(header.name.len() + header.value.len());
|
||||
if header_bytes > MAX_DESCRIPTOR_HEADER_BYTES {
|
||||
return Err(invalid_params(
|
||||
"file transfer descriptor headers exceed the size limit".to_string(),
|
||||
));
|
||||
}
|
||||
let name = HeaderName::from_bytes(header.name.as_bytes())
|
||||
.map_err(|_| invalid_params("file transfer header name is invalid".to_string()))?;
|
||||
if !is_allowed_header(&name) {
|
||||
return Err(invalid_params(format!(
|
||||
"file transfer header `{name}` is not permitted"
|
||||
)));
|
||||
}
|
||||
if result.contains_key(&name) {
|
||||
return Err(invalid_params(format!(
|
||||
"file transfer header `{name}` must not be repeated"
|
||||
)));
|
||||
}
|
||||
let value = HeaderValue::from_str(&header.value)
|
||||
.map_err(|_| invalid_params("file transfer header value is invalid".to_string()))?;
|
||||
if name == "x-ms-blob-type" && value != "BlockBlob" {
|
||||
return Err(invalid_params(
|
||||
"file transfer x-ms-blob-type must be BlockBlob".to_string(),
|
||||
));
|
||||
}
|
||||
result.insert(name, value);
|
||||
}
|
||||
if azure_blob && !result.contains_key("x-ms-blob-type") {
|
||||
result.insert("x-ms-blob-type", HeaderValue::from_static("BlockBlob"));
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn is_allowed_header(name: &HeaderName) -> bool {
|
||||
matches!(
|
||||
name.as_str(),
|
||||
"content-type" | "content-disposition" | "x-ms-blob-type" | "x-ms-date" | "x-ms-version"
|
||||
) || name.as_str().starts_with("x-ms-meta-")
|
||||
}
|
||||
|
||||
fn is_trusted_transfer_host(host: &str) -> bool {
|
||||
let host = host.trim_end_matches('.').to_ascii_lowercase();
|
||||
host.ends_with(".blob.core.windows.net") || host.ends_with(".oaiusercontent.com")
|
||||
}
|
||||
|
||||
fn is_local_development_url(url: &Url) -> bool {
|
||||
cfg!(debug_assertions)
|
||||
&& url.scheme() == "http"
|
||||
&& url
|
||||
.host_str()
|
||||
.is_some_and(|host| matches!(host, "localhost" | "127.0.0.1" | "::1"))
|
||||
}
|
||||
|
||||
fn is_disallowed_transfer_address(address: IpAddr) -> bool {
|
||||
match address {
|
||||
IpAddr::V4(address) => {
|
||||
let octets = address.octets();
|
||||
address.is_private()
|
||||
|| address.is_link_local()
|
||||
|| address.is_loopback()
|
||||
|| address.is_unspecified()
|
||||
|| address.is_multicast()
|
||||
|| address.is_broadcast()
|
||||
|| address.is_documentation()
|
||||
|| octets[0] == 0
|
||||
|| (octets[0] == 100 && (64..=127).contains(&octets[1]))
|
||||
|| (octets[0] == 192 && octets[1] == 0 && octets[2] == 0)
|
||||
|| (octets[0] == 198 && (18..=19).contains(&octets[1]))
|
||||
|| octets[0] >= 240
|
||||
}
|
||||
IpAddr::V6(address) => {
|
||||
let segments = address.segments();
|
||||
address.is_loopback()
|
||||
|| address.is_unspecified()
|
||||
|| address.is_multicast()
|
||||
|| (segments[0] & 0xfe00) == 0xfc00
|
||||
|| (segments[0] & 0xffc0) == 0xfe80
|
||||
|| (segments[0] == 0x2001 && segments[1] == 0x0db8)
|
||||
|| address
|
||||
.to_ipv4_mapped()
|
||||
.is_some_and(|address| is_disallowed_transfer_address(IpAddr::V4(address)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unix_seconds(time: SystemTime) -> i64 {
|
||||
time.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_secs() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
@@ -17,6 +17,14 @@ use crate::client::http_client::ReqwestHttpRequestRunner;
|
||||
use crate::protocol::EnvironmentInfo;
|
||||
use crate::protocol::ExecParams;
|
||||
use crate::protocol::ExecResponse;
|
||||
use crate::protocol::FileTransferCancelParams;
|
||||
use crate::protocol::FileTransferCancelResponse;
|
||||
use crate::protocol::FileTransferPrepareUploadParams;
|
||||
use crate::protocol::FileTransferPrepareUploadResponse;
|
||||
use crate::protocol::FileTransferStartUploadParams;
|
||||
use crate::protocol::FileTransferStartUploadResponse;
|
||||
use crate::protocol::FileTransferStatusParams;
|
||||
use crate::protocol::FileTransferStatusResponse;
|
||||
use crate::protocol::FsCanonicalizeParams;
|
||||
use crate::protocol::FsCanonicalizeResponse;
|
||||
use crate::protocol::FsCloseParams;
|
||||
@@ -160,8 +168,42 @@ impl ExecServerHandler {
|
||||
}
|
||||
|
||||
pub(crate) fn environment_info(&self) -> Result<EnvironmentInfo, JSONRPCErrorError> {
|
||||
self.require_initialized_for("environment info")?;
|
||||
Ok(EnvironmentInfo::local())
|
||||
let session = self.require_initialized_for("environment info")?;
|
||||
Ok(EnvironmentInfo::executor(
|
||||
session.file_transfer().capability(),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) async fn file_transfer_prepare_upload(
|
||||
&self,
|
||||
params: FileTransferPrepareUploadParams,
|
||||
) -> Result<FileTransferPrepareUploadResponse, JSONRPCErrorError> {
|
||||
let session = self.require_initialized_for("file transfer")?;
|
||||
session.file_transfer().prepare_upload(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn file_transfer_start_upload(
|
||||
&self,
|
||||
params: FileTransferStartUploadParams,
|
||||
) -> Result<FileTransferStartUploadResponse, JSONRPCErrorError> {
|
||||
let session = self.require_initialized_for("file transfer")?;
|
||||
session.file_transfer().start_upload(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn file_transfer_status(
|
||||
&self,
|
||||
params: FileTransferStatusParams,
|
||||
) -> Result<FileTransferStatusResponse, JSONRPCErrorError> {
|
||||
let session = self.require_initialized_for("file transfer")?;
|
||||
session.file_transfer().status(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn file_transfer_cancel(
|
||||
&self,
|
||||
params: FileTransferCancelParams,
|
||||
) -> Result<FileTransferCancelResponse, JSONRPCErrorError> {
|
||||
let session = self.require_initialized_for("file transfer")?;
|
||||
session.file_transfer().cancel(params).await
|
||||
}
|
||||
|
||||
pub(crate) async fn exec_read(
|
||||
|
||||
@@ -7,6 +7,10 @@ use crate::protocol::EXEC_SIGNAL_METHOD;
|
||||
use crate::protocol::EXEC_TERMINATE_METHOD;
|
||||
use crate::protocol::EXEC_WRITE_METHOD;
|
||||
use crate::protocol::ExecParams;
|
||||
use crate::protocol::FILE_TRANSFER_CANCEL_METHOD;
|
||||
use crate::protocol::FILE_TRANSFER_PREPARE_UPLOAD_METHOD;
|
||||
use crate::protocol::FILE_TRANSFER_START_UPLOAD_METHOD;
|
||||
use crate::protocol::FILE_TRANSFER_STATUS_METHOD;
|
||||
use crate::protocol::FS_CANONICALIZE_METHOD;
|
||||
use crate::protocol::FS_CLOSE_METHOD;
|
||||
use crate::protocol::FS_COPY_METHOD;
|
||||
@@ -18,6 +22,10 @@ use crate::protocol::FS_READ_DIRECTORY_METHOD;
|
||||
use crate::protocol::FS_READ_FILE_METHOD;
|
||||
use crate::protocol::FS_REMOVE_METHOD;
|
||||
use crate::protocol::FS_WRITE_FILE_METHOD;
|
||||
use crate::protocol::FileTransferCancelParams;
|
||||
use crate::protocol::FileTransferPrepareUploadParams;
|
||||
use crate::protocol::FileTransferStartUploadParams;
|
||||
use crate::protocol::FileTransferStatusParams;
|
||||
use crate::protocol::FsCanonicalizeParams;
|
||||
use crate::protocol::FsCloseParams;
|
||||
use crate::protocol::FsCopyParams;
|
||||
@@ -69,6 +77,30 @@ pub(crate) fn build_router() -> RpcRouter<ExecServerHandler> {
|
||||
ENVIRONMENT_INFO_METHOD,
|
||||
|handler: Arc<ExecServerHandler>, _params: ()| async move { handler.environment_info() },
|
||||
);
|
||||
router.request(
|
||||
FILE_TRANSFER_PREPARE_UPLOAD_METHOD,
|
||||
|handler: Arc<ExecServerHandler>, params: FileTransferPrepareUploadParams| async move {
|
||||
handler.file_transfer_prepare_upload(params).await
|
||||
},
|
||||
);
|
||||
router.request(
|
||||
FILE_TRANSFER_START_UPLOAD_METHOD,
|
||||
|handler: Arc<ExecServerHandler>, params: FileTransferStartUploadParams| async move {
|
||||
handler.file_transfer_start_upload(params).await
|
||||
},
|
||||
);
|
||||
router.request(
|
||||
FILE_TRANSFER_STATUS_METHOD,
|
||||
|handler: Arc<ExecServerHandler>, params: FileTransferStatusParams| async move {
|
||||
handler.file_transfer_status(params).await
|
||||
},
|
||||
);
|
||||
router.request(
|
||||
FILE_TRANSFER_CANCEL_METHOD,
|
||||
|handler: Arc<ExecServerHandler>, params: FileTransferCancelParams| async move {
|
||||
handler.file_transfer_cancel(params).await
|
||||
},
|
||||
);
|
||||
router.request(
|
||||
EXEC_READ_METHOD,
|
||||
|handler: Arc<ExecServerHandler>, params: ReadParams| async move {
|
||||
|
||||
@@ -11,6 +11,9 @@ use crate::ExecServerRuntimePaths;
|
||||
use crate::rpc::RpcNotificationSender;
|
||||
use crate::rpc::invalid_request;
|
||||
use crate::rpc::session_already_attached;
|
||||
use crate::server::file_transfer_handler::FILE_TRANSFER_ENABLED_ENV_VAR;
|
||||
use crate::server::file_transfer_handler::FileTransferHandler;
|
||||
use crate::server::file_transfer_handler::PreparedFileUploadAvailability;
|
||||
use crate::server::process_handler::ProcessHandler;
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -20,11 +23,13 @@ const DETACHED_SESSION_TTL: Duration = Duration::from_secs(30);
|
||||
|
||||
pub(crate) struct SessionRegistry {
|
||||
sessions: Mutex<HashMap<String, Arc<SessionEntry>>>,
|
||||
file_transfer_availability: PreparedFileUploadAvailability,
|
||||
}
|
||||
|
||||
struct SessionEntry {
|
||||
session_id: String,
|
||||
process: ProcessHandler,
|
||||
file_transfer: FileTransferHandler,
|
||||
attachment: StdMutex<AttachmentState>,
|
||||
}
|
||||
|
||||
@@ -54,6 +59,7 @@ impl SessionRegistry {
|
||||
pub(crate) fn new() -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
sessions: Mutex::new(HashMap::new()),
|
||||
file_transfer_availability: file_transfer_availability_from_environment(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -97,7 +103,8 @@ impl SessionRegistry {
|
||||
let session_id = Uuid::new_v4().to_string();
|
||||
let entry = Arc::new(SessionEntry::new(
|
||||
session_id.clone(),
|
||||
ProcessHandler::new(notifications, runtime_paths),
|
||||
ProcessHandler::new(notifications, runtime_paths.clone()),
|
||||
FileTransferHandler::new(runtime_paths, self.file_transfer_availability),
|
||||
connection_id,
|
||||
));
|
||||
sessions.insert(session_id, Arc::clone(&entry));
|
||||
@@ -107,7 +114,7 @@ impl SessionRegistry {
|
||||
let entry = match outcome? {
|
||||
AttachOutcome::Attached(entry) => entry,
|
||||
AttachOutcome::Expired { session_id, entry } => {
|
||||
entry.process.shutdown().await;
|
||||
entry.shutdown().await;
|
||||
return Err(invalid_request(format!("unknown session id {session_id}")));
|
||||
}
|
||||
};
|
||||
@@ -134,7 +141,7 @@ impl SessionRegistry {
|
||||
};
|
||||
|
||||
if let Some(entry) = removed {
|
||||
entry.process.shutdown().await;
|
||||
entry.shutdown().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,15 +150,36 @@ impl Default for SessionRegistry {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sessions: Mutex::new(HashMap::new()),
|
||||
file_transfer_availability: PreparedFileUploadAvailability::Disabled,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn file_transfer_availability_from_environment() -> PreparedFileUploadAvailability {
|
||||
if !cfg!(debug_assertions) {
|
||||
return PreparedFileUploadAvailability::Disabled;
|
||||
}
|
||||
if std::env::var(FILE_TRANSFER_ENABLED_ENV_VAR)
|
||||
.ok()
|
||||
.is_some_and(|value| matches!(value.as_str(), "1" | "true" | "TRUE"))
|
||||
{
|
||||
PreparedFileUploadAvailability::EnabledForDevelopment
|
||||
} else {
|
||||
PreparedFileUploadAvailability::Disabled
|
||||
}
|
||||
}
|
||||
|
||||
impl SessionEntry {
|
||||
fn new(session_id: String, process: ProcessHandler, connection_id: ConnectionId) -> Self {
|
||||
fn new(
|
||||
session_id: String,
|
||||
process: ProcessHandler,
|
||||
file_transfer: FileTransferHandler,
|
||||
connection_id: ConnectionId,
|
||||
) -> Self {
|
||||
Self {
|
||||
session_id,
|
||||
process,
|
||||
file_transfer,
|
||||
attachment: StdMutex::new(AttachmentState {
|
||||
current_connection_id: Some(connection_id),
|
||||
detached_connection_id: None,
|
||||
@@ -160,6 +188,11 @@ impl SessionEntry {
|
||||
}
|
||||
}
|
||||
|
||||
async fn shutdown(&self) {
|
||||
self.file_transfer.shutdown().await;
|
||||
self.process.shutdown().await;
|
||||
}
|
||||
|
||||
fn attach(&self, connection_id: ConnectionId) {
|
||||
let mut attachment = self
|
||||
.attachment
|
||||
@@ -244,6 +277,10 @@ impl SessionHandle {
|
||||
&self.entry.process
|
||||
}
|
||||
|
||||
pub(crate) fn file_transfer(&self) -> &FileTransferHandler {
|
||||
&self.entry.file_transfer
|
||||
}
|
||||
|
||||
pub(crate) async fn detach(&self) {
|
||||
if !self.entry.detach(self.connection_id) {
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
#![cfg(unix)]
|
||||
|
||||
mod common;
|
||||
|
||||
use codex_app_server_protocol::JSONRPCMessage;
|
||||
use codex_app_server_protocol::JSONRPCResponse;
|
||||
use codex_exec_server::FileSystemSandboxContext;
|
||||
use codex_exec_server::FsReadFileParams;
|
||||
use codex_exec_server::InitializeParams;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::permissions::FileSystemAccessMode;
|
||||
use codex_protocol::permissions::FileSystemPath;
|
||||
use codex_protocol::permissions::FileSystemSandboxEntry;
|
||||
use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use common::exec_server::ExecServerHarness;
|
||||
use common::exec_server::exec_server;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn sandbox_helper_enforces_fs_read_file_max_bytes() -> anyhow::Result<()> {
|
||||
let source_dir = tempfile::tempdir()?;
|
||||
let source = source_dir.path().join("bounded.txt");
|
||||
tokio::fs::write(&source, b"four").await?;
|
||||
let mut server = exec_server().await?;
|
||||
initialize_exec_server(&mut server).await?;
|
||||
|
||||
let request_id = server
|
||||
.send_request(
|
||||
"fs/readFile",
|
||||
serde_json::to_value(FsReadFileParams {
|
||||
path: PathUri::from_path(&source)?,
|
||||
sandbox: Some(read_only_sandbox(source_dir.path().to_path_buf())?),
|
||||
max_bytes: Some(3),
|
||||
})?,
|
||||
)
|
||||
.await?;
|
||||
let error = server.next_event().await?;
|
||||
let JSONRPCMessage::Error(error) = error else {
|
||||
anyhow::bail!("expected JSON-RPC error response, got {error:?}")
|
||||
};
|
||||
assert_eq!(error.id, request_id);
|
||||
assert_eq!(
|
||||
(error.error.code, error.error.message),
|
||||
(
|
||||
-32600,
|
||||
"file is too large to read: limit is 3 bytes".to_string()
|
||||
)
|
||||
);
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn initialize_exec_server(server: &mut ExecServerHarness) -> anyhow::Result<()> {
|
||||
let initialize_id = server
|
||||
.send_request(
|
||||
"initialize",
|
||||
serde_json::to_value(InitializeParams {
|
||||
client_name: "bounded-read-test".to_string(),
|
||||
resume_session_id: None,
|
||||
})?,
|
||||
)
|
||||
.await?;
|
||||
let response = server
|
||||
.wait_for_event(|event| {
|
||||
matches!(
|
||||
event,
|
||||
JSONRPCMessage::Response(JSONRPCResponse { id, .. }) if id == &initialize_id
|
||||
)
|
||||
})
|
||||
.await?;
|
||||
let JSONRPCMessage::Response(JSONRPCResponse { id, .. }) = response else {
|
||||
anyhow::bail!("expected initialize response")
|
||||
};
|
||||
assert_eq!(id, initialize_id);
|
||||
server
|
||||
.send_notification("initialized", serde_json::json!({}))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_only_sandbox(path: std::path::PathBuf) -> anyhow::Result<FileSystemSandboxContext> {
|
||||
let path = AbsolutePathBuf::from_absolute_path(&path)?;
|
||||
Ok(FileSystemSandboxContext::from_permission_profile(
|
||||
PermissionProfile::from_runtime_permissions(
|
||||
&FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path },
|
||||
access: FileSystemAccessMode::Read,
|
||||
}]),
|
||||
NetworkSandboxPolicy::Restricted,
|
||||
),
|
||||
))
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
mod common;
|
||||
|
||||
use codex_exec_server::Environment;
|
||||
use codex_exec_server::EnvironmentCapabilities;
|
||||
use codex_exec_server::EnvironmentInfo;
|
||||
use common::exec_server::exec_server;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
@@ -29,7 +31,13 @@ async fn remote_environment_fetches_info_from_exec_server() -> anyhow::Result<()
|
||||
|
||||
let remote_info = environment.info().await?;
|
||||
let local_info = Environment::default_for_tests().info().await?;
|
||||
assert_eq!(remote_info, local_info);
|
||||
assert_eq!(
|
||||
remote_info,
|
||||
EnvironmentInfo {
|
||||
shell: local_info.shell,
|
||||
capabilities: EnvironmentCapabilities::default(),
|
||||
}
|
||||
);
|
||||
|
||||
server.shutdown().await?;
|
||||
Ok(())
|
||||
|
||||
@@ -169,6 +169,7 @@ async fn remote_environment_routes_encrypted_exec_server_rpc() -> Result<()> {
|
||||
.fs_read_file(FsReadFileParams {
|
||||
path: PathUri::from_path(large_file_path)?,
|
||||
sandbox: None,
|
||||
max_bytes: None,
|
||||
})
|
||||
.await?;
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user