mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
Migrate the Windows MXC sandbox to the public V1 SDK (#62331)
## What changed Update `mxc-sdk` and use its V1 API to launch commands in a `ProcessContainer` with inherited stdio. Build the V1 request from Codex's filesystem, network, UI, and environment settings. Cache the SDK's ProcessContainer availability probe and surface failures to create the security environment at launch. Let the SDK reject deny paths the host cannot enforce and drop Codex's direct `flatbuffers` dependency. ## Testing Update the request-building tests to check the V1 filesystem and network policies, including loopback access for proxied commands. GitOrigin-RevId: 0fb9955b5df847595c142f5fd1da7a0cc83e1c28
This commit is contained in:
@@ -518,10 +518,6 @@ crate.annotation(
|
||||
|
||||
# MXC's resource compiler lookup needs an explicit path for Windows GNU targets.
|
||||
crate.annotation(
|
||||
build_script_deps = [
|
||||
"@crates//cc-1.2.55",
|
||||
"@crates//winresource-0.1.34",
|
||||
],
|
||||
build_script_env = {
|
||||
"RC_PATH": "$(execpath @llvm//tools:llvm-rc)",
|
||||
},
|
||||
|
||||
Generated
+7
-2
File diff suppressed because one or more lines are too long
Generated
+64
-9
@@ -4508,7 +4508,6 @@ dependencies = [
|
||||
"codex-utils-absolute-path",
|
||||
"codex-utils-path-uri",
|
||||
"codex-windows-sandbox",
|
||||
"flatbuffers",
|
||||
"mxc-sdk",
|
||||
"pretty_assertions",
|
||||
"serde",
|
||||
@@ -6902,7 +6901,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb"
|
||||
dependencies = [
|
||||
"const-oid 0.9.6",
|
||||
"pem-rfc7468",
|
||||
"pem-rfc7468 0.7.0",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "der"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a996a108feb8b427a6e12fcf6af4aa83409b79d72c15337d7741368131ee004"
|
||||
dependencies = [
|
||||
"pem-rfc7468 1.0.0",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
@@ -7267,7 +7276,7 @@ version = "0.16.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee27f32b5c5292967d2d4a9d7f1e0b0aed2c15daded5a60300e4abb9d8020bca"
|
||||
dependencies = [
|
||||
"der",
|
||||
"der 0.7.10",
|
||||
"digest 0.10.7",
|
||||
"elliptic-curve",
|
||||
"rfc6979",
|
||||
@@ -7321,7 +7330,7 @@ dependencies = [
|
||||
"generic-array",
|
||||
"group",
|
||||
"hkdf 0.12.4",
|
||||
"pem-rfc7468",
|
||||
"pem-rfc7468 0.7.0",
|
||||
"pkcs8",
|
||||
"rand_core 0.6.4",
|
||||
"sec1",
|
||||
@@ -10821,8 +10830,8 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mxc-sdk"
|
||||
version = "1.0.0"
|
||||
source = "git+https://github.com/microsoft/mxc?rev=b21120168d70befe12356411423266bfef168446#b21120168d70befe12356411423266bfef168446"
|
||||
version = "1.1.0"
|
||||
source = "git+https://github.com/microsoft/mxc?rev=c66201839d63cae2239db5e92f1f1d6076cf5181#c66201839d63cae2239db5e92f1f1d6076cf5181"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.22.1",
|
||||
@@ -10832,6 +10841,7 @@ dependencies = [
|
||||
"flatbuffers",
|
||||
"getrandom 0.2.17",
|
||||
"libc",
|
||||
"native-tls",
|
||||
"nix 0.29.0",
|
||||
"same-file",
|
||||
"semver",
|
||||
@@ -10844,6 +10854,7 @@ dependencies = [
|
||||
"tokio",
|
||||
"tracelogging",
|
||||
"unicode-general-category",
|
||||
"ureq",
|
||||
"url",
|
||||
"uuid",
|
||||
"widestring",
|
||||
@@ -11952,6 +11963,15 @@ dependencies = [
|
||||
"base64ct",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pem-rfc7468"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6305423e0e7738146434843d1694d621cce767262b2a86910beab705e4493d9"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.2"
|
||||
@@ -12112,7 +12132,7 @@ version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7"
|
||||
dependencies = [
|
||||
"der",
|
||||
"der 0.7.10",
|
||||
"spki",
|
||||
]
|
||||
|
||||
@@ -14288,7 +14308,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc"
|
||||
dependencies = [
|
||||
"base16ct",
|
||||
"der",
|
||||
"der 0.7.10",
|
||||
"generic-array",
|
||||
"pkcs8",
|
||||
"subtle",
|
||||
@@ -15004,7 +15024,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
"der",
|
||||
"der 0.7.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -16930,6 +16950,35 @@ version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
|
||||
|
||||
[[package]]
|
||||
name = "ureq"
|
||||
version = "3.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dea7109cdcd5864d4eeb1b58a1648dc9bf520360d7af16ec26d0a9354bafcfc0"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"der 0.8.3",
|
||||
"log",
|
||||
"native-tls",
|
||||
"percent-encoding",
|
||||
"rustls-pki-types",
|
||||
"ureq-proto",
|
||||
"utf8-zero",
|
||||
"webpki-root-certs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ureq-proto"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e994ba84b0bd1b1b0cf92878b7ef898a5c1760108fe7b6010327e274917a808c"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"http 1.4.0",
|
||||
"httparse",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.8"
|
||||
@@ -16955,6 +17004,12 @@ version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
||||
|
||||
[[package]]
|
||||
name = "utf8-zero"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e"
|
||||
|
||||
[[package]]
|
||||
name = "utf8_iter"
|
||||
version = "1.0.4"
|
||||
|
||||
+1
-2
@@ -399,7 +399,6 @@ ed25519-dalek = { version = "2.2.0", features = ["pkcs8"] }
|
||||
encoding_rs = "0.8.35"
|
||||
eventsource-stream = "0.2.3"
|
||||
file-id = "0.2.3"
|
||||
flatbuffers = "25"
|
||||
flate2 = "1.1.8"
|
||||
futures = { version = "0.3", default-features = false }
|
||||
gethostname = "1.1.0"
|
||||
@@ -440,7 +439,7 @@ maplit = "1.0.2"
|
||||
memchr = "2.7.6"
|
||||
mime_guess = "2.0.5"
|
||||
multimap = "0.10.0"
|
||||
mxc-sdk = { git = "https://github.com/microsoft/mxc", rev = "b21120168d70befe12356411423266bfef168446" }
|
||||
mxc-sdk = { git = "https://github.com/microsoft/mxc", rev = "c66201839d63cae2239db5e92f1f1d6076cf5181" }
|
||||
notify = "8.2.0"
|
||||
nucleo = { git = "https://github.com/helix-editor/nucleo.git", rev = "4253de9faabb4e5c6d81d946a5e35a90f87347ee" }
|
||||
once_cell = "1.20.2"
|
||||
|
||||
@@ -27,7 +27,6 @@ serde_json = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
flatbuffers = { workspace = true }
|
||||
# 1.2.4 imports OneCore_apiset, which the GNU Windows toolchain does not ship.
|
||||
tracelogging = "=1.2.3"
|
||||
windows-sys = { workspace = true, features = [
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# Native Windows MXC sandbox
|
||||
|
||||
This crate routes a command through the current Codex executable and directly
|
||||
into Microsoft's MXC `BaseContainerRunner`. It requires a working Windows
|
||||
process security environment (PSEC). It never invokes MXC's AppContainer
|
||||
into Microsoft's public MXC V1 SDK with inherited stdio. It requires a working
|
||||
Windows process security environment (PSEC). It never invokes MXC's AppContainer
|
||||
dispatcher, edits host ACLs, creates sandbox users, runs setup, or requests
|
||||
elevation. The existing Codex Windows sandboxes remain separate backends.
|
||||
|
||||
@@ -10,11 +10,10 @@ Windows executors record `codex.windows_mxc.available` once per process with an
|
||||
`available=true|false` tag. This measures runtime availability independently of
|
||||
selection. Unsupported Windows executors reject MXC requests before execution.
|
||||
|
||||
`is_available()` uses MXC's cached create/close probe, rather than an OS build
|
||||
number or the SDK's broad `platform_support()` result. The latter also reports
|
||||
older AppContainer backends as supported. A requested deny path additionally
|
||||
requires the native `PSE_SUPPORT_FS_DENY` capability; otherwise the command
|
||||
fails before launch.
|
||||
`is_available()` caches the public SDK's ProcessContainer probe. This checks
|
||||
API presence and capabilities without creating a security environment; creation
|
||||
and request-specific failures remain launch errors. The SDK rejects requested
|
||||
deny paths when the host cannot enforce them.
|
||||
|
||||
The wrapper inherits the command's pipes or ConPTY console. MXC creates its
|
||||
child suspended, assigns a kill-on-close job before resuming it, and retains
|
||||
@@ -35,8 +34,10 @@ Do not call MXC's shared `normalize_object_conflicts` before or after volume
|
||||
expansion. Its identity preflight is used by mount-based backends and can reject
|
||||
an otherwise usable Windows policy when a network share does not support
|
||||
`FileIdInfo`, or a protected deny path cannot be opened for metadata. Codex sends
|
||||
explicit path grants to BaseContainer without requiring that additional lookup.
|
||||
We intentionally accept two limitations:
|
||||
explicit path grants without that object-identity lookup. The public SDK does
|
||||
perform metadata checks for missing paths: immediate failures produce warnings,
|
||||
while a check that exceeds five seconds rejects startup, including on a slow or
|
||||
unreachable network share. We intentionally accept two alias limitations:
|
||||
|
||||
- Hard-link names retain independent path permissions. An allowed name can read
|
||||
or modify the same file as a denied or read-only name; we do not tighten all
|
||||
|
||||
@@ -1,55 +1,11 @@
|
||||
//! Probe PSEC creation and startup attributes without launching a process.
|
||||
//! Cache the result so automatic MXC selection requires a usable native API.
|
||||
//! Cache the public SDK's ProcessContainer availability probe.
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use flatbuffers::FlatBufferBuilder;
|
||||
use mxc_sdk::mxc_common::models::ContainerPolicy;
|
||||
use mxc_sdk::mxc_common::ui_policy::resolve_ui_restrictions;
|
||||
use mxc_sdk::process_container_common::PROCESS_SECURITY_ENVIRONMENT_FLAG_NONE;
|
||||
use mxc_sdk::process_container_common::ProcessSecurityEnvironmentApi;
|
||||
use mxc_sdk::process_container_common::SecurityEnvironmentStartupInfo;
|
||||
use mxc_sdk::process_container_common::job_object::to_job_object_uilimit_mask;
|
||||
use mxc_sdk::process_security_environment_spec::process_security_environment_layout::EndpointPolicyT;
|
||||
use mxc_sdk::process_security_environment_spec::process_security_environment_layout::FilterAction;
|
||||
use mxc_sdk::process_security_environment_spec::process_security_environment_layout::NetworkPolicyT;
|
||||
use mxc_sdk::process_security_environment_spec::process_security_environment_layout::ProcessSecurityEnvironmentT;
|
||||
use mxc_sdk::process_security_environment_spec::process_security_environment_layout::SchemaVersionT;
|
||||
use mxc_sdk::process_security_environment_spec::process_security_environment_layout::finish_process_security_environment_buffer;
|
||||
|
||||
pub(super) fn is_available() -> bool {
|
||||
static USABLE: OnceLock<bool> = OnceLock::new();
|
||||
*USABLE.get_or_init(|| {
|
||||
// Preserve the previous SDK's probe: no filesystem, network, or UI
|
||||
// grants, and no process launch. API-set presence alone is insufficient.
|
||||
let policy = ContainerPolicy::default();
|
||||
let mut egress = EndpointPolicyT::default();
|
||||
egress.default_action = FilterAction::deny;
|
||||
let mut network = NetworkPolicyT::default();
|
||||
network.egress = Some(Box::new(egress));
|
||||
let mut specification = ProcessSecurityEnvironmentT::default();
|
||||
specification.version = SchemaVersionT { major: 1, minor: 0 };
|
||||
specification.disallow_win32k_system_calls = policy.ui.disable;
|
||||
specification.ui_restrictions = u64::from(to_job_object_uilimit_mask(
|
||||
&resolve_ui_restrictions(&policy.ui, &policy.base_process_ui),
|
||||
));
|
||||
specification.network_policy = Some(Box::new(network));
|
||||
let mut builder = FlatBufferBuilder::with_capacity(/* capacity */ 1024);
|
||||
let specification = specification.pack(&mut builder);
|
||||
finish_process_security_environment_buffer(&mut builder, specification);
|
||||
ProcessSecurityEnvironmentApi::load()
|
||||
.and_then(|api| {
|
||||
api.create(
|
||||
builder.finished_data(),
|
||||
PROCESS_SECURITY_ENVIRONMENT_FLAG_NONE,
|
||||
)
|
||||
})
|
||||
.and_then(|environment| {
|
||||
let startup_info =
|
||||
SecurityEnvironmentStartupInfo::new(Default::default(), environment.raw(), &[]);
|
||||
environment.close();
|
||||
startup_info.map(drop)
|
||||
})
|
||||
.is_ok()
|
||||
mxc_sdk::v1::probe(/* request */ None)
|
||||
.is_ok_and(|probe| probe.error.is_none() && probe.probes.base_container_api_present)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,61 +2,19 @@
|
||||
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use anyhow::ensure;
|
||||
use mxc_sdk::mxc_common::logger::Logger;
|
||||
use mxc_sdk::mxc_common::logger::Mode;
|
||||
use mxc_sdk::mxc_common::models::ExecutionRequest;
|
||||
use mxc_sdk::mxc_common::sandbox_process::SandboxBackend;
|
||||
use mxc_sdk::mxc_common::sandbox_process::StdioMode;
|
||||
use mxc_sdk::process_container_common::base_container_runner::BaseContainerRunner;
|
||||
use mxc_sdk::v1;
|
||||
|
||||
// The ETW functions used by tracelogging are documented Advapi32 exports on
|
||||
// both Windows toolchains; 1.2.3 leaves their import library to the application.
|
||||
#[link(name = "advapi32")]
|
||||
unsafe extern "system" {}
|
||||
|
||||
pub(super) fn launch(request: &ExecutionRequest) -> Result<i32> {
|
||||
ensure!(
|
||||
!request
|
||||
.policy
|
||||
.capabilities
|
||||
.iter()
|
||||
.any(|capability| capability.eq_ignore_ascii_case("permissiveLearningMode")),
|
||||
"MXC native launch does not support permissiveLearningMode"
|
||||
);
|
||||
if !request.policy.denied_paths.is_empty() {
|
||||
ensure!(
|
||||
BaseContainerRunner::supports_native_denied_paths(),
|
||||
"this Windows build cannot enforce native MXC deny paths"
|
||||
);
|
||||
pub(super) fn launch(request: v1::ContainerRequest) -> Result<i32> {
|
||||
let mut options = v1::SpawnExtendedOptions::default();
|
||||
options.stdio = v1::SpawnStdio::Inherit;
|
||||
let mut child = v1::spawn_with_extended_options(request, options)?;
|
||||
match child.wait().context("waiting for the MXC command")? {
|
||||
v1::WaitResult::Exited(code) => Ok(code),
|
||||
v1::WaitResult::TimedOut => anyhow::bail!("MXC command timed out"),
|
||||
}
|
||||
let mut logger = Logger::new(Mode::Buffer);
|
||||
let mut child = BaseContainerRunner::new()
|
||||
.spawn(request, &mut logger, StdioMode::Inherit)
|
||||
.map_err(|error| match create_process_error(&error.extended_error) {
|
||||
Some(code) => anyhow::anyhow!(
|
||||
"{} (CreateProcessW, Win32 error {code})",
|
||||
error.error_message
|
||||
),
|
||||
None => anyhow::anyhow!("{}", error.error_message),
|
||||
})?;
|
||||
// Do not publish the SDK diagnostic buffer: it may contain command or
|
||||
// environment data. This wrapper emits only the resulting launch error.
|
||||
child.wait().context("waiting for the MXC command")
|
||||
}
|
||||
|
||||
// The SDK appends the working directory and other diagnostics to this field.
|
||||
// Only the Windows error number is safe to copy into the command result.
|
||||
fn create_process_error(detail: &str) -> Option<u32> {
|
||||
let number = detail
|
||||
.strip_prefix(
|
||||
"CreateProcessW(PROC_THREAD_ATTRIBUTE_SECURITY_ENVIRONMENT) failed: WIN32_ERROR(",
|
||||
)?
|
||||
.split_once(')')?
|
||||
.0;
|
||||
number.parse().ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "native_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use super::create_process_error;
|
||||
|
||||
#[test]
|
||||
fn returns_only_the_native_create_process_error() {
|
||||
let api = "CreateProcessW(PROC_THREAD_ATTRIBUTE_SECURITY_ENVIRONMENT) failed: ";
|
||||
assert_eq!(
|
||||
create_process_error(&format!("{api}WIN32_ERROR(5) (working directory: private)")),
|
||||
Some(5)
|
||||
);
|
||||
assert_eq!(
|
||||
create_process_error(&format!("{api}WIN32_ERROR(private)")),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
create_process_error("another API failed: WIN32_ERROR(5)"),
|
||||
None
|
||||
);
|
||||
}
|
||||
@@ -21,17 +21,19 @@ use codex_windows_sandbox::resolve_windows_deny_read_paths;
|
||||
use mxc_sdk::mxc_common::cmdline::CommandLineContext;
|
||||
use mxc_sdk::mxc_common::cmdline::CommandLineError;
|
||||
use mxc_sdk::mxc_common::cmdline::cmdline_from_argv_for_context;
|
||||
use mxc_sdk::mxc_common::models::BaseProcessUiConfig;
|
||||
use mxc_sdk::mxc_common::models::ContainerPolicy;
|
||||
use mxc_sdk::mxc_common::models::ExecutionRequest;
|
||||
use mxc_sdk::mxc_common::models::FallbackPolicy;
|
||||
use mxc_sdk::mxc_common::models::NetworkAction;
|
||||
use mxc_sdk::mxc_common::models::NetworkCidr;
|
||||
use mxc_sdk::mxc_common::models::NetworkEgressPolicy;
|
||||
use mxc_sdk::mxc_common::models::NetworkIngressPolicy;
|
||||
use mxc_sdk::mxc_common::models::NetworkPeer;
|
||||
use mxc_sdk::mxc_common::models::NetworkRule;
|
||||
use mxc_sdk::mxc_common::models::UiPolicy;
|
||||
use mxc_sdk::v1::ContainerRequest;
|
||||
use mxc_sdk::v1::Containment;
|
||||
use mxc_sdk::v1::FilesystemPolicy;
|
||||
use mxc_sdk::v1::NetworkAction;
|
||||
use mxc_sdk::v1::NetworkEgressPolicy;
|
||||
use mxc_sdk::v1::NetworkIngressPolicy;
|
||||
use mxc_sdk::v1::NetworkPeerPolicy;
|
||||
use mxc_sdk::v1::NetworkPolicy;
|
||||
use mxc_sdk::v1::NetworkRulePolicy;
|
||||
use mxc_sdk::v1::UiPolicy;
|
||||
use mxc_sdk::v1::configs::ProcessContainerConfig;
|
||||
use mxc_sdk::v1::configs::ProcessContainerUi;
|
||||
use mxc_sdk::v1::configs::ProcessContainerUiIsolation;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::MxcCommand;
|
||||
@@ -120,7 +122,7 @@ pub(super) fn build_request(
|
||||
env: Vec<String>,
|
||||
volume_roots: &[PathBuf],
|
||||
platform_read_roots: &[PathBuf],
|
||||
) -> Result<ExecutionRequest, PolicyError> {
|
||||
) -> Result<ContainerRequest, PolicyError> {
|
||||
if command.command.is_empty() {
|
||||
return Err(PolicyError::EmptyCommand);
|
||||
}
|
||||
@@ -293,78 +295,77 @@ pub(super) fn build_request(
|
||||
NetworkAction::Deny
|
||||
};
|
||||
let mut egress = NetworkEgressPolicy {
|
||||
default: egress_default,
|
||||
default: Some(egress_default),
|
||||
..Default::default()
|
||||
};
|
||||
if proxied {
|
||||
// PSEC host loopback can be bidirectional. This shape is supported only
|
||||
// when the caller already allows local clients and servers. Keep
|
||||
// private-network ingress denied and allow no direct DNS bypass.
|
||||
egress.allow.push(NetworkRule {
|
||||
to: vec![
|
||||
NetworkPeer {
|
||||
cidr: NetworkCidr {
|
||||
address: std::net::Ipv4Addr::new(127, 0, 0, 0).into(),
|
||||
prefix_length: 8,
|
||||
},
|
||||
except: Vec::new(),
|
||||
},
|
||||
NetworkPeer {
|
||||
cidr: NetworkCidr {
|
||||
address: std::net::Ipv6Addr::LOCALHOST.into(),
|
||||
prefix_length: 128,
|
||||
},
|
||||
except: Vec::new(),
|
||||
},
|
||||
],
|
||||
ports: Vec::new(),
|
||||
});
|
||||
egress.allow = Some(vec![NetworkRulePolicy {
|
||||
to: Some(vec![
|
||||
NetworkPeerPolicy::new("127.0.0.0/8"),
|
||||
NetworkPeerPolicy::new("::1/128"),
|
||||
]),
|
||||
..Default::default()
|
||||
}]);
|
||||
}
|
||||
Ok(ExecutionRequest {
|
||||
script_code: match cmd_script_command_line(&command.command) {
|
||||
let mut process_container = ProcessContainerConfig::default();
|
||||
process_container.capabilities = vec!["registryRead".to_owned()];
|
||||
let mut ui = ProcessContainerUi::default();
|
||||
ui.isolation = ProcessContainerUiIsolation::Desktop;
|
||||
process_container.ui = Some(ui);
|
||||
let environment = env
|
||||
.iter()
|
||||
.map(|entry| {
|
||||
let (key, value) = entry.split_once('=').ok_or_else(|| {
|
||||
PolicyError::PolicyResolution(
|
||||
"MXC environment entries must be KEY=VALUE".to_owned(),
|
||||
)
|
||||
})?;
|
||||
Ok((key.to_owned(), value.to_owned()))
|
||||
})
|
||||
.collect::<Result<Vec<_>, PolicyError>>()?;
|
||||
Ok(ContainerRequest {
|
||||
working_directory: Some(
|
||||
command_cwd
|
||||
.to_str()
|
||||
.ok_or(PolicyError::NonUnicodeCommandCwd)?
|
||||
.to_owned(),
|
||||
),
|
||||
environment: (!environment.is_empty()).then_some(environment),
|
||||
filesystem: Some(FilesystemPolicy {
|
||||
readwrite_paths: unicode_paths(write)?,
|
||||
readonly_paths: unicode_paths(read)?,
|
||||
denied_paths: unicode_paths(deny)?,
|
||||
..Default::default()
|
||||
}),
|
||||
network: Some(NetworkPolicy {
|
||||
egress: Some(egress),
|
||||
ingress: Some(NetworkIngressPolicy {
|
||||
default: Some(ingress_default),
|
||||
host_loopback: Some(if network_enabled || proxied {
|
||||
NetworkAction::Allow
|
||||
} else {
|
||||
NetworkAction::Deny
|
||||
}),
|
||||
}),
|
||||
..Default::default()
|
||||
}),
|
||||
// PowerShell needs Win32k and desktop handles during DLL startup.
|
||||
// Keep clipboard, input injection, and system-control restrictions.
|
||||
ui: Some(UiPolicy {
|
||||
disable: false,
|
||||
..Default::default()
|
||||
}),
|
||||
containment: Containment::ProcessContainer(process_container),
|
||||
..ContainerRequest::new(match cmd_script_command_line(&command.command) {
|
||||
Some(script) => script,
|
||||
None => cmdline_from_argv_for_context(
|
||||
&command.command,
|
||||
CommandLineContext::WindowsCreateProcess,
|
||||
)?,
|
||||
},
|
||||
working_directory: command_cwd
|
||||
.to_str()
|
||||
.ok_or(PolicyError::NonUnicodeCommandCwd)?
|
||||
.to_owned(),
|
||||
env: (!env.is_empty()).then_some(env),
|
||||
policy: ContainerPolicy {
|
||||
capabilities: vec!["registryRead".to_owned()],
|
||||
readwrite_paths: unicode_paths(write)?,
|
||||
readonly_paths: unicode_paths(read)?,
|
||||
denied_paths: unicode_paths(deny)?,
|
||||
fallback: FallbackPolicy {
|
||||
allow_dacl_mutation: false,
|
||||
},
|
||||
network_egress: Some(egress),
|
||||
network_ingress: Some(NetworkIngressPolicy {
|
||||
default: ingress_default,
|
||||
host_loopback: if network_enabled || proxied {
|
||||
NetworkAction::Allow
|
||||
} else {
|
||||
NetworkAction::Deny
|
||||
},
|
||||
}),
|
||||
network_specified: true,
|
||||
network_mode_specified: true,
|
||||
// PowerShell needs Win32k and desktop handles during DLL startup.
|
||||
// Keep clipboard, input injection, and system-control restrictions.
|
||||
ui: UiPolicy {
|
||||
disable: false,
|
||||
..Default::default()
|
||||
},
|
||||
base_process_ui: BaseProcessUiConfig {
|
||||
isolation: "desktop".to_owned(),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -16,12 +16,11 @@ use codex_protocol::protocol::FileSystemSpecialPath;
|
||||
use codex_protocol::protocol::NetworkSandboxPolicy;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use mxc_sdk::mxc_common::models::NetworkAction;
|
||||
use mxc_sdk::mxc_common::models::NetworkCidr;
|
||||
use mxc_sdk::mxc_common::models::NetworkEgressPolicy;
|
||||
use mxc_sdk::mxc_common::models::NetworkIngressPolicy;
|
||||
use mxc_sdk::mxc_common::models::NetworkPeer;
|
||||
use mxc_sdk::mxc_common::models::NetworkRule;
|
||||
use mxc_sdk::v1::NetworkAction;
|
||||
use mxc_sdk::v1::NetworkEgressPolicy;
|
||||
use mxc_sdk::v1::NetworkIngressPolicy;
|
||||
use mxc_sdk::v1::NetworkPeerPolicy;
|
||||
use mxc_sdk::v1::NetworkRulePolicy;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use crate::CreateMxcCommandArgsParams;
|
||||
@@ -202,17 +201,21 @@ fn wrapper_preserves_exact_argv_and_separate_command_cwd() -> Result<()> {
|
||||
),
|
||||
(&profile, root.path(), &argv)
|
||||
);
|
||||
let request = build_request(&parsed, &cwd, vec!["CUSTOM=value".to_owned()], &[], &[])?;
|
||||
let request = build_request(&parsed, &cwd, vec!["CUSTOM=x=y".to_owned()], &[], &[])?;
|
||||
assert_eq!(
|
||||
(request.script_code, request.working_directory, request.env),
|
||||
(
|
||||
request.command,
|
||||
request.working_directory,
|
||||
request.environment
|
||||
),
|
||||
(
|
||||
r#""C:\Program Files\tool.exe" --permissions "" "quotes\" and slash\\""#.to_owned(),
|
||||
cwd.to_str().unwrap().to_owned(),
|
||||
Some(vec!["CUSTOM=value".to_owned()]),
|
||||
Some(cwd.to_str().unwrap().to_owned()),
|
||||
Some(vec![("CUSTOM".to_owned(), "x=y".to_owned())]),
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
build_request(&parsed, &cwd, Vec::new(), &[], &[])?.env,
|
||||
build_request(&parsed, &cwd, Vec::new(), &[], &[])?.environment,
|
||||
None
|
||||
);
|
||||
Ok(())
|
||||
@@ -250,6 +253,7 @@ fn native_grants_preserve_denies_and_read_only_carveouts() -> Result<()> {
|
||||
let profile =
|
||||
PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted);
|
||||
let request = build_request(&command(&profile, root), root, Vec::new(), &[], &[])?;
|
||||
let request = request.filesystem.unwrap();
|
||||
let mut expected_read = [
|
||||
root.join(".agents"),
|
||||
root.join(".aws"),
|
||||
@@ -264,9 +268,9 @@ fn native_grants_preserve_denies_and_read_only_carveouts() -> Result<()> {
|
||||
expected_read.sort();
|
||||
assert_eq!(
|
||||
(
|
||||
request.policy.readwrite_paths,
|
||||
request.policy.readonly_paths,
|
||||
request.policy.denied_paths
|
||||
request.readwrite_paths,
|
||||
request.readonly_paths,
|
||||
request.denied_paths
|
||||
),
|
||||
(
|
||||
vec![
|
||||
@@ -280,7 +284,6 @@ fn native_grants_preserve_denies_and_read_only_carveouts() -> Result<()> {
|
||||
vec![denied.to_str().unwrap().to_owned()],
|
||||
)
|
||||
);
|
||||
assert!(!request.policy.fallback.allow_dacl_mutation);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -309,6 +312,7 @@ fn volume_expansion_does_not_turn_read_only_child_writable() -> Result<()> {
|
||||
&[root.to_owned()],
|
||||
&[],
|
||||
)?;
|
||||
let request = request.filesystem.unwrap();
|
||||
let mut expected_write_paths = vec![root.to_str().unwrap(), writable.to_str().unwrap()];
|
||||
if cfg!(windows) {
|
||||
expected_write_paths.insert(1, missing.to_str().unwrap());
|
||||
@@ -321,10 +325,10 @@ fn volume_expansion_does_not_turn_read_only_child_writable() -> Result<()> {
|
||||
assert!(!directory.join(".chatgptcreated").exists());
|
||||
}
|
||||
}
|
||||
assert_eq!(request.policy.readwrite_paths, expected_write_paths);
|
||||
assert_eq!(request.policy.denied_paths, vec![denied.to_str().unwrap()]);
|
||||
assert_eq!(request.readwrite_paths, expected_write_paths);
|
||||
assert_eq!(request.denied_paths, vec![denied.to_str().unwrap()]);
|
||||
assert_eq!(
|
||||
request.policy.readonly_paths,
|
||||
request.readonly_paths,
|
||||
vec![
|
||||
root.join(".agents").to_str().unwrap(),
|
||||
root.join(".aws").to_str().unwrap(),
|
||||
@@ -363,8 +367,9 @@ fn volume_expansion_skips_broken_links_and_preserves_other_grants() -> Result<()
|
||||
std::slice::from_ref(&root),
|
||||
&[],
|
||||
)?;
|
||||
let request = request.filesystem.unwrap();
|
||||
assert_eq!(
|
||||
(request.policy.readwrite_paths, request.policy.denied_paths),
|
||||
(request.readwrite_paths, request.denied_paths),
|
||||
(
|
||||
vec![
|
||||
root.to_str().unwrap().to_owned(),
|
||||
@@ -407,7 +412,7 @@ fn only_write_access_expands_volume_children() -> Result<()> {
|
||||
&volumes,
|
||||
&[],
|
||||
)?;
|
||||
let native = request.policy;
|
||||
let native = request.filesystem.unwrap();
|
||||
let (granted, other) = if access == FileSystemAccessMode::Write {
|
||||
(native.readwrite_paths, native.readonly_paths)
|
||||
} else {
|
||||
@@ -447,34 +452,26 @@ fn managed_network_allows_authorized_loopback_without_lan_or_dns_access() -> Res
|
||||
})?;
|
||||
let parsed = crate::transport::decode(&mut env)?;
|
||||
let request = build_request(&parsed, root.path(), Vec::new(), &[], &[])?;
|
||||
let network = request.network.unwrap();
|
||||
assert_eq!(
|
||||
request.policy.network_egress,
|
||||
network.egress,
|
||||
Some(NetworkEgressPolicy {
|
||||
default: NetworkAction::Deny,
|
||||
allow: vec![NetworkRule {
|
||||
to: vec![
|
||||
NetworkPeer {
|
||||
cidr: "127.0.0.0/8".parse()?,
|
||||
except: Vec::new()
|
||||
},
|
||||
NetworkPeer {
|
||||
cidr: NetworkCidr {
|
||||
address: std::net::Ipv6Addr::LOCALHOST.into(),
|
||||
prefix_length: 128
|
||||
},
|
||||
except: Vec::new()
|
||||
},
|
||||
],
|
||||
ports: Vec::new(),
|
||||
}],
|
||||
deny: Vec::new(),
|
||||
default: Some(NetworkAction::Deny),
|
||||
allow: Some(vec![NetworkRulePolicy {
|
||||
to: Some(vec![
|
||||
NetworkPeerPolicy::new("127.0.0.0/8"),
|
||||
NetworkPeerPolicy::new("::1/128"),
|
||||
]),
|
||||
ports: None,
|
||||
}]),
|
||||
deny: None,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
request.policy.network_ingress,
|
||||
network.ingress,
|
||||
Some(NetworkIngressPolicy {
|
||||
default: NetworkAction::Deny,
|
||||
host_loopback: NetworkAction::Allow,
|
||||
default: Some(NetworkAction::Deny),
|
||||
host_loopback: Some(NetworkAction::Allow),
|
||||
})
|
||||
);
|
||||
Ok(())
|
||||
@@ -536,16 +533,17 @@ fn deny_globs_expand_files_and_directories_before_launch() -> Result<()> {
|
||||
let profile =
|
||||
PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted);
|
||||
let request = build_request(&command(&profile, &root), &nested, Vec::new(), &[], &[])?;
|
||||
let request = request.filesystem.unwrap();
|
||||
#[cfg(windows)]
|
||||
{
|
||||
assert!(missing.parent().unwrap().is_dir());
|
||||
assert!(!missing.exists());
|
||||
}
|
||||
assert_eq!(request.policy.readwrite_paths, vec![root.to_str().unwrap()]);
|
||||
assert_eq!(request.readwrite_paths, vec![root.to_str().unwrap()]);
|
||||
let mut expected = [file, directory, missing];
|
||||
expected.sort();
|
||||
assert_eq!(
|
||||
request.policy.denied_paths,
|
||||
request.denied_paths,
|
||||
expected.map(|path| path.to_str().unwrap().to_owned())
|
||||
);
|
||||
Ok(())
|
||||
@@ -574,10 +572,11 @@ fn denied_parents_are_created_before_glob_expansion() -> Result<()> {
|
||||
let profile =
|
||||
PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted);
|
||||
let request = build_request(&command(&profile, &root), &root, Vec::new(), &[], &[])?;
|
||||
let request = request.filesystem.unwrap();
|
||||
assert!(missing.is_dir());
|
||||
assert!(!missing.join("child").exists());
|
||||
assert_eq!(
|
||||
request.policy.denied_paths,
|
||||
request.denied_paths,
|
||||
vec![
|
||||
missing.to_str().unwrap(),
|
||||
missing.join("child").to_str().unwrap()
|
||||
@@ -654,6 +653,7 @@ fn root_deny_keeps_only_narrow_explicit_grants() -> Result<()> {
|
||||
&[root.to_owned()],
|
||||
&[],
|
||||
)?;
|
||||
let request = request.filesystem.unwrap();
|
||||
let allowed = vec![child.to_str().unwrap().to_owned()];
|
||||
let expected = match access {
|
||||
FileSystemAccessMode::Read => (Vec::new(), allowed),
|
||||
@@ -665,14 +665,8 @@ fn root_deny_keeps_only_narrow_explicit_grants() -> Result<()> {
|
||||
),
|
||||
FileSystemAccessMode::Deny => unreachable!(),
|
||||
};
|
||||
assert_eq!(
|
||||
(
|
||||
request.policy.readwrite_paths,
|
||||
request.policy.readonly_paths
|
||||
),
|
||||
expected
|
||||
);
|
||||
assert_eq!(request.policy.denied_paths, Vec::<String>::new());
|
||||
assert_eq!((request.readwrite_paths, request.readonly_paths), expected);
|
||||
assert_eq!(request.denied_paths, Vec::<String>::new());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -712,11 +706,12 @@ fn windows_temp_roots_use_filtered_environment_and_keep_explicit_denies() -> Res
|
||||
&[],
|
||||
&[],
|
||||
)?;
|
||||
let request = request.filesystem.unwrap();
|
||||
assert_eq!(
|
||||
request.policy.readwrite_paths,
|
||||
request.readwrite_paths,
|
||||
vec![first.to_str().unwrap(), second.to_str().unwrap()]
|
||||
);
|
||||
assert_eq!(request.policy.denied_paths, vec![denied.to_str().unwrap()]);
|
||||
assert_eq!(request.denied_paths, vec![denied.to_str().unwrap()]);
|
||||
|
||||
for value in [
|
||||
None,
|
||||
@@ -736,8 +731,9 @@ fn windows_temp_roots_use_filtered_environment_and_keep_explicit_denies() -> Res
|
||||
&[],
|
||||
&[],
|
||||
)?;
|
||||
assert_eq!(request.policy.readwrite_paths, Vec::<String>::new());
|
||||
assert_eq!(request.policy.denied_paths, vec![denied.to_str().unwrap()]);
|
||||
let request = request.filesystem.unwrap();
|
||||
assert_eq!(request.readwrite_paths, Vec::<String>::new());
|
||||
assert_eq!(request.denied_paths, vec![denied.to_str().unwrap()]);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -769,13 +765,14 @@ fn minimal_platform_roots_preserve_explicit_denies() -> Result<()> {
|
||||
&[],
|
||||
&[allowed.clone(), denied.clone()],
|
||||
)?;
|
||||
let request = request.filesystem.unwrap();
|
||||
let expected_read = if include_defaults {
|
||||
vec![allowed.to_str().unwrap().to_owned()]
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
assert_eq!(
|
||||
(request.policy.readonly_paths, request.policy.denied_paths),
|
||||
(request.readonly_paths, request.denied_paths),
|
||||
(expected_read, vec![denied.to_str().unwrap().to_owned()])
|
||||
);
|
||||
}
|
||||
@@ -813,8 +810,9 @@ fn symbolic_root_preserves_equal_path_precedence_and_denies() -> Result<()> {
|
||||
&volumes,
|
||||
&[],
|
||||
)?;
|
||||
let request = request.filesystem.unwrap();
|
||||
assert_eq!(
|
||||
request.policy.readwrite_paths,
|
||||
request.readwrite_paths,
|
||||
vec![
|
||||
volumes[0].to_str().unwrap().to_owned(),
|
||||
volumes[0].join("child").to_str().unwrap().to_owned(),
|
||||
@@ -823,7 +821,6 @@ fn symbolic_root_preserves_equal_path_precedence_and_denies() -> Result<()> {
|
||||
]
|
||||
);
|
||||
let reads_in_volumes: Vec<_> = request
|
||||
.policy
|
||||
.readonly_paths
|
||||
.into_iter()
|
||||
.filter(|path| Path::new(path).starts_with(&base))
|
||||
@@ -841,10 +838,7 @@ fn symbolic_root_preserves_equal_path_precedence_and_denies() -> Result<()> {
|
||||
)
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
assert_eq!(
|
||||
request.policy.denied_paths,
|
||||
vec![volumes[2].to_str().unwrap()]
|
||||
);
|
||||
assert_eq!(request.denied_paths, vec![volumes[2].to_str().unwrap()]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -878,10 +872,11 @@ fn narrowing_one_volume_keeps_the_other_volume_root_grant() -> Result<()> {
|
||||
&volumes,
|
||||
&[],
|
||||
)?;
|
||||
let request = request.filesystem.unwrap();
|
||||
let granted = if root_access == FileSystemAccessMode::Write {
|
||||
request.policy.readwrite_paths
|
||||
request.readwrite_paths
|
||||
} else {
|
||||
request.policy.readonly_paths
|
||||
request.readonly_paths
|
||||
};
|
||||
let second_grants: Vec<_> = granted
|
||||
.into_iter()
|
||||
@@ -917,8 +912,9 @@ fn volume_enumeration_uses_windows_identity_for_read_write_overrides() -> Result
|
||||
std::slice::from_ref(&root),
|
||||
&[],
|
||||
)?;
|
||||
let request = request.filesystem.unwrap();
|
||||
assert_eq!(
|
||||
request.policy.readwrite_paths,
|
||||
request.readwrite_paths,
|
||||
vec![
|
||||
PathUri::from_host_native_path(&alias)?
|
||||
.to_abs_path()?
|
||||
@@ -929,7 +925,6 @@ fn volume_enumeration_uses_windows_identity_for_read_write_overrides() -> Result
|
||||
]
|
||||
);
|
||||
let readonly_child: Vec<_> = request
|
||||
.policy
|
||||
.readonly_paths
|
||||
.into_iter()
|
||||
.filter(|path| path.eq_ignore_ascii_case(child.to_str().unwrap()))
|
||||
|
||||
@@ -67,7 +67,7 @@ pub(super) fn run() -> Result<i32> {
|
||||
&volumes,
|
||||
&platform_read_roots()?,
|
||||
)?;
|
||||
crate::native::launch(&request)
|
||||
crate::native::launch(request)
|
||||
}
|
||||
|
||||
pub(super) fn volume_roots(
|
||||
|
||||
Reference in New Issue
Block a user