use crate::parse::{Token, WordSet};
use crate::verdict::{SafetyLevel, Verdict};
static CONVERT_FORMATS: WordSet = WordSet::new(&["binary1", "json", "swift", "xml1"]);
static EXTRACT_FORMATS: WordSet = WordSet::new(&["binary1", "json", "raw", "xml1"]);
pub fn check_plutil_convert(tokens: &[Token]) -> Verdict {
if tokens.len() == 2 && matches!(tokens[1].as_str(), "--help" | "-h") {
return Verdict::Allowed(SafetyLevel::Inert);
}
let Some(fmt) = tokens.get(1) else {
return Verdict::Denied;
};
if !CONVERT_FORMATS.contains(fmt) {
return Verdict::Denied;
}
walk_modifier_args(tokens, 2)
}
pub fn check_plutil_extract(tokens: &[Token]) -> Verdict {
if tokens.len() == 2 && matches!(tokens[1].as_str(), "--help" | "-h") {
return Verdict::Allowed(SafetyLevel::Inert);
}
let Some(_keypath) = tokens.get(1) else {
return Verdict::Denied;
};
let Some(fmt) = tokens.get(2) else {
return Verdict::Denied;
};
if !EXTRACT_FORMATS.contains(fmt) {
return Verdict::Denied;
}
walk_modifier_args(tokens, 3)
}
fn walk_modifier_args(tokens: &[Token], start: usize) -> Verdict {
let mut i = start;
let mut output_mode = OutputMode::InPlace;
while i < tokens.len() {
match tokens[i].as_str() {
"-r" | "-s" => i += 1,
"-o" => {
let Some(value) = tokens.get(i + 1) else {
return Verdict::Denied;
};
if value.as_str().starts_with("--") {
return Verdict::Denied;
}
output_mode = if value.as_str() == "-" { OutputMode::Stdout } else { OutputMode::File };
i += 2;
}
"--" => break,
arg if arg.starts_with('-') => return Verdict::Denied,
_ => i += 1,
}
}
match output_mode {
OutputMode::Stdout => Verdict::Allowed(SafetyLevel::Inert),
OutputMode::File | OutputMode::InPlace => Verdict::Allowed(SafetyLevel::SafeWrite),
}
}
enum OutputMode {
Stdout,
File,
InPlace,
}
#[cfg(test)]
mod tests {
use crate::is_safe_command;
fn check(cmd: &str) -> bool {
is_safe_command(cmd)
}
safe! {
convert_stdout_xml: "plutil -convert xml1 -o - /tmp/foo.plist",
convert_stdout_binary: "plutil -convert binary1 -o - /tmp/foo.plist",
convert_stdout_json: "plutil -convert json -o - /tmp/foo.plist",
convert_stdout_swift: "plutil -convert swift -o - /tmp/foo.plist",
convert_stdout_stdin: "plutil -convert xml1 -o -",
convert_stdout_readable: "plutil -convert json -r -o - /tmp/foo.plist",
convert_stdout_silent: "plutil -convert xml1 -s -o - /tmp/foo.plist",
convert_stdout_multi_input: "plutil -convert xml1 -o - -- /tmp/a.plist /tmp/b.plist",
convert_stdout_piped: "plutil -convert xml1 -o - /tmp/foo.plist | grep boundary",
convert_to_file: "plutil -convert json Info.plist -o out.json",
convert_to_file_explicit: "plutil -convert xml1 -o /tmp/out.plist /tmp/in.plist",
convert_in_place: "plutil -convert xml1 /tmp/foo.plist",
convert_help: "plutil -convert --help",
convert_help_short: "plutil -convert -h",
extract_raw: "plutil -extract CFBundleVersion raw Info.plist",
extract_json: "plutil -extract CFBundleShortVersionString json Info.plist",
extract_to_file: "plutil -extract CFBundleURLTypes xml1 -o urls.plist Info.plist",
extract_to_stdout: "plutil -extract CFBundleURLTypes xml1 -o - Info.plist",
extract_help: "plutil -extract --help",
}
denied! {
convert_bad_format: "plutil -convert invalid -o - /tmp/foo.plist",
convert_objc_removed: "plutil -convert objc -o - /tmp/foo.plist",
convert_no_format: "plutil -convert",
convert_dash_dash_as_o_value: "plutil -convert xml1 -o -- /tmp/in",
convert_extension_flag: "plutil -convert xml1 -e plist -o - /tmp/in",
convert_unknown_flag: "plutil -convert xml1 -o - --unknown /tmp/in",
extract_no_keypath: "plutil -extract",
extract_bad_format: "plutil -extract Key bogus Info.plist",
extract_unknown_flag: "plutil -extract Key json --evil Info.plist",
}
use proptest::prelude::*;
use super::check_plutil_convert;
use crate::parse::Token;
use crate::verdict::{SafetyLevel, Verdict};
fn arb_benign_path_token() -> impl Strategy<Value = Token> {
"[a-zA-Z0-9._/]{1,16}".prop_map(|s| Token::from_raw(s.to_string()))
}
fn arb_o_value_other_than_dash() -> impl Strategy<Value = Token> {
prop_oneof![
Just(Token::from_raw("out".to_string())),
Just(Token::from_raw("a.json".to_string())),
Just(Token::from_raw("foo.plist".to_string())),
"[a-zA-Z0-9._/]{1,8}".prop_map(|s| Token::from_raw(s.to_string())),
]
}
fn arb_rs_token() -> impl Strategy<Value = Token> {
proptest::sample::select(vec!["-r", "-s"]).prop_map(|s| Token::from_raw(s.to_string()))
}
fn arb_convert_two_token_slice() -> impl Strategy<Value = Vec<Token>> {
proptest::sample::select(vec!["binary1", "json", "swift", "xml1"])
.prop_map(|fmt| vec![Token::from_raw("-convert".to_string()), Token::from_raw(fmt.to_string())])
}
fn arb_o_free_modifier_tokens() -> impl Strategy<Value = Vec<Token>> {
prop_oneof![
Just(Vec::new()),
proptest::collection::vec(arb_rs_token(), 0..4),
arb_benign_path_token().prop_map(|t| vec![t]),
proptest::collection::vec(arb_benign_path_token(), 1..3),
]
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(256))]
#[test]
fn convert_help_or_h_in_two_token_slice_is_inert(
help in proptest::sample::select(vec!["--help", "-h"]),
) {
let tokens = vec![Token::from_raw("-convert".to_string()), Token::from_raw(help.to_string())];
prop_assert_eq!(
check_plutil_convert(&tokens),
Verdict::Allowed(SafetyLevel::Inert),
"convert help: tokens={:?}",
tokens.iter().map(|t| t.as_str()).collect::<Vec<_>>(),
);
}
#[test]
fn convert_format_outside_allowlist_is_denied_for_any_modifier_tail(
base in arb_convert_two_token_slice(),
fmt_off in "[a-zA-Z0-9_]{1,8}",
tail in arb_o_free_modifier_tokens(),
) {
let tokens_already = base[1].as_str().to_string();
let mut off = Token::from_raw(fmt_off.clone());
while off.as_str() == tokens_already || matches!(off.as_str(), "binary1" | "json" | "swift" | "xml1") {
off = Token::from_raw(format!("{}_x", fmt_off));
}
let mut tokens = vec![Token::from_raw("-convert".to_string()), off.clone()];
tokens.extend(tail);
prop_assert_eq!(
check_plutil_convert(&tokens),
Verdict::Denied,
"convert bad format: tokens={:?}",
tokens.iter().map(|t| t.as_str()).collect::<Vec<_>>(),
);
}
#[test]
fn convert_dash_o_dash_is_inert_and_dash_o_other_value_is_safe_write(
base in arb_convert_two_token_slice(),
modifiers in arb_o_free_modifier_tokens(),
non_dash in arb_o_value_other_than_dash(),
) {
let mut with_dash = base.clone();
with_dash.push(Token::from_raw("-o".to_string()));
with_dash.push(Token::from_raw("-".to_string()));
with_dash.extend(modifiers.iter().cloned());
let mut with_other = base;
with_other.push(Token::from_raw("-o".to_string()));
with_other.push(non_dash);
with_other.extend(modifiers);
prop_assert_eq!(
check_plutil_convert(&with_dash),
Verdict::Allowed(SafetyLevel::Inert),
"with_dash tokens={:?}",
with_dash.iter().map(|t| t.as_str()).collect::<Vec<_>>(),
);
let non_dash_value = with_other[3].as_str();
prop_assert!(
!non_dash_value.starts_with('-'),
"arb_o_value_other_than_dash leaked a `-`-value: {:?}", non_dash_value,
);
prop_assert_eq!(
check_plutil_convert(&with_other),
Verdict::Allowed(SafetyLevel::SafeWrite),
"with_other tokens={:?}",
with_other.iter().map(|t| t.as_str()).collect::<Vec<_>>(),
);
}
#[test]
fn convert_reordering_dash_r_and_dash_s_preserves_the_verdict(
base in arb_convert_two_token_slice(),
picks in proptest::collection::vec(arb_rs_token(), 1..6),
trailer in proptest::collection::vec(arb_benign_path_token(), 0..3),
) {
let mut original = base.clone();
original.extend(picks.iter().cloned());
original.extend(trailer.iter().cloned());
let mut reversed = base;
let mut picks_rev = picks.clone();
picks_rev.reverse();
reversed.extend(picks_rev);
reversed.extend(trailer);
prop_assert_eq!(
check_plutil_convert(&original),
check_plutil_convert(&reversed),
"reordering -r/-s must not change verdict",
);
}
}
}