From dc9e1478fbde08617c1484d2b15e6a95b60bd943 Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Tue, 24 Nov 2026 00:42:24 +0000 Subject: [PATCH] Verify eval plugin archives with scoped signing keys (#63528) ## Why The archive verifier counted keys returned in `eval_keys` but ignored them during key lookup, so it could not verify archives signed with those keys. ## What changed Accept eval signing keys from `eval_keys` and release signing keys from `keys`. Enforce each list's signing purpose and reject duplicate key IDs across both lists. Reuse the existing cache scoped by endpoint and authentication headers. ## Testing Cover rejection of legacy eval keys in `keys`, successful verification with a scoped eval key, and cache isolation across endpoints, accounts, and tokens. Add an app-server test that checks local installation precedes the remote install request for a valid eval archive and that tampering prevents both operations. GitOrigin-RevId: e8b6132f204c7e4e5f30d1dcb30357bac4c136ab --- .../src/archive_signing_keys.rs | 33 +++-- .../src/archive_signing_keys_tests.rs | 56 +++++++++ .../src/archive_verification_tests.rs | 35 +++++- .../tests/app_server.rs | 116 ++++++++++++++++-- 4 files changed, 218 insertions(+), 22 deletions(-) diff --git a/codex-rs/plugin-archive-verification/src/archive_signing_keys.rs b/codex-rs/plugin-archive-verification/src/archive_signing_keys.rs index 2260a9e1f4..83e5d5e70d 100644 --- a/codex-rs/plugin-archive-verification/src/archive_signing_keys.rs +++ b/codex-rs/plugin-archive-verification/src/archive_signing_keys.rs @@ -20,6 +20,7 @@ use std::time::Instant; use tokio::sync::Semaphore; const MARKETPLACE_ARCHIVE_PURPOSE: &str = "PLUGIN_RELEASE_ARCHIVE_SIGNING"; +const EVAL_ARCHIVE_PURPOSE: &str = "EVAL_PLUGIN_RELEASE_ARCHIVE_SIGNING"; const MAX_CACHED_SCOPES: usize = 8; const MAX_SIGNING_KEYS: usize = 64; const MAX_SIGNING_KEYS_RESPONSE_BYTES: usize = 128 * 1024; @@ -108,6 +109,7 @@ impl SigningKey { pub(crate) fn archive_kind(&self) -> &'static str { match self.purpose.as_str() { MARKETPLACE_ARCHIVE_PURPOSE => "release", + EVAL_ARCHIVE_PURPOSE => "eval", _ => "unknown", } } @@ -263,20 +265,25 @@ async fn fetch_signing_keys( return Err(ArchiveVerificationFailure::InvalidSigningKeysResponse); } let mut keys = HashMap::with_capacity(key_count); - // Older servers published eval keys to all callers in `keys`. - for key in response.keys { - if key.key_id.is_empty() - || key.public_key_spki_der_base64.len() > MAX_PUBLIC_KEY_BASE64_BYTES - || keys.contains_key(&key.key_id) - { - return Err(ArchiveVerificationFailure::InvalidSigningKeysResponse); + for (list, expected_purpose) in [ + // Older servers published eval keys to all callers in `keys`. Only `eval_keys` is scoped. + (response.keys, MARKETPLACE_ARCHIVE_PURPOSE), + (response.eval_keys, EVAL_ARCHIVE_PURPOSE), + ] { + for key in list { + if key.key_id.is_empty() + || key.public_key_spki_der_base64.len() > MAX_PUBLIC_KEY_BASE64_BYTES + || keys.contains_key(&key.key_id) + { + return Err(ArchiveVerificationFailure::InvalidSigningKeysResponse); + } + keys.insert( + key.key_id.clone(), + (key.purpose == expected_purpose) + .then_some(key) + .ok_or(ArchiveVerificationFailure::UnsupportedKeyPurpose), + ); } - keys.insert( - key.key_id.clone(), - (key.purpose == MARKETPLACE_ARCHIVE_PURPOSE) - .then_some(key) - .ok_or(ArchiveVerificationFailure::UnsupportedKeyPurpose), - ); } let now = Instant::now(); let expires_at = now diff --git a/codex-rs/plugin-archive-verification/src/archive_signing_keys_tests.rs b/codex-rs/plugin-archive-verification/src/archive_signing_keys_tests.rs index 5f957941ad..7c3134dd80 100644 --- a/codex-rs/plugin-archive-verification/src/archive_signing_keys_tests.rs +++ b/codex-rs/plugin-archive-verification/src/archive_signing_keys_tests.rs @@ -375,8 +375,11 @@ async fn canceled_refresh_leaves_waiter_free_to_fetch_new_keys() { #[tokio::test] async fn rejects_invalid_key_responses() { let valid = key_response(); + let mut eval_key = valid["keys"][0].clone(); + eval_key["purpose"] = json!(EVAL_ARCHIVE_PURPOSE); for response in [ json!({"keys": [], "cache_ttl_seconds": 600}), + json!({"keys": valid["keys"], "eval_keys": [eval_key], "cache_ttl_seconds": 600}), json!({"keys": valid["keys"], "cache_ttl_seconds": -1}), ] { let server = MockServer::start().await; @@ -400,3 +403,56 @@ async fn rejects_invalid_key_responses() { ); } } + +#[tokio::test] +async fn eval_key_cache_requires_the_same_endpoint_account_and_token() { + let server = MockServer::start().await; + let cache = SigningKeyCache::default(); + let mut keys = key_response(); + let mut eval_key = keys["keys"][0].clone(); + eval_key["key_id"] = json!("eval-key"); + eval_key["purpose"] = json!(EVAL_ARCHIVE_PURPOSE); + keys["eval_keys"] = json!([eval_key]); + Mock::given(method("GET")) + .and(path("/first/ps/plugins/signing-keys")) + .and(header("authorization", "Bearer e30.e30.b2xk")) + .and(header("chatgpt-account-id", "employee")) + .respond_with(ResponseTemplate::new(200).set_body_json(keys)) + .with_priority(/*p*/ 1) + .expect(1) + .mount(&server) + .await; + // Other callers and endpoints publish only the marketplace key. + Mock::given(method("GET")) + .respond_with(ResponseTemplate::new(200).set_body_json(key_response())) + .expect(3) + .mount(&server) + .await; + let eval_key: SigningKey = serde_json::from_value(eval_key).unwrap(); + let unknown = ArchiveVerificationFailure::UnknownSigningKey; + for (endpoint, account, token, expected) in [ + ("first", "employee", "e30.e30.b2xk", Ok(eval_key)), + ("second", "employee", "e30.e30.b2xk", Err(unknown)), + ("first", "other", "e30.e30.b2xk", Err(unknown)), + ("first", "employee", "e30.e30.bmV3", Err(unknown)), + ] { + let base_url = format!("{}/{endpoint}", server.uri()); + let auth = auth_headers(token, account); + assert_eq!( + cache + .key(&base_url, "eval-key", key_request(auth.clone())) + .await, + expected + ); + if expected.is_ok() { + assert_eq!( + cache + .key(&base_url, "eval-key", |_| async { + Err(ArchiveVerificationFailure::AuthenticationRequired) + }) + .await, + Err(ArchiveVerificationFailure::AuthenticationRequired) + ); + } + } +} diff --git a/codex-rs/plugin-archive-verification/src/archive_verification_tests.rs b/codex-rs/plugin-archive-verification/src/archive_verification_tests.rs index 2b4ff924f8..55f88cad33 100644 --- a/codex-rs/plugin-archive-verification/src/archive_verification_tests.rs +++ b/codex-rs/plugin-archive-verification/src/archive_verification_tests.rs @@ -47,7 +47,15 @@ async fn server_with_keys(keys: Value) -> (MockServer, String) { async fn verifies_production_compatible_archive_and_rejects_tampering() { let cache = SigningKeyCache::default(); let (archive, fixture) = signed_fixture(); - let (_server, base_url) = server_with_keys(fixture["signing_keys_response"].clone()).await; + // Legacy servers included eval keys in the same list as marketplace keys for all callers. + let mut keys = fixture["signing_keys_response"].clone(); + let mut eval_key = keys["keys"][0].clone(); + eval_key["key_id"] = json!("old-eval-key"); + eval_key["purpose"] = json!("EVAL_PLUGIN_RELEASE_ARCHIVE_SIGNING"); + keys["keys"].as_array_mut().unwrap().push(eval_key.clone()); + eval_key["key_id"] = json!("scoped-eval-key"); + keys["eval_keys"] = json!([eval_key]); + let (_server, base_url) = server_with_keys(keys).await; let auth = auth_headers("user-token", "account"); let metadata = download(&fixture["signing"]); @@ -86,6 +94,31 @@ async fn verifies_production_compatible_archive_and_rejects_tampering() { Err(ArchiveVerificationFailure::SigningKeysUnavailable) ); + let mut eval_signing = fixture["signing"].clone(); + eval_signing["key_id"] = json!("old-eval-key"); + assert_eq!( + download(&eval_signing) + .verify( + archive.clone(), + &base_url, + &cache, + key_request(auth.clone()) + ) + .await, + Err(ArchiveVerificationFailure::UnsupportedKeyPurpose) + ); + eval_signing["key_id"] = json!("scoped-eval-key"); + assert_eq!( + download(&eval_signing) + .verify( + archive.clone(), + &base_url, + &cache, + key_request(auth.clone()) + ) + .await, + Ok((archive.clone(), PluginArchiveVerification::Verified)) + ); let mut altered_archive = archive.clone(); altered_archive[0] ^= 1; assert_eq!( diff --git a/codex-rs/plugin-archive-verification/tests/app_server.rs b/codex-rs/plugin-archive-verification/tests/app_server.rs index 746922c1af..a9efed2f27 100644 --- a/codex-rs/plugin-archive-verification/tests/app_server.rs +++ b/codex-rs/plugin-archive-verification/tests/app_server.rs @@ -23,12 +23,15 @@ use pretty_assertions::assert_eq; use serde_json::Value; use serde_json::json; use std::path::Path; +use std::path::PathBuf; use std::time::Duration; use tempfile::TempDir; use tokio::time::timeout; use wiremock::Match; use wiremock::Mock; +use wiremock::MockBuilder; use wiremock::MockServer; +use wiremock::Request; use wiremock::ResponseTemplate; use wiremock::matchers::header; use wiremock::matchers::method; @@ -115,6 +118,31 @@ fn bundle(bytes: Vec) -> Mock { .respond_with(ResponseTemplate::new(200).set_body_bytes(bytes)) } +fn remote_plugin_install(remote_plugin_id: &str) -> MockBuilder { + Mock::given(method("POST")) + .and(path(format!( + "/backend-api/ps/plugins/{remote_plugin_id}/install" + ))) + .and(header("authorization", "Bearer chatgpt-token")) + .and(header("chatgpt-account-id", "account-123")) +} + +fn remote_plugin_install_response(remote_plugin_id: &str) -> ResponseTemplate { + ResponseTemplate::new(200).set_body_json(json!({ + "id": remote_plugin_id, + "enabled": true, + })) +} + +#[derive(Debug)] +struct CacheManifestExists(PathBuf); + +impl Match for CacheManifestExists { + fn matches(&self, _request: &Request) -> bool { + self.0.is_file() + } +} + fn signing_keys(response: ResponseTemplate) -> Mock { Mock::given(method("GET")) .and(path("/backend-api/ps/plugins/signing-keys")) @@ -153,14 +181,8 @@ async fn unsigned_install_skips_signing_keys() -> Result<()> { .mount(&server) .await; bundle(fixture.archive).expect(1).mount(&server).await; - Mock::given(method("POST")) - .and(path(format!( - "/backend-api/ps/plugins/{REMOTE_PLUGIN_ID}/install" - ))) - .respond_with(ResponseTemplate::new(200).set_body_json(json!({ - "id": REMOTE_PLUGIN_ID, - "enabled": true, - }))) + remote_plugin_install(REMOTE_PLUGIN_ID) + .respond_with(remote_plugin_install_response(REMOTE_PLUGIN_ID)) .expect(1) .mount(&server) .await; @@ -414,3 +436,81 @@ async fn share_checkout_verifies_before_writing_local_files() -> Result<()> { } Ok(()) } + +#[tokio::test] +async fn install_verifies_eval_archives_before_cloud_mutation() -> Result<()> { + let mut fixture = PluginArchiveSigningFixture::load()?; + let mut eval_key = fixture.signing_keys_response["keys"][0].clone(); + eval_key["key_id"] = json!("scoped-eval-key"); + eval_key["purpose"] = json!("EVAL_PLUGIN_RELEASE_ARCHIVE_SIGNING"); + fixture.signing_keys_response["eval_keys"] = json!([eval_key]); + fixture.signing["key_id"] = json!("scoped-eval-key"); + for tampered in [false, true] { + let server = MockServer::start().await; + let codex_home = TempDir::new()?; + let installed = codex_home.path().join(CACHE_DIR).join("1.2.3"); + let requested_id = "linear-alias"; + let mut plugin = catalog_plugin(&server, "1.2.3"); + plugin["release"]["signing"] = fixture.signing.clone(); + catalog_detail(requested_id, plugin).mount(&server).await; + installed_snapshot(query_param("scope", "GLOBAL"), Vec::new()) + .mount(&server) + .await; + let archive = fixture.archive.clone(); + bundle(if tampered { + tampered_archive(archive) + } else { + archive + }) + .expect(1) + .mount(&server) + .await; + signing_keys(ResponseTemplate::new(200).set_body_json(&fixture.signing_keys_response)) + .expect(u64::from(!tampered)) + .mount(&server) + .await; + remote_plugin_install(requested_id) + .and(CacheManifestExists(installed.join(MANIFEST))) + .respond_with(remote_plugin_install_response(requested_id)) + .mount(&server) + .await; + let mut app = app_server(&server, codex_home.path())? + .build_initialized() + .await?; + app.start_thread(Default::default()).await?; + let id = app + .send_plugin_install_request(PluginInstallParams { + marketplace_path: None, + remote_marketplace_name: Some("caller-marketplace-is-ignored".to_owned()), + install_attempt_id: None, + plugin_name: requested_id.to_owned(), + }) + .await?; + if tampered { + assert_digest_error(&mut app, id).await?; + } else { + let response: PluginInstallResponse = + timeout(DEFAULT_TIMEOUT, app.read_response(id)).await??; + assert_eq!( + response, + PluginInstallResponse { + auth_policy: PluginAuthPolicy::OnUse, + apps_needing_auth: Vec::new(), + } + ); + } + assert_eq!(installed.is_dir(), !tampered); + let requests = server + .received_requests() + .await + .context("recorded requests")?; + assert_eq!( + requests + .iter() + .filter(|req| req.method == "POST" && req.url.path().ends_with("/install")) + .count(), + usize::from(!tampered) + ); + } + Ok(()) +}