use crate::process::{
ActionReviewTrigger, ProcessActionIntent, ProcessEnvPolicy, ProcessIntentClass,
ProcessPermissionProfileId, classify_process_action_review_triggers, classify_process_intent,
is_low_risk_process_action_intent, is_safe_cargo_package_token,
required_process_permission_profile_id, requires_process_action_review,
};
#[test]
fn git_global_overrides_and_destructive_subcommands_classify_as_high_risk_once_only() {
for argv in [
vec!["git", "-c", "core.sshCommand=evil", "commit", "-m", "wip"],
vec!["git", "-c", "core.pager=cat", "status"],
vec!["git", "--git-dir=/tmp/repo/.git", "status"],
vec!["git", "--work-tree=/tmp/other", "status"],
vec!["git", "--exec-path=/tmp/bin", "status"],
vec!["git", "--config-env=core.pager=PAGER", "status"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("git override intent is valid");
let triggers = classify_process_action_review_triggers(&intent);
assert!(
triggers.iter().any(|trigger| matches!(
trigger,
ActionReviewTrigger::PrivilegeOrEnvInjection { .. }
)),
"git global override must emit PrivilegeOrEnvInjection: {triggers:?}"
);
assert!(
triggers
.iter()
.all(|trigger| !trigger.is_session_grantable())
);
assert!(triggers.iter().all(ActionReviewTrigger::is_high_risk));
}
let checkout = ProcessActionIntent::new(
vec![
"git".to_owned(),
"checkout".to_owned(),
"--".to_owned(),
"README.md".to_owned(),
],
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("checkout intent is valid");
let triggers = classify_process_action_review_triggers(&checkout);
assert!(
triggers
.iter()
.any(|trigger| matches!(trigger, ActionReviewTrigger::GitDestructive { .. })),
"git checkout -- <path> must be classified as high-risk destructive git: {triggers:?}"
);
for safe_argv in [
vec!["git", "status", "--short"],
vec!["git", "diff"],
vec!["git", "log", "--oneline", "-5"],
vec!["git", "branch", "--show-current"],
vec!["git", "fetch"],
vec!["git", "add", "src/lib.rs"],
vec!["git", "commit", "-m", "feat: update"],
vec!["git", "switch", "-c", "feat/demo"],
vec!["git", "checkout", "-b", "feat/branch"],
vec!["git", "stash"],
vec!["git", "stash", "pop"],
vec!["git", "restore", "--staged", "src/lib.rs"],
vec!["git", "reset", "--soft", "HEAD~1"],
] {
let intent = ProcessActionIntent::new(
safe_argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("safe git intent");
assert!(
classify_process_action_review_triggers(&intent).is_empty(),
"allowlisted safe git command must not trigger review: {:?}",
intent.argv()
);
}
for review_argv in [
vec!["git", "checkout", "main"],
vec!["git", "checkout", "HEAD", "README.md"],
vec!["git", "merge", "feat/demo"],
vec!["git", "rebase", "main"],
vec!["git", "pull"],
vec!["git", "push", "origin", "feat"],
] {
let intent = ProcessActionIntent::new(
review_argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("review git intent");
let triggers = classify_process_action_review_triggers(&intent);
assert!(
matches!(triggers.as_slice(), [ActionReviewTrigger::GitReview { .. }]),
"non-allowlisted git command must route to GitReview: {:?} -> {triggers:?}",
intent.argv()
);
assert!(!triggers[0].is_high_risk());
}
for destructive_argv in [
vec!["git", "push", "--force"],
vec!["git", "reset", "--hard"],
vec!["git", "clean", "-fd"],
vec!["git", "stash", "-q", "drop"],
vec!["git", "branch", "-D", "old"],
vec!["git", "checkout", "--ours", "README.md"],
] {
let intent = ProcessActionIntent::new(
destructive_argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("destructive git intent");
let triggers = classify_process_action_review_triggers(&intent);
assert!(
matches!(
triggers.as_slice(),
[ActionReviewTrigger::GitDestructive { .. }]
),
"destructive git command must emit GitDestructive: {:?} -> {triggers:?}",
intent.argv()
);
assert!(triggers[0].is_high_risk());
}
}
#[test]
fn safe_cargo_package_token_allows_package_names_without_paths_or_flags() {
for package in [
"merry-runtime",
"other-crate",
"merry_coding_loop_task_status_text",
"crate123",
] {
assert!(is_safe_cargo_package_token(package));
}
for package in [
"",
"-package",
"bad.package",
"../other-crate",
"crate/name",
] {
assert!(!is_safe_cargo_package_token(package));
}
}
#[test]
fn plain_rm_of_named_files_does_not_require_action_review() {
for argv in [
vec!["rm", "temp.txt"],
vec!["rm", "-f", "/tmp/scratch.txt"],
vec!["rm", "--", "-odd-name"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("named-file rm is a valid process intent");
assert_ne!(
classify_process_intent(&intent),
ProcessIntentClass::Forbidden,
"plain rm should not be classified forbidden"
);
assert!(!requires_process_action_review(&intent));
}
for argv in [
vec!["rm", "-rf", "target"],
vec!["rm", "-r", "temp.txt"],
vec!["rm", "."],
vec!["rm", "*"],
vec!["rm"],
vec!["sh", "-c", "rm -rf target"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("broad rm is a valid process intent");
assert!(
requires_process_action_review(&intent),
"broad rm should still be reviewed"
);
}
}
#[test]
fn classifies_known_process_argv_shapes() {
for argv in [
vec!["rustc", "--version"],
vec!["rg", "--version"],
vec!["rg", "--files"],
vec!["rg", "ProcessRunner"],
vec!["cargo", "fmt", "--all", "--check"],
vec!["sed", "-n", "1,80p", "crates/merry-runtime/src/process.rs"],
vec!["git", "status", "--short"],
vec!["git", "status", "--short", "--branch"],
vec!["git", "status", "--branch", "--short"],
vec!["git", "log", "--oneline", "-5"],
vec!["git", "diff", "--", "crates/merry-runtime/src/process.rs"],
vec!["git", "show", "--stat", "HEAD"],
vec!["git", "branch", "--show-current"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("informational argv is a valid process intent");
assert_eq!(
classify_process_intent(&intent),
ProcessIntentClass::Informational
);
}
for argv in [
vec!["cargo", "test", "-p", "merry-runtime"],
vec!["cargo", "test", "--package", "merry-runtime"],
vec!["cargo", "check", "-p", "merry-runtime"],
vec!["cargo", "check", "--package", "merry-runtime"],
vec!["cargo", "test", "-p", "other-crate"],
vec!["cargo", "check", "-p", "merry_coding_loop_task_status_text"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("local workspace argv is a valid process intent");
assert_eq!(
classify_process_intent(&intent),
ProcessIntentClass::LocalWorkspaceEffect
);
}
for argv in [
vec!["sh", "-c", "rm -rf target"],
vec!["bash", "-lc", "rm -rf target"],
vec!["zsh", "-c", "rm -rf target"],
vec!["cmd", "/C", "echo unsafe"],
vec!["powershell", "-Command", "Write-Host unsafe"],
vec!["pwsh", "-Command", "Write-Host unsafe"],
vec!["rm", "-rf", "target"],
vec!["../bin/rm", "-rf", "target"],
vec!["git", "clean", "-fd"],
vec!["git", "reset", "--hard"],
vec!["git", "checkout", "--", "README.md"],
vec!["git", "-c", "core.fsmonitor=evil", "status"],
vec!["bash", "-lc", "rg ProcessRunner | rm -rf target"],
vec!["bash", "-lc", "echo $(rm -rf target)"],
vec!["/bin/bash", "-lc", "rm -rf target"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("forbidden argv is still a syntactically valid process intent");
assert_eq!(
classify_process_intent(&intent),
ProcessIntentClass::Forbidden
);
}
for argv in [
vec!["curl", "https://example.invalid"],
vec!["wget", "https://example.invalid"],
vec!["ssh", "example.invalid"],
vec!["scp", "a", "b"],
vec!["rsync", "a", "b"],
vec!["nc", "example.invalid", "443"],
vec!["netcat", "example.invalid", "443"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("network argv is a syntactically valid process intent");
assert_eq!(
classify_process_intent(&intent),
ProcessIntentClass::Unknown
);
assert_eq!(
required_process_permission_profile_id(&intent),
Some(ProcessPermissionProfileId::LOCAL_WORKSPACE)
);
}
for argv in [
vec!["fish", "-c", "echo unsafe"],
vec!["python", "-c", "print('workspace effect')"],
vec!["python3", "script.py"],
vec!["perl", "-e", "print 'workspace effect'"],
vec!["ruby", "-e", "puts 'workspace effect'"],
vec!["node", "-e", "console.log('workspace effect')"],
vec!["cargo", "test"],
vec!["cargo", "test", "-p", "-package"],
vec!["cargo", "test", "-p", "bad.package"],
vec!["cargo", "test", "-p", "../other-crate"],
vec!["/tmp/cargo", "test", "-p", "merry-runtime"],
vec!["./cargo", "test", "-p", "merry-runtime"],
vec!["../bin/cargo", "test", "-p", "merry-runtime"],
vec!["/tmp/rustc", "--version"],
vec!["./rg", "--version"],
vec!["/tmp/rg", "--files"],
vec!["./rg", "ProcessRunner"],
vec!["rg", "-n", "ProcessRunner"],
vec!["rg", "--glob", "*.rs"],
vec!["rg", "-"],
vec!["rg", "-pattern"],
vec!["rg", "Process.*"],
vec!["rg", "Process|Runner"],
vec!["rg", "call()"],
vec!["sed", "-e", "1,80p", "crates/merry-runtime/src/process.rs"],
vec!["sed", "-n", "1,80d", "crates/merry-runtime/src/process.rs"],
vec!["sed", "-n", "1,80p"],
vec!["sed", "-n", "1,80p", "../outside.rs"],
vec!["sed", "-n", "1,80p", "/tmp/outside.rs"],
vec!["git", "status", "--porcelain=v2"],
vec!["git", "diff", "--cached"],
vec!["git", "show", "HEAD:README.md"],
vec!["unknown-readonly-ish", "--version"],
vec!["python3.12", "-c", "print('unknown')"],
vec!["docker", "run", "image"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("unknown argv is still a syntactically valid process intent");
assert_eq!(
classify_process_intent(&intent),
ProcessIntentClass::Unknown
);
}
}
#[test]
fn sp3a_low_risk_process_admission_allows_narrow_read_only_argv() {
for argv in [
vec!["rustc", "--version"],
vec!["rg", "--version"],
vec!["rg", "--files"],
vec!["rg", "ProcessRunner"],
vec!["sed", "-n", "1,80p", "crates/merry-runtime/src/process.rs"],
vec!["git", "status", "--short"],
vec!["git", "log", "--oneline", "-5"],
vec!["git", "diff", "--", "crates/merry-runtime/src/process.rs"],
vec!["git", "show", "--stat", "HEAD"],
vec!["git", "branch", "--show-current"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("informational argv is a valid process intent");
assert!(is_low_risk_process_action_intent(&intent));
}
for argv in [
vec!["cargo", "test", "-p", "merry-runtime"],
vec!["cargo", "test", "--package", "merry-runtime"],
vec!["/tmp/rustc", "--version"],
vec!["./rg", "--version"],
vec!["rg", "-n", "ProcessRunner"],
vec!["rg", "--glob", "*.rs"],
vec!["rg", "-"],
vec!["rg", "Process.*"],
vec!["rg", "Process|Runner"],
vec!["sed", "-n", "1,80d", "crates/merry-runtime/src/process.rs"],
vec!["git", "clean", "-fd"],
vec!["unknown-readonly-ish", "--version"],
vec!["sh", "-c", "rm -rf target"],
vec!["bash", "-lc", "rg ProcessRunner | wc -l"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("non-informational argv is still a valid process intent");
assert!(!is_low_risk_process_action_intent(&intent));
}
}
#[test]
fn sp3a_low_risk_process_admission_rejects_stdin_or_env() {
let stdin_intent = ProcessActionIntent::new(
vec!["rg".to_owned(), "--files".to_owned()],
None,
ProcessEnvPolicy::empty(),
Some("payload must not enter the auto-admitted lane".to_owned()),
1024,
1024,
)
.expect("stdin process intent is syntactically valid");
assert_eq!(
classify_process_intent(&stdin_intent),
ProcessIntentClass::Informational
);
assert!(!is_low_risk_process_action_intent(&stdin_intent));
let env_intent = ProcessActionIntent::new(
vec!["rg".to_owned(), "--version".to_owned()],
None,
ProcessEnvPolicy::NonEmptyForTest,
None,
1024,
1024,
)
.expect("non-empty env process intent is syntactically valid");
assert_eq!(
classify_process_intent(&env_intent),
ProcessIntentClass::Informational
);
assert!(!is_low_risk_process_action_intent(&env_intent));
}
#[cfg(not(feature = "bash-ast"))]
#[test]
fn without_bash_ast_shell_wrappers_fail_closed() {
let intent = ProcessActionIntent::new(
vec!["bash".to_owned(), "-lc".to_owned(), "ls -la".to_owned()],
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("valid process intent");
assert_eq!(
classify_process_intent(&intent),
ProcessIntentClass::Forbidden
);
assert!(!crate::is_read_only_shell_process_action_intent(&intent));
assert!(requires_process_action_review(&intent));
}
#[test]
fn windows_executable_extensions_are_classified() {
for argv in [
vec![
"powershell.exe",
"-NoProfile",
"-Command",
"Invoke-Expression $x",
],
vec!["cmd.exe", "/C", "dir"],
vec!["git.exe", "push", "origin", "main"],
] {
let intent = ProcessActionIntent::new(
argv.into_iter().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("valid process intent");
assert!(
requires_process_action_review(&intent),
"Windows executable variant must require review: {:?}",
intent.argv()
);
}
}