diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index e115e833d6..0c1ac879b6 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -3261,6 +3261,7 @@ dependencies = [ "codex-model-provider", "codex-otel", "codex-plugin", + "codex-plugin-archive-verification", "codex-protocol", "codex-runtime-agent-instance", "codex-shell-command", diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index ca91641d83..46b3c131f8 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -292,6 +292,7 @@ codex-ollama = { path = "ollama" } codex-otel = { path = "otel" } codex-otel-trace-websocket = { path = "otel-trace-websocket" } codex-plugin = { path = "plugin" } +codex-plugin-archive-verification = { path = "plugin-archive-verification" } codex-model-provider = { path = "model-provider" } codex-process-hardening = { path = "process-hardening" } codex-protocol = { path = "protocol" } diff --git a/codex-rs/app-server/src/request_processors/plugins.rs b/codex-rs/app-server/src/request_processors/plugins.rs index 7a6b99f80a..4d43b2ccc2 100644 --- a/codex-rs/app-server/src/request_processors/plugins.rs +++ b/codex-rs/app-server/src/request_processors/plugins.rs @@ -2550,6 +2550,9 @@ fn remote_plugin_bundle_install_error_type(err: &RemotePluginBundleInstallError) RemotePluginBundleInstallError::UnsupportedBundleDownloadFinalUrl { .. } => { "remote_bundle_unsupported_download_final_url" } + RemotePluginBundleInstallError::ArchiveVerification { .. } => { + "remote_bundle_archive_verification" + } RemotePluginBundleInstallError::ExtractedBundleTooLarge { .. } => { "remote_bundle_extracted_too_large" } diff --git a/codex-rs/core-plugins/Cargo.toml b/codex-rs/core-plugins/Cargo.toml index 2c190c07bf..7353f2a4ad 100644 --- a/codex-rs/core-plugins/Cargo.toml +++ b/codex-rs/core-plugins/Cargo.toml @@ -32,6 +32,7 @@ codex-mcp = { workspace = true } codex-model-provider = { workspace = true } codex-otel = { workspace = true } codex-plugin = { workspace = true } +codex-plugin-archive-verification = { workspace = true } codex-protocol = { workspace = true } codex-runtime-agent-instance = { workspace = true } codex-shell-command = { workspace = true } diff --git a/codex-rs/core-plugins/src/plugin_measurement_catalog.rs b/codex-rs/core-plugins/src/plugin_measurement_catalog.rs index dc1400f31a..1d953e6e0b 100644 --- a/codex-rs/core-plugins/src/plugin_measurement_catalog.rs +++ b/codex-rs/core-plugins/src/plugin_measurement_catalog.rs @@ -56,7 +56,7 @@ pub(crate) async fn fetch_measurement_reference_bundle( REMOTE_GLOBAL_MARKETPLACE_NAME, &plugin.name, plugin.release.version.as_deref(), - plugin.release.bundle_download_url.as_deref(), + &plugin.release.archive_download, plugin.release.app_manifest, )?; Ok(Some((plugin.id, bundle))) diff --git a/codex-rs/core-plugins/src/plugin_measurement_references.rs b/codex-rs/core-plugins/src/plugin_measurement_references.rs index 4d3a8dce52..069db909e2 100644 --- a/codex-rs/core-plugins/src/plugin_measurement_references.rs +++ b/codex-rs/core-plugins/src/plugin_measurement_references.rs @@ -196,6 +196,7 @@ impl RemotePluginMeasurementCache { return Ok(None); }; if let Some(sender) = initializer { + let auth = auth.clone(); // The cache keeps the receiver alive when individual callers cancel. // Once the cache and all callers disappear, stop waiting/downloading. tokio::spawn(async move { @@ -203,7 +204,7 @@ impl RemotePluginMeasurementCache { biased; _ = sender.closed() => return, reference = async { - let bytes = download_remote_plugin_bundle(&service, &bundle).await?; + let bytes = download_remote_plugin_bundle(&service, Some(&auth), &bundle).await?; // Blocking extraction cannot be aborted once started. Its // owner must keep the store alive even if this task stops. tokio::task::spawn_blocking(move || { diff --git a/codex-rs/core-plugins/src/remote.rs b/codex-rs/core-plugins/src/remote.rs index eb1644a5c8..9cd19d0f2b 100644 --- a/codex-rs/core-plugins/src/remote.rs +++ b/codex-rs/core-plugins/src/remote.rs @@ -33,8 +33,11 @@ use codex_plugin::PluginCapabilitySummary; use codex_plugin::PluginId; use codex_plugin::app_connector_ids_from_declarations; use codex_plugin::prompt_safe_plugin_description; +use codex_plugin_archive_verification::ArchiveVerificationFailure; +use codex_plugin_archive_verification::RemotePluginArchiveDownload; use codex_runtime_agent_instance::RuntimeAuthSlot; use codex_utils_absolute_path::AbsolutePathBuf; +use futures::future::BoxFuture; use http::HeaderMap; use http::Method; use http::StatusCode; @@ -225,19 +228,25 @@ impl RemotePluginServiceConfig { config } + pub(crate) async fn authenticated_request( + &self, + method: Method, + url: &str, + auth: &CodexAuth, + ) -> Result { + let (request, headers) = self + .prepare_authenticated_request(method, url, auth) + .await?; + Ok(request.headers(headers)) + } + // Keep runtime registration out of callers' async stack frames. - pub(crate) fn authenticated_request<'a>( + fn prepare_authenticated_request<'a>( &'a self, method: Method, url: &'a str, auth: &'a CodexAuth, - ) -> std::pin::Pin< - Box< - dyn std::future::Future> - + Send - + 'a, - >, - > { + ) -> BoxFuture<'a, Result<(RequestBuilder, HeaderMap), codex_api::AuthError>> { Box::pin(async move { if self.runtime_auth.is_some() { codex_model_provider::validate_runtime_destination(Some(&self.chatgpt_base_url)) @@ -279,7 +288,7 @@ impl RemotePluginServiceConfig { None => self.http_clients.request(method, url), }; let headers = resolved.auth.apply_auth_headers(HeaderMap::new()).await?; - Ok(request.headers(default_headers()).headers(headers)) + Ok((request.headers(default_headers()), headers)) }) } @@ -427,7 +436,7 @@ pub struct RemotePluginDetail { pub share_url: Option, pub description: Option, pub release_version: Option, - pub bundle_download_url: Option, + pub archive_download: RemotePluginArchiveDownload, pub app_manifest: Option, pub skills: Vec, pub onboarding_skill_name: Option, @@ -757,8 +766,8 @@ struct RemotePluginReleaseResponse { version: Option, display_name: String, description: String, - #[serde(default)] - bundle_download_url: Option, + #[serde(flatten)] + archive_download: RemotePluginArchiveDownload, #[serde(default)] app_ids: Vec, #[serde(default)] @@ -1632,7 +1641,7 @@ async fn build_remote_plugin_detail( share_url: plugin.share_url, description: non_empty_string(Some(&plugin.release.description)), release_version: plugin.release.version, - bundle_download_url: plugin.release.bundle_download_url, + archive_download: plugin.release.archive_download, app_manifest: plugin.release.app_manifest, skills, onboarding_skill_name: plugin.release.onboarding_skill_name, @@ -2485,13 +2494,36 @@ async fn authenticated_request( url: &str, auth: &CodexAuth, ) -> Result { - Ok(config + let request = config .authenticated_request(method, url, auth) .await .map_err(std::io::Error::other) - .map_err(RemotePluginCatalogError::AuthToken)? + .map_err(RemotePluginCatalogError::AuthToken)?; + Ok(with_catalog_request_options(config, request)) +} + +/// Pair the selected transport with the exact credentials used to scope the signing-key cache. +pub(crate) async fn prepare_signing_key_request( + config: &RemotePluginServiceConfig, + url: String, + auth: Option<&CodexAuth>, +) -> Result<(RequestBuilder, HeaderMap), ArchiveVerificationFailure> { + let auth = ensure_chatgpt_auth(auth) + .map_err(|_| ArchiveVerificationFailure::AuthenticationRequired)?; + let (request, headers) = config + .prepare_authenticated_request(Method::GET, &url, auth) + .await + .map_err(|_| ArchiveVerificationFailure::AuthenticationRequired)?; + Ok((with_catalog_request_options(config, request), headers)) +} + +fn with_catalog_request_options( + config: &RemotePluginServiceConfig, + request: RequestBuilder, +) -> RequestBuilder { + request .timeout(REMOTE_PLUGIN_CATALOG_TIMEOUT) - .header(OAI_PRODUCT_SKU_HEADER, &config.product_sku)) + .header(OAI_PRODUCT_SKU_HEADER, &config.product_sku) } async fn send_and_decode Deserialize<'de>>( diff --git a/codex-rs/core-plugins/src/remote/remote_installed_plugin_sync.rs b/codex-rs/core-plugins/src/remote/remote_installed_plugin_sync.rs index c09f0efec1..34822ae430 100644 --- a/codex-rs/core-plugins/src/remote/remote_installed_plugin_sync.rs +++ b/codex-rs/core-plugins/src/remote/remote_installed_plugin_sync.rs @@ -267,7 +267,7 @@ pub(crate) async fn sync_remote_installed_plugin_bundles_once_with_snapshot( &marketplace_name, &plugin.name, release_version, - plugin.release.bundle_download_url.as_deref(), + &plugin.release.archive_download, plugin.release.app_manifest.clone(), ) { Ok(bundle) => bundle, @@ -292,6 +292,7 @@ pub(crate) async fn sync_remote_installed_plugin_bundles_once_with_snapshot( capabilities.include_active_bundle(&store, &plugin_id).await; match crate::remote_bundle::download_and_install_remote_plugin_bundle( config, + Some(auth), codex_home.clone(), bundle, ) diff --git a/codex-rs/core-plugins/src/remote/share/checkout.rs b/codex-rs/core-plugins/src/remote/share/checkout.rs index 223c667b9c..25b03aeea2 100644 --- a/codex-rs/core-plugins/src/remote/share/checkout.rs +++ b/codex-rs/core-plugins/src/remote/share/checkout.rs @@ -85,7 +85,7 @@ pub async fn checkout_remote_plugin_share( &detail.marketplace_name, &plugin_name, detail.release_version.as_deref(), - detail.bundle_download_url.as_deref(), + &detail.archive_download, /*app_manifest*/ None, ) .map_err(|err| { @@ -95,6 +95,7 @@ pub async fn checkout_remote_plugin_share( })?; crate::remote_bundle::download_and_extract_remote_plugin_bundle_to_path( config, + auth, bundle, local_plugin_path.clone(), ) diff --git a/codex-rs/core-plugins/src/remote_bundle.rs b/codex-rs/core-plugins/src/remote_bundle.rs index 9292e2c2c5..1f91cab255 100644 --- a/codex-rs/core-plugins/src/remote_bundle.rs +++ b/codex-rs/core-plugins/src/remote_bundle.rs @@ -3,6 +3,7 @@ use crate::plugin_bundle_archive::PluginBundleUnpackError; use crate::plugin_bundle_archive::unpack_plugin_bundle_tar_gz; use crate::remote::REMOTE_GLOBAL_MARKETPLACE_NAME; use crate::remote::RemotePluginServiceConfig; +use crate::remote::prepare_signing_key_request; use crate::store::PluginInstallResult; use crate::store::PluginStore; use crate::store::PluginStoreError; @@ -10,6 +11,7 @@ use crate::store::error_context_sub_error_type; use crate::store::validate_plugin_version_segment; use codex_http_client::HttpResponse; use codex_http_client::RouteAwareRequestError; +use codex_login::CodexAuth; use codex_plugin::PluginId; use codex_plugin::PluginIdError; use codex_utils_absolute_path::AbsolutePathBuf; @@ -28,6 +30,9 @@ use std::time::Duration; use url::Host; use url::Url; +use codex_plugin_archive_verification::ArchiveVerificationFailure; +use codex_plugin_archive_verification::RemotePluginArchiveDownload; + const REMOTE_PLUGIN_BUNDLE_DOWNLOAD_TIMEOUT: Duration = Duration::from_secs(60); const REMOTE_PLUGIN_BUNDLE_MAX_DOWNLOAD_BYTES: u64 = 100 * 1024 * 1024; const REMOTE_PLUGIN_BUNDLE_ERROR_BODY_MAX_BYTES: u64 = 8 * 1024; @@ -44,6 +49,7 @@ pub struct ValidatedRemotePluginBundle { remote_plugin_id: String, app_manifest: Option, bundle_download_url: String, + archive_download: RemotePluginArchiveDownload, } impl std::fmt::Debug for ValidatedRemotePluginBundle { @@ -128,6 +134,16 @@ pub enum RemotePluginBundleInstallError { )] UnsupportedBundleDownloadFinalUrl { host: String, scheme: String }, + #[error( + "plugin `{plugin_id}` version `{plugin_version}` failed archive verification: {source}" + )] + ArchiveVerification { + plugin_id: String, + plugin_version: String, + #[source] + source: ArchiveVerificationFailure, + }, + #[error( "remote plugin bundle extracted size would be {bytes} bytes, exceeding the maximum total size of {max_bytes} bytes" )] @@ -154,6 +170,7 @@ impl RemotePluginBundleInstallError { pub fn sub_error_type(&self) -> Option { match self { + Self::ArchiveVerification { source, .. } => Some(source.as_str().to_owned()), Self::Io { context, .. } => Some(error_context_sub_error_type(context)), Self::Store(err) => err.sub_error_type(), Self::DownloadStatus { status, .. } => { @@ -180,7 +197,7 @@ pub fn validate_remote_plugin_bundle( remote_marketplace_name: &str, plugin_name: &str, release_version: Option<&str>, - bundle_download_url: Option<&str>, + archive_download: &RemotePluginArchiveDownload, app_manifest: Option, ) -> Result { let plugin_id = PluginId::new(plugin_name.to_string(), remote_marketplace_name.to_string()) @@ -201,7 +218,9 @@ pub fn validate_remote_plugin_bundle( message, } })?; - let bundle_download_url = bundle_download_url + let bundle_download_url = archive_download + .bundle_download_url + .as_deref() .map(str::trim) .filter(|url| !url.is_empty()) .ok_or_else( @@ -234,6 +253,7 @@ pub fn validate_remote_plugin_bundle( remote_plugin_id: remote_plugin_id.to_string(), app_manifest, bundle_download_url, + archive_download: archive_download.clone(), }) } @@ -267,10 +287,11 @@ fn is_loopback_url(url: &Url) -> bool { pub async fn download_and_install_remote_plugin_bundle( config: &RemotePluginServiceConfig, + auth: Option<&CodexAuth>, codex_home: PathBuf, bundle: ValidatedRemotePluginBundle, ) -> Result { - let bundle_bytes = download_remote_plugin_bundle(config, &bundle).await?; + let bundle_bytes = download_remote_plugin_bundle(config, auth, &bundle).await?; tokio::task::spawn_blocking(move || { install_remote_plugin_bundle(codex_home, bundle, bundle_bytes) }) @@ -284,10 +305,11 @@ pub async fn download_and_install_remote_plugin_bundle( pub(crate) async fn download_and_extract_remote_plugin_bundle_to_path( config: &RemotePluginServiceConfig, + auth: Option<&CodexAuth>, bundle: ValidatedRemotePluginBundle, destination: AbsolutePathBuf, ) -> Result { - let bundle_bytes = download_remote_plugin_bundle(config, &bundle).await?; + let bundle_bytes = download_remote_plugin_bundle(config, auth, &bundle).await?; tokio::task::spawn_blocking(move || { extract_remote_plugin_bundle_to_path(bundle, bundle_bytes, destination) }) @@ -301,14 +323,29 @@ pub(crate) async fn download_and_extract_remote_plugin_bundle_to_path( pub(crate) async fn download_remote_plugin_bundle( config: &RemotePluginServiceConfig, + auth: Option<&CodexAuth>, bundle: &ValidatedRemotePluginBundle, ) -> Result, RemotePluginBundleInstallError> { - download_remote_plugin_bundle_with_limit( + let bytes = download_remote_plugin_bundle_with_limit( config, &bundle.bundle_download_url, /*max_bytes*/ REMOTE_PLUGIN_BUNDLE_MAX_DOWNLOAD_BYTES, ) - .await + .await?; + bundle + .archive_download + .verify(bytes, &config.chatgpt_base_url, |url| { + prepare_signing_key_request(config, url, auth) + }) + .await + .map(|(bytes, _)| bytes) + .map_err( + |source| RemotePluginBundleInstallError::ArchiveVerification { + plugin_id: bundle.plugin_id.as_key(), + plugin_version: bundle.plugin_version.clone(), + source, + }, + ) } async fn download_remote_plugin_bundle_with_limit( @@ -696,7 +733,7 @@ mod tests { "openai-curated-remote", "linear", Some("1.2.3"), - Some("https://example.com/linear.tar.gz?sig=signed-token"), + &archive_download("https://example.com/linear.tar.gz?sig=signed-token"), /*app_manifest*/ None, ) .expect("valid install plan"); @@ -718,7 +755,7 @@ mod tests { "openai-curated-remote", "linear", /*release_version*/ None, - Some("https://example.com/linear.tar.gz"), + &archive_download("https://example.com/linear.tar.gz"), /*app_manifest*/ None, ) .expect_err("missing release version should be rejected"); @@ -736,7 +773,7 @@ mod tests { "openai-curated-remote", "linear", Some("../1.2.3"), - Some("https://example.com/linear.tar.gz"), + &archive_download("https://example.com/linear.tar.gz"), /*app_manifest*/ None, ) .expect_err("invalid release version should be rejected"); @@ -754,7 +791,7 @@ mod tests { "openai-curated-remote", "linear", Some("1.2.3"), - /*bundle_download_url*/ None, + &RemotePluginArchiveDownload::default(), /*app_manifest*/ None, ) .expect_err("missing bundle download URL should be rejected"); @@ -772,7 +809,7 @@ mod tests { "openai-curated-remote", "linear", Some("1.2.3"), - Some("http://example.com/linear.tar.gz"), + &archive_download("http://example.com/linear.tar.gz"), /*app_manifest*/ None, ) .expect_err("plain HTTP URLs should be rejected before cloud install"); @@ -903,7 +940,7 @@ mod tests { "workspace-shared-with-me", "linear", Some("backend-version"), - Some("https://example.com/linear.tar.gz"), + &archive_download("https://example.com/linear.tar.gz"), Some(serde_json::json!({ "apps": { "remote": { @@ -1078,13 +1115,17 @@ mod tests { assert_eq!(mode, 0o755); } + fn archive_download(url: &str) -> RemotePluginArchiveDownload { + RemotePluginArchiveDownload::new(Some(url.to_owned())) + } + fn valid_remote_plugin_bundle() -> ValidatedRemotePluginBundle { validate_remote_plugin_bundle( REMOTE_PLUGIN_ID, "openai-curated-remote", "linear", Some("1.2.3"), - Some("https://example.com/linear.tar.gz"), + &archive_download("https://example.com/linear.tar.gz"), /*app_manifest*/ None, ) .expect("valid install plan") diff --git a/codex-rs/core-plugins/src/remote_mutations.rs b/codex-rs/core-plugins/src/remote_mutations.rs index 0ec012d28c..5d103642ca 100644 --- a/codex-rs/core-plugins/src/remote_mutations.rs +++ b/codex-rs/core-plugins/src/remote_mutations.rs @@ -143,7 +143,7 @@ impl PluginsManager { &detail.marketplace_name, &detail.summary.name, detail.release_version.as_deref(), - detail.bundle_download_url.as_deref(), + &detail.archive_download, detail.app_manifest.clone(), ) .map_err(|err| resolved(Error::Bundle(err)))?; @@ -164,6 +164,7 @@ impl PluginsManager { // Materialize first: a failed download must never leave a backend installation behind. let installed = remote_bundle::download_and_install_remote_plugin_bundle( &service, + auth, self.codex_home.clone(), bundle, ) diff --git a/codex-rs/core-plugins/src/remote_runtime_auth_tests.rs b/codex-rs/core-plugins/src/remote_runtime_auth_tests.rs index b9bdadd3eb..6bd0f21ec3 100644 --- a/codex-rs/core-plugins/src/remote_runtime_auth_tests.rs +++ b/codex-rs/core-plugins/src/remote_runtime_auth_tests.rs @@ -22,6 +22,7 @@ use uuid::Uuid; use wiremock::Mock; use wiremock::MockServer; use wiremock::ResponseTemplate; +use wiremock::matchers::header; use wiremock::matchers::path; #[tokio::test] @@ -53,7 +54,13 @@ async fn plugin_runtime_auth_shares_registration_and_strips_cross_origin_auth() .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "plugins": [], "pagination": {"next_page_token": null}, }))) - .expect(2) + .expect(1) + .mount(&server) + .await; + Mock::given(path("/backend-api/ps/plugins/signing-keys")) + .and(header(OAI_PRODUCT_SKU_HEADER, CODEX_PRODUCT_SKU)) + .respond_with(ResponseTemplate::new(200)) + .expect(1) .mount(&server) .await; Mock::given(path("/backend-api/plugins/featured")) @@ -175,24 +182,20 @@ async fn plugin_runtime_auth_shares_registration_and_strips_cross_origin_auth() .contains_key("x-openai-agent-assertion") ); destination.verify().await; - let url = format!("{}/backend-api/ps/plugins/list", server.uri()); - let pending = config - .authenticated_request(Method::GET, &url, &auth) + let url = format!("{}/backend-api/ps/plugins/signing-keys", server.uri()); + let (pending, headers) = prepare_signing_key_request(&config, url.clone(), Some(&auth)) .await .unwrap(); policy.invalidate(); assert!(controller.publish(policy.revision(), DestinationPolicy::Unrestricted)); assert!(matches!( - pending.send().await, + pending.headers(headers).send().await, Err(RouteAwareRequestError::Policy(NetworkPolicyDenied::Revoked)) )); - let response = config - .authenticated_request(Method::GET, &url, &auth) - .await - .unwrap() - .send() + let (request, headers) = prepare_signing_key_request(&config, url, Some(&auth)) .await .unwrap(); + let response = request.headers(headers).send().await.unwrap(); assert_eq!(response.status(), StatusCode::OK); let requests = server.received_requests().await.unwrap(); let mut proof_ids = HashSet::new(); diff --git a/codex-rs/core-plugins/src/remote_tests.rs b/codex-rs/core-plugins/src/remote_tests.rs index 89ab677826..f4a119e8f1 100644 --- a/codex-rs/core-plugins/src/remote_tests.rs +++ b/codex-rs/core-plugins/src/remote_tests.rs @@ -808,7 +808,7 @@ fn directory_plugin(id: &str, name: &str) -> RemotePluginDirectoryItem { version: None, display_name: name.to_string(), description: String::new(), - bundle_download_url: None, + archive_download: RemotePluginArchiveDownload::default(), app_ids: Vec::new(), app_manifest: None, app_templates: Vec::new(),