use super::*;
use crate::{PathAccessRuleSource, parse_permission_request};
use merry_core::{PendingToolCall, ToolCallArguments, ToolCallId, ToolName};
use serde_json::json;
use std::path::Path;
use tokio_util::sync::CancellationToken;
struct Backend {
reject: bool,
}
impl PermissionedProcessRunnerFactory for Backend {
fn validate_request(&self, _request: &PermissionRequest) -> Result<(), ProcessRunnerError> {
if self.reject {
Err(ProcessRunnerError::infrastructure(
"fixture policy rejected request",
))
} else {
Ok(())
}
}
fn prepare_approved_request(
&self,
request: &PermissionRequest,
) -> Result<PreparedProcessPermission, ProcessRunnerError> {
self.validate_request(request)?;
let grants = request
.requested()
.iter()
.filter_map(|capability| {
let RequestedCapability::Path(path) = capability else {
return None;
};
let path_ref = Path::new(path.path());
let constraint = if path_ref.starts_with("/metadata") {
ProcessPathGrantConstraint::ProtectedMetadata
} else {
ProcessPathGrantConstraint::Ordinary
};
Some(ProcessPathGrant::new(
PathAccessRule::new(
Path::new(path.path()),
path.access(),
PathAccessRuleSource::PermissionReview,
),
constraint,
))
})
.collect();
Ok(PreparedProcessPermission::new(
self.runner_for(request),
grants,
))
}
fn runner_for(&self, _request: &PermissionRequest) -> Arc<dyn ProcessRunner> {
Arc::new(RejectedRunner(ProcessRunnerError::infrastructure(
"unused fixture runner",
)))
}
}
fn request(requested: serde_json::Value) -> PermissionRequest {
parse_permission_request(&PendingToolCall::new(
ToolCallId::new("permission-fixture").unwrap(),
ToolName::new("request_permissions").unwrap(),
ToolCallArguments::try_from(json!({
"requested": requested,
"for_action": { "command": "true", "cwd": null }
}))
.unwrap(),
))
.unwrap()
}
#[test]
fn runtime_retains_capabilities_only_under_explicit_session_scope() {
let permissions = ProcessSessionPermissions::new();
let view = permissions.view();
let factory = permissions.with_factory(Backend { reject: false });
let request = request(json!({
"network": true,
"host_integrations": ["ssh-agent"],
"paths": [
{ "path": "/ordinary", "access": "ro" },
{ "path": "/metadata", "access": "rw" }
]
}));
factory.validate_request(&request).unwrap();
assert!(
!factory
.request_capabilities_are_satisfied(&request)
.unwrap()
);
assert!(view.snapshot().unwrap().path_rules().is_empty());
let _runner = factory.runner_for(&request);
let snapshot = view.snapshot().unwrap();
assert!(snapshot.path_rules().is_empty());
assert!(snapshot.host_integrations().is_empty());
assert!(!snapshot.network_allowed());
let _runner = factory.runner_for_decision(&request, ApprovalScope::Session);
let snapshot = view.snapshot().unwrap();
assert_eq!(snapshot.path_rules().len(), 1);
assert_eq!(snapshot.path_rules()[0].path(), Path::new("/ordinary"));
assert_eq!(snapshot.host_integrations(), &[HostIntegration::SshAgent]);
assert!(snapshot.network_allowed());
}
#[test]
fn repeated_grants_upgrade_and_coalesce_without_cross_session_leaks() {
let permissions = ProcessSessionPermissions::new();
let view = permissions.view();
let other = ProcessSessionPermissions::new();
let factory = permissions.with_factory(Backend { reject: false });
for (path, access) in [
("/ordinary/child", "ro"),
("/ordinary", "ro"),
("/ordinary", "rw"),
("/ordinary/child", "rw"),
] {
let request = request(json!({"paths": [{"path": path, "access": access}]}));
let _runner = factory.runner_for_decision(&request, ApprovalScope::Session);
}
let snapshot = view.snapshot().unwrap();
assert_eq!(snapshot.path_rules().len(), 1);
assert_eq!(snapshot.path_rules()[0].path(), Path::new("/ordinary"));
assert_eq!(snapshot.path_rules()[0].access(), PathAccess::ReadWrite);
assert!(other.view().snapshot().unwrap().path_rules().is_empty());
}
#[tokio::test]
async fn rejected_preparation_does_not_retain_any_capability() {
let permissions = ProcessSessionPermissions::new();
let view = permissions.view();
let factory = permissions.with_factory(Backend { reject: true });
let request = request(json!({
"host_integrations": ["ssh-agent"],
"paths": [{"path": "/ordinary", "access": "rw"}]
}));
let crate::PermissionedAction::Process(intent) = request.action() else {
panic!("expected process action");
};
let runner = factory.runner_for_decision(&request, ApprovalScope::Session);
let error = runner
.run(
intent.clone(),
ProcessRunnerContext::new(CancellationToken::new()),
)
.await
.unwrap_err();
assert!(
error
.to_string()
.contains("fixture policy rejected request")
);
let snapshot = view.snapshot().unwrap();
assert!(snapshot.path_rules().is_empty());
assert!(snapshot.host_integrations().is_empty());
}
#[test]
fn stable_grant_ids_support_targeted_revocation_and_monotonic_clear() {
let permissions = ProcessSessionPermissions::new();
let view = permissions.view();
let factory = permissions.clone().with_factory(Backend { reject: false });
let initial = request(json!({
"network": true,
"host_integrations": ["ssh-agent"],
"paths": [
{ "path": "/alpha", "access": "ro" },
{ "path": "/beta", "access": "rw" }
]
}));
let _ = factory.runner_for_decision(&initial, ApprovalScope::Session);
let grants = permissions.grants().expect("grants snapshot");
assert_eq!(grants.len(), 4);
let net_id = grants
.iter()
.find(|g| matches!(g.kind(), SessionPermissionGrantKind::Network))
.expect("network grant")
.id();
let ssh_id = grants
.iter()
.find(|g| {
matches!(
g.kind(),
SessionPermissionGrantKind::HostIntegration(HostIntegration::SshAgent)
)
})
.expect("ssh grant")
.id();
let alpha_id = grants
.iter()
.find(|g| {
matches!(
g.kind(),
SessionPermissionGrantKind::Path { path, .. } if path == Path::new("/alpha")
)
})
.expect("alpha grant")
.id();
let beta_id = grants
.iter()
.find(|g| {
matches!(
g.kind(),
SessionPermissionGrantKind::Path { path, .. } if path == Path::new("/beta")
)
})
.expect("beta grant")
.id();
let repeat_net = request(json!({ "network": true }));
let _ = factory.runner_for_decision(&repeat_net, ApprovalScope::Session);
let repeated_net_id = permissions
.grants()
.expect("grants")
.into_iter()
.find(|g| matches!(g.kind(), SessionPermissionGrantKind::Network))
.expect("net")
.id();
assert_eq!(repeated_net_id, net_id);
assert!(permissions.revoke(alpha_id).expect("revoke alpha"));
assert!(
!permissions
.revoke(alpha_id)
.expect("second revoke is false")
);
let after_alpha = view.snapshot().expect("snapshot");
assert!(after_alpha.network_allowed());
assert_eq!(
after_alpha.host_integrations(),
&[HostIntegration::SshAgent]
);
assert_eq!(after_alpha.path_rules().len(), 1);
assert_eq!(after_alpha.path_rules()[0].path(), Path::new("/beta"));
let remaining_ids: Vec<SessionGrantId> = after_alpha.grants().iter().map(|g| g.id()).collect();
assert_eq!(remaining_ids, vec![net_id, ssh_id, beta_id]);
assert!(permissions.revoke(net_id).expect("revoke net"));
assert!(!view.snapshot().expect("snapshot").network_allowed());
permissions.clear().expect("clear");
assert!(permissions.grants().expect("empty").is_empty());
let _ = factory.runner_for_decision(&repeat_net, ApprovalScope::Session);
let new_net_id = permissions.grants().expect("grants")[0].id();
assert!(new_net_id > beta_id);
assert!(!permissions.revoke(net_id).expect("stale id does nothing"));
assert!(view.snapshot().expect("snapshot").network_allowed());
}