use crate::process::{
ProcessActionIntent, ProcessEnvPolicy, ProcessIntentClass, classify_process_intent,
requires_host_process_path_review, requires_process_action_review,
};
fn intent_for(argv: &[&str]) -> ProcessActionIntent {
ProcessActionIntent::new(
argv.iter().copied().map(str::to_owned).collect(),
None,
ProcessEnvPolicy::empty(),
None,
1024,
1024,
)
.expect("valid process intent")
}
#[test]
fn plain_shell_rm_and_read_only_wrappers_classify_via_bash_ast() {
let plain_rm = intent_for(&["sh", "-c", "rm temp.txt"]);
assert_ne!(
classify_process_intent(&plain_rm),
ProcessIntentClass::Forbidden,
"plain rm inside sh -c should not be classified forbidden"
);
assert!(!requires_process_action_review(&plain_rm));
for argv in [
&["bash", "-lc", "rg ProcessRunner | wc -l"][..],
&[
"sh",
"-c",
"sed -n '1,5p' crates/merry-runtime/src/process.rs | wc -l",
][..],
&["zsh", "-lc", "rg ProcessRunner && pwd"][..],
] {
assert_eq!(
classify_process_intent(&intent_for(argv)),
ProcessIntentClass::Informational,
"plain read-only shell wrapper should classify Informational: {argv:?}"
);
}
for argv in [
&["/tmp/sh", "-c", "echo unsafe"][..],
&["./bash", "-lc", "echo unsafe"][..],
&["bash", "-lc", "rg ProcessRunner > out.txt"][..],
&["bash", "-lc", "echo $(pwd)"][..],
&["bash", "-lc", "(pwd)"][..],
&["bash", "-lc", "rg ProcessRunner | tee out.txt"][..],
&["/bin/bash", "-lc", "rg ProcessRunner | wc -l"][..],
&[
"bash",
"-lc",
"HOME=.merry/local/home cargo check --all-targets -p merry-runtime",
][..],
] {
assert_eq!(
classify_process_intent(&intent_for(argv)),
ProcessIntentClass::Unknown,
"non-read-only, non-forbidden shell wrapper should classify Unknown: {argv:?}"
);
}
}
#[test]
fn shell_forbidden_classification_ignores_non_executable_arguments_and_catches_nested_commands() {
for argv in [
&["bash", "-lc", "rg rm src/ 2>&1"][..],
&["bash", "-lc", "echo \"use bash and sudo\" 2>&1"][..],
&["bash", "-lc", "git log --grep=\"reset\" 2>&1 | head -n 20"][..],
&["bash", "-lc", "rm temp.txt 2>&1"][..],
&["bash", "-lc", "git stash list"][..],
&["bash", "-lc", "git stash show"][..],
] {
let intent = intent_for(argv);
assert!(
!requires_process_action_review(&intent),
"ordinary command should not false-trigger forbidden review: {:?}",
intent.argv()
);
}
for argv in [
&["bash", "-lc", "echo \"$(rm -rf target)\""][..],
&["bash", "-lc", "echo `sudo id`"][..],
&["bash", "-lc", "if true; then rm -rf target; fi"][..],
&["bash", "-lc", "env FOO=1 timeout 5 sudo ls"][..],
&["bash", "-lc", "git -C subdir reset --hard"][..],
&["bash", "-lc", "rm -f $TARGET"][..],
&["bash", "-lc", "rm -f *.rs"][..],
&["bash", "-lc", "git stash drop"][..],
&["bash", "-lc", "echo \"unclosed quote rm -rf target"][..],
] {
let intent = intent_for(argv);
assert!(
requires_process_action_review(&intent),
"nested/dynamic forbidden command must stay reviewed: {:?}",
intent.argv()
);
}
}
#[test]
fn shell_host_process_path_review_allows_read_only_git_and_urls() {
for argv in [
&["bash", "-lc", "git diff --cached 2>&1 | head -n 20"][..],
&["bash", "-lc", "curl -sI https://example.com"][..],
] {
let intent = intent_for(argv);
assert!(
!requires_host_process_path_review(&intent),
"read-only git or network URL should not trigger host path review: {:?}",
intent.argv()
);
}
}
#[test]
fn tree_sitter_bash_analysis_detects_env_injection_and_ignores_comments_or_quoted_literals() {
for argv in [
&["bash", "-lc", "LD_PRELOAD=/tmp/evil.so cargo test"][..],
&[
"bash",
"-lc",
"export GIT_CONFIG_PARAMETERS=\"core.pager=evil\" && git status",
][..],
&["bash", "-lc", "env \"LD_PRELOAD=/tmp/evil.so\" cargo test"][..],
&["bash", "-lc", "env -S \"sudo id\""][..],
&["env", "DYLD_INSERT_LIBRARIES=/tmp/evil.dylib", "cargo"][..],
] {
let intent = intent_for(argv);
assert!(
requires_process_action_review(&intent),
"env injection must require action review: {:?}",
intent.argv()
);
}
for argv in [
&["bash", "-lc", "echo '$(literal not executed)'"][..],
&["bash", "-lc", "git status # comment mentioning /etc/hosts"][..],
] {
let intent = intent_for(argv);
assert!(
!requires_host_process_path_review(&intent),
"single-quoted literals and comments must not trigger host path review: {:?}",
intent.argv()
);
}
}