Skip to content

Commit 92e2f2a

Browse files
authored
fix(mxc): complete proxy CA staging isolation (#3535)
* test(mxc): provide workload dir for CA staging Signed-off-by: Prekshi Vyas <[email protected]> * fix(mxc): isolate staged proxy CA files Signed-off-by: Prekshi Vyas <[email protected]> --------- Signed-off-by: Prekshi Vyas <[email protected]> Signed-off-by: Prekshi Vyas <[email protected]>
1 parent 0348267 commit 92e2f2a

2 files changed

Lines changed: 71 additions & 51 deletions

File tree

‎crates/openshell-driver-mxc/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ command as the static sandbox process identity because MXC does not expose
163163
Linux-style procfs socket ownership. For HTTPS L7 inspection, the host proxy generates a
164164
per-sandbox CA and injects `NODE_EXTRA_CA_CERTS`, `DENO_CERT`, `SSL_CERT_FILE`,
165165
`REQUESTS_CA_BUNDLE`, `CURL_CA_BUNDLE`, and `GIT_SSL_CAINFO` into the agent
166-
process env. In curated-environment mode, the driver stages the public CA files
167-
under the authorized `<cwd>/.openshell-proxy/<sandbox-id>` directory. Other
168-
environment modes grant the sandbox's unique public-CA directory as an internal
169-
read-write share. The directory contains only public CA certificates;
170-
the ephemeral CA private key remains in the host proxy's memory. The driver
166+
process env. In every environment mode, the driver stages the public CA files
167+
under the authorized `<cwd>/.openshell-proxy/<sandbox-id>` directory. The host
168+
proxy's private temporary directory is never added to the sandbox's read-write
169+
shares. The staged directory contains only public CA certificates; the ephemeral
170+
CA private key remains in the host proxy's memory. The driver
171171
seeds only `SYSTEMROOT`, `WINDIR`, `PATH`, `COMSPEC`, and `LOCALAPPDATA` from the
172172
gateway host before applying sandbox and TLS overrides, so required Windows
173173
bootstrap values remain available without exposing the gateway's full

‎crates/openshell-driver-mxc/src/driver.rs‎

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -761,25 +761,6 @@ fn quote_windows_argument(arg: &str) -> String {
761761
quoted.push('"');
762762
quoted
763763
}
764-
fn append_tls_readwrite_grant(
765-
readwrite_paths: &mut Vec<String>,
766-
ca_paths: Option<&(PathBuf, PathBuf)>,
767-
) {
768-
let Some((ca_cert_path, _)) = ca_paths else {
769-
return;
770-
};
771-
let Some(dir) = ca_cert_path.parent() else {
772-
return;
773-
};
774-
let dir = dir.display().to_string();
775-
if !readwrite_paths
776-
.iter()
777-
.any(|existing| existing.eq_ignore_ascii_case(&dir))
778-
{
779-
readwrite_paths.push(dir);
780-
}
781-
}
782-
783764
impl MxcComputeBackend {
784765
pub fn new(config: MxcComputeConfig) -> Self {
785766
let invoker = WxcExecInvoker::new(&config.wxc_exec_path, config.debug);
@@ -1553,10 +1534,7 @@ async fn run_lifecycle(
15531534
));
15541535
}
15551536

1556-
let mut readwrite_paths = mapped.readwrite_paths;
1557-
if !config.pc_minimal_env {
1558-
append_tls_readwrite_grant(&mut readwrite_paths, host_proxy_ca_paths.as_ref());
1559-
}
1537+
let readwrite_paths = mapped.readwrite_paths;
15601538
let readonly_paths = mapped.readonly_paths;
15611539
let ui = mapped.ui;
15621540
let filesystem = MxcFilesystem {
@@ -1592,8 +1570,8 @@ async fn run_lifecycle(
15921570
.map(|(k, v)| format!("{k}={v}"))
15931571
.collect();
15941572
append_provider_child_env(&mut env, provider_credentials.as_ref());
1595-
// Layer proxy configuration for every env tier. Curated ProcessContainers
1596-
// use the staged CA copies above; other tiers use the original paths.
1573+
// Layer proxy configuration for every env tier using the staged CA copies
1574+
// above. The host proxy's private temporary directory is never shared.
15971575
append_tls_env_vars(&mut env, agent_proxy_ca_paths.as_ref());
15981576
append_proxy_env_vars(&mut env, proxy_addr, proxy_auth.as_ref());
15991577
env.sort(); // deterministic order for logging / debugging
@@ -2721,6 +2699,8 @@ mod lifecycle_tests {
27212699

27222700
let shell =
27232701
std::env::var("COMSPEC").unwrap_or_else(|_| r"C:\Windows\System32\cmd.exe".to_string());
2702+
let workload = tempfile::tempdir().expect("temporary workload directory");
2703+
let workload_dir = workload.path().to_string_lossy().into_owned();
27242704
let mut policy = fs_policy(&[]);
27252705
policy.network_policies.insert(
27262706
"github".to_string(),
@@ -2735,10 +2715,19 @@ mod lifecycle_tests {
27352715
provider_credentialed: true,
27362716
..Default::default()
27372717
}],
2738-
binaries: vec![NetworkBinary { path: shell }],
2718+
binaries: vec![NetworkBinary {
2719+
path: shell.clone(),
2720+
}],
27392721
},
27402722
);
2741-
let mut sandbox = with_policy(driver_sandbox("sb-provider-env"), policy);
2723+
let mut sandbox = with_policy(
2724+
driver_sandbox_with_command(
2725+
"sb-provider-env",
2726+
&workload_dir,
2727+
vec![shell, "/c".into(), "exit 0".into()],
2728+
),
2729+
policy,
2730+
);
27422731
sandbox
27432732
.spec
27442733
.as_mut()
@@ -3021,19 +3010,6 @@ mod lifecycle_tests {
30213010
assert!(env.contains(&format!("GIT_SSL_CAINFO={bundle_path}")));
30223011
}
30233012

3024-
#[test]
3025-
fn tls_readwrite_grant_adds_ca_directory_once() {
3026-
let tls_dir = std::env::temp_dir().join("openshell-mxc-tls-test");
3027-
let ca_cert = tls_dir.join("openshell-ca.pem");
3028-
let bundle = tls_dir.join("ca-bundle.pem");
3029-
let existing = tls_dir.display().to_string().to_ascii_lowercase();
3030-
let mut readwrite = vec![existing.clone()];
3031-
3032-
append_tls_readwrite_grant(&mut readwrite, Some(&(ca_cert, bundle)));
3033-
3034-
assert_eq!(readwrite, vec![existing]);
3035-
}
3036-
30373013
#[test]
30383014
fn windows_command_line_preserves_argument_boundaries() {
30393015
assert_eq!(
@@ -3228,18 +3204,22 @@ mod lifecycle_tests {
32283204
);
32293205
assert!(recorded["network"].get("proxy").is_none());
32303206

3231-
let proxy_addr = {
3207+
let (proxy_addr, host_proxy_ca_paths) = {
32323208
let registry = backend.registry.lock().await;
32333209
let entry = registry.get(sandbox_id).expect("registry entry");
3234-
assert!(
3235-
entry.host_proxy.is_some(),
3236-
"{sandbox_id}: governed egress must hold a live host proxy"
3237-
);
3210+
let host_proxy = entry.host_proxy.as_ref().unwrap_or_else(|| {
3211+
panic!("{sandbox_id}: governed egress must hold a live host proxy")
3212+
});
32383213
assert_eq!(
32393214
entry.trimmed_policy.as_ref().unwrap().network_policies,
32403215
policy.network_policies
32413216
);
3242-
entry.proxy_addr.expect("proxy address")
3217+
(
3218+
entry.proxy_addr.expect("proxy address"),
3219+
host_proxy
3220+
.ca_file_paths()
3221+
.expect("governed egress proxy must expose public CA paths"),
3222+
)
32433223
};
32443224
tokio::time::timeout(
32453225
Duration::from_secs(2),
@@ -3250,6 +3230,46 @@ mod lifecycle_tests {
32503230
.expect("proxy listener must accept connections");
32513231

32523232
let child_env = recorded["process"]["env"].as_array().expect("child env");
3233+
let tls_env = child_env
3234+
.iter()
3235+
.filter_map(serde_json::Value::as_str)
3236+
.filter_map(|entry| entry.split_once('='))
3237+
.filter(|(key, _)| TLS_ENV_KEYS.contains(key))
3238+
.collect::<HashMap<_, _>>();
3239+
assert_eq!(
3240+
tls_env.len(),
3241+
TLS_ENV_KEYS.len(),
3242+
"{sandbox_id}: every TLS trust variable must be replaced"
3243+
);
3244+
let staged_ca_dir = PathBuf::from(&share)
3245+
.join(".openshell-proxy")
3246+
.join(sandbox_id);
3247+
for key in TLS_ENV_KEYS {
3248+
let path = PathBuf::from(
3249+
tls_env
3250+
.get(key)
3251+
.unwrap_or_else(|| panic!("{sandbox_id}: missing {key}")),
3252+
);
3253+
assert_eq!(
3254+
path.parent(),
3255+
Some(staged_ca_dir.as_path()),
3256+
"{sandbox_id}: {key} must use the staged CA directory"
3257+
);
3258+
assert!(path.is_file(), "{sandbox_id}: staged {key} path must exist");
3259+
}
3260+
let host_ca_dir = host_proxy_ca_paths
3261+
.0
3262+
.parent()
3263+
.expect("host CA path must have a parent");
3264+
assert!(
3265+
recorded["filesystem"]["readwritePaths"]
3266+
.as_array()
3267+
.expect("read-write paths")
3268+
.iter()
3269+
.filter_map(serde_json::Value::as_str)
3270+
.all(|path| Path::new(path) != host_ca_dir),
3271+
"{sandbox_id}: the host proxy CA directory must not be writable"
3272+
);
32533273
let proxy_env = child_env
32543274
.iter()
32553275
.filter_map(serde_json::Value::as_str)

0 commit comments

Comments
 (0)