Skip to content

Commit edde8d5

Browse files
committed
fix(driver-mxc): emit MXC network.proxy as {localhost: port}
Verified against the real wxc-exec 0.6.0-alpha via --dry-run: MXC accepts only the {localhost: N} proxy shape (the form the design doc specifies) and rejects {host, port} with a parse error. Schema 0.6.0-alpha can express only a loopback port, so non-127.0.0.1 redirect addresses are now rejected: split_policy emits an error loss (no proxy block) and the driver refuses egress_proxy_addr values off 127.0.0.1. Per-sandbox attribution must use per-sandbox ports until the schema widens. Signed-off-by: Giedrius Burachas <[email protected]>
1 parent eae6a62 commit edde8d5

4 files changed

Lines changed: 207 additions & 20 deletions

File tree

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,24 @@ fn configured_egress_addr(config: &MxcComputeConfig) -> Result<Option<SocketAddr
217217
"mxc egress_proxy_addr is required when egress_proxy is enabled",
218218
));
219219
}
220-
raw.parse::<SocketAddr>().map(Some).map_err(|e| {
220+
let addr = raw.parse::<SocketAddr>().map_err(|e| {
221221
tonic::Status::invalid_argument(format!(
222222
"mxc egress_proxy_addr must be an IP:PORT socket address: {e}"
223223
))
224-
})
224+
})?;
225+
// MXC 0.6.0-alpha expresses the redirect as {"proxy": {"localhost": N}}
226+
// — it has no way to encode a non-loopback host. Reject early so the
227+
// operator gets a clear message rather than a silent policy gap.
228+
// Use 127.0.0.1:PORT. Future schema versions may lift this restriction.
229+
if addr.ip() != std::net::IpAddr::from([127, 0, 0, 1]) {
230+
return Err(tonic::Status::invalid_argument(format!(
231+
"mxc egress_proxy_addr must be 127.0.0.1:PORT — MXC 0.6.0-alpha \
232+
expresses the redirect as {{\"localhost\": N}} and cannot encode \
233+
non-loopback addresses (got {})",
234+
addr.ip()
235+
)));
236+
}
237+
Ok(Some(addr))
225238
}
226239

227240
impl MxcComputeBackend {
@@ -870,6 +883,29 @@ mod lifecycle_tests {
870883
assert!(config.egress_proxy_addr.is_empty());
871884
}
872885

886+
#[test]
887+
fn egress_non_loopback_addr_is_rejected() {
888+
// MXC 0.6.0-alpha can only express {"proxy": {"localhost": N}}, so
889+
// non-127.0.0.1 redirect addresses must be rejected at validate time.
890+
let mut config = demo_config(
891+
"C:/work/demo",
892+
vec!["cmd".into(), "/c".into(), "exit 0".into()],
893+
);
894+
config.backend = MxcBackend::ProcessContainer;
895+
config.egress_proxy = true;
896+
config.egress_proxy_addr = "10.0.0.1:18080".into();
897+
let backend = MxcComputeBackend::new_mocked(config);
898+
let err = backend
899+
.validate_sandbox_create(&driver_sandbox("sb-nonlocal"))
900+
.unwrap_err();
901+
assert_eq!(err.code(), tonic::Code::InvalidArgument);
902+
assert!(
903+
err.message().contains("127.0.0.1"),
904+
"error should mention 127.0.0.1, got: {}",
905+
err.message()
906+
);
907+
}
908+
873909
#[test]
874910
fn egress_on_isolation_session_is_rejected() {
875911
let mut config = demo_config(
@@ -1061,8 +1097,16 @@ mod lifecycle_tests {
10611097
.unwrap()
10621098
.is_empty()
10631099
);
1064-
assert_eq!(recorded["network"]["proxy"]["host"], "127.0.0.1");
1065-
assert_eq!(recorded["network"]["proxy"]["port"], 18080);
1100+
// MXC 0.6.0-alpha accepts only {"proxy": {"localhost": N}}.
1101+
assert_eq!(recorded["network"]["proxy"]["localhost"], 18080);
1102+
assert!(
1103+
recorded["network"]["proxy"].get("host").is_none(),
1104+
"proxy must not contain 'host' key"
1105+
);
1106+
assert!(
1107+
recorded["network"]["proxy"].get("port").is_none(),
1108+
"proxy must not contain 'port' key"
1109+
);
10661110

10671111
let reg = backend.registry.lock().await;
10681112
let entry = reg.get("sb-egress").expect("registry entry");

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,19 @@ pub struct MxcProcess {
9494
}
9595

9696
fn network_json(network: &MxcNetwork) -> serde_json::Value {
97+
// MXC 0.6.0-alpha schema accepts ONLY {"proxy": {"localhost": <port>}}.
98+
// {"host": ..., "port": ...} and every other shape is rejected — verified
99+
// empirically against the real wxc-exec 0.6.0-alpha binary via --dry-run.
100+
// See also docs/reference/mxc-compute-driver-design.mdx §network.proxy.
101+
// The MxcNetwork.proxy field remains SocketAddr so callers keep full
102+
// precision; only the port is serialized into the localhost key.
97103
let mut value = serde_json::json!({
98104
"defaultPolicy": network.default_policy.as_str(),
99105
"allowedHosts": [],
100106
"blockedHosts": [],
101107
});
102108
if let Some(proxy) = network.proxy {
103-
value["proxy"] = serde_json::json!({
104-
"host": proxy.ip().to_string(),
105-
"port": proxy.port(),
106-
});
109+
value["proxy"] = serde_json::json!({ "localhost": proxy.port() });
107110
}
108111
value
109112
}
@@ -755,8 +758,31 @@ mod tests {
755758
.unwrap()
756759
.is_empty()
757760
);
758-
assert_eq!(config["network"]["proxy"]["host"], "127.0.0.1");
759-
assert_eq!(config["network"]["proxy"]["port"], 18080);
761+
// MXC 0.6.0-alpha accepts only {"proxy": {"localhost": N}}.
762+
assert_eq!(config["network"]["proxy"]["localhost"], 18080);
763+
assert!(
764+
config["network"]["proxy"].get("host").is_none(),
765+
"proxy must not contain 'host' key"
766+
);
767+
assert!(
768+
config["network"]["proxy"].get("port").is_none(),
769+
"proxy must not contain 'port' key"
770+
);
771+
}
772+
773+
#[test]
774+
fn network_json_emits_localhost_port_shape() {
775+
// MXC 0.6.0-alpha rejects {"host":...,"port":...} and accepts only
776+
// {"proxy": {"localhost": N}} — verified against the real binary via
777+
// --dry-run. This test pins the exact emitted JSON shape.
778+
let network = MxcNetwork {
779+
default_policy: "block".into(),
780+
proxy: Some("127.0.0.1:18080".parse().unwrap()),
781+
};
782+
let value = network_json(&network);
783+
assert_eq!(value["proxy"]["localhost"], 18080);
784+
assert!(value["proxy"].get("host").is_none());
785+
assert!(value["proxy"].get("port").is_none());
760786
}
761787

762788
#[test]

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,33 @@ fn build_split_mxc_config(
172172

173173
// Direct egress is blocked; all outbound flows through the OpenShell proxy.
174174
// allowedHosts is intentionally empty — the proxy enforces the full policy.
175+
//
176+
// MXC 0.6.0-alpha schema accepts ONLY {"proxy": {"localhost": <port>}}.
177+
// {"host": ..., "port": ...} and every other shape is rejected — verified
178+
// empirically against the real wxc-exec 0.6.0-alpha binary via --dry-run.
179+
// See also docs/reference/mxc-compute-driver-design.mdx §network.proxy.
180+
if proxy_supported && proxy_addr.ip() != std::net::IpAddr::from([127, 0, 0, 1]) {
181+
add_loss(
182+
items,
183+
"network.proxy",
184+
"error",
185+
&format!(
186+
"MXC schema 0.6.0-alpha can only express a localhost port \
187+
({{\"localhost\": N}}); non-127.0.0.1 redirect address {} \
188+
is not representable.",
189+
proxy_addr
190+
),
191+
"per-sandbox egress attribution",
192+
"The redirect cannot be emitted; use a 127.0.0.1:PORT address.",
193+
);
194+
}
175195
let mut network = json!({
176196
"defaultPolicy": "block",
177197
"allowedHosts": [],
178198
"blockedHosts": [],
179199
});
180-
if proxy_supported {
181-
network["proxy"] = json!({
182-
"host": proxy_addr.ip().to_string(),
183-
"port": proxy_addr.port(),
184-
});
200+
if proxy_supported && proxy_addr.ip() == std::net::IpAddr::from([127, 0, 0, 1]) {
201+
network["proxy"] = json!({ "localhost": proxy_addr.port() });
185202
}
186203

187204
let mut config = json!({

‎crates/openshell-driver-mxc/tests/policy_mapper_examples.rs‎

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ fn all_example_policies_split_with_lossless_invariants() {
212212

213213
assert_eq!(cfg["network"]["defaultPolicy"], "block");
214214
assert!(str_list(&cfg["network"]["allowedHosts"]).is_empty());
215-
assert_eq!(cfg["network"]["proxy"]["host"], "127.0.0.1");
216-
assert_eq!(cfg["network"]["proxy"]["port"], 18080);
215+
// MXC 0.6.0-alpha accepts only {"proxy": {"localhost": N}}.
216+
assert_eq!(cfg["network"]["proxy"]["localhost"], 18080);
217+
assert!(cfg["network"]["proxy"].get("host").is_none());
218+
assert!(cfg["network"]["proxy"].get("port").is_none());
217219
assert!(
218220
result.loss.iter().all(|i| i.severity != "error"),
219221
"processcontainer split must not emit error losses for {}: {:?}",
@@ -277,9 +279,23 @@ fn split_policy_routes_network_to_proxy() {
277279
let result = split_policy(&policy, &opts).expect("split_policy returns Some when addr is set");
278280
let cfg = &result.mxc_config;
279281

280-
// Proxy redirect is emitted.
281-
assert_eq!(cfg["network"]["proxy"]["host"], "127.0.0.2");
282-
assert_eq!(cfg["network"]["proxy"]["port"], 8080);
282+
// Proxy redirect is emitted. 127.0.0.2 is not the loopback 127.0.0.1 so
283+
// the mapper records an error loss and omits the proxy block entirely.
284+
// (MXC 0.6.0-alpha can only encode {"localhost": N}; non-127.0.0.1 is
285+
// not representable.)
286+
assert!(
287+
cfg["network"].get("proxy").is_none() || cfg["network"]["proxy"].is_null(),
288+
"non-127.0.0.1 redirect must NOT produce a proxy block: {:?}",
289+
cfg["network"].get("proxy")
290+
);
291+
let has_proxy_loss = result
292+
.loss
293+
.iter()
294+
.any(|i| i.path == "network.proxy" && i.severity == "error");
295+
assert!(
296+
has_proxy_loss,
297+
"non-127.0.0.1 redirect must produce an error loss item"
298+
);
283299

284300
// Direct egress is blocked; allowedHosts is empty (proxy enforces the list).
285301
assert_eq!(cfg["network"]["defaultPolicy"], "block");
@@ -390,3 +406,87 @@ fn network_only_policy_has_empty_filesystem() {
390406
vec!["api.anthropic.com".to_owned()]
391407
);
392408
}
409+
410+
// ── New tests: proxy JSON shape and non-127.0.0.1 guard ──────────────────────
411+
412+
#[test]
413+
fn split_with_loopback_addr_emits_localhost_port_shape() {
414+
// MXC 0.6.0-alpha accepts ONLY {"proxy": {"localhost": N}}.
415+
// Verified against the real wxc-exec 0.6.0-alpha binary via --dry-run.
416+
let path = examples_root().join("sandbox-policy-quickstart/policy.yaml");
417+
let yaml = std::fs::read_to_string(&path).expect("read quickstart");
418+
let policy = parse_sandbox_policy(&yaml).expect("parse quickstart");
419+
420+
let opts = MxcMappingOptions {
421+
containment: "processcontainer".to_owned(),
422+
proxy_redirect: Some("127.0.0.1:18080".parse().unwrap()),
423+
..Default::default()
424+
};
425+
let result = split_policy(&policy, &opts).expect("split returns Some");
426+
let cfg = &result.mxc_config;
427+
428+
assert_eq!(
429+
cfg["network"]["proxy"]["localhost"], 18080,
430+
"proxy must use {{\"localhost\": N}} shape"
431+
);
432+
assert!(
433+
cfg["network"]["proxy"].get("host").is_none(),
434+
"proxy must not contain 'host' key"
435+
);
436+
assert!(
437+
cfg["network"]["proxy"].get("port").is_none(),
438+
"proxy must not contain 'port' key"
439+
);
440+
// No error losses — 127.0.0.1 is representable.
441+
assert!(
442+
result.loss.iter().all(|i| i.severity != "error"),
443+
"127.0.0.1 proxy must not emit error losses: {:?}",
444+
result
445+
.loss
446+
.iter()
447+
.filter(|i| i.severity == "error")
448+
.collect::<Vec<_>>()
449+
);
450+
}
451+
452+
#[test]
453+
fn split_with_non_loopback_addr_emits_error_loss_and_no_proxy_block() {
454+
// Non-127.0.0.1 redirect addresses are not representable in MXC 0.6.0-alpha.
455+
// The mapper must record an error loss and omit the proxy block.
456+
let path = examples_root().join("sandbox-policy-quickstart/policy.yaml");
457+
let yaml = std::fs::read_to_string(&path).expect("read quickstart");
458+
let policy = parse_sandbox_policy(&yaml).expect("parse quickstart");
459+
460+
let opts = MxcMappingOptions {
461+
containment: "processcontainer".to_owned(),
462+
proxy_redirect: Some("127.0.0.5:18080".parse().unwrap()),
463+
..Default::default()
464+
};
465+
let result = split_policy(&policy, &opts).expect("split returns Some");
466+
let cfg = &result.mxc_config;
467+
468+
// Proxy block must be absent.
469+
assert!(
470+
cfg["network"].get("proxy").is_none() || cfg["network"]["proxy"].is_null(),
471+
"non-127.0.0.1 redirect must not produce a proxy block: {:?}",
472+
cfg["network"].get("proxy")
473+
);
474+
475+
// An error loss for "network.proxy" must be present.
476+
let proxy_loss = result
477+
.loss
478+
.iter()
479+
.find(|i| i.path == "network.proxy" && i.severity == "error");
480+
assert!(
481+
proxy_loss.is_some(),
482+
"non-127.0.0.1 redirect must produce an error loss item on network.proxy: {:?}",
483+
result.loss
484+
);
485+
let loss = proxy_loss.unwrap();
486+
assert_eq!(loss.openshell_feature, "per-sandbox egress attribution");
487+
assert!(
488+
loss.message.contains("localhost"),
489+
"loss message should mention 'localhost': {}",
490+
loss.message
491+
);
492+
}

0 commit comments

Comments
 (0)