mirror of
https://github.com/openai/codex.git
synced 2026-12-01 00:20:48 +00:00
Honor network policy in history notes and image generation extensions (#47742)
## Why History notes and image generation requests used default HTTP clients that did not carry the configured application network policy. ## What changed Pass `Config::http_client_factory()` into both extension backends and create transports with `ClientRouteClass::Api`, so their requests honor application network restrictions and configured proxy behavior. ## Testing Add regression coverage verifying that denied image requests never reach the server and that history notes context requests stop after the network policy becomes restrictive. Retain coverage for image file access restrictions. GitOrigin-RevId: 7ba61fdc60fa1afe1842d869da9ccd744ab7d82f
This commit is contained in:
Generated
+2
-1
@@ -3560,11 +3560,11 @@ name = "codex-history-notes-extension"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"codex-analytics",
|
||||
"codex-api",
|
||||
"codex-client",
|
||||
"codex-config",
|
||||
"codex-core",
|
||||
"codex-extension-api",
|
||||
"codex-http-client",
|
||||
"codex-login",
|
||||
"codex-model-provider",
|
||||
"codex-model-provider-info",
|
||||
@@ -3665,6 +3665,7 @@ dependencies = [
|
||||
"codex-exec-server",
|
||||
"codex-extension-api",
|
||||
"codex-extension-items",
|
||||
"codex-http-client",
|
||||
"codex-login",
|
||||
"codex-model-provider",
|
||||
"codex-model-provider-info",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::collections::BTreeSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Context;
|
||||
@@ -8,6 +9,8 @@ use codex_core::config::Constrained;
|
||||
use codex_extension_api::ExtensionRegistry;
|
||||
use codex_extension_api::ExtensionRegistryBuilder;
|
||||
use codex_features::Feature;
|
||||
use codex_http_client::DestinationPolicy;
|
||||
use codex_http_client::NetworkPolicyController;
|
||||
use codex_image_generation_extension::install as install_image_generation_extension;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_protocol::config_types::ApprovalsReviewer;
|
||||
@@ -67,11 +70,14 @@ fn image_generation_extensions(
|
||||
Arc::new(extension_builder.build())
|
||||
}
|
||||
|
||||
#[test_case(false; "file_access")]
|
||||
#[test_case(true; "application_network")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn extension_tool_receives_turn_environment_sandbox() -> Result<()> {
|
||||
async fn extension_tool_respects_restrictions(deny_network: bool) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = responses::start_mock_server().await;
|
||||
let responses_url = format!("{}/v1/responses", server.uri()).parse()?;
|
||||
let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing();
|
||||
let extensions = image_generation_extensions(&auth, |config| Some(config.codex_home.clone()));
|
||||
let mut builder = test_codex()
|
||||
@@ -81,12 +87,21 @@ async fn extension_tool_receives_turn_environment_sandbox() -> Result<()> {
|
||||
model_info.use_responses_lite = true;
|
||||
model_info.input_modalities = vec![InputModality::Text, InputModality::Image];
|
||||
})
|
||||
.with_config(|config| {
|
||||
.with_config(move |config| {
|
||||
assert!(config.web_search_mode.set(WebSearchMode::Live).is_ok());
|
||||
if deny_network {
|
||||
let controller = NetworkPolicyController::default();
|
||||
let policy = controller
|
||||
.policy()
|
||||
.restrict_to_endpoints(BTreeSet::from([responses_url]));
|
||||
assert!(controller.publish(policy.revision(), DestinationPolicy::Unrestricted));
|
||||
config.application_network_policy = policy;
|
||||
config.respect_system_proxy = false;
|
||||
}
|
||||
});
|
||||
let test = builder.build(&server).await?;
|
||||
let denied_path = test.config.cwd.join("denied.png");
|
||||
std::fs::write(&denied_path, b"not readable")?;
|
||||
std::fs::write(&denied_path, TINY_PNG_BYTES)?;
|
||||
|
||||
let call_id = "image-edit-denied";
|
||||
let response_mock = responses::mount_sse_sequence(
|
||||
@@ -116,15 +131,17 @@ async fn extension_tool_receives_turn_environment_sandbox() -> Result<()> {
|
||||
.await;
|
||||
|
||||
let mut file_system_sandbox_policy = FileSystemSandboxPolicy::default();
|
||||
file_system_sandbox_policy
|
||||
.entries
|
||||
.push(FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path {
|
||||
path: denied_path.clone().into(),
|
||||
},
|
||||
access: FileSystemAccessMode::Deny,
|
||||
missing_path_behavior: None,
|
||||
});
|
||||
if !deny_network {
|
||||
file_system_sandbox_policy
|
||||
.entries
|
||||
.push(FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path {
|
||||
path: denied_path.clone().into(),
|
||||
},
|
||||
access: FileSystemAccessMode::Deny,
|
||||
missing_path_behavior: None,
|
||||
});
|
||||
}
|
||||
let permission_profile = PermissionProfile::from_runtime_permissions(
|
||||
&file_system_sandbox_policy,
|
||||
NetworkSandboxPolicy::Restricted,
|
||||
@@ -140,13 +157,29 @@ async fn extension_tool_receives_turn_environment_sandbox() -> Result<()> {
|
||||
.function_call_output_content_and_success(call_id)
|
||||
.and_then(|(content, _)| content)
|
||||
.context("extension error text should be present")?;
|
||||
assert!(
|
||||
output.starts_with(&format!(
|
||||
"unable to read referenced image at `{}`:",
|
||||
denied_path.display()
|
||||
)),
|
||||
"unexpected extension error: {output}"
|
||||
);
|
||||
if deny_network {
|
||||
assert!(
|
||||
output.contains("destination denied by application network policy"),
|
||||
"{output}"
|
||||
);
|
||||
let requests = server
|
||||
.received_requests()
|
||||
.await
|
||||
.context("image request history should be recorded")?;
|
||||
assert!(
|
||||
requests
|
||||
.iter()
|
||||
.all(|request| !request.url.path().starts_with("/v1/images/"))
|
||||
);
|
||||
} else {
|
||||
assert!(
|
||||
output.starts_with(&format!(
|
||||
"unable to read referenced image at `{}`:",
|
||||
denied_path.display()
|
||||
)),
|
||||
"unexpected extension error: {output}"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
codex-analytics = { workspace = true }
|
||||
codex-api = { workspace = true }
|
||||
codex-client = { workspace = true }
|
||||
codex-core = { workspace = true }
|
||||
codex-extension-api = { workspace = true }
|
||||
codex-http-client = { workspace = true }
|
||||
codex-login = { workspace = true }
|
||||
codex-model-provider = { workspace = true }
|
||||
codex-protocol = { workspace = true }
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_api::ReqwestTransport;
|
||||
use codex_client::HttpTransport;
|
||||
use codex_client::RequestBody;
|
||||
use codex_login::default_client::create_client;
|
||||
use codex_http_client::ClientRouteClass;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_login::default_client::create_transport_for_routes_async;
|
||||
use codex_model_provider::SharedModelProvider;
|
||||
use codex_utils_output_truncation::TruncationPolicy;
|
||||
use http::HeaderValue;
|
||||
@@ -19,11 +20,18 @@ const OPERATION_ERROR_PREFIX: &str = "Unable to perform operation:";
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct HistoryNotesBackend {
|
||||
provider: SharedModelProvider,
|
||||
http_client_factory: HttpClientFactory,
|
||||
}
|
||||
|
||||
impl HistoryNotesBackend {
|
||||
pub(crate) fn new(provider: SharedModelProvider) -> Self {
|
||||
Self { provider }
|
||||
pub(crate) fn new(
|
||||
provider: SharedModelProvider,
|
||||
http_client_factory: HttpClientFactory,
|
||||
) -> Self {
|
||||
Self {
|
||||
provider,
|
||||
http_client_factory,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn call(
|
||||
@@ -82,7 +90,13 @@ impl HistoryNotesBackend {
|
||||
let request = auth.apply_auth(request).await.map_err(|_| {
|
||||
format!("{OPERATION_ERROR_PREFIX} Could not apply backend authentication.")
|
||||
})?;
|
||||
let response = ReqwestTransport::from_http_client(create_client())
|
||||
let transport = create_transport_for_routes_async(
|
||||
self.http_client_factory.clone(),
|
||||
ClientRouteClass::Api,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| format!("{OPERATION_ERROR_PREFIX} The backend request failed."))?;
|
||||
let response = transport
|
||||
.execute(request)
|
||||
.await
|
||||
.map_err(|_| format!("{OPERATION_ERROR_PREFIX} The backend request failed."))?;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_http_client::OutboundProxyPolicy;
|
||||
use codex_login::AuthHeaders;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::CodexAuth;
|
||||
@@ -43,7 +45,10 @@ async fn routes_through_codex_backend_and_injects_trusted_session_agent_context(
|
||||
))),
|
||||
Some(auth_manager),
|
||||
);
|
||||
let backend = HistoryNotesBackend::new(provider);
|
||||
let backend = HistoryNotesBackend::new(
|
||||
provider,
|
||||
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
);
|
||||
|
||||
let response = backend
|
||||
.call(
|
||||
@@ -116,13 +121,16 @@ async fn marks_encrypted_history_and_notes_arguments_without_changing_the_json_b
|
||||
|
||||
let auth_manager =
|
||||
AuthManager::from_auth_for_testing(CodexAuth::Headers(AuthHeaders::new(HeaderMap::new())));
|
||||
let backend = HistoryNotesBackend::new(create_model_provider(
|
||||
ModelProviderInfo::create_openai_provider(Some(format!(
|
||||
"{}/backend-api/codex",
|
||||
server.uri()
|
||||
))),
|
||||
Some(auth_manager),
|
||||
));
|
||||
let backend = HistoryNotesBackend::new(
|
||||
create_model_provider(
|
||||
ModelProviderInfo::create_openai_provider(Some(format!(
|
||||
"{}/backend-api/codex",
|
||||
server.uri()
|
||||
))),
|
||||
Some(auth_manager),
|
||||
),
|
||||
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
);
|
||||
|
||||
for (route, arguments) in cases {
|
||||
backend
|
||||
|
||||
@@ -52,10 +52,13 @@ impl HistoryNotesExtension {
|
||||
&& self.auth_manager.current_auth_uses_codex_backend()
|
||||
{
|
||||
thread_store.insert(HistoryNotesExtensionConfig {
|
||||
backend: HistoryNotesBackend::new(create_model_provider(
|
||||
config.model_provider.clone(),
|
||||
Some(self.auth_manager.clone()),
|
||||
)),
|
||||
backend: HistoryNotesBackend::new(
|
||||
create_model_provider(
|
||||
config.model_provider.clone(),
|
||||
Some(self.auth_manager.clone()),
|
||||
),
|
||||
config.http_client_factory(),
|
||||
),
|
||||
});
|
||||
} else {
|
||||
thread_store.remove::<HistoryNotesExtensionConfig>();
|
||||
|
||||
@@ -19,6 +19,8 @@ use codex_extension_api::ToolExecutor;
|
||||
use codex_extension_api::ToolName;
|
||||
use codex_extension_api::ToolPayload;
|
||||
use codex_history_notes_extension::install;
|
||||
use codex_http_client::DestinationPolicy;
|
||||
use codex_http_client::NetworkPolicyController;
|
||||
use codex_login::AuthHeaders;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::CodexAuth;
|
||||
@@ -73,6 +75,12 @@ async fn installed_extension_exposes_and_invokes_history_notes_tools() -> TestRe
|
||||
use_history_notes_extension: true,
|
||||
..TokenBudgetConfig::default()
|
||||
});
|
||||
let controller = NetworkPolicyController::default();
|
||||
config.application_network_policy = controller.policy();
|
||||
assert!(controller.publish(
|
||||
controller.policy().revision(),
|
||||
DestinationPolicy::Unrestricted
|
||||
));
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
@@ -342,6 +350,27 @@ async fn installed_extension_exposes_and_invokes_history_notes_tools() -> TestRe
|
||||
);
|
||||
}
|
||||
|
||||
server.reset().await;
|
||||
assert!(controller.publish(
|
||||
controller.policy().revision(),
|
||||
DestinationPolicy::Restricted {
|
||||
allowed_hosts: Default::default(),
|
||||
},
|
||||
));
|
||||
assert!(
|
||||
registry.context_contributors()[0]
|
||||
.contribute_thread_context(&session_store, &thread_store)
|
||||
.await
|
||||
.is_empty()
|
||||
);
|
||||
assert!(
|
||||
server
|
||||
.received_requests()
|
||||
.await
|
||||
.expect("recorded requests")
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
let mut disabled_config = config.clone();
|
||||
disabled_config.token_budget = None;
|
||||
for contributor in registry.config_contributors() {
|
||||
|
||||
@@ -19,6 +19,7 @@ codex-core = { workspace = true }
|
||||
codex-exec-server = { workspace = true }
|
||||
codex-extension-api = { workspace = true }
|
||||
codex-extension-items = { workspace = true }
|
||||
codex-http-client = { workspace = true }
|
||||
codex-login = { workspace = true }
|
||||
codex-model-provider = { workspace = true }
|
||||
codex-model-provider-info = { workspace = true }
|
||||
|
||||
@@ -5,8 +5,10 @@ use codex_api::ImageResponse;
|
||||
use codex_api::ImagesClient;
|
||||
use codex_api::ReqwestTransport;
|
||||
use codex_api::map_api_error;
|
||||
use codex_http_client::ClientRouteClass;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_login::default_client::add_originator_header;
|
||||
use codex_login::default_client::create_client;
|
||||
use codex_login::default_client::create_transport_for_routes_async;
|
||||
use codex_model_provider::SharedModelProvider;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use http::HeaderMap;
|
||||
@@ -55,14 +57,20 @@ impl ImageBackendError {
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct CodexImagesBackend {
|
||||
provider: SharedModelProvider,
|
||||
http_client_factory: HttpClientFactory,
|
||||
originator: Option<String>,
|
||||
}
|
||||
|
||||
impl CodexImagesBackend {
|
||||
/// Creates a backend that sends image requests through the active model provider.
|
||||
pub(crate) fn new(provider: SharedModelProvider, originator: Option<String>) -> Self {
|
||||
pub(crate) fn new(
|
||||
provider: SharedModelProvider,
|
||||
http_client_factory: HttpClientFactory,
|
||||
originator: Option<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
provider,
|
||||
http_client_factory,
|
||||
originator,
|
||||
}
|
||||
}
|
||||
@@ -79,11 +87,13 @@ impl CodexImagesBackend {
|
||||
.api_auth()
|
||||
.await
|
||||
.map_err(|err| ImageBackendError::from_message(err.to_string()))?;
|
||||
Ok(ImagesClient::new(
|
||||
ReqwestTransport::from_http_client(create_client()),
|
||||
provider,
|
||||
auth,
|
||||
))
|
||||
let transport = create_transport_for_routes_async(
|
||||
self.http_client_factory.clone(),
|
||||
ClientRouteClass::Api,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| ImageBackendError::from_message(err.to_string()))?;
|
||||
Ok(ImagesClient::new(transport, provider, auth))
|
||||
}
|
||||
|
||||
/// Sends a standalone image generation request through the configured Images client.
|
||||
|
||||
@@ -11,6 +11,7 @@ use codex_extension_api::ThreadStartInput;
|
||||
use codex_extension_api::ToolCall;
|
||||
use codex_extension_api::ToolContributor;
|
||||
use codex_extension_api::ToolExecutor;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_login::AuthManager;
|
||||
use codex_model_provider::create_model_provider;
|
||||
use codex_model_provider_info::ModelProviderInfo;
|
||||
@@ -30,6 +31,7 @@ type SaveRootResolver = dyn Fn(&Config) -> Option<AbsolutePathBuf> + Send + Sync
|
||||
#[derive(Clone)]
|
||||
struct ImageGenerationExtensionConfig {
|
||||
available: bool,
|
||||
http_client_factory: HttpClientFactory,
|
||||
provider: ModelProviderInfo,
|
||||
save_root: Option<AbsolutePathBuf>,
|
||||
}
|
||||
@@ -41,6 +43,7 @@ impl ImageGenerationExtensionConfig {
|
||||
available: config.model_provider.is_openai()
|
||||
|| config.model_provider.requires_openai_auth
|
||||
|| config.model_provider.uses_openai_actor_authorization(),
|
||||
http_client_factory: config.http_client_factory(),
|
||||
provider: config.model_provider.clone(),
|
||||
save_root: resolve_save_root(config),
|
||||
}
|
||||
@@ -97,6 +100,7 @@ impl ToolContributor for ImageGenerationExtension {
|
||||
vec![Arc::new(ImageGenerationTool::new(
|
||||
CodexImagesBackend::new(
|
||||
create_model_provider(config.provider.clone(), Some(self.auth_manager.clone())),
|
||||
config.http_client_factory.clone(),
|
||||
thread_store
|
||||
.get::<ThreadOriginator>()
|
||||
.map(|originator| originator.0.clone()),
|
||||
|
||||
Reference in New Issue
Block a user