use crate::mxc_common::default_env::EnvResolution;
use crate::mxc_common::models::{
ExecutionRequest, NetworkAction, NetworkEnforcementMode, NetworkPolicy,
};
use super::error::IsolationSessionError;
const ERR_FILESYSTEM_POLICY: &str =
"filesystem policy is not supported by the isolation session backend";
const ERR_UI_POLICY: &str = "UI policy is not supported by the isolation session backend; the \
session isolates the host's UI from the contained code but does not deny it UI \
capabilities (window creation, GDI, and the session's own clipboard all work inside \
it), so no ui posture is truthful here. Omitting the ui section is accepted but \
applies no restriction — it is not the lockdown the schema's default implies. Use a \
backend that enforces UI policy if you need one";
const ERR_NETWORK_POLICY: &str = "the network is unrestricted and cannot be filtered or denied; \
describe that posture with network.egress.default=allow, \
network.ingress.default=allow, and network.ingress.hostLoopback=allow; do not supply rules, \
proxy settings, or legacy network fields, or use a backend that enforces network policy";
const ERR_PROXY_POLICY: &str =
"the network cannot be routed through a proxy; remove network.proxy \
(the container's network is unrestricted and unproxied)";
const ERR_NETWORK_IMMUTABLE: &str =
"network policy is fixed at provision and cannot be changed on \
this phase; omit the network policy on post-provision phases";
const ERR_ENVIRONMENT_POLICY: &str = "process.env without process.inheritDefaultEnv=true is not \
supported by the isolation session backend: every process starts from the agent user's \
default environment, which cannot be replaced or emptied; set process.inheritDefaultEnv to \
true to layer process.env over it, or omit process.env";
pub(super) fn validate_provision_policy(
request: &ExecutionRequest,
) -> Result<(), IsolationSessionError> {
reject_filesystem_policy(request)?;
reject_ui_policy(request)?;
validate_provision_network_policy(request)
}
pub(super) fn validate_post_provision_policy(
request: &ExecutionRequest,
) -> Result<(), IsolationSessionError> {
reject_filesystem_policy(request)?;
reject_ui_policy(request)?;
if request.policy.network_proxy.is_enabled() {
return Err(IsolationSessionError::Policy(ERR_PROXY_POLICY.to_string()));
}
if request.policy.network_specified
|| request.policy.network_mode_specified
|| request.policy.network_egress.is_some()
|| request.policy.network_ingress.is_some()
{
return Err(IsolationSessionError::Policy(
ERR_NETWORK_IMMUTABLE.to_string(),
));
}
Ok(())
}
pub(super) fn reject_unhonorable_environment(
request: &ExecutionRequest,
) -> Result<(), IsolationSessionError> {
match EnvResolution::of(request) {
EnvResolution::Replace => Err(IsolationSessionError::Policy(
ERR_ENVIRONMENT_POLICY.to_string(),
)),
EnvResolution::Default | EnvResolution::Overlay => Ok(()),
}
}
fn reject_filesystem_policy(request: &ExecutionRequest) -> Result<(), IsolationSessionError> {
if !request.policy.readwrite_paths.is_empty()
|| !request.policy.readonly_paths.is_empty()
|| !request.policy.enumerate_paths.is_empty()
|| !request.policy.denied_paths.is_empty()
{
return Err(IsolationSessionError::Policy(
ERR_FILESYSTEM_POLICY.to_string(),
));
}
Ok(())
}
fn reject_ui_policy(request: &ExecutionRequest) -> Result<(), IsolationSessionError> {
if request.policy.ui_specified {
return Err(IsolationSessionError::Policy(ERR_UI_POLICY.to_string()));
}
Ok(())
}
fn validate_provision_network_policy(
request: &ExecutionRequest,
) -> Result<(), IsolationSessionError> {
let policy = &request.policy;
let is_directional_allow = policy.default_network_policy == NetworkPolicy::Block
&& !policy.allow_local_network
&& policy.allowed_hosts.is_empty()
&& policy.blocked_hosts.is_empty()
&& policy.network_enforcement_mode == NetworkEnforcementMode::Capabilities
&& policy.network_egress.as_ref().is_some_and(|egress| {
egress.default == NetworkAction::Allow
&& egress.allow.is_empty()
&& egress.deny.is_empty()
})
&& policy.network_ingress.as_ref().is_some_and(|ingress| {
ingress.default == NetworkAction::Allow && ingress.host_loopback == NetworkAction::Allow
});
if !is_directional_allow {
return Err(IsolationSessionError::Policy(
ERR_NETWORK_POLICY.to_string(),
));
}
if policy.network_proxy.is_enabled() {
return Err(IsolationSessionError::Policy(ERR_PROXY_POLICY.to_string()));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mxc_common::models::{
ContainerPolicy, DefaultEnvCompatibility, NetworkEgressPolicy, NetworkIngressPolicy,
ProxyAddress, ProxyConfig, UiPolicy,
};
use crate::mxc_common::mxc_error::MxcErrorCode;
fn assert_policy_err_contains(err: IsolationSessionError, expected: &str) {
match err {
IsolationSessionError::Policy(msg) => {
assert!(msg.contains(expected), "expected '{}' in {}", expected, msg)
}
other => panic!("expected Policy variant, got {:?}", other),
}
}
fn canonical_allow_policy() -> ContainerPolicy {
ContainerPolicy {
default_network_policy: NetworkPolicy::Allow,
allow_local_network: true,
..Default::default()
}
}
fn directional_allow_policy() -> ContainerPolicy {
ContainerPolicy {
network_egress: Some(NetworkEgressPolicy {
default: NetworkAction::Allow,
..Default::default()
}),
network_ingress: Some(NetworkIngressPolicy {
default: NetworkAction::Allow,
host_loopback: NetworkAction::Allow,
}),
network_specified: true,
network_mode_specified: true,
..Default::default()
}
}
#[test]
fn provision_policy_rejects_readwrite_paths() {
let request = ExecutionRequest {
policy: ContainerPolicy {
readwrite_paths: vec!["C:\\src".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn provision_policy_rejects_readonly_paths() {
let request = ExecutionRequest {
policy: ContainerPolicy {
readonly_paths: vec!["C:\\data".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn provision_policy_rejects_enumerate_paths() {
let request = ExecutionRequest {
policy: ContainerPolicy {
enumerate_paths: vec!["C:\\data".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn provision_policy_rejects_readwrite_and_readonly_together() {
let request = ExecutionRequest {
policy: ContainerPolicy {
readwrite_paths: vec!["C:\\src".to_string()],
readonly_paths: vec!["C:\\data".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn provision_policy_rejects_denied_paths() {
let request = ExecutionRequest {
policy: ContainerPolicy {
denied_paths: vec!["C:\\secret".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn provision_policy_rejects_denied_even_with_rw() {
let request = ExecutionRequest {
policy: ContainerPolicy {
readwrite_paths: vec!["C:\\src".to_string()],
denied_paths: vec!["C:\\secret".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn provision_policy_rejects_legacy_allow() {
let request = ExecutionRequest {
policy: canonical_allow_policy(),
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_NETWORK_POLICY,
);
}
#[test]
fn provision_policy_accepts_directional_allow() {
let request = ExecutionRequest {
policy: directional_allow_policy(),
..Default::default()
};
validate_provision_policy(&request).unwrap();
}
#[test]
fn provision_policy_rejects_mixed_or_partial_directional_postures() {
for policy in [
ContainerPolicy {
network_ingress: None,
..directional_allow_policy()
},
ContainerPolicy {
network_egress: None,
..directional_allow_policy()
},
ContainerPolicy {
network_ingress: Some(NetworkIngressPolicy {
default: NetworkAction::Allow,
host_loopback: NetworkAction::Deny,
}),
..directional_allow_policy()
},
ContainerPolicy {
default_network_policy: NetworkPolicy::Allow,
allow_local_network: true,
..directional_allow_policy()
},
ContainerPolicy {
network_egress: Some(NetworkEgressPolicy {
default: NetworkAction::Allow,
allow: vec![Default::default()],
deny: Vec::new(),
}),
..directional_allow_policy()
},
] {
assert_policy_err_contains(
validate_provision_policy(&ExecutionRequest {
policy,
..Default::default()
})
.unwrap_err(),
ERR_NETWORK_POLICY,
);
}
}
#[test]
fn provision_policy_rejects_default_request() {
let request = ExecutionRequest::default();
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_NETWORK_POLICY,
);
}
#[test]
fn provision_policy_rejects_block_even_with_local_network() {
let request = ExecutionRequest {
policy: ContainerPolicy {
default_network_policy: NetworkPolicy::Block,
allow_local_network: true,
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_NETWORK_POLICY,
);
}
#[test]
fn provision_policy_rejects_allow_without_local_network() {
let request = ExecutionRequest {
policy: ContainerPolicy {
default_network_policy: NetworkPolicy::Allow,
allow_local_network: false,
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_NETWORK_POLICY,
);
}
#[test]
fn provision_policy_rejects_allowed_hosts() {
let request = ExecutionRequest {
policy: ContainerPolicy {
allowed_hosts: vec!["example.com".to_string()],
..directional_allow_policy()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_NETWORK_POLICY,
);
}
#[test]
fn provision_policy_rejects_blocked_hosts() {
let request = ExecutionRequest {
policy: ContainerPolicy {
blocked_hosts: vec!["evil.com".to_string()],
..directional_allow_policy()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_NETWORK_POLICY,
);
}
#[test]
fn provision_policy_rejects_firewall_enforcement() {
let request = ExecutionRequest {
policy: ContainerPolicy {
network_enforcement_mode: NetworkEnforcementMode::Firewall,
..canonical_allow_policy()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_NETWORK_POLICY,
);
}
#[test]
fn provision_policy_rejects_both_enforcement() {
let request = ExecutionRequest {
policy: ContainerPolicy {
network_enforcement_mode: NetworkEnforcementMode::Both,
..canonical_allow_policy()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_NETWORK_POLICY,
);
}
#[test]
fn provision_policy_rejects_proxy() {
let request = ExecutionRequest {
policy: ContainerPolicy {
network_proxy: ProxyConfig {
address: Some(ProxyAddress::new("127.0.0.1".to_string(), 8080)),
builtin_test_server: false,
},
..directional_allow_policy()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_PROXY_POLICY,
);
}
#[test]
fn provision_policy_filesystem_error_takes_precedence_over_network() {
let request = ExecutionRequest {
policy: ContainerPolicy {
readwrite_paths: vec!["C:\\src".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn provision_policy_rejects_supplied_ui() {
let request = ExecutionRequest {
policy: ContainerPolicy {
ui_specified: true,
..canonical_allow_policy()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
"UI policy is not supported",
);
}
#[test]
fn provision_policy_accepts_absent_ui() {
let request = ExecutionRequest {
policy: directional_allow_policy(),
..Default::default()
};
validate_provision_policy(&request).unwrap();
}
#[test]
fn provision_policy_rejects_lockdown_equivalent_ui() {
let request = ExecutionRequest {
policy: ContainerPolicy {
ui_specified: true,
ui: UiPolicy::default(),
..canonical_allow_policy()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&request).unwrap_err(),
"UI policy is not supported",
);
}
#[test]
fn post_provision_policy_rejects_supplied_ui() {
let request = ExecutionRequest {
policy: ContainerPolicy {
ui_specified: true,
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_post_provision_policy(&request).unwrap_err(),
"UI policy is not supported",
);
}
#[test]
fn post_provision_policy_accepts_absent_ui() {
let request = ExecutionRequest::default();
assert!(validate_post_provision_policy(&request).is_ok());
}
#[test]
fn ui_error_takes_precedence_over_network_but_not_filesystem() {
let fs_and_ui = ExecutionRequest {
policy: ContainerPolicy {
readwrite_paths: vec!["C:\\src".to_string()],
ui_specified: true,
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&fs_and_ui).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
let ui_and_network = ExecutionRequest {
policy: ContainerPolicy {
ui_specified: true,
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_provision_policy(&ui_and_network).unwrap_err(),
"UI policy is not supported",
);
}
#[test]
fn ui_rejection_maps_to_policy_validation() {
let request = ExecutionRequest {
policy: ContainerPolicy {
ui_specified: true,
..Default::default()
},
..Default::default()
};
let err = super::super::error::map_lifecycle_error(
validate_provision_policy(&request).unwrap_err(),
);
assert_eq!(err.code, MxcErrorCode::PolicyValidation);
}
#[test]
fn post_provision_policy_rejects_readwrite_paths() {
let request = ExecutionRequest {
policy: ContainerPolicy {
readwrite_paths: vec!["C:\\src".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_post_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn post_provision_policy_rejects_readonly_paths() {
let request = ExecutionRequest {
policy: ContainerPolicy {
readonly_paths: vec!["C:\\data".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_post_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn post_provision_policy_rejects_denied_paths() {
let request = ExecutionRequest {
policy: ContainerPolicy {
denied_paths: vec!["C:\\secret".to_string()],
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_post_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
#[test]
fn post_provision_policy_rejects_specified_network() {
let request = ExecutionRequest {
policy: ContainerPolicy {
network_specified: true,
..canonical_allow_policy()
},
..Default::default()
};
assert_policy_err_contains(
validate_post_provision_policy(&request).unwrap_err(),
ERR_NETWORK_IMMUTABLE,
);
}
#[test]
fn post_provision_policy_rejects_specified_network_even_when_block() {
let request = ExecutionRequest {
policy: ContainerPolicy {
network_specified: true,
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_post_provision_policy(&request).unwrap_err(),
ERR_NETWORK_IMMUTABLE,
);
}
#[test]
fn post_provision_policy_accepts_absent_network() {
let request = ExecutionRequest::default();
assert!(validate_post_provision_policy(&request).is_ok());
}
#[test]
fn post_provision_policy_rejects_raw_directional_policy() {
let request = ExecutionRequest {
policy: ContainerPolicy {
network_egress: Some(NetworkEgressPolicy::default()),
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_post_provision_policy(&request).unwrap_err(),
ERR_NETWORK_IMMUTABLE,
);
}
#[test]
fn post_provision_policy_filesystem_error_takes_precedence() {
let request = ExecutionRequest {
policy: ContainerPolicy {
readwrite_paths: vec!["C:\\src".to_string()],
network_specified: true,
..Default::default()
},
..Default::default()
};
assert_policy_err_contains(
validate_post_provision_policy(&request).unwrap_err(),
ERR_FILESYSTEM_POLICY,
);
}
fn request_with_env(
compatibility: DefaultEnvCompatibility,
env: Option<Vec<&str>>,
inherit_default_env: bool,
) -> ExecutionRequest {
ExecutionRequest {
default_env_compatibility: compatibility,
env: env.map(|e| e.into_iter().map(String::from).collect()),
inherit_default_env,
..Default::default()
}
}
#[test]
fn only_an_environment_that_would_replace_the_default_is_refused() {
use DefaultEnvCompatibility::DefaultBlock;
let cases = [
(None, false, DefaultBlock, true),
(None, true, DefaultBlock, true),
(Some(vec![]), false, DefaultBlock, false),
(Some(vec![]), true, DefaultBlock, true),
(Some(vec!["FOO=bar"]), false, DefaultBlock, false),
(Some(vec!["FOO=bar"]), true, DefaultBlock, true),
];
for (env, inherit_default_env, compatibility, accepted) in cases {
let state =
format!("{env:?}, inherit_default_env={inherit_default_env}, {compatibility:?}");
let request = request_with_env(compatibility, env, inherit_default_env);
match (reject_unhonorable_environment(&request), accepted) {
(Ok(()), true) => {}
(Err(err), false) => assert_policy_err_contains(err, ERR_ENVIRONMENT_POLICY),
(result, _) => panic!("{state}: expected accepted={accepted}, got {result:?}"),
}
}
}
}