mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
feat(app-server): stream staged uploads over websocket sftp
This commit is contained in:
@@ -969,6 +969,19 @@
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"FsCreateUploadParams": {
|
||||
"description": "Allocate a Codex-managed host path for a staged upload.",
|
||||
"properties": {
|
||||
"fileName": {
|
||||
"description": "User-visible file name. App-server stores only the final path component.",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"fileName"
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"FsGetMetadataParams": {
|
||||
"description": "Request metadata for an absolute path.",
|
||||
"properties": {
|
||||
@@ -5148,6 +5161,30 @@
|
||||
"title": "Fs/writeFileRequest",
|
||||
"type": "object"
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"id": {
|
||||
"$ref": "#/definitions/RequestId"
|
||||
},
|
||||
"method": {
|
||||
"enum": [
|
||||
"fs/createUpload"
|
||||
],
|
||||
"title": "Fs/createUploadRequestMethod",
|
||||
"type": "string"
|
||||
},
|
||||
"params": {
|
||||
"$ref": "#/definitions/FsCreateUploadParams"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"method",
|
||||
"params"
|
||||
],
|
||||
"title": "Fs/createUploadRequest",
|
||||
"type": "object"
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"id": {
|
||||
|
||||
+58
@@ -954,6 +954,30 @@
|
||||
"title": "Fs/writeFileRequest",
|
||||
"type": "object"
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"id": {
|
||||
"$ref": "#/definitions/v2/RequestId"
|
||||
},
|
||||
"method": {
|
||||
"enum": [
|
||||
"fs/createUpload"
|
||||
],
|
||||
"title": "Fs/createUploadRequestMethod",
|
||||
"type": "string"
|
||||
},
|
||||
"params": {
|
||||
"$ref": "#/definitions/v2/FsCreateUploadParams"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"method",
|
||||
"params"
|
||||
],
|
||||
"title": "Fs/createUploadRequest",
|
||||
"type": "object"
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"id": {
|
||||
@@ -8687,6 +8711,40 @@
|
||||
"title": "FsCreateDirectoryResponse",
|
||||
"type": "object"
|
||||
},
|
||||
"FsCreateUploadParams": {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Allocate a Codex-managed host path for a staged upload.",
|
||||
"properties": {
|
||||
"fileName": {
|
||||
"description": "User-visible file name. App-server stores only the final path component.",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"fileName"
|
||||
],
|
||||
"title": "FsCreateUploadParams",
|
||||
"type": "object"
|
||||
},
|
||||
"FsCreateUploadResponse": {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Codex-managed host path created by `fs/createUpload`.",
|
||||
"properties": {
|
||||
"path": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/v2/AbsolutePathBuf"
|
||||
}
|
||||
],
|
||||
"description": "Absolute path where the client should stream staged upload bytes."
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"path"
|
||||
],
|
||||
"title": "FsCreateUploadResponse",
|
||||
"type": "object"
|
||||
},
|
||||
"FsGetMetadataParams": {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Request metadata for an absolute path.",
|
||||
|
||||
+58
@@ -1713,6 +1713,30 @@
|
||||
"title": "Fs/writeFileRequest",
|
||||
"type": "object"
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"id": {
|
||||
"$ref": "#/definitions/RequestId"
|
||||
},
|
||||
"method": {
|
||||
"enum": [
|
||||
"fs/createUpload"
|
||||
],
|
||||
"title": "Fs/createUploadRequestMethod",
|
||||
"type": "string"
|
||||
},
|
||||
"params": {
|
||||
"$ref": "#/definitions/FsCreateUploadParams"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"id",
|
||||
"method",
|
||||
"params"
|
||||
],
|
||||
"title": "Fs/createUploadRequest",
|
||||
"type": "object"
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"id": {
|
||||
@@ -5143,6 +5167,40 @@
|
||||
"title": "FsCreateDirectoryResponse",
|
||||
"type": "object"
|
||||
},
|
||||
"FsCreateUploadParams": {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Allocate a Codex-managed host path for a staged upload.",
|
||||
"properties": {
|
||||
"fileName": {
|
||||
"description": "User-visible file name. App-server stores only the final path component.",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"fileName"
|
||||
],
|
||||
"title": "FsCreateUploadParams",
|
||||
"type": "object"
|
||||
},
|
||||
"FsCreateUploadResponse": {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Codex-managed host path created by `fs/createUpload`.",
|
||||
"properties": {
|
||||
"path": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/AbsolutePathBuf"
|
||||
}
|
||||
],
|
||||
"description": "Absolute path where the client should stream staged upload bytes."
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"path"
|
||||
],
|
||||
"title": "FsCreateUploadResponse",
|
||||
"type": "object"
|
||||
},
|
||||
"FsGetMetadataParams": {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Request metadata for an absolute path.",
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Allocate a Codex-managed host path for a staged upload.",
|
||||
"properties": {
|
||||
"fileName": {
|
||||
"description": "User-visible file name. App-server stores only the final path component.",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"fileName"
|
||||
],
|
||||
"title": "FsCreateUploadParams",
|
||||
"type": "object"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"definitions": {
|
||||
"AbsolutePathBuf": {
|
||||
"description": "A path that is guaranteed to be absolute and normalized (though it is not guaranteed to be canonicalized or exist on the filesystem).\n\nIMPORTANT: When deserializing an `AbsolutePathBuf`, a base path must be set using [AbsolutePathBufGuard::new]. If no base path is set, the deserialization will fail unless the path being deserialized is already absolute.",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"description": "Codex-managed host path created by `fs/createUpload`.",
|
||||
"properties": {
|
||||
"path": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/AbsolutePathBuf"
|
||||
}
|
||||
],
|
||||
"description": "Absolute path where the client should stream staged upload bytes."
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"path"
|
||||
],
|
||||
"title": "FsCreateUploadResponse",
|
||||
"type": "object"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// GENERATED CODE! DO NOT MODIFY BY HAND!
|
||||
|
||||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
||||
|
||||
/**
|
||||
* Allocate a Codex-managed host path for a staged upload.
|
||||
*/
|
||||
export type FsCreateUploadParams = {
|
||||
/**
|
||||
* User-visible file name. App-server stores only the final path component.
|
||||
*/
|
||||
fileName: string, };
|
||||
@@ -0,0 +1,13 @@
|
||||
// GENERATED CODE! DO NOT MODIFY BY HAND!
|
||||
|
||||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
||||
import type { AbsolutePathBuf } from "../AbsolutePathBuf";
|
||||
|
||||
/**
|
||||
* Codex-managed host path created by `fs/createUpload`.
|
||||
*/
|
||||
export type FsCreateUploadResponse = {
|
||||
/**
|
||||
* Absolute path where the client should stream staged upload bytes.
|
||||
*/
|
||||
path: AbsolutePathBuf, };
|
||||
@@ -116,6 +116,8 @@ export type { FsCopyParams } from "./FsCopyParams";
|
||||
export type { FsCopyResponse } from "./FsCopyResponse";
|
||||
export type { FsCreateDirectoryParams } from "./FsCreateDirectoryParams";
|
||||
export type { FsCreateDirectoryResponse } from "./FsCreateDirectoryResponse";
|
||||
export type { FsCreateUploadParams } from "./FsCreateUploadParams";
|
||||
export type { FsCreateUploadResponse } from "./FsCreateUploadResponse";
|
||||
export type { FsGetMetadataParams } from "./FsGetMetadataParams";
|
||||
export type { FsGetMetadataResponse } from "./FsGetMetadataResponse";
|
||||
export type { FsReadDirectoryEntry } from "./FsReadDirectoryEntry";
|
||||
|
||||
@@ -664,6 +664,11 @@ client_request_definitions! {
|
||||
serialization: None,
|
||||
response: v2::FsWriteFileResponse,
|
||||
},
|
||||
FsCreateUpload => "fs/createUpload" {
|
||||
params: v2::FsCreateUploadParams,
|
||||
serialization: None,
|
||||
response: v2::FsCreateUploadResponse,
|
||||
},
|
||||
FsCreateDirectory => "fs/createDirectory" {
|
||||
params: v2::FsCreateDirectoryParams,
|
||||
serialization: None,
|
||||
|
||||
@@ -39,6 +39,24 @@ pub struct FsWriteFileParams {
|
||||
#[ts(export_to = "v2/")]
|
||||
pub struct FsWriteFileResponse {}
|
||||
|
||||
/// Allocate a Codex-managed host path for a staged upload.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export_to = "v2/")]
|
||||
pub struct FsCreateUploadParams {
|
||||
/// User-visible file name. App-server stores only the final path component.
|
||||
pub file_name: String,
|
||||
}
|
||||
|
||||
/// Codex-managed host path created by `fs/createUpload`.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export_to = "v2/")]
|
||||
pub struct FsCreateUploadResponse {
|
||||
/// Absolute path where the client should stream staged upload bytes.
|
||||
pub path: AbsolutePathBuf,
|
||||
}
|
||||
|
||||
/// Create a directory on the host filesystem.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
||||
@@ -665,6 +665,201 @@ fn fs_read_file_params_round_trip() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fs_create_upload_params_round_trip() {
|
||||
let params = FsCreateUploadParams {
|
||||
file_name: "example.bin".to_string(),
|
||||
};
|
||||
|
||||
let value = serde_json::to_value(¶ms).expect("serialize fs/createUpload params");
|
||||
assert_eq!(
|
||||
value,
|
||||
json!({
|
||||
"fileName": "example.bin",
|
||||
})
|
||||
);
|
||||
|
||||
let decoded = serde_json::from_value::<FsCreateUploadParams>(value)
|
||||
.expect("deserialize fs/createUpload params");
|
||||
assert_eq!(decoded, params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_key_create_params_round_trip_uses_protection_policy() {
|
||||
let params = DeviceKeyCreateParams {
|
||||
protection_policy: None,
|
||||
account_user_id: "account-user-1".to_string(),
|
||||
client_id: "cli_123".to_string(),
|
||||
};
|
||||
|
||||
let value = serde_json::to_value(¶ms).expect("serialize device/key/create params");
|
||||
assert_eq!(
|
||||
value,
|
||||
json!({
|
||||
"accountUserId": "account-user-1",
|
||||
"clientId": "cli_123",
|
||||
"protectionPolicy": null,
|
||||
})
|
||||
);
|
||||
|
||||
let decoded = serde_json::from_value::<DeviceKeyCreateParams>(value)
|
||||
.expect("deserialize device/key/create params");
|
||||
assert_eq!(decoded, params);
|
||||
|
||||
let params = DeviceKeyCreateParams {
|
||||
protection_policy: Some(DeviceKeyProtectionPolicy::AllowOsProtectedNonextractable),
|
||||
account_user_id: "account-user-1".to_string(),
|
||||
client_id: "cli_123".to_string(),
|
||||
};
|
||||
let value = serde_json::to_value(¶ms)
|
||||
.expect("serialize device/key/create params with protection policy");
|
||||
assert_eq!(
|
||||
value,
|
||||
json!({
|
||||
"accountUserId": "account-user-1",
|
||||
"clientId": "cli_123",
|
||||
"protectionPolicy": "allow_os_protected_nonextractable",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_key_create_response_round_trips_protection_class() {
|
||||
let response = DeviceKeyCreateResponse {
|
||||
key_id: "dk_123".to_string(),
|
||||
public_key_spki_der_base64: "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE".to_string(),
|
||||
algorithm: DeviceKeyAlgorithm::EcdsaP256Sha256,
|
||||
protection_class: DeviceKeyProtectionClass::OsProtectedNonextractable,
|
||||
};
|
||||
|
||||
let value = serde_json::to_value(&response).expect("serialize device/key/create response");
|
||||
assert_eq!(
|
||||
value,
|
||||
json!({
|
||||
"keyId": "dk_123",
|
||||
"publicKeySpkiDerBase64": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE",
|
||||
"algorithm": "ecdsa_p256_sha256",
|
||||
"protectionClass": "os_protected_nonextractable",
|
||||
})
|
||||
);
|
||||
|
||||
let decoded = serde_json::from_value::<DeviceKeyCreateResponse>(value)
|
||||
.expect("deserialize device/key/create response");
|
||||
assert_eq!(decoded, response);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_key_sign_params_round_trip_uses_accepted_payload_enum() {
|
||||
let params = DeviceKeySignParams {
|
||||
key_id: "dk_123".to_string(),
|
||||
payload: DeviceKeySignPayload::RemoteControlClientConnection {
|
||||
nonce: "nonce-1".to_string(),
|
||||
audience: RemoteControlClientConnectionAudience::RemoteControlClientWebsocket,
|
||||
session_id: "wssess_123".to_string(),
|
||||
target_origin: "https://chatgpt.com".to_string(),
|
||||
target_path: "/api/codex/remote/control/client".to_string(),
|
||||
account_user_id: "account-user-1".to_string(),
|
||||
client_id: "cli_123".to_string(),
|
||||
token_sha256_base64url: "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU".to_string(),
|
||||
token_expires_at: 1_700_000_000,
|
||||
scopes: vec!["remote_control_controller_websocket".to_string()],
|
||||
},
|
||||
};
|
||||
|
||||
let value = serde_json::to_value(¶ms).expect("serialize device/key/sign params");
|
||||
assert_eq!(
|
||||
value,
|
||||
json!({
|
||||
"keyId": "dk_123",
|
||||
"payload": {
|
||||
"type": "remoteControlClientConnection",
|
||||
"nonce": "nonce-1",
|
||||
"audience": "remote_control_client_websocket",
|
||||
"sessionId": "wssess_123",
|
||||
"targetOrigin": "https://chatgpt.com",
|
||||
"targetPath": "/api/codex/remote/control/client",
|
||||
"accountUserId": "account-user-1",
|
||||
"clientId": "cli_123",
|
||||
"tokenSha256Base64url": "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU",
|
||||
"tokenExpiresAt": 1_700_000_000,
|
||||
"scopes": ["remote_control_controller_websocket"],
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
let decoded = serde_json::from_value::<DeviceKeySignParams>(value)
|
||||
.expect("deserialize device/key/sign params");
|
||||
assert_eq!(decoded, params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_key_sign_params_round_trip_uses_enrollment_payload() {
|
||||
let params = DeviceKeySignParams {
|
||||
key_id: "dk_123".to_string(),
|
||||
payload: DeviceKeySignPayload::RemoteControlClientEnrollment {
|
||||
nonce: "nonce-1".to_string(),
|
||||
audience: RemoteControlClientEnrollmentAudience::RemoteControlClientEnrollment,
|
||||
challenge_id: "rch_123".to_string(),
|
||||
target_origin: "https://chatgpt.com".to_string(),
|
||||
target_path: "/wham/remote/control/client/enroll".to_string(),
|
||||
account_user_id: "account-user-1".to_string(),
|
||||
client_id: "cli_123".to_string(),
|
||||
device_identity_sha256_base64url: "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU"
|
||||
.to_string(),
|
||||
challenge_expires_at: 1_700_000_000,
|
||||
},
|
||||
};
|
||||
|
||||
let value = serde_json::to_value(¶ms)
|
||||
.expect("serialize device/key/sign params with enrollment payload");
|
||||
assert_eq!(
|
||||
value,
|
||||
json!({
|
||||
"keyId": "dk_123",
|
||||
"payload": {
|
||||
"type": "remoteControlClientEnrollment",
|
||||
"nonce": "nonce-1",
|
||||
"audience": "remote_control_client_enrollment",
|
||||
"challengeId": "rch_123",
|
||||
"targetOrigin": "https://chatgpt.com",
|
||||
"targetPath": "/wham/remote/control/client/enroll",
|
||||
"accountUserId": "account-user-1",
|
||||
"clientId": "cli_123",
|
||||
"deviceIdentitySha256Base64url": "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU",
|
||||
"challengeExpiresAt": 1_700_000_000,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
let decoded = serde_json::from_value::<DeviceKeySignParams>(value)
|
||||
.expect("deserialize device/key/sign params with enrollment payload");
|
||||
assert_eq!(decoded, params);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_key_sign_response_returns_signed_payload_bytes() {
|
||||
let response = DeviceKeySignResponse {
|
||||
signature_der_base64: "MEUCIQD".to_string(),
|
||||
signed_payload_base64: "eyJkb21haW4iOiJjb2RleA".to_string(),
|
||||
algorithm: DeviceKeyAlgorithm::EcdsaP256Sha256,
|
||||
};
|
||||
|
||||
let value = serde_json::to_value(&response).expect("serialize device/key/sign response");
|
||||
assert_eq!(
|
||||
value,
|
||||
json!({
|
||||
"signatureDerBase64": "MEUCIQD",
|
||||
"signedPayloadBase64": "eyJkb21haW4iOiJjb2RleA",
|
||||
"algorithm": "ecdsa_p256_sha256",
|
||||
})
|
||||
);
|
||||
|
||||
let decoded = serde_json::from_value::<DeviceKeySignResponse>(value)
|
||||
.expect("deserialize device/key/sign response");
|
||||
assert_eq!(decoded, response);
|
||||
}
|
||||
|
||||
#[test]
|
||||
>>>>>>> 9b6a3a6fa0 (feat(app-server): stream staged uploads over websocket sftp)
|
||||
fn fs_create_directory_params_round_trip_with_default_recursive() {
|
||||
let params = FsCreateDirectoryParams {
|
||||
path: absolute_path("tmp/example"),
|
||||
|
||||
@@ -152,6 +152,7 @@ pub enum TransportEvent {
|
||||
connection_id: ConnectionId,
|
||||
origin: ConnectionOrigin,
|
||||
writer: mpsc::Sender<QueuedOutgoingMessage>,
|
||||
binary_writer: Option<mpsc::Sender<Vec<u8>>>,
|
||||
disconnect_sender: Option<CancellationToken>,
|
||||
},
|
||||
ConnectionClosed {
|
||||
@@ -161,6 +162,10 @@ pub enum TransportEvent {
|
||||
connection_id: ConnectionId,
|
||||
message: JSONRPCMessage,
|
||||
},
|
||||
IncomingBinary {
|
||||
connection_id: ConnectionId,
|
||||
bytes: Vec<u8>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
|
||||
@@ -163,6 +163,7 @@ impl ClientTracker {
|
||||
connection_id,
|
||||
origin: ConnectionOrigin::RemoteControl,
|
||||
writer: writer_tx,
|
||||
binary_writer: None,
|
||||
disconnect_sender: Some(disconnect_token.clone()),
|
||||
})
|
||||
.await?;
|
||||
|
||||
@@ -34,6 +34,7 @@ pub async fn start_stdio_connection(
|
||||
connection_id,
|
||||
origin: ConnectionOrigin::Stdio,
|
||||
writer: writer_tx,
|
||||
binary_writer: None,
|
||||
disconnect_sender: None,
|
||||
})
|
||||
.await
|
||||
|
||||
@@ -53,7 +53,7 @@ fn listen_unix_socket_accepts_relative_custom_path() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn control_socket_acceptor_upgrades_and_forwards_websocket_text_messages_and_pings() {
|
||||
async fn control_socket_acceptor_upgrades_and_forwards_websocket_text_binary_messages_and_pings() {
|
||||
let temp_dir = tempfile::TempDir::new().expect("temp dir");
|
||||
let socket_path = test_socket_path(temp_dir.path());
|
||||
let (transport_event_tx, mut transport_event_rx) =
|
||||
@@ -112,6 +112,25 @@ async fn control_socket_acceptor_upgrades_and_forwards_websocket_text_messages_a
|
||||
(connection_id, notification)
|
||||
);
|
||||
|
||||
websocket
|
||||
.send(WebSocketMessage::Binary(Bytes::from_static(b"sftp")))
|
||||
.await
|
||||
.expect("binary payload should send");
|
||||
let incoming_binary = timeout(Duration::from_secs(1), transport_event_rx.recv())
|
||||
.await
|
||||
.expect("incoming binary event should arrive")
|
||||
.expect("incoming binary event");
|
||||
assert_eq!(
|
||||
match incoming_binary {
|
||||
TransportEvent::IncomingBinary {
|
||||
connection_id: incoming_connection_id,
|
||||
bytes,
|
||||
} => (incoming_connection_id, bytes),
|
||||
_ => panic!("expected incoming binary event"),
|
||||
},
|
||||
(connection_id, b"sftp".to_vec())
|
||||
);
|
||||
|
||||
websocket
|
||||
.send(WebSocketMessage::Ping(Bytes::from_static(b"check")))
|
||||
.await
|
||||
|
||||
@@ -182,12 +182,14 @@ pub(crate) async fn run_websocket_connection<M, SinkError, StreamError>(
|
||||
let (writer_tx, writer_rx) =
|
||||
mpsc::channel::<QueuedOutgoingMessage>(WEBSOCKET_OUTBOUND_CHANNEL_CAPACITY);
|
||||
let writer_tx_for_reader = writer_tx.clone();
|
||||
let (binary_writer_tx, binary_writer_rx) = mpsc::channel::<Vec<u8>>(CHANNEL_CAPACITY);
|
||||
let disconnect_token = CancellationToken::new();
|
||||
if transport_event_tx
|
||||
.send(TransportEvent::ConnectionOpened {
|
||||
connection_id,
|
||||
origin: ConnectionOrigin::WebSocket,
|
||||
writer: writer_tx,
|
||||
binary_writer: Some(binary_writer_tx.clone()),
|
||||
disconnect_sender: Some(disconnect_token.clone()),
|
||||
})
|
||||
.await
|
||||
@@ -201,6 +203,7 @@ pub(crate) async fn run_websocket_connection<M, SinkError, StreamError>(
|
||||
websocket_writer,
|
||||
writer_rx,
|
||||
writer_control_rx,
|
||||
binary_writer_rx,
|
||||
disconnect_token.clone(),
|
||||
));
|
||||
let mut inbound_task = tokio::spawn(run_websocket_inbound_loop(
|
||||
@@ -230,7 +233,7 @@ pub(crate) async fn run_websocket_connection<M, SinkError, StreamError>(
|
||||
|
||||
pub(crate) enum IncomingWebSocketMessage {
|
||||
Text(String),
|
||||
Binary,
|
||||
Binary(Vec<u8>),
|
||||
Ping(Bytes),
|
||||
Pong,
|
||||
Close,
|
||||
@@ -241,6 +244,7 @@ pub(crate) enum IncomingWebSocketMessage {
|
||||
/// sends directly.
|
||||
pub(crate) trait AppServerWebSocketMessage: Sized {
|
||||
fn text(text: String) -> Self;
|
||||
fn binary(payload: Vec<u8>) -> Self;
|
||||
fn pong(payload: Bytes) -> Self;
|
||||
fn into_incoming(self) -> Option<IncomingWebSocketMessage>;
|
||||
}
|
||||
@@ -250,6 +254,10 @@ impl AppServerWebSocketMessage for AxumWebSocketMessage {
|
||||
Self::Text(text.into())
|
||||
}
|
||||
|
||||
fn binary(payload: Vec<u8>) -> Self {
|
||||
Self::Binary(payload.into())
|
||||
}
|
||||
|
||||
fn pong(payload: Bytes) -> Self {
|
||||
Self::Pong(payload)
|
||||
}
|
||||
@@ -257,7 +265,7 @@ impl AppServerWebSocketMessage for AxumWebSocketMessage {
|
||||
fn into_incoming(self) -> Option<IncomingWebSocketMessage> {
|
||||
Some(match self {
|
||||
Self::Text(text) => IncomingWebSocketMessage::Text(text.to_string()),
|
||||
Self::Binary(_) => IncomingWebSocketMessage::Binary,
|
||||
Self::Binary(payload) => IncomingWebSocketMessage::Binary(payload.to_vec()),
|
||||
Self::Ping(payload) => IncomingWebSocketMessage::Ping(payload),
|
||||
Self::Pong(_) => IncomingWebSocketMessage::Pong,
|
||||
Self::Close(_) => IncomingWebSocketMessage::Close,
|
||||
@@ -270,6 +278,10 @@ impl AppServerWebSocketMessage for TungsteniteWebSocketMessage {
|
||||
Self::Text(text.into())
|
||||
}
|
||||
|
||||
fn binary(payload: Vec<u8>) -> Self {
|
||||
Self::Binary(payload.into())
|
||||
}
|
||||
|
||||
fn pong(payload: Bytes) -> Self {
|
||||
Self::Pong(payload)
|
||||
}
|
||||
@@ -277,7 +289,7 @@ impl AppServerWebSocketMessage for TungsteniteWebSocketMessage {
|
||||
fn into_incoming(self) -> Option<IncomingWebSocketMessage> {
|
||||
Some(match self {
|
||||
Self::Text(text) => IncomingWebSocketMessage::Text(text.to_string()),
|
||||
Self::Binary(_) => IncomingWebSocketMessage::Binary,
|
||||
Self::Binary(payload) => IncomingWebSocketMessage::Binary(payload.to_vec()),
|
||||
Self::Ping(payload) => IncomingWebSocketMessage::Ping(payload),
|
||||
Self::Pong(_) => IncomingWebSocketMessage::Pong,
|
||||
Self::Close(_) => IncomingWebSocketMessage::Close,
|
||||
@@ -290,6 +302,7 @@ async fn run_websocket_outbound_loop<M, SinkError>(
|
||||
websocket_writer: impl futures::sink::Sink<M, Error = SinkError> + Send + 'static,
|
||||
mut writer_rx: mpsc::Receiver<QueuedOutgoingMessage>,
|
||||
mut writer_control_rx: mpsc::Receiver<M>,
|
||||
mut binary_writer_rx: mpsc::Receiver<Vec<u8>>,
|
||||
disconnect_token: CancellationToken,
|
||||
) where
|
||||
M: AppServerWebSocketMessage + Send + 'static,
|
||||
@@ -309,6 +322,14 @@ async fn run_websocket_outbound_loop<M, SinkError>(
|
||||
break;
|
||||
}
|
||||
}
|
||||
payload = binary_writer_rx.recv() => {
|
||||
let Some(payload) = payload else {
|
||||
break;
|
||||
};
|
||||
if websocket_writer.send(M::binary(payload)).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
queued_message = writer_rx.recv() => {
|
||||
let Some(queued_message) = queued_message else {
|
||||
break;
|
||||
@@ -371,8 +392,17 @@ async fn run_websocket_inbound_loop<M, StreamError>(
|
||||
}
|
||||
Some(IncomingWebSocketMessage::Pong) => {}
|
||||
Some(IncomingWebSocketMessage::Close) => break,
|
||||
Some(IncomingWebSocketMessage::Binary) => {
|
||||
warn!("dropping unsupported binary websocket message");
|
||||
Some(IncomingWebSocketMessage::Binary(bytes)) => {
|
||||
if transport_event_tx
|
||||
.send(TransportEvent::IncomingBinary {
|
||||
connection_id,
|
||||
bytes,
|
||||
})
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
},
|
||||
|
||||
@@ -189,6 +189,7 @@ Example with notification opt-out:
|
||||
- `process/exited` — experimental; notification emitted when a `process/spawn` session exits.
|
||||
- `fs/readFile` — read an absolute file path and return `{ dataBase64 }`.
|
||||
- `fs/writeFile` — write an absolute file path from base64-encoded `{ dataBase64 }`; returns `{}`.
|
||||
- `fs/createUpload` — allocate a Codex-managed host path for a staged upload and return the absolute host `{ path }`.
|
||||
- `fs/createDirectory` — create an absolute directory path; `recursive` defaults to `true`.
|
||||
- `fs/getMetadata` — return metadata for an absolute path: `isDirectory`, `isFile`, `isSymlink`, `createdAtMs`, and `modifiedAtMs`.
|
||||
- `fs/readDirectory` — list direct child entries for an absolute directory path; each entry contains `fileName`, `isDirectory`, and `isFile`, and `fileName` is just the child name, not a path.
|
||||
@@ -1101,20 +1102,26 @@ All filesystem paths in this section must be absolute.
|
||||
"dataBase64": "aGVsbG8="
|
||||
} }
|
||||
{ "id": 41, "result": {} }
|
||||
{ "method": "fs/getMetadata", "id": 42, "params": {
|
||||
"path": "/tmp/example/nested/note.txt"
|
||||
{ "method": "fs/createUpload", "id": 42, "params": {
|
||||
"fileName": "notes.txt"
|
||||
} }
|
||||
{ "id": 42, "result": {
|
||||
"path": "/Users/me/.codex/uploads/019a7f95-9d35-75e2-9d0e-82bd8cf1af58/notes.txt"
|
||||
} }
|
||||
{ "method": "fs/getMetadata", "id": 43, "params": {
|
||||
"path": "/tmp/example/nested/note.txt"
|
||||
} }
|
||||
{ "id": 43, "result": {
|
||||
"isDirectory": false,
|
||||
"isFile": true,
|
||||
"isSymlink": false,
|
||||
"createdAtMs": 1730910000000,
|
||||
"modifiedAtMs": 1730910000000
|
||||
} }
|
||||
{ "method": "fs/readFile", "id": 43, "params": {
|
||||
{ "method": "fs/readFile", "id": 44, "params": {
|
||||
"path": "/tmp/example/nested/note.txt"
|
||||
} }
|
||||
{ "id": 43, "result": {
|
||||
{ "id": 44, "result": {
|
||||
"dataBase64": "aGVsbG8="
|
||||
} }
|
||||
```
|
||||
@@ -1123,6 +1130,8 @@ All filesystem paths in this section must be absolute.
|
||||
- `fs/createDirectory` defaults `recursive` to `true` when omitted.
|
||||
- `fs/remove` defaults both `recursive` and `force` to `true` when omitted.
|
||||
- `fs/readFile` always returns base64 bytes via `dataBase64`, and `fs/writeFile` always expects base64 bytes in `dataBase64`.
|
||||
- `fs/createUpload` accepts a client-visible `fileName`, stores only its final path component, and chooses the destination under `$CODEX_HOME/uploads/<uuid>/`.
|
||||
- Direct websocket transports keep JSON-RPC on text frames and reserve binary frames for the staged-upload SFTP lane. The SFTP lane is scoped to paths allocated by `fs/createUpload`.
|
||||
- `fs/copy` handles both file copies and directory-tree copies; it requires `recursive: true` when `sourcePath` is a directory. Recursive copies traverse regular files, directories, and symlinks; other entry types are skipped.
|
||||
|
||||
### Example: Filesystem watch
|
||||
|
||||
@@ -828,6 +828,7 @@ pub async fn run_main_with_transport_options(
|
||||
connection_id,
|
||||
origin,
|
||||
writer,
|
||||
binary_writer,
|
||||
disconnect_sender,
|
||||
} => {
|
||||
let outbound_initialized = Arc::new(AtomicBool::new(false));
|
||||
@@ -860,6 +861,7 @@ pub async fn run_main_with_transport_options(
|
||||
outbound_initialized,
|
||||
outbound_experimental_api_enabled,
|
||||
outbound_opted_out_notification_methods,
|
||||
binary_writer,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -962,6 +964,28 @@ pub async fn run_main_with_transport_options(
|
||||
}
|
||||
}
|
||||
}
|
||||
TransportEvent::IncomingBinary { connection_id, bytes } => {
|
||||
let Some(connection_state) = connections.get(&connection_id) else {
|
||||
warn!("dropping binary payload from unknown connection: {connection_id:?}");
|
||||
continue;
|
||||
};
|
||||
let Some(binary_writer) = connection_state.binary_writer.clone() else {
|
||||
warn!("dropping binary payload from connection without a binary lane: {connection_id:?}");
|
||||
continue;
|
||||
};
|
||||
match processor.process_upload_binary(connection_id, bytes).await {
|
||||
Ok(responses) => {
|
||||
for response in responses {
|
||||
if binary_writer.send(response).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
warn!("failed to process upload binary payload: {}", err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
changed = remote_control_status_rx.changed() => {
|
||||
|
||||
@@ -438,6 +438,7 @@ impl MessageProcessor {
|
||||
.local_environment()
|
||||
.get_filesystem(),
|
||||
fs_watch_manager,
|
||||
config.codex_home.to_path_buf(),
|
||||
);
|
||||
let windows_sandbox_processor = WindowsSandboxRequestProcessor::new(
|
||||
outgoing.clone(),
|
||||
@@ -594,6 +595,16 @@ impl MessageProcessor {
|
||||
tracing::info!("<- typed notification: {:?}", notification);
|
||||
}
|
||||
|
||||
pub(crate) async fn process_upload_binary(
|
||||
&self,
|
||||
connection_id: ConnectionId,
|
||||
bytes: Vec<u8>,
|
||||
) -> Result<Vec<Vec<u8>>, JSONRPCErrorError> {
|
||||
self.fs_processor
|
||||
.process_upload_sftp_bytes(connection_id, bytes)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn run_request_with_context<F>(
|
||||
outgoing: Arc<OutgoingMessageSender>,
|
||||
request_context: RequestContext,
|
||||
@@ -860,6 +871,11 @@ impl MessageProcessor {
|
||||
.write_file(params)
|
||||
.await
|
||||
.map(|response| Some(response.into())),
|
||||
ClientRequest::FsCreateUpload { params, .. } => self
|
||||
.fs_processor
|
||||
.create_upload(params)
|
||||
.await
|
||||
.map(|response| Some(response.into())),
|
||||
ClientRequest::FsCreateDirectory { params, .. } => self
|
||||
.fs_processor
|
||||
.create_directory(params)
|
||||
|
||||
@@ -448,6 +448,7 @@ mod search;
|
||||
mod thread_processor;
|
||||
mod token_usage_replay;
|
||||
mod turn_processor;
|
||||
mod upload_sftp;
|
||||
mod windows_sandbox_processor;
|
||||
|
||||
pub(crate) use account_processor::AccountRequestProcessor;
|
||||
|
||||
@@ -2,12 +2,15 @@ use crate::error_code::internal_error;
|
||||
use crate::error_code::invalid_request;
|
||||
use crate::fs_watch::FsWatchManager;
|
||||
use crate::outgoing_message::ConnectionId;
|
||||
use crate::request_processors::upload_sftp::UploadSftpSession;
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use codex_app_server_protocol::FsCopyParams;
|
||||
use codex_app_server_protocol::FsCopyResponse;
|
||||
use codex_app_server_protocol::FsCreateDirectoryParams;
|
||||
use codex_app_server_protocol::FsCreateDirectoryResponse;
|
||||
use codex_app_server_protocol::FsCreateUploadParams;
|
||||
use codex_app_server_protocol::FsCreateUploadResponse;
|
||||
use codex_app_server_protocol::FsGetMetadataParams;
|
||||
use codex_app_server_protocol::FsGetMetadataResponse;
|
||||
use codex_app_server_protocol::FsReadDirectoryEntry;
|
||||
@@ -28,28 +31,46 @@ use codex_exec_server::CopyOptions;
|
||||
use codex_exec_server::CreateDirectoryOptions;
|
||||
use codex_exec_server::ExecutorFileSystem;
|
||||
use codex_exec_server::RemoveOptions;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct FsRequestProcessor {
|
||||
file_system: Arc<dyn ExecutorFileSystem>,
|
||||
fs_watch_manager: FsWatchManager,
|
||||
codex_home: PathBuf,
|
||||
upload_paths: Arc<Mutex<HashSet<PathBuf>>>,
|
||||
upload_sftp_sessions: Arc<Mutex<HashMap<ConnectionId, UploadSftpSession>>>,
|
||||
}
|
||||
|
||||
impl FsRequestProcessor {
|
||||
pub(crate) fn new(
|
||||
file_system: Arc<dyn ExecutorFileSystem>,
|
||||
fs_watch_manager: FsWatchManager,
|
||||
codex_home: PathBuf,
|
||||
) -> Self {
|
||||
Self {
|
||||
file_system,
|
||||
fs_watch_manager,
|
||||
codex_home,
|
||||
upload_paths: Arc::new(Mutex::new(HashSet::new())),
|
||||
upload_sftp_sessions: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn connection_closed(&self, connection_id: ConnectionId) {
|
||||
self.fs_watch_manager.connection_closed(connection_id).await;
|
||||
self.upload_sftp_sessions
|
||||
.lock()
|
||||
.await
|
||||
.remove(&connection_id);
|
||||
}
|
||||
|
||||
pub(crate) async fn read_file(
|
||||
@@ -82,6 +103,52 @@ impl FsRequestProcessor {
|
||||
Ok(FsWriteFileResponse {})
|
||||
}
|
||||
|
||||
pub(crate) async fn create_upload(
|
||||
&self,
|
||||
params: FsCreateUploadParams,
|
||||
) -> Result<FsCreateUploadResponse, JSONRPCErrorError> {
|
||||
let file_name = sanitize_upload_file_name(¶ms.file_name)?;
|
||||
let upload_dir = absolute_path(
|
||||
self.codex_home
|
||||
.join("uploads")
|
||||
.join(Uuid::now_v7().to_string()),
|
||||
)?;
|
||||
let upload_path = absolute_path(upload_dir.as_path().join(file_name))?;
|
||||
self.file_system
|
||||
.create_directory(
|
||||
&upload_dir,
|
||||
CreateDirectoryOptions { recursive: true },
|
||||
/*sandbox*/ None,
|
||||
)
|
||||
.await
|
||||
.map_err(map_fs_error)?;
|
||||
self.upload_paths
|
||||
.lock()
|
||||
.await
|
||||
.insert(upload_path.as_path().to_path_buf());
|
||||
Ok(FsCreateUploadResponse { path: upload_path })
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "SFTP packets for one connection must be processed in arrival order"
|
||||
)]
|
||||
pub(crate) async fn process_upload_sftp_bytes(
|
||||
&self,
|
||||
connection_id: ConnectionId,
|
||||
bytes: Vec<u8>,
|
||||
) -> Result<Vec<Vec<u8>>, JSONRPCErrorError> {
|
||||
let mut sessions = self.upload_sftp_sessions.lock().await;
|
||||
let session = sessions
|
||||
.entry(connection_id)
|
||||
.or_insert_with(|| UploadSftpSession::new(Arc::clone(&self.upload_paths)));
|
||||
let result = session.process_bytes(bytes).await;
|
||||
if result.is_err() {
|
||||
sessions.remove(&connection_id);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub(crate) async fn create_directory(
|
||||
&self,
|
||||
params: FsCreateDirectoryParams,
|
||||
@@ -191,6 +258,18 @@ impl FsRequestProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
fn sanitize_upload_file_name(file_name: &str) -> Result<&str, JSONRPCErrorError> {
|
||||
Path::new(file_name)
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.filter(|name| !name.is_empty())
|
||||
.ok_or_else(|| invalid_request("fs/createUpload requires a fileName".to_string()))
|
||||
}
|
||||
|
||||
fn absolute_path(path: PathBuf) -> Result<AbsolutePathBuf, JSONRPCErrorError> {
|
||||
AbsolutePathBuf::try_from(path).map_err(|err| internal_error(err.to_string()))
|
||||
}
|
||||
|
||||
fn map_fs_error(err: io::Error) -> JSONRPCErrorError {
|
||||
if err.kind() == io::ErrorKind::InvalidInput {
|
||||
invalid_request(err.to_string())
|
||||
|
||||
@@ -0,0 +1,482 @@
|
||||
use crate::error_code::internal_error;
|
||||
use crate::error_code::invalid_request;
|
||||
use codex_app_server_protocol::JSONRPCErrorError;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::fs::File;
|
||||
use tokio::fs::OpenOptions;
|
||||
use tokio::io::AsyncSeekExt;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::io::SeekFrom;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
const SSH_FXP_INIT: u8 = 1;
|
||||
const SSH_FXP_VERSION: u8 = 2;
|
||||
const SSH_FXP_OPEN: u8 = 3;
|
||||
const SSH_FXP_CLOSE: u8 = 4;
|
||||
const SSH_FXP_WRITE: u8 = 6;
|
||||
const SSH_FXP_STAT: u8 = 17;
|
||||
const SSH_FXP_STATUS: u8 = 101;
|
||||
const SSH_FXP_HANDLE: u8 = 102;
|
||||
const SSH_FXP_ATTRS: u8 = 105;
|
||||
|
||||
const SSH_FX_OK: u32 = 0;
|
||||
const SSH_FX_NO_SUCH_FILE: u32 = 2;
|
||||
const SSH_FX_PERMISSION_DENIED: u32 = 3;
|
||||
const SSH_FX_OP_UNSUPPORTED: u32 = 8;
|
||||
|
||||
const SSH_FXF_WRITE: u32 = 0x0000_0002;
|
||||
const SSH_FXF_APPEND: u32 = 0x0000_0004;
|
||||
const SSH_FXF_CREAT: u32 = 0x0000_0008;
|
||||
const SSH_FXF_TRUNC: u32 = 0x0000_0010;
|
||||
|
||||
const SSH_FILEXFER_ATTR_SIZE: u32 = 0x0000_0001;
|
||||
const MAX_SFTP_PACKET_BYTES: usize = 1024 * 1024;
|
||||
|
||||
pub(crate) struct UploadSftpSession {
|
||||
allowed_paths: Arc<Mutex<HashSet<PathBuf>>>,
|
||||
pending_bytes: Vec<u8>,
|
||||
handles: HashMap<Vec<u8>, UploadHandle>,
|
||||
next_handle_id: u64,
|
||||
}
|
||||
|
||||
struct UploadHandle {
|
||||
file: File,
|
||||
}
|
||||
|
||||
impl UploadSftpSession {
|
||||
pub(crate) fn new(allowed_paths: Arc<Mutex<HashSet<PathBuf>>>) -> Self {
|
||||
Self {
|
||||
allowed_paths,
|
||||
pending_bytes: Vec::new(),
|
||||
handles: HashMap::new(),
|
||||
next_handle_id: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn process_bytes(
|
||||
&mut self,
|
||||
bytes: Vec<u8>,
|
||||
) -> Result<Vec<Vec<u8>>, JSONRPCErrorError> {
|
||||
self.pending_bytes.extend(bytes);
|
||||
let mut responses = Vec::new();
|
||||
while let Some(packet) = take_packet(&mut self.pending_bytes)? {
|
||||
if let Some(response) = self.process_packet(packet).await? {
|
||||
responses.push(response);
|
||||
}
|
||||
}
|
||||
Ok(responses)
|
||||
}
|
||||
|
||||
async fn process_packet(
|
||||
&mut self,
|
||||
packet: Vec<u8>,
|
||||
) -> Result<Option<Vec<u8>>, JSONRPCErrorError> {
|
||||
let mut cursor = PacketCursor::new(&packet);
|
||||
let packet_type = cursor.read_u8()?;
|
||||
match packet_type {
|
||||
SSH_FXP_INIT => {
|
||||
let _client_version = cursor.read_u32()?;
|
||||
cursor.finish()?;
|
||||
Ok(Some(version_packet()))
|
||||
}
|
||||
SSH_FXP_OPEN => {
|
||||
let request_id = cursor.read_u32()?;
|
||||
let path = cursor.read_string_utf8()?;
|
||||
let pflags = cursor.read_u32()?;
|
||||
let attrs = cursor.read_u32()?;
|
||||
cursor.finish()?;
|
||||
if attrs != 0 {
|
||||
return Ok(Some(status_packet(
|
||||
request_id,
|
||||
SSH_FX_OP_UNSUPPORTED,
|
||||
"open attributes are not supported",
|
||||
)));
|
||||
}
|
||||
if !self.is_allowed_path(&path).await {
|
||||
return Ok(Some(status_packet(
|
||||
request_id,
|
||||
SSH_FX_PERMISSION_DENIED,
|
||||
"path is not a staged upload",
|
||||
)));
|
||||
}
|
||||
let mut options = OpenOptions::new();
|
||||
options.write(pflags & SSH_FXF_WRITE != 0);
|
||||
options.create(pflags & SSH_FXF_CREAT != 0);
|
||||
options.truncate(pflags & SSH_FXF_TRUNC != 0);
|
||||
options.append(pflags & SSH_FXF_APPEND != 0);
|
||||
match options.open(&path).await {
|
||||
Ok(file) => {
|
||||
let handle = self.next_handle();
|
||||
self.handles.insert(handle.clone(), UploadHandle { file });
|
||||
Ok(Some(handle_packet(request_id, &handle)))
|
||||
}
|
||||
Err(err) => Ok(Some(status_packet(
|
||||
request_id,
|
||||
map_status_code(&err),
|
||||
&err.to_string(),
|
||||
))),
|
||||
}
|
||||
}
|
||||
SSH_FXP_WRITE => {
|
||||
let request_id = cursor.read_u32()?;
|
||||
let handle = cursor.read_string_bytes()?;
|
||||
let offset = cursor.read_u64()?;
|
||||
let data = cursor.read_string_bytes()?;
|
||||
cursor.finish()?;
|
||||
let Some(upload_handle) = self.handles.get_mut(&handle) else {
|
||||
return Ok(Some(status_packet(
|
||||
request_id,
|
||||
SSH_FX_NO_SUCH_FILE,
|
||||
"unknown upload handle",
|
||||
)));
|
||||
};
|
||||
if let Err(err) = upload_handle.file.seek(SeekFrom::Start(offset)).await {
|
||||
return Ok(Some(status_packet(
|
||||
request_id,
|
||||
map_status_code(&err),
|
||||
&err.to_string(),
|
||||
)));
|
||||
}
|
||||
match upload_handle.file.write_all(&data).await {
|
||||
Ok(()) => Ok(Some(status_packet(request_id, SSH_FX_OK, "Ok"))),
|
||||
Err(err) => Ok(Some(status_packet(
|
||||
request_id,
|
||||
map_status_code(&err),
|
||||
&err.to_string(),
|
||||
))),
|
||||
}
|
||||
}
|
||||
SSH_FXP_CLOSE => {
|
||||
let request_id = cursor.read_u32()?;
|
||||
let handle = cursor.read_string_bytes()?;
|
||||
cursor.finish()?;
|
||||
let Some(mut upload_handle) = self.handles.remove(&handle) else {
|
||||
return Ok(Some(status_packet(
|
||||
request_id,
|
||||
SSH_FX_NO_SUCH_FILE,
|
||||
"unknown upload handle",
|
||||
)));
|
||||
};
|
||||
match upload_handle.file.flush().await {
|
||||
Ok(()) => Ok(Some(status_packet(request_id, SSH_FX_OK, "Ok"))),
|
||||
Err(err) => Ok(Some(status_packet(
|
||||
request_id,
|
||||
map_status_code(&err),
|
||||
&err.to_string(),
|
||||
))),
|
||||
}
|
||||
}
|
||||
SSH_FXP_STAT => {
|
||||
let request_id = cursor.read_u32()?;
|
||||
let path = cursor.read_string_utf8()?;
|
||||
cursor.finish()?;
|
||||
if !self.is_allowed_path(&path).await {
|
||||
return Ok(Some(status_packet(
|
||||
request_id,
|
||||
SSH_FX_PERMISSION_DENIED,
|
||||
"path is not a staged upload",
|
||||
)));
|
||||
}
|
||||
match tokio::fs::metadata(path).await {
|
||||
Ok(metadata) => Ok(Some(attrs_packet(request_id, metadata.len()))),
|
||||
Err(err) => Ok(Some(status_packet(
|
||||
request_id,
|
||||
map_status_code(&err),
|
||||
&err.to_string(),
|
||||
))),
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let request_id = cursor.read_u32().unwrap_or_default();
|
||||
Ok(Some(status_packet(
|
||||
request_id,
|
||||
SSH_FX_OP_UNSUPPORTED,
|
||||
"operation is not supported",
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn is_allowed_path(&self, path: &str) -> bool {
|
||||
self.allowed_paths
|
||||
.lock()
|
||||
.await
|
||||
.contains(&PathBuf::from(path))
|
||||
}
|
||||
|
||||
fn next_handle(&mut self) -> Vec<u8> {
|
||||
let handle = self.next_handle_id.to_be_bytes().to_vec();
|
||||
self.next_handle_id = self.next_handle_id.wrapping_add(1);
|
||||
handle
|
||||
}
|
||||
}
|
||||
|
||||
fn take_packet(buffer: &mut Vec<u8>) -> Result<Option<Vec<u8>>, JSONRPCErrorError> {
|
||||
if buffer.len() < 4 {
|
||||
return Ok(None);
|
||||
}
|
||||
let packet_len = u32::from_be_bytes(
|
||||
buffer[..4]
|
||||
.try_into()
|
||||
.map_err(|_| invalid_request("invalid SFTP packet length prefix".to_string()))?,
|
||||
) as usize;
|
||||
if packet_len > MAX_SFTP_PACKET_BYTES {
|
||||
return Err(invalid_sftp_packet("packet exceeds 1 MiB"));
|
||||
}
|
||||
if buffer.len() < packet_len + 4 {
|
||||
return Ok(None);
|
||||
}
|
||||
let packet = buffer[4..packet_len + 4].to_vec();
|
||||
buffer.drain(..packet_len + 4);
|
||||
Ok(Some(packet))
|
||||
}
|
||||
|
||||
fn version_packet() -> Vec<u8> {
|
||||
packet(|payload| {
|
||||
payload.push(SSH_FXP_VERSION);
|
||||
payload.extend_from_slice(&3_u32.to_be_bytes());
|
||||
})
|
||||
}
|
||||
|
||||
fn status_packet(request_id: u32, status_code: u32, message: &str) -> Vec<u8> {
|
||||
packet(|payload| {
|
||||
payload.push(SSH_FXP_STATUS);
|
||||
payload.extend_from_slice(&request_id.to_be_bytes());
|
||||
payload.extend_from_slice(&status_code.to_be_bytes());
|
||||
push_string(payload, message.as_bytes());
|
||||
push_string(payload, b"en-US");
|
||||
})
|
||||
}
|
||||
|
||||
fn handle_packet(request_id: u32, handle: &[u8]) -> Vec<u8> {
|
||||
packet(|payload| {
|
||||
payload.push(SSH_FXP_HANDLE);
|
||||
payload.extend_from_slice(&request_id.to_be_bytes());
|
||||
push_string(payload, handle);
|
||||
})
|
||||
}
|
||||
|
||||
fn attrs_packet(request_id: u32, size: u64) -> Vec<u8> {
|
||||
packet(|payload| {
|
||||
payload.push(SSH_FXP_ATTRS);
|
||||
payload.extend_from_slice(&request_id.to_be_bytes());
|
||||
payload.extend_from_slice(&SSH_FILEXFER_ATTR_SIZE.to_be_bytes());
|
||||
payload.extend_from_slice(&size.to_be_bytes());
|
||||
})
|
||||
}
|
||||
|
||||
fn packet(build_payload: impl FnOnce(&mut Vec<u8>)) -> Vec<u8> {
|
||||
let mut payload = Vec::new();
|
||||
build_payload(&mut payload);
|
||||
let mut packet = Vec::with_capacity(payload.len() + 4);
|
||||
packet.extend_from_slice(&(payload.len() as u32).to_be_bytes());
|
||||
packet.extend_from_slice(&payload);
|
||||
packet
|
||||
}
|
||||
|
||||
fn push_string(payload: &mut Vec<u8>, value: &[u8]) {
|
||||
payload.extend_from_slice(&(value.len() as u32).to_be_bytes());
|
||||
payload.extend_from_slice(value);
|
||||
}
|
||||
|
||||
fn map_status_code(err: &io::Error) -> u32 {
|
||||
match err.kind() {
|
||||
io::ErrorKind::NotFound => SSH_FX_NO_SUCH_FILE,
|
||||
io::ErrorKind::PermissionDenied => SSH_FX_PERMISSION_DENIED,
|
||||
_ => SSH_FX_OP_UNSUPPORTED,
|
||||
}
|
||||
}
|
||||
|
||||
struct PacketCursor<'a> {
|
||||
bytes: &'a [u8],
|
||||
offset: usize,
|
||||
}
|
||||
|
||||
impl<'a> PacketCursor<'a> {
|
||||
fn new(bytes: &'a [u8]) -> Self {
|
||||
Self { bytes, offset: 0 }
|
||||
}
|
||||
|
||||
fn read_u8(&mut self) -> Result<u8, JSONRPCErrorError> {
|
||||
let value = *self
|
||||
.bytes
|
||||
.get(self.offset)
|
||||
.ok_or_else(|| invalid_sftp_packet("missing u8"))?;
|
||||
self.offset += 1;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn read_u32(&mut self) -> Result<u32, JSONRPCErrorError> {
|
||||
let bytes = self
|
||||
.bytes
|
||||
.get(self.offset..self.offset + 4)
|
||||
.ok_or_else(|| invalid_sftp_packet("missing u32"))?;
|
||||
self.offset += 4;
|
||||
Ok(u32::from_be_bytes(bytes.try_into().map_err(|_| {
|
||||
internal_error("failed to decode SFTP u32".to_string())
|
||||
})?))
|
||||
}
|
||||
|
||||
fn read_u64(&mut self) -> Result<u64, JSONRPCErrorError> {
|
||||
let bytes = self
|
||||
.bytes
|
||||
.get(self.offset..self.offset + 8)
|
||||
.ok_or_else(|| invalid_sftp_packet("missing u64"))?;
|
||||
self.offset += 8;
|
||||
Ok(u64::from_be_bytes(bytes.try_into().map_err(|_| {
|
||||
internal_error("failed to decode SFTP u64".to_string())
|
||||
})?))
|
||||
}
|
||||
|
||||
fn read_string_bytes(&mut self) -> Result<Vec<u8>, JSONRPCErrorError> {
|
||||
let len = self.read_u32()? as usize;
|
||||
let bytes = self
|
||||
.bytes
|
||||
.get(self.offset..self.offset + len)
|
||||
.ok_or_else(|| invalid_sftp_packet("truncated string"))?;
|
||||
self.offset += len;
|
||||
Ok(bytes.to_vec())
|
||||
}
|
||||
|
||||
fn read_string_utf8(&mut self) -> Result<String, JSONRPCErrorError> {
|
||||
String::from_utf8(self.read_string_bytes()?)
|
||||
.map_err(|_| invalid_sftp_packet("path must be UTF-8"))
|
||||
}
|
||||
|
||||
fn finish(&self) -> Result<(), JSONRPCErrorError> {
|
||||
if self.offset == self.bytes.len() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(invalid_sftp_packet("unexpected trailing bytes"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid_sftp_packet(message: &str) -> JSONRPCErrorError {
|
||||
invalid_request(format!("invalid staged upload SFTP packet: {message}"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[tokio::test]
|
||||
async fn writes_only_paths_allocated_for_uploads() {
|
||||
let tempdir = TempDir::new().expect("create temp dir");
|
||||
let path = tempdir.path().join("uploads").join("note.txt");
|
||||
tokio::fs::create_dir_all(path.parent().expect("path parent"))
|
||||
.await
|
||||
.expect("create upload parent");
|
||||
let allowed_paths = Arc::new(Mutex::new(HashSet::from([path.clone()])));
|
||||
let mut session = UploadSftpSession::new(allowed_paths);
|
||||
|
||||
let responses = session
|
||||
.process_bytes(init_packet())
|
||||
.await
|
||||
.expect("init packet should succeed");
|
||||
assert_eq!(packet_type(&responses[0]), SSH_FXP_VERSION);
|
||||
|
||||
let responses = session
|
||||
.process_bytes(open_packet(
|
||||
/*request_id*/ 1,
|
||||
&path,
|
||||
SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC,
|
||||
))
|
||||
.await
|
||||
.expect("open packet should succeed");
|
||||
assert_eq!(packet_type(&responses[0]), SSH_FXP_HANDLE);
|
||||
let handle = response_handle(&responses[0]);
|
||||
|
||||
let responses = session
|
||||
.process_bytes(write_packet(
|
||||
/*request_id*/ 2, &handle, /*offset*/ 0, b"hello",
|
||||
))
|
||||
.await
|
||||
.expect("write packet should succeed");
|
||||
assert_eq!(response_status(&responses[0]), SSH_FX_OK);
|
||||
|
||||
let responses = session
|
||||
.process_bytes(close_packet(/*request_id*/ 3, &handle))
|
||||
.await
|
||||
.expect("close packet should succeed");
|
||||
assert_eq!(response_status(&responses[0]), SSH_FX_OK);
|
||||
assert_eq!(
|
||||
tokio::fs::read_to_string(path).await.expect("read upload"),
|
||||
"hello"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_paths_that_were_not_allocated() {
|
||||
let tempdir = TempDir::new().expect("create temp dir");
|
||||
let path = tempdir.path().join("uploads").join("note.txt");
|
||||
tokio::fs::create_dir_all(path.parent().expect("path parent"))
|
||||
.await
|
||||
.expect("create upload parent");
|
||||
let mut session = UploadSftpSession::new(Arc::new(Mutex::new(HashSet::new())));
|
||||
|
||||
let responses = session
|
||||
.process_bytes(open_packet(
|
||||
/*request_id*/ 1,
|
||||
&path,
|
||||
SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC,
|
||||
))
|
||||
.await
|
||||
.expect("open packet should be handled");
|
||||
assert_eq!(response_status(&responses[0]), SSH_FX_PERMISSION_DENIED);
|
||||
assert!(!path.exists());
|
||||
}
|
||||
|
||||
fn init_packet() -> Vec<u8> {
|
||||
packet(|payload| {
|
||||
payload.push(SSH_FXP_INIT);
|
||||
payload.extend_from_slice(&3_u32.to_be_bytes());
|
||||
})
|
||||
}
|
||||
|
||||
fn open_packet(request_id: u32, path: &std::path::Path, flags: u32) -> Vec<u8> {
|
||||
packet(|payload| {
|
||||
payload.push(SSH_FXP_OPEN);
|
||||
payload.extend_from_slice(&request_id.to_be_bytes());
|
||||
push_string(payload, path.to_string_lossy().as_bytes());
|
||||
payload.extend_from_slice(&flags.to_be_bytes());
|
||||
payload.extend_from_slice(&0_u32.to_be_bytes());
|
||||
})
|
||||
}
|
||||
|
||||
fn write_packet(request_id: u32, handle: &[u8], offset: u64, data: &[u8]) -> Vec<u8> {
|
||||
packet(|payload| {
|
||||
payload.push(SSH_FXP_WRITE);
|
||||
payload.extend_from_slice(&request_id.to_be_bytes());
|
||||
push_string(payload, handle);
|
||||
payload.extend_from_slice(&offset.to_be_bytes());
|
||||
push_string(payload, data);
|
||||
})
|
||||
}
|
||||
|
||||
fn close_packet(request_id: u32, handle: &[u8]) -> Vec<u8> {
|
||||
packet(|payload| {
|
||||
payload.push(SSH_FXP_CLOSE);
|
||||
payload.extend_from_slice(&request_id.to_be_bytes());
|
||||
push_string(payload, handle);
|
||||
})
|
||||
}
|
||||
|
||||
fn packet_type(packet: &[u8]) -> u8 {
|
||||
packet[4]
|
||||
}
|
||||
|
||||
fn response_status(packet: &[u8]) -> u32 {
|
||||
u32::from_be_bytes(packet[9..13].try_into().expect("status code bytes"))
|
||||
}
|
||||
|
||||
fn response_handle(packet: &[u8]) -> Vec<u8> {
|
||||
let len = u32::from_be_bytes(packet[9..13].try_into().expect("handle len")) as usize;
|
||||
packet[13..13 + len].to_vec()
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ pub(crate) struct ConnectionState {
|
||||
pub(crate) outbound_initialized: Arc<AtomicBool>,
|
||||
pub(crate) outbound_experimental_api_enabled: Arc<AtomicBool>,
|
||||
pub(crate) outbound_opted_out_notification_methods: Arc<RwLock<HashSet<String>>>,
|
||||
pub(crate) binary_writer: Option<mpsc::Sender<Vec<u8>>>,
|
||||
pub(crate) session: Arc<ConnectionSessionState>,
|
||||
}
|
||||
|
||||
@@ -40,11 +41,13 @@ impl ConnectionState {
|
||||
outbound_initialized: Arc<AtomicBool>,
|
||||
outbound_experimental_api_enabled: Arc<AtomicBool>,
|
||||
outbound_opted_out_notification_methods: Arc<RwLock<HashSet<String>>>,
|
||||
binary_writer: Option<mpsc::Sender<Vec<u8>>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
outbound_initialized,
|
||||
outbound_experimental_api_enabled,
|
||||
outbound_opted_out_notification_methods,
|
||||
binary_writer,
|
||||
session: Arc::new(ConnectionSessionState::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ use codex_app_server_protocol::ExperimentalFeatureListParams;
|
||||
use codex_app_server_protocol::FeedbackUploadParams;
|
||||
use codex_app_server_protocol::FsCopyParams;
|
||||
use codex_app_server_protocol::FsCreateDirectoryParams;
|
||||
use codex_app_server_protocol::FsCreateUploadParams;
|
||||
use codex_app_server_protocol::FsGetMetadataParams;
|
||||
use codex_app_server_protocol::FsReadDirectoryParams;
|
||||
use codex_app_server_protocol::FsReadFileParams;
|
||||
@@ -988,6 +989,14 @@ impl McpProcess {
|
||||
self.send_request("fs/writeFile", params).await
|
||||
}
|
||||
|
||||
pub async fn send_fs_create_upload_request(
|
||||
&mut self,
|
||||
params: FsCreateUploadParams,
|
||||
) -> anyhow::Result<i64> {
|
||||
let params = Some(serde_json::to_value(params)?);
|
||||
self.send_request("fs/createUpload", params).await
|
||||
}
|
||||
|
||||
pub async fn send_fs_create_directory_request(
|
||||
&mut self,
|
||||
params: FsCreateDirectoryParams,
|
||||
|
||||
@@ -6,6 +6,8 @@ use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use codex_app_server_protocol::FsChangedNotification;
|
||||
use codex_app_server_protocol::FsCopyParams;
|
||||
use codex_app_server_protocol::FsCreateUploadParams;
|
||||
use codex_app_server_protocol::FsCreateUploadResponse;
|
||||
use codex_app_server_protocol::FsGetMetadataResponse;
|
||||
use codex_app_server_protocol::FsReadDirectoryEntry;
|
||||
use codex_app_server_protocol::FsReadFileResponse;
|
||||
@@ -299,6 +301,37 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn fs_create_upload_allocates_codex_managed_storage() -> Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
let mut mcp = initialized_mcp(&codex_home).await?;
|
||||
|
||||
let request_id = mcp
|
||||
.send_fs_create_upload_request(FsCreateUploadParams {
|
||||
file_name: "../note.txt".to_string(),
|
||||
})
|
||||
.await?;
|
||||
let response: FsCreateUploadResponse = to_response(
|
||||
timeout(
|
||||
DEFAULT_READ_TIMEOUT,
|
||||
mcp.read_stream_until_response_message(RequestId::Integer(request_id)),
|
||||
)
|
||||
.await??,
|
||||
)?;
|
||||
|
||||
let path = response.path.as_path();
|
||||
let parent = path.parent().expect("upload path should have a parent");
|
||||
let canonical_parent = std::fs::canonicalize(parent)?;
|
||||
let canonical_codex_home = std::fs::canonicalize(codex_home.path())?;
|
||||
assert!(canonical_parent.starts_with(canonical_codex_home.join("uploads")));
|
||||
assert_eq!(
|
||||
path.file_name().and_then(|name| name.to_str()),
|
||||
Some("note.txt")
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn fs_write_file_accepts_base64_bytes() -> Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
|
||||
Reference in New Issue
Block a user