use super::{PathAccessDecision, PathDenialReason, WorkspaceAccessPolicy};
use crate::{
PathAccess, PathAccessRule, PathAccessRuleSource, ProcessPathGrant, ProcessPathGrantConstraint,
ProcessSessionPermissions,
};
use std::path::PathBuf;
#[test]
fn workspace_access_policy_enforces_workspace_baseline_session_and_once_grants() {
let workspace = tempfile::tempdir().expect("workspace");
let outside = tempfile::tempdir().expect("outside");
let session = ProcessSessionPermissions::new();
let policy = WorkspaceAccessPolicy::new(workspace.path(), true)
.with_static_rules([PathAccessRule::new(
outside.path().join("readonly"),
PathAccess::ReadOnly,
PathAccessRuleSource::TrustedGlobalConfig,
)])
.with_session_permissions(session.view());
assert_eq!(
policy.evaluate_read(&workspace.path().join("src/lib.rs"), &[]),
PathAccessDecision::Allow
);
assert_eq!(
policy.evaluate_write(&workspace.path().join("src/lib.rs"), &[]),
PathAccessDecision::Allow
);
let ro_file = outside.path().join("readonly/doc.txt");
assert_eq!(
policy.evaluate_read(&ro_file, &[]),
PathAccessDecision::Allow
);
assert_eq!(
policy.evaluate_write(&ro_file, &[]),
PathAccessDecision::Deny
);
let secret = outside.path().join("secret/id_rsa");
assert_eq!(
policy.evaluate_read(&secret, &[]),
PathAccessDecision::RequireReview
);
assert_eq!(
policy.evaluate_write(&secret, &[]),
PathAccessDecision::RequireReview
);
let once = [PathAccessRule::new(
&secret,
PathAccess::ReadOnly,
PathAccessRuleSource::PermissionReview,
)];
assert_eq!(
policy.evaluate_read(&secret, &once),
PathAccessDecision::Allow
);
assert_eq!(
policy.evaluate_write(&secret, &once),
PathAccessDecision::RequireReview
);
assert_eq!(
policy.evaluate_read(&secret, &[]),
PathAccessDecision::RequireReview
);
let session_dir = outside.path().join("shared");
session
.retain_path_grants(vec![ProcessPathGrant::new(
PathAccessRule::new(
&session_dir,
PathAccess::ReadWrite,
PathAccessRuleSource::PermissionReview,
),
ProcessPathGrantConstraint::Ordinary,
)])
.expect("retain session path");
assert_eq!(
policy.evaluate_read(&session_dir.join("a.txt"), &[]),
PathAccessDecision::Allow
);
assert_eq!(
policy.evaluate_write(&session_dir.join("a.txt"), &[]),
PathAccessDecision::Allow
);
session.clear().expect("clear session");
assert_eq!(
policy.evaluate_read(&session_dir.join("a.txt"), &[]),
PathAccessDecision::RequireReview
);
}
#[test]
fn read_only_workspace_denies_workspace_writes() {
let workspace = tempfile::tempdir().expect("workspace");
let policy = WorkspaceAccessPolicy::new(workspace.path(), false);
assert_eq!(
policy.evaluate_read(&workspace.path().join("README.md"), &[]),
PathAccessDecision::Allow
);
assert_eq!(
policy.evaluate_write(&workspace.path().join("README.md"), &[]),
PathAccessDecision::Deny
);
assert_eq!(
policy.workspace_root(),
PathBuf::from(workspace.path()).as_path()
);
}
#[cfg(unix)]
#[test]
fn symlink_inside_workspace_or_allowed_dir_cannot_escape_policy() {
use std::os::unix::fs::symlink;
let root = tempfile::tempdir().expect("root");
let workspace = root.path().join("workspace");
let allowed = root.path().join("allowed");
let secret_dir = root.path().join("secret");
std::fs::create_dir_all(&workspace).expect("workspace");
std::fs::create_dir_all(&allowed).expect("allowed");
std::fs::create_dir_all(&secret_dir).expect("secret");
let secret_file = secret_dir.join("id_rsa");
std::fs::write(&secret_file, "private").expect("seed secret");
let ws_escape = workspace.join("escape");
symlink(&secret_file, &ws_escape).expect("workspace symlink");
let allowed_escape = allowed.join("escape");
symlink(&secret_file, &allowed_escape).expect("allowed symlink");
let policy =
WorkspaceAccessPolicy::new(&workspace, true).with_static_rules([PathAccessRule::new(
&allowed,
PathAccess::ReadWrite,
PathAccessRuleSource::TrustedGlobalConfig,
)]);
assert_eq!(
policy.evaluate_read(&ws_escape, &[]),
PathAccessDecision::RequireReview,
"symlink inside workspace pointing outside must not inherit workspace access"
);
assert_eq!(
policy.evaluate_write(&ws_escape, &[]),
PathAccessDecision::RequireReview,
"symlink inside workspace pointing outside must not inherit workspace write access"
);
assert_eq!(
policy.evaluate_read(&allowed_escape, &[]),
PathAccessDecision::RequireReview,
"symlink inside allowed rule pointing outside must not inherit rule access"
);
}
#[test]
fn static_deny_and_trusted_readonly_ceilings_return_deny_while_allowing_deeper_overrides() {
let root = tempfile::tempdir().expect("root");
let parent = root.path().join("parent");
let workspace = parent.join("workspace");
let external = root.path().join("external");
let secret = external.join("providers/api.key");
let ro_dir = external.join("readonly");
let deeper_rw = ro_dir.join("writable");
std::fs::create_dir_all(&workspace).expect("workspace");
std::fs::create_dir_all(secret.parent().expect("parent")).expect("providers");
std::fs::create_dir_all(&deeper_rw).expect("deeper rw");
std::fs::write(&secret, "secret").expect("secret");
let ro_file = ro_dir.join("config.txt");
std::fs::write(&ro_file, "readonly").expect("ro file");
let rw_file = deeper_rw.join("output.txt");
std::fs::write(&rw_file, "writable").expect("rw file");
let session = ProcessSessionPermissions::new();
session
.retain_path_grants(vec![ProcessPathGrant::new(
PathAccessRule::new(
&external,
PathAccess::ReadWrite,
PathAccessRuleSource::PermissionReview,
),
ProcessPathGrantConstraint::Ordinary,
)])
.expect("retain broad external session grant");
let policy = WorkspaceAccessPolicy::new(&workspace, true)
.with_static_rules([
PathAccessRule::new(
&parent,
PathAccess::ReadOnly,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
external.join("providers"),
PathAccess::Deny,
PathAccessRuleSource::ProductPrivate,
),
PathAccessRule::new(
external.join("user-denied"),
PathAccess::Deny,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
&ro_dir,
PathAccess::ReadOnly,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
&deeper_rw,
PathAccess::ReadWrite,
PathAccessRuleSource::TrustedGlobalConfig,
),
])
.with_session_permissions(session.view());
assert_eq!(
policy.evaluate_read(&secret, &[]),
PathAccessDecision::RequireReview,
"ProductPrivate must require review even when a broad session grant exists"
);
let once_secret = [PathAccessRule::new(
&secret,
PathAccess::ReadWrite,
PathAccessRuleSource::PermissionReview,
)];
assert_eq!(
policy.evaluate_read(&secret, &once_secret),
PathAccessDecision::Allow,
"ProductPrivate must allow access when the user explicitly grants once"
);
assert_eq!(
policy.evaluate_write(&secret, &once_secret),
PathAccessDecision::Allow,
"ProductPrivate must allow write when the user explicitly grants once"
);
let user_denied_file = external.join("user-denied/file.txt");
let once_user_denied = [PathAccessRule::new(
&user_denied_file,
PathAccess::ReadWrite,
PathAccessRuleSource::PermissionReview,
)];
assert_eq!(
policy.evaluate_read(&user_denied_file, &once_user_denied),
PathAccessDecision::Deny,
"user-configured Deny must fail closed even with once/session grants"
);
let once_ro = [PathAccessRule::new(
&ro_file,
PathAccess::ReadWrite,
PathAccessRuleSource::PermissionReview,
)];
assert_eq!(
policy.evaluate_read(&ro_file, &[]),
PathAccessDecision::Allow
);
assert_eq!(
policy.evaluate_write(&ro_file, &once_ro),
PathAccessDecision::Deny,
"TrustedGlobalConfig ReadOnly ceiling must hard-deny write escalation via session or once grants"
);
assert_eq!(
policy.evaluate_write(&workspace.join("src/lib.rs"), &[]),
PathAccessDecision::Allow
);
assert_eq!(
policy.evaluate_write(&rw_file, &[]),
PathAccessDecision::Allow
);
}
#[test]
fn unified_path_precedence_eliminates_all_four_legacy_drifts_and_note_redlines() {
let root = tempfile::tempdir().expect("tempdir");
let workspace = root.path().join("workspace");
let cfg_ro = root.path().join("cfg-ro");
let cfg_rw_child = cfg_ro.join("writable");
let denied_root = root.path().join("denied-root");
let product_ro_under_denied = denied_root.join("config-merry");
let shared_equal_depth = root.path().join("shared-equal");
let product_private = root.path().join("providers");
let ws_equal_depth = root.path().join("ws-equal");
let shallow_home = root.path().join("home");
let shallow_config = shallow_home.join(".config");
let deep_product_ro = shallow_config.join("merry");
for dir in [
&workspace,
&cfg_rw_child,
&product_ro_under_denied,
&shared_equal_depth,
&ws_equal_depth,
&product_private,
&deep_product_ro,
] {
std::fs::create_dir_all(dir).expect("create dir");
}
let policy = WorkspaceAccessPolicy::new(&workspace, true).with_static_rules([
PathAccessRule::new(
&cfg_ro,
PathAccess::ReadOnly,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
&cfg_rw_child,
PathAccess::ReadWrite,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
&denied_root,
PathAccess::Deny,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
&product_ro_under_denied,
PathAccess::ReadOnly,
PathAccessRuleSource::ProductReadOnly,
),
PathAccessRule::new(
&shared_equal_depth,
PathAccess::ReadWrite,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
&shared_equal_depth,
PathAccess::ReadOnly,
PathAccessRuleSource::ProductReadOnly,
),
PathAccessRule::new(
&ws_equal_depth,
PathAccess::ReadWrite,
PathAccessRuleSource::WorkspacePermissions,
),
PathAccessRule::new(
&ws_equal_depth,
PathAccess::ReadOnly,
PathAccessRuleSource::ProductReadOnly,
),
PathAccessRule::new(
&product_private,
PathAccess::Deny,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
&product_private,
PathAccess::Deny,
PathAccessRuleSource::ProductPrivate,
),
PathAccessRule::new(
&shallow_home,
PathAccess::ReadWrite,
PathAccessRuleSource::TrustedGlobalConfig,
),
PathAccessRule::new(
&deep_product_ro,
PathAccess::ReadOnly,
PathAccessRuleSource::ProductReadOnly,
),
]);
assert_eq!(
policy.evaluate_write(&cfg_rw_child.join("out.txt"), &[]),
PathAccessDecision::Allow
);
assert_eq!(
policy.evaluate_read(&product_ro_under_denied.join("config.toml"), &[]),
PathAccessDecision::Deny
);
assert_eq!(
policy.evaluate_write(&product_ro_under_denied.join("config.toml"), &[]),
PathAccessDecision::Deny
);
let trusted_equal = policy.evaluate_path(
&shared_equal_depth.join("file.txt"),
PathAccess::ReadWrite,
None,
&[],
);
assert_eq!(trusted_equal.decision, PathAccessDecision::Allow);
assert!(!trusted_equal.protected_target);
let ws_equal = policy.evaluate_path(
&ws_equal_depth.join("file.txt"),
PathAccess::ReadWrite,
None,
&[],
);
assert_eq!(ws_equal.decision, PathAccessDecision::RequireReview);
assert!(ws_equal.protected_target);
let deep_ro_single = policy.evaluate_path(
&deep_product_ro.join("config.toml"),
PathAccess::ReadWrite,
None,
&[],
);
assert_eq!(deep_ro_single.decision, PathAccessDecision::RequireReview);
assert!(deep_ro_single.protected_target);
let shallow_dir_grant =
policy.evaluate_directory_grant(&shallow_config, PathAccess::ReadWrite, None, &[]);
assert_eq!(shallow_dir_grant.decision, PathAccessDecision::Deny);
assert_eq!(
shallow_dir_grant.denial_reason,
Some(PathDenialReason::CoversProtectedReadOnlyDescendant)
);
let once_private = [PathAccessRule::new(
&product_private,
PathAccess::ReadOnly,
PathAccessRuleSource::PermissionReview,
)];
assert_eq!(
policy.evaluate_read(&product_private.join("secret.key"), &once_private),
PathAccessDecision::Deny
);
assert_eq!(
crate::validate_permission_user_note(" line 1\n\tline 2 ").expect("multiline note"),
"line 1\n\tline 2"
);
assert!(crate::validate_permission_user_note("bad\u{0000}note").is_err());
}