mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Enforce application network policy across app-server requests (#47407)
## What changed - Load `application.network` requirements at startup and explicit config/account reloads, applying local policy before authentication and cloud configuration bootstrap. - Block traffic when policy loading fails, cancel requests that a new policy no longer permits, and reject configuration loads based on superseded policy snapshots. Local requirements edits take effect on the next explicit reload or restart. - Bind authenticated clients to the current account and revoke them when account ownership changes. Carry policy enforcement through backend requests, realtime connections, workload identity exchanges, and code-mode gRPC streams while preserving gRPC trailers. - Treat denied credential refreshes as policy errors without retrying them or invalidating stored credentials. ## Testing Add regression coverage for startup restrictions, reload ordering, cancellation after restrictive or malformed requirements edits, account revocation, gRPC destination denial, workload identity cancellation, and credential preservation after denied OAuth refresh. GitOrigin-RevId: 0cb4c720a906397e9df06675f01ecf60d741101f
This commit is contained in:
@@ -19,119 +19,106 @@ use super::GrpcClient;
|
||||
|
||||
pub(super) type GrpcTransport = BoxCloneSyncService<Request<Body>, Response<Body>, io::Error>;
|
||||
|
||||
pub(super) struct SharedTransport {
|
||||
endpoint: TransportEndpoint,
|
||||
client: tokio::sync::OnceCell<GrpcClient>,
|
||||
}
|
||||
|
||||
enum TransportEndpoint {
|
||||
pub(super) enum SharedTransport {
|
||||
Url {
|
||||
endpoint: String,
|
||||
http_client_factory: HttpClientFactory,
|
||||
client: tokio::sync::Mutex<Option<(HttpClientFactory, GrpcClient)>>,
|
||||
},
|
||||
Connected(Channel),
|
||||
Connected(GrpcClient),
|
||||
}
|
||||
|
||||
impl SharedTransport {
|
||||
pub(super) fn new(endpoint: String, http_client_factory: HttpClientFactory) -> Self {
|
||||
Self {
|
||||
endpoint: TransportEndpoint::Url {
|
||||
endpoint,
|
||||
http_client_factory,
|
||||
},
|
||||
client: tokio::sync::OnceCell::new(),
|
||||
Self::Url {
|
||||
endpoint,
|
||||
http_client_factory,
|
||||
client: tokio::sync::Mutex::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn with_channel(channel: Channel) -> Self {
|
||||
Self {
|
||||
endpoint: TransportEndpoint::Connected(channel),
|
||||
client: tokio::sync::OnceCell::new(),
|
||||
}
|
||||
let transport = channel.map_err(io::Error::other);
|
||||
Self::Connected(
|
||||
CodeModeHostClient::new(BoxCloneSyncService::new(transport))
|
||||
.max_decoding_message_size(MAX_FRAME_BYTES)
|
||||
.max_encoding_message_size(MAX_FRAME_BYTES),
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) async fn client(&self) -> Result<GrpcClient, String> {
|
||||
self.client
|
||||
.get_or_try_init(|| async {
|
||||
let client = match &self.endpoint {
|
||||
TransportEndpoint::Url { endpoint, .. } if endpoint.starts_with("unix:") => {
|
||||
let channel = Endpoint::from_shared(endpoint.clone())
|
||||
.map_err(|error| {
|
||||
format!("invalid gRPC code-mode Unix socket endpoint: {error}")
|
||||
})?
|
||||
.connect_lazy();
|
||||
let transport = channel.map_err(io::Error::other);
|
||||
CodeModeHostClient::new(BoxCloneSyncService::new(transport))
|
||||
}
|
||||
TransportEndpoint::Url {
|
||||
endpoint,
|
||||
http_client_factory,
|
||||
} => {
|
||||
let target = reqwest::Url::parse(endpoint)
|
||||
.map_err(|error| format!("invalid gRPC code-mode host URL: {error}"))?;
|
||||
if !matches!(target.scheme(), "http" | "https") {
|
||||
return Err("gRPC code-mode host URL must use http or https".to_string());
|
||||
}
|
||||
if !target.username().is_empty() || target.password().is_some() {
|
||||
return Err(
|
||||
"gRPC code-mode host URL must not include credentials".to_string(),
|
||||
);
|
||||
}
|
||||
if target.path() != "/"
|
||||
|| target.query().is_some()
|
||||
|| target.fragment().is_some()
|
||||
{
|
||||
return Err("gRPC code-mode host URL must not include a path, query, or fragment".to_string());
|
||||
}
|
||||
let origin: Uri = endpoint
|
||||
.parse()
|
||||
.map_err(|error| format!("invalid gRPC code-mode host origin: {error}"))?;
|
||||
let endpoint = endpoint.clone();
|
||||
let http_client_factory = http_client_factory.clone();
|
||||
let client = tokio::task::spawn_blocking(move || {
|
||||
http_client_factory
|
||||
.build_reqwest_client(
|
||||
reqwest::Client::builder()
|
||||
.http2_prior_knowledge()
|
||||
.redirect(reqwest::redirect::Policy::none()),
|
||||
&endpoint,
|
||||
ClientRouteClass::Other,
|
||||
)
|
||||
.map_err(|error| {
|
||||
format!(
|
||||
"failed to configure gRPC code-mode host transport: {error}"
|
||||
)
|
||||
})
|
||||
})
|
||||
let (endpoint, http_client_factory, client) = match self {
|
||||
Self::Url {
|
||||
endpoint,
|
||||
http_client_factory,
|
||||
client,
|
||||
} => (endpoint, http_client_factory, client),
|
||||
Self::Connected(client) => return Ok(client.clone()),
|
||||
};
|
||||
let bound_factory = http_client_factory.clone().with_network_policy(
|
||||
http_client_factory
|
||||
.network_policy()
|
||||
.clone()
|
||||
.for_current_account(),
|
||||
);
|
||||
let mut cached = client.lock().await;
|
||||
if let Some((factory, client)) = cached.as_ref()
|
||||
&& factory == &bound_factory
|
||||
{
|
||||
return Ok(client.clone());
|
||||
}
|
||||
let client = if endpoint.starts_with("unix:") {
|
||||
let channel = Endpoint::from_shared(endpoint.clone())
|
||||
.map_err(|error| format!("invalid gRPC code-mode Unix socket endpoint: {error}"))?
|
||||
.connect_lazy();
|
||||
let transport = channel.map_err(io::Error::other);
|
||||
CodeModeHostClient::new(BoxCloneSyncService::new(transport))
|
||||
} else {
|
||||
let target = reqwest::Url::parse(endpoint)
|
||||
.map_err(|error| format!("invalid gRPC code-mode host URL: {error}"))?;
|
||||
if !matches!(target.scheme(), "http" | "https") {
|
||||
return Err("gRPC code-mode host URL must use http or https".to_string());
|
||||
}
|
||||
if !target.username().is_empty() || target.password().is_some() {
|
||||
return Err("gRPC code-mode host URL must not include credentials".to_string());
|
||||
}
|
||||
if target.path() != "/" || target.query().is_some() || target.fragment().is_some() {
|
||||
return Err(
|
||||
"gRPC code-mode host URL must not include a path, query, or fragment"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
let origin: Uri = endpoint
|
||||
.parse()
|
||||
.map_err(|error| format!("invalid gRPC code-mode host origin: {error}"))?;
|
||||
let client = codex_http_client::RouteAwareClientPool::with_builder(
|
||||
bound_factory.clone(),
|
||||
ClientRouteClass::Other,
|
||||
codex_http_client::HttpClientBuilder::new()
|
||||
.without_request_logging()
|
||||
.http2_prior_knowledge()
|
||||
.without_redirects(),
|
||||
);
|
||||
let transport = service_fn(move |request: Request<Body>| {
|
||||
let client = client.clone();
|
||||
async move {
|
||||
let request =
|
||||
request.map(|body| reqwest::Body::wrap_stream(body.into_data_stream()));
|
||||
let request = reqwest::Request::try_from(request).map_err(io::Error::other)?;
|
||||
let response = client
|
||||
.execute(request)
|
||||
.await
|
||||
.map_err(|error| {
|
||||
format!("gRPC code-mode host transport task failed: {error}")
|
||||
})??;
|
||||
let transport = service_fn(move |request: Request<Body>| {
|
||||
let client = client.clone();
|
||||
async move {
|
||||
let request = request.map(|body| {
|
||||
reqwest::Body::wrap_stream(body.into_data_stream())
|
||||
});
|
||||
let request =
|
||||
reqwest::Request::try_from(request).map_err(io::Error::other)?;
|
||||
let response: Response<reqwest::Body> =
|
||||
client.execute(request).await.map_err(io::Error::other)?.into();
|
||||
Ok::<_, io::Error>(response.map(Body::new))
|
||||
}
|
||||
});
|
||||
CodeModeHostClient::with_origin(BoxCloneSyncService::new(transport), origin)
|
||||
}
|
||||
TransportEndpoint::Connected(channel) => {
|
||||
let transport = channel.clone().map_err(io::Error::other);
|
||||
CodeModeHostClient::new(BoxCloneSyncService::new(transport))
|
||||
}
|
||||
};
|
||||
Ok(client
|
||||
.max_decoding_message_size(MAX_FRAME_BYTES)
|
||||
.max_encoding_message_size(MAX_FRAME_BYTES))
|
||||
})
|
||||
.await
|
||||
.cloned()
|
||||
.map_err(io::Error::other)?
|
||||
.into_http_response();
|
||||
Ok::<_, io::Error>(response.map(Body::new))
|
||||
}
|
||||
});
|
||||
CodeModeHostClient::with_origin(BoxCloneSyncService::new(transport), origin)
|
||||
};
|
||||
let client = client
|
||||
.max_decoding_message_size(MAX_FRAME_BYTES)
|
||||
.max_encoding_message_size(MAX_FRAME_BYTES);
|
||||
*cached = Some((bound_factory, client.clone()));
|
||||
Ok(client)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user