//! Portable policy and launcher regressions, with Windows identity coverage. use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; use anyhow::Result; use akode_network_proxy::ManagedNetworkSandboxContext; use akode_protocol::models::PermissionProfile; use akode_protocol::permissions::FileSystemSandboxPolicyContext; use akode_protocol::protocol::FileSystemAccessMode; use akode_protocol::protocol::FileSystemPath; use akode_protocol::protocol::FileSystemSandboxEntry; use akode_protocol::protocol::FileSystemSandboxPolicy; use akode_protocol::protocol::FileSystemSpecialPath; use akode_protocol::protocol::NetworkSandboxPolicy; use akode_utils_absolute_path::AbsolutePathBuf; use akode_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 pretty_assertions::assert_eq; use crate::CreateMxcCommandArgsParams; use crate::MxcCommand; use crate::create_command_args; use crate::policy::PolicyError; use crate::policy::build_request; use crate::policy::materialize_volume_roots; #[test] fn symbolic_root_precedence_is_independent_of_drive_and_cwd() -> Result<()> { use FileSystemAccessMode::Deny; use FileSystemAccessMode::Read; use FileSystemAccessMode::Write; let volumes = [ PathUri::parse("file:///C:/")?, PathUri::parse("file:///D:/")?, PathUri::parse("file:///R:/")?, ]; let readonly = PathUri::parse("file:///R:/foobar")?; let writable = PathUri::parse("file:///R:/writable")?; let mut fs = FileSystemSandboxPolicy::read_only(); fs.entries.extend([ FileSystemSandboxEntry::new(volumes[1].clone().into(), Read), FileSystemSandboxEntry::new(readonly.clone().into(), Read), FileSystemSandboxEntry::new(writable.clone().into(), Read), FileSystemSandboxEntry::new(writable.clone().into(), Write), ]); for access in [Read, Write, Deny] { fs.entries[0].access = access; let resolved = materialize_volume_roots(fs.clone(), &volumes, &volumes[..2])?; for volume in &volumes { let cwd = volume.join("work")?; let context = FileSystemSandboxPolicyContext { cwd: &cwd, workspace_roots: std::slice::from_ref(&cwd), user_home_dir: None, temporary_directories: None, }; assert_eq!( [&volumes[0], &volumes[1], &volumes[2], &readonly, &writable] .map(|path| resolved.resolve_access(path, &context)), [ access, access, if access == Read { Deny } else { access }, Read, Write ] ); } } fs.entries[0].access = Write; fs.entries.push(FileSystemSandboxEntry::new( FileSystemPath::Special { value: FileSystemSpecialPath::Tmpdir, }, Read, )); assert!(matches!( materialize_volume_roots(fs, &volumes, &volumes), Err(PolicyError::UnsupportedSymbolicPath) )); Ok(()) } #[test] fn approved_command_expands_volume_roots_without_retaining_original_grants() -> Result<()> { use FileSystemAccessMode::Deny; use FileSystemAccessMode::Read; use FileSystemAccessMode::Write; let volumes = [ PathUri::parse("file:///C:/")?, PathUri::parse("file:///D:/")?, ]; let cwd = volumes[0].join("work")?; let secret = volumes[0].join("private")?; let formerly_readonly = volumes[1].join("scoped")?; let reopened = secret.join("reopened")?; let root_git = volumes[0].join(".git")?; let denied_root_metadata = volumes[0].join(".agents")?; let workspace_git = cwd.join(".git/FETCH_HEAD")?; let context = FileSystemSandboxPolicyContext { cwd: &cwd, workspace_roots: std::slice::from_ref(&cwd), user_home_dir: None, temporary_directories: Some(&[]), }; let mut policy = FileSystemSandboxPolicy::read_only(); policy.entries.extend([ FileSystemSandboxEntry::new(secret.clone().into(), Deny), FileSystemSandboxEntry::new(denied_root_metadata.clone().into(), Deny), FileSystemSandboxEntry::new(reopened.clone().into(), Write), FileSystemSandboxEntry::new(formerly_readonly.clone().into(), Read), ]); let approved = policy.for_approved_command(&context); assert_eq!(approved.resolve_access(&formerly_readonly, &context), Deny); let translated = materialize_volume_roots(approved, &volumes, &volumes[..1])?; assert_eq!( [ &volumes[0], &volumes[1], &formerly_readonly, &secret, &reopened, ] .map(|path| translated.resolve_access(path, &context)), [Write, Write, Write, Deny, Deny] ); assert_eq!( [&root_git, &denied_root_metadata, &workspace_git] .map(|path| translated.can_write_path(path, &context)), [true, false, true] ); Ok(()) } fn canonical_root(temp: &tempfile::TempDir) -> Result { Ok( PathUri::from_host_native_path(std::fs::canonicalize(temp.path())?)? .to_abs_path()? .into_path_buf(), ) } fn entry(path: &Path, access: FileSystemAccessMode) -> Result { Ok(FileSystemSandboxEntry::new( AbsolutePathBuf::from_absolute_path(path)?.into(), access, )) } fn command(permissions: &PermissionProfile, cwd: &Path) -> MxcCommand { MxcCommand { permissions: permissions.clone(), sandbox_policy_cwd: cwd.to_owned(), managed_network: None, command: vec!["program.exe".to_owned(), "--arg".to_owned()], default_child_env: false, } } #[test] fn wrapper_preserves_exact_argv_and_separate_command_cwd() -> Result<()> { let root = tempfile::tempdir()?; let cwd = root.path().join("command-cwd"); let profile = PermissionProfile::from_runtime_permissions( &FileSystemSandboxPolicy::restricted(Vec::new()), NetworkSandboxPolicy::Restricted, ); let argv = vec![ r"C:\Program Files\tool.exe".to_owned(), "--permissions".to_owned(), String::new(), "quotes\" and slash\\".to_owned(), ]; let mut env = HashMap::new(); akode_windows_sandbox::environment_transport::encode("stale", &mut env)?; let wrapped = create_command_args(CreateMxcCommandArgsParams { command: argv.clone(), permission_profile: &profile, sandbox_policy_cwd: root.path(), managed_network: None, env: &mut env, })?; assert_eq!(wrapped, vec![crate::AKODE_WINDOWS_MXC_ARG1]); let parsed = crate::transport::decode(&mut env)?; assert!(parsed.default_child_env); assert!(env.is_empty()); assert_eq!( ( &parsed.permissions, parsed.sandbox_policy_cwd.as_path(), &parsed.command ), (&profile, root.path(), &argv) ); let request = build_request(&parsed, &cwd, vec!["CUSTOM=value".to_owned()], &[], &[])?; assert_eq!( (request.script_code, request.working_directory, request.env), ( r#""C:\Program Files\tool.exe" --permissions "" "quotes\" and slash\\""#.to_owned(), cwd.to_str().unwrap().to_owned(), Some(vec!["CUSTOM=value".to_owned()]), ) ); assert_eq!( build_request(&parsed, &cwd, Vec::new(), &[], &[])?.env, None ); Ok(()) } #[test] fn empty_command_is_rejected_before_native_launch() -> Result<()> { let root = tempfile::tempdir()?; let mut parsed = command(&PermissionProfile::read_only(), root.path()); parsed.command.clear(); let error = build_request(&parsed, root.path(), Vec::new(), &[], &[]).unwrap_err(); assert_eq!(error.to_string(), "MXC command must not be empty"); Ok(()) } #[test] fn native_grants_preserve_denies_and_read_only_carveouts() -> Result<()> { let temp = tempfile::tempdir()?; let canonical = canonical_root(&temp)?; let root = canonical.as_path(); let gitdir = tempfile::tempdir()?; std::fs::write( root.join(".git"), format!("gitdir: {}", gitdir.path().display()), )?; let readonly = root.join("readonly"); let writable_child = readonly.join("writable"); let denied = root.join("secret"); let fs = FileSystemSandboxPolicy::restricted(vec![ entry(root, FileSystemAccessMode::Write)?, entry(&readonly, FileSystemAccessMode::Read)?, entry(&writable_child, FileSystemAccessMode::Write)?, entry(&denied, FileSystemAccessMode::Deny)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request(&command(&profile, root), root, Vec::new(), &[], &[])?; let mut expected_read = [ root.join(".agents"), root.join(".aws"), root.join(".akode"), root.join(".git"), readonly, writable_child.join(".agents"), writable_child.join(".aws"), writable_child.join(".akode"), writable_child.join(".git"), ]; expected_read.sort(); assert_eq!( ( request.policy.readwrite_paths, request.policy.readonly_paths, request.policy.denied_paths ), ( vec![ root.to_str().unwrap().to_owned(), writable_child.to_str().unwrap().to_owned() ], expected_read .iter() .map(|path| path.to_str().unwrap().to_owned()) .collect::>(), vec![denied.to_str().unwrap().to_owned()], ) ); assert!(!request.policy.fallback.allow_dacl_mutation); Ok(()) } #[test] fn volume_expansion_does_not_turn_read_only_child_writable() -> Result<()> { let temp = tempfile::tempdir()?; let canonical = canonical_root(&temp)?; let root = canonical.as_path(); let readonly = root.join("protected"); let writable = root.join("work"); let missing = root.join("new"); let denied = missing.join("parent").join("secret"); std::fs::create_dir(&readonly)?; std::fs::create_dir(&writable)?; let fs = FileSystemSandboxPolicy::restricted(vec![ entry(root, FileSystemAccessMode::Write)?, entry(&readonly, FileSystemAccessMode::Read)?, entry(&denied, FileSystemAccessMode::Deny)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request( &command(&profile, root), root, Vec::new(), &[root.to_owned()], &[], )?; 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()); assert!(denied.parent().unwrap().is_dir()); assert!(!denied.exists()); for directory in [&missing, &missing.join("parent")] { assert_eq!(std::fs::read(directory.join(".chatgptcreated"))?, b""); } for directory in [root, readonly.as_path(), writable.as_path()] { 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.policy.readonly_paths, vec![ root.join(".agents").to_str().unwrap(), root.join(".aws").to_str().unwrap(), root.join(".akode").to_str().unwrap(), root.join(".git").to_str().unwrap(), readonly.to_str().unwrap() ] ); Ok(()) } #[cfg(unix)] #[test] fn volume_expansion_skips_broken_links_and_preserves_other_grants() -> Result<()> { let temp = tempfile::tempdir()?; let root = canonical_root(&temp)?; let uninspectable = root.join("loop"); std::os::unix::fs::symlink(&uninspectable, &uninspectable)?; std::os::unix::fs::symlink(root.join("missing"), root.join("dangling"))?; let readable = root.join("readable"); std::fs::write(&readable, "readable")?; let alias = root.join("alias"); std::os::unix::fs::symlink(&readable, &alias)?; let denied = root.join("secret"); std::fs::write(&denied, "secret")?; let fs = FileSystemSandboxPolicy::restricted(vec![ entry(&root, FileSystemAccessMode::Write)?, entry(&denied, FileSystemAccessMode::Deny)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request( &command(&profile, &root), &root, Vec::new(), std::slice::from_ref(&root), &[], )?; assert_eq!( (request.policy.readwrite_paths, request.policy.denied_paths), ( vec![ root.to_str().unwrap().to_owned(), alias.to_str().unwrap().to_owned(), readable.to_str().unwrap().to_owned() ], vec![denied.to_str().unwrap().to_owned()], ) ); Ok(()) } #[test] fn only_write_access_expands_volume_children() -> Result<()> { let first = tempfile::tempdir()?; let second = tempfile::tempdir()?; let unmapped = tempfile::tempdir()?; let explicit = canonical_root(&unmapped)?; let volumes = [canonical_root(&first)?, canonical_root(&second)?]; std::fs::create_dir(volumes[0].join("one"))?; std::fs::create_dir(volumes[1].join("two"))?; let mut fs = FileSystemSandboxPolicy::read_only(); // Like an unmapped UNC share, this grant is outside the supplied volumes. fs.entries .push(entry(&explicit, FileSystemAccessMode::Read)?); for access in [FileSystemAccessMode::Read, FileSystemAccessMode::Write] { let mut expected = vec![volumes[0].clone(), volumes[1].clone(), explicit.clone()]; if access == FileSystemAccessMode::Write { expected.extend([volumes[0].join("one"), volumes[1].join("two")]); } expected.sort(); fs.entries[0].access = access; fs.entries[1].access = access; let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request( &command(&profile, &volumes[0]), &volumes[0], Vec::new(), &volumes, &[], )?; let native = request.policy; let (granted, other) = if access == FileSystemAccessMode::Write { (native.readwrite_paths, native.readonly_paths) } else { (native.readonly_paths, native.readwrite_paths) }; assert_eq!( granted, expected .iter() .map(|path| path.to_str().unwrap()) .collect::>() ); assert_eq!(other, Vec::::new()); } Ok(()) } #[test] fn managed_network_allows_authorized_loopback_without_lan_or_dns_access() -> Result<()> { let root = tempfile::tempdir()?; let profile = PermissionProfile::from_runtime_permissions( &FileSystemSandboxPolicy::restricted(Vec::new()), NetworkSandboxPolicy::Enabled, ); let proxy = ManagedNetworkSandboxContext { loopback_ports: vec![43123, 43124], allow_local_binding: true, ..Default::default() }; let mut env = HashMap::new(); create_command_args(CreateMxcCommandArgsParams { command: vec!["program.exe".to_owned()], permission_profile: &profile, sandbox_policy_cwd: root.path(), managed_network: Some(&proxy), env: &mut env, })?; let parsed = crate::transport::decode(&mut env)?; let request = build_request(&parsed, root.path(), Vec::new(), &[], &[])?; assert_eq!( request.policy.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(), }) ); assert_eq!( request.policy.network_ingress, Some(NetworkIngressPolicy { default: NetworkAction::Deny, host_loopback: NetworkAction::Allow, }) ); Ok(()) } #[test] fn invalid_managed_network_is_rejected_at_both_boundaries() -> Result<()> { let root = tempfile::tempdir()?; let cwd = root.path(); let profile = PermissionProfile::read_only(); for (loopback_ports, allow_local_binding) in [(Vec::new(), true), (vec![0], true), (vec![43123], false)] { let proxy = ManagedNetworkSandboxContext { loopback_ports, allow_local_binding, ..Default::default() }; let mut env = HashMap::new(); assert!( create_command_args(CreateMxcCommandArgsParams { command: Vec::new(), permission_profile: &profile, sandbox_policy_cwd: cwd, managed_network: Some(&proxy), env: &mut env, }) .is_err() ); let mut parsed = command(&profile, cwd); parsed.managed_network = Some(proxy); assert!(build_request(&parsed, cwd, Vec::new(), &[], &[]).is_err()); } Ok(()) } #[test] fn deny_globs_expand_files_and_directories_before_launch() -> Result<()> { let temp = tempfile::tempdir()?; let root = canonical_root(&temp)?; let nested = root.join("nested"); std::fs::create_dir(&nested)?; let file = nested.join("file.secret"); let directory = nested.join("directory.secret"); std::fs::write(&file, "secret")?; std::fs::create_dir(&directory)?; std::fs::write(nested.join("allowed.txt"), "allowed")?; let missing = root.join("missing").join("parent").join("explicit.secret"); let fs = FileSystemSandboxPolicy::restricted(vec![ entry(&root, FileSystemAccessMode::Write)?, entry(&missing, FileSystemAccessMode::Deny)?, FileSystemSandboxEntry::new( FileSystemPath::GlobPattern { pattern: "**/*.secret".to_owned(), }, FileSystemAccessMode::Deny, ), ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request(&command(&profile, &root), &nested, Vec::new(), &[], &[])?; #[cfg(windows)] { assert!(missing.parent().unwrap().is_dir()); assert!(!missing.exists()); } assert_eq!(request.policy.readwrite_paths, vec![root.to_str().unwrap()]); let mut expected = [file, directory, missing]; expected.sort(); assert_eq!( request.policy.denied_paths, expected.map(|path| path.to_str().unwrap().to_owned()) ); Ok(()) } #[cfg(windows)] #[test] fn denied_parents_are_created_before_glob_expansion() -> Result<()> { let temp = tempfile::tempdir()?; let root = canonical_root(&temp)?; let missing = root.join("missing"); for parent_deny in [ entry(&missing, FileSystemAccessMode::Deny)?, FileSystemSandboxEntry::new( FileSystemPath::GlobPattern { pattern: "**/missing".to_owned(), }, FileSystemAccessMode::Deny, ), ] { let fs = FileSystemSandboxPolicy::restricted(vec![ entry(&root, FileSystemAccessMode::Write)?, parent_deny, entry(&missing.join("child"), FileSystemAccessMode::Deny)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request(&command(&profile, &root), &root, Vec::new(), &[], &[])?; assert!(missing.is_dir()); assert!(!missing.join("child").exists()); assert_eq!( request.policy.denied_paths, vec![ missing.to_str().unwrap(), missing.join("child").to_str().unwrap() ] ); let sentinel = missing.join(".chatgptcreated"); assert_eq!(std::fs::read(&sentinel)?, b""); std::fs::write(&sentinel, "preserve existing contents")?; build_request(&command(&profile, &root), &root, Vec::new(), &[], &[])?; assert_eq!( std::fs::read_to_string(&sentinel)?, "preserve existing contents" ); std::fs::remove_file(sentinel)?; std::fs::remove_dir(&missing)?; } Ok(()) } #[cfg(windows)] #[test] fn deny_parent_cannot_use_sentinel_name() -> Result<()> { let temp = tempfile::tempdir()?; let root = canonical_root(&temp)?; let missing = root.join("missing"); for name in [".chatgptcreated", ".CHATGPTCREATED", ".chatgptcreated. "] { let denied = missing.join(name).join("secret"); let fs = FileSystemSandboxPolicy::restricted(vec![ entry(&root, FileSystemAccessMode::Write)?, entry(&denied, FileSystemAccessMode::Deny)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let error = build_request(&command(&profile, &root), &root, Vec::new(), &[], &[]) .expect_err("sentinel directory names must be rejected before creating parents"); assert!( error .to_string() .contains("reserved directory name .chatgptcreated") ); assert!(!missing.exists()); } Ok(()) } #[test] fn root_deny_keeps_only_narrow_explicit_grants() -> Result<()> { let temp = tempfile::tempdir()?; let canonical = canonical_root(&temp)?; let root = canonical.as_path(); let child = root.join("allowed"); for access in [FileSystemAccessMode::Read, FileSystemAccessMode::Write] { let fs = FileSystemSandboxPolicy::restricted(vec![ FileSystemSandboxEntry::new( FileSystemPath::Special { value: FileSystemSpecialPath::Root, }, access, ), FileSystemSandboxEntry::new( FileSystemPath::Special { value: FileSystemSpecialPath::Root, }, FileSystemAccessMode::Deny, ), entry(&child, access)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request( &command(&profile, root), root, Vec::new(), &[root.to_owned()], &[], )?; let allowed = vec![child.to_str().unwrap().to_owned()]; let expected = match access { FileSystemAccessMode::Read => (Vec::new(), allowed), FileSystemAccessMode::Write => ( allowed, [".agents", ".aws", ".akode", ".git"] .map(|name| child.join(name).to_str().unwrap().to_owned()) .to_vec(), ), FileSystemAccessMode::Deny => unreachable!(), }; assert_eq!( ( request.policy.readwrite_paths, request.policy.readonly_paths ), expected ); assert_eq!(request.policy.denied_paths, Vec::::new()); } Ok(()) } #[test] fn windows_temp_roots_use_filtered_environment_and_keep_explicit_denies() -> Result<()> { let temp = tempfile::tempdir()?; let canonical = canonical_root(&temp)?; let root = canonical.as_path(); let first = root.join("temp").join("~"); let second = root.join("tmp with spaces"); let denied = first.join("secret"); let fs = FileSystemSandboxPolicy::restricted(vec![ FileSystemSandboxEntry::new( FileSystemPath::Special { value: FileSystemSpecialPath::Tmpdir, }, FileSystemAccessMode::Write, ), FileSystemSandboxEntry::new( FileSystemPath::Special { value: FileSystemSpecialPath::SlashTmp, }, FileSystemAccessMode::Write, ), entry(&denied, FileSystemAccessMode::Deny)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request( &command(&profile, root), root, vec![ format!("temp={}", first.display()), format!("Tmp={}", second.display()), ], &[], &[], )?; assert_eq!( request.policy.readwrite_paths, vec![first.to_str().unwrap(), second.to_str().unwrap()] ); assert_eq!(request.policy.denied_paths, vec![denied.to_str().unwrap()]); for value in [ None, Some(""), Some("relative"), Some("~/scratch"), Some("~\\scratch"), Some("C:temp"), Some("\\temp"), ] { let request = build_request( &command(&profile, root), root, value .map(|value| vec![format!("TEMP={value}"), format!("TMP={value}")]) .unwrap_or_default(), &[], &[], )?; assert_eq!(request.policy.readwrite_paths, Vec::::new()); assert_eq!(request.policy.denied_paths, vec![denied.to_str().unwrap()]); } Ok(()) } #[test] fn minimal_platform_roots_preserve_explicit_denies() -> Result<()> { let temp = tempfile::tempdir()?; let root = canonical_root(&temp)?; let allowed = root.join("platform"); let denied = root.join("secret"); for include_defaults in [false, true] { let mut entries = vec![entry(&denied, FileSystemAccessMode::Deny)?]; if include_defaults { entries.push(FileSystemSandboxEntry::new( FileSystemPath::Special { value: FileSystemSpecialPath::Minimal, }, FileSystemAccessMode::Read, )); } let profile = PermissionProfile::from_runtime_permissions( &FileSystemSandboxPolicy::restricted(entries), NetworkSandboxPolicy::Restricted, ); let request = build_request( &command(&profile, &root), &root, Vec::new(), &[], &[allowed.clone(), denied.clone()], )?; 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), (expected_read, vec![denied.to_str().unwrap().to_owned()]) ); } Ok(()) } #[test] fn symbolic_root_preserves_equal_path_precedence_and_denies() -> Result<()> { let temp = tempfile::tempdir()?; let base = canonical_root(&temp)?; let volumes = [ base.join("first"), base.join("readonly"), base.join("denied"), ]; for volume in &volumes { std::fs::create_dir_all(volume.join("child"))?; } let fs = FileSystemSandboxPolicy::restricted(vec![ FileSystemSandboxEntry::new( FileSystemPath::Special { value: FileSystemSpecialPath::Root, }, FileSystemAccessMode::Write, ), entry(&volumes[1], FileSystemAccessMode::Read)?, entry(&volumes[2], FileSystemAccessMode::Deny)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request( &command(&profile, &volumes[0]), &volumes[0], Vec::new(), &volumes, &[], )?; assert_eq!( request.policy.readwrite_paths, vec![ volumes[0].to_str().unwrap().to_owned(), volumes[0].join("child").to_str().unwrap().to_owned(), volumes[1].to_str().unwrap().to_owned(), volumes[1].join("child").to_str().unwrap().to_owned(), ] ); let reads_in_volumes: Vec<_> = request .policy .readonly_paths .into_iter() .filter(|path| Path::new(path).starts_with(&base)) .collect(); assert_eq!( reads_in_volumes, volumes[..2] .iter() .flat_map( |volume| [".agents", ".aws", ".akode", ".git"].map(|name| volume .join(name) .to_str() .unwrap() .to_owned()) ) .collect::>() ); assert_eq!( request.policy.denied_paths, vec![volumes[2].to_str().unwrap()] ); Ok(()) } #[test] fn narrowing_one_volume_keeps_the_other_volume_root_grant() -> Result<()> { let temp = tempfile::tempdir()?; let base = canonical_root(&temp)?; let volumes = [base.join("first"), base.join("second")]; for volume in &volumes { std::fs::create_dir_all(volume.join("child"))?; } for (root_access, narrowed) in [ (FileSystemAccessMode::Read, FileSystemAccessMode::Deny), (FileSystemAccessMode::Write, FileSystemAccessMode::Read), ] { let fs = FileSystemSandboxPolicy::restricted(vec![ FileSystemSandboxEntry::new( FileSystemPath::Special { value: FileSystemSpecialPath::Root, }, root_access, ), entry(&volumes[0].join("child"), narrowed)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request( &command(&profile, &volumes[0]), &volumes[0], Vec::new(), &volumes, &[], )?; let granted = if root_access == FileSystemAccessMode::Write { request.policy.readwrite_paths } else { request.policy.readonly_paths }; let second_grants: Vec<_> = granted .into_iter() .filter(|path| Path::new(path).starts_with(&volumes[1])) .collect(); let mut expected = vec![volumes[1].to_str().unwrap().to_owned()]; if root_access == FileSystemAccessMode::Write { expected.push(volumes[1].join("child").to_str().unwrap().to_owned()); } assert_eq!(second_grants, expected); } Ok(()) } #[cfg(windows)] #[test] fn volume_enumeration_uses_windows_identity_for_read_write_overrides() -> Result<()> { let temp = tempfile::tempdir()?; let root = canonical_root(&temp)?; let child = root.join("MiXeD"); std::fs::create_dir(&child)?; let alias = child.to_str().unwrap().to_ascii_lowercase(); let fs = FileSystemSandboxPolicy::restricted(vec![ entry(&root, FileSystemAccessMode::Read)?, entry(Path::new(&alias), FileSystemAccessMode::Write)?, ]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let request = build_request( &command(&profile, &root), &root, Vec::new(), std::slice::from_ref(&root), &[], )?; assert_eq!( request.policy.readwrite_paths, vec![ PathUri::from_host_native_path(&alias)? .to_abs_path()? .as_path() .to_str() .unwrap() .to_owned() ] ); let readonly_child: Vec<_> = request .policy .readonly_paths .into_iter() .filter(|path| path.eq_ignore_ascii_case(child.to_str().unwrap())) .collect(); assert_eq!(readonly_child, Vec::::new()); Ok(()) } #[test] fn relative_working_directories_fail_before_launch() -> Result<()> { let root = tempfile::tempdir()?; let profile = PermissionProfile::from_runtime_permissions( &FileSystemSandboxPolicy::restricted(Vec::new()), NetworkSandboxPolicy::Restricted, ); for (policy_cwd, command_cwd, kind) in [ (Path::new("relative"), root.path(), "policy"), (root.path(), Path::new("relative"), "command"), ] { let error = build_request( &command(&profile, policy_cwd), command_cwd, Vec::new(), &[], &[], ) .unwrap_err(); assert_eq!( error.to_string(), format!("MXC requires an absolute {kind} working directory") ); } Ok(()) } #[test] fn non_unicode_paths_fail_before_launch() -> Result<()> { #[cfg(unix)] let name = ::from_vec(vec![0xff]); #[cfg(windows)] let name = ::from_wide(&[0xd800]); let temp = tempfile::tempdir()?; let root = canonical_root(&temp)?; let invalid = root.join(name); for (policy_path, command_cwd, expected) in [ ( root.as_path(), invalid.as_path(), "MXC requires a Unicode command working directory", ), ( invalid.as_path(), root.as_path(), "MXC requires Unicode filesystem policy paths", ), ] { let fs = FileSystemSandboxPolicy::restricted(vec![entry( policy_path, FileSystemAccessMode::Deny, )?]); let profile = PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted); let error = build_request(&command(&profile, &root), command_cwd, Vec::new(), &[], &[]) .unwrap_err(); assert_eq!(error.to_string(), expected); } Ok(()) }