Files
codex/codex-rs/plugin-archive-verification/tests/measurement.rs
T
Alex Ionescu 4e5cd2f07d Test remote plugin archive verification end to end (#63527)
## Why

Verifier unit tests exercise the signing checks in isolation. Integration
tests cover how callers handle verification failures, including whether
they preserve an existing cache or write checkout files.

## What changed

Reuse the signed archive fixture with a mock backend to cover these paths:

- `plugin/install` accepts an unsigned archive without fetching signing keys.
- `plugin/reconcile` caches unsigned and valid signed releases, reuses a
  cached version without downloading it again, and preserves that version
  when a newer archive fails verification or signing keys are unavailable.
- `plugin/share/checkout` writes the checkout and local metadata after
  successful verification and leaves them absent for a tampered archive.
- The measurement reference cache rejects a tampered archive and accepts
  valid bytes on retry for the same plugin version.

Wire the tests into Bazel and give the new RPC suite the existing app-server
integration test concurrency limits in nextest.

GitOrigin-RevId: bfac72216ccee6c5a33fe08ef0081fad48ac842d
2026-11-24 03:55:44 +00:00

139 lines
5.3 KiB
Rust

//! Exercises archive verification through the measurement reference cache.
mod common;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use codex_core_plugins::PluginMeasurementTarget;
use codex_core_plugins::PluginsConfigInput;
use codex_core_plugins::RemotePluginMeasurementCache;
use codex_core_plugins::TrustedPluginRoots;
use codex_exec_server::LOCAL_FS;
use codex_http_client::HttpClientFactory;
use codex_http_client::OutboundProxyPolicy;
use codex_login::CodexAuth;
use codex_utils_path_uri::PathUri;
use common::ALLOW_HTTP_BUNDLE_DOWNLOADS;
use common::PluginArchiveSigningFixture;
use pretty_assertions::assert_eq;
use serde_json::json;
use std::process::Command;
use tempfile::TempDir;
use wiremock::Mock;
use wiremock::MockServer;
use wiremock::ResponseTemplate;
use wiremock::matchers::header;
use wiremock::matchers::method;
use wiremock::matchers::path;
use wiremock::matchers::query_param;
#[tokio::test]
async fn signed_reference_rejects_tampering_and_can_retry_with_valid_bytes() -> Result<()> {
// Keep the HTTP override inside the subprocess.
const CHILD: &str = "CODEX_SIGNED_MEASUREMENT_TEST";
if std::env::var_os(CHILD).is_none() {
let output = Command::new(std::env::current_exe()?)
.args([
"--exact",
"signed_reference_rejects_tampering_and_can_retry_with_valid_bytes",
"--nocapture",
])
.env(CHILD, "1")
.env(ALLOW_HTTP_BUNDLE_DOWNLOADS, "1")
.output()?;
ensure!(
output.status.success() && String::from_utf8_lossy(&output.stdout).contains("1 passed"),
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
return Ok(());
}
let signing = PluginArchiveSigningFixture::load()?;
let server = MockServer::start().await;
let config = PluginsConfigInput::new(
Default::default(),
Default::default(),
"openai".to_owned(),
/*plugins_enabled*/ true,
/*remote_plugin_enabled*/ true,
server.uri(),
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
/*product_sku*/ None,
);
let executor = TempDir::new()?;
let script = "plugins/cache/openai-curated-remote/linear/1.2.3/scripts/install.mjs";
let script_path = executor.path().join(script);
std::fs::create_dir_all(script_path.parent().context("script directory")?)?;
std::fs::write(script_path, "")?;
let cwd = PathUri::from_host_native_path(executor.path())?;
let target = PluginMeasurementTarget::from_command(
&["node".to_owned(), script.to_owned()],
&cwd,
LOCAL_FS.as_ref(),
)
.await
.context("canonical plugin target")?;
let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing();
let cache = RemotePluginMeasurementCache::new();
let plugin = json!({
"id": "plugins~linear", "name": "linear", "scope": "GLOBAL", "enabled": true,
"installation_policy": "AVAILABLE", "authentication_policy": "ON_USE", "status": "ENABLED",
"release": {
"version": "1.2.3", "display_name": "Linear", "description": "",
"bundle_download_url": format!("{}/bundle/linear", server.uri()),
"interface": {}, "signing": signing.signing,
},
});
// Retry the same plugin/version through the same cache after failure.
for tampered in [true, false] {
Mock::given(method("GET"))
.and(path("/ps/plugins/installed"))
.and(query_param("scope", "GLOBAL"))
.and(query_param("includeDownloadUrls", "true"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"plugins": [plugin], "pagination": {},
})))
.expect(1)
.mount(&server)
.await;
let mut bytes = signing.archive.clone();
if tampered {
// Still a valid gzip archive: extraction alone must not accept it.
bytes[4] ^= 1;
}
Mock::given(method("GET"))
.and(path("/bundle/linear"))
.respond_with(ResponseTemplate::new(200).set_body_bytes(bytes))
.expect(1)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/ps/plugins/signing-keys"))
.and(header("authorization", "Bearer Access Token"))
.and(header("chatgpt-account-id", "account_id"))
.respond_with(ResponseTemplate::new(200).set_body_json(&signing.signing_keys_response))
.expect(u64::from(!tampered))
.mount(&server)
.await;
let result = cache
.prepare(&config, &auth, &target, /*runtime_auth*/ None)
.await;
if tampered {
let error = match result {
Err(error) => error,
Ok(_) => anyhow::bail!("tampered archive must never return a trusted reference"),
};
assert_eq!(error.to_string(), "plugin measurement preparation failed");
} else {
let reference = result?.context("valid signed reference")?;
assert_ne!(reference.roots(), &TrustedPluginRoots::default());
}
server.verify().await;
server.reset().await;
}
Ok(())
}