Files
codex/codex-rs/network-proxy/src/main.rs
T
viyatb-oai bb1037ba04 Compose managed domain rules across network config layers (#62882)
## Why

Managed requirements from different sources could not be combined when one
used `[permissions.network]` and another used `[network_proxy]` or
`[experimental_network]`.

## What changed

Allow these tables to compose across sources. The highest-priority list of
each kind replaces lower lists of that kind, while keyed `domains` entries
update individual selectors by source priority. A deny wins when a selector
appears in both surviving lists. A surviving `allowed_domains = []` clears
lower-priority grants, while an empty `domains` map contributes no updates.

Require mirrored domain declarations to agree on how they compose with other
domain sources. Unix socket path rules still require a single network table
spelling across sources.

Carry equivalent denied selector spellings into the effective proxy policy.
When local binding is disabled, an attached executor's explicit local grant
must also satisfy the controller's allowlist constraints. The standalone
proxy can still narrow a wildcard to an explicit local grant.

## Testing

Cover mixed table spellings, list replacement and clearing, conflicting
mirrors, equivalent IPv6 spellings, and local grants in standalone and
attached proxy modes.

GitOrigin-RevId: b3d550d9f462564c89663ea4b15e723f0ea76881
2026-11-21 01:28:12 +00:00

107 lines
3.0 KiB
Rust

#![deny(clippy::print_stdout, clippy::print_stderr)]
use anyhow::Context as _;
use anyhow::Result;
use anyhow::ensure;
use clap::Parser;
use codex_network_proxy::ConfigReloader;
use codex_network_proxy::ConfigReloaderFuture;
use codex_network_proxy::ConfigState;
use codex_network_proxy::NetworkProxy;
use codex_network_proxy::NetworkProxyState;
use codex_network_proxy::Platform;
use codex_network_proxy::StaticNetworkProxyConfig;
use std::io::Read;
use std::path::PathBuf;
use std::sync::Arc;
use tracing_subscriber::EnvFilter;
const MAX_CONFIG_BYTES: usize = 1024 * 1024;
#[derive(Debug, Parser)]
#[command(name = "codex-network-proxy", about = "Codex network policy proxy")]
struct Args {
/// Standalone JSON configuration containing a `network` object.
#[arg(long, value_name = "PATH")]
config: PathBuf,
/// Reject unknown config fields and require explicit managed constraints.
#[arg(long)]
strict: bool,
}
impl Args {
fn parse_config(&self, bytes: &[u8]) -> Result<StaticNetworkProxyConfig> {
if self.strict {
StaticNetworkProxyConfig::from_slice_strict(bytes)
} else {
StaticNetworkProxyConfig::from_slice(bytes)
}
}
}
struct StaticConfigReloader {
state: ConfigState,
}
impl ConfigReloader for StaticConfigReloader {
fn source_label(&self) -> String {
"standalone network proxy config".to_string()
}
fn maybe_reload(&self) -> ConfigReloaderFuture<'_, Option<ConfigState>> {
Box::pin(async { Ok(None) })
}
fn reload_now(&self) -> ConfigReloaderFuture<'_, ConfigState> {
Box::pin(async { Ok(self.state.clone()) })
}
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
let args = Args::parse();
let mut bytes = Vec::new();
std::fs::File::open(&args.config)
.with_context(|| {
format!(
"failed to read network proxy config: {}",
args.config.display()
)
})?
.take((MAX_CONFIG_BYTES + 1) as u64)
.read_to_end(&mut bytes)?;
ensure!(
bytes.len() <= MAX_CONFIG_BYTES,
"network proxy config exceeds {MAX_CONFIG_BYTES} bytes"
);
let config = args
.parse_config(&bytes)
.with_context(|| format!("invalid network proxy config: {}", args.config.display()))?;
let config_state = config
.build_state(Platform::native())
.context("failed to initialize network proxy policy")?;
let reloader = Arc::new(StaticConfigReloader {
state: config_state.clone(),
});
let state = Arc::new(NetworkProxyState::from_standalone_config(
config_state,
reloader,
));
NetworkProxy::builder()
.state(state)
.managed_by_codex(/*managed_by_codex*/ false)
.build()
.await?
.run()
.await?
.wait()
.await
}
#[cfg(test)]
#[path = "main_tests.rs"]
mod tests;