use std::time::Duration;
use super::parse;
const VALUE: &str = "fake-cmd-token-7f3a91";
const LIFETIME: Duration = Duration::from_mins(15);
const NOW: u64 = 1_800_000_000;
#[test]
fn a_plain_answer_is_the_value_for_the_whole_lifetime() {
let answer = parse(format!("{VALUE}\r\n").as_bytes(), LIFETIME, NOW).unwrap();
assert_eq!(answer.value.as_str(), VALUE);
assert_eq!(answer.lifetime, LIFETIME);
}
#[test]
fn a_json_answer_can_only_shorten_the_lifetime() {
let at = |secs: u64| format!("{{\"value\": \"{VALUE}\", \"expires_at\": {}}}", NOW + secs);
let answer = parse(at(120).as_bytes(), LIFETIME, NOW).unwrap();
assert_eq!(
(answer.value.as_str(), answer.lifetime.as_secs()),
(VALUE, 120)
);
let longer = parse(at(86_400).as_bytes(), LIFETIME, NOW).unwrap();
assert_eq!(longer.lifetime, LIFETIME);
let within = format!("{{\"value\": \"{VALUE}\", \"expires_in\": 300}}");
assert_eq!(
parse(within.as_bytes(), LIFETIME, NOW)
.unwrap()
.lifetime
.as_secs(),
300
);
let bare = format!("{{\"value\": \"{VALUE}\"}}");
assert_eq!(
parse(bare.as_bytes(), LIFETIME, NOW).unwrap().lifetime,
LIFETIME
);
}
#[test]
fn an_unusable_answer_is_refused_without_quoting_it() {
for (out, why) in [
(String::new(), "no value"),
(format!("{VALUE}\nsecond-line"), "control character"),
(format!("{VALUE}\u{7}"), "control character"),
(
format!("{{\"value\": \"{VALUE}\", \"expires_at\": {NOW}}}"),
"less than 10s",
),
(
format!("{{\"value\": \"{VALUE}\", \"expires_in\": 3}}"),
"less than 10s",
),
(
format!("{{\"value\": \"{VALUE}\", \"expiry\": 300}}"),
"is not {\"value\"",
),
(format!("{{\"value\": \"{VALUE}\""), "is not {\"value\""),
(format!("{{\"token\": \"{VALUE}\"}}"), "is not {\"value\""),
(
format!("{{\"value\": \"{VALUE}\", \"expires_in\": 60, \"expires_at\": {NOW}}}"),
"both",
),
(
format!("{{\"value\": \"{VALUE}\\r\\nX-Evil: 1\"}}"),
"control character",
),
] {
let error = format!("{:#}", parse(out.as_bytes(), LIFETIME, NOW).err().unwrap());
assert!(error.contains(why), "{out:?}: {error}");
assert!(!error.contains(VALUE), "{error}");
}
let error = format!("{:#}", parse(b"\xff\xfe", LIFETIME, NOW).err().unwrap());
assert!(error.contains("not UTF-8"), "{error}");
}
#[cfg(unix)]
mod programs {
use std::os::unix::fs::PermissionsExt as _;
use std::path::{Path, PathBuf};
use std::time::Duration;
use openmoat_core::Policy;
use super::VALUE;
use crate::secrets::stand::{Stand, scratch};
#[test]
fn a_program_answers_with_its_arguments_and_a_bare_environment() {
let stand = Stand::new();
let program = stand.shell(&format!(
"[ \"$1\" = --print ] || exit 3\n\
printf '{VALUE}-%s\\n' \"${{CARGO_PKG_NAME:-none}}\""
));
assert_eq!(program.id(), "t");
let answer = program.fetch().unwrap();
assert_eq!(answer.value.as_str(), format!("{VALUE}-none"));
assert_eq!(answer.lifetime, Duration::from_mins(15));
}
#[test]
fn a_program_runs_in_the_root_with_a_private_home_unless_told_otherwise() {
let stand = Stand::new();
let body =
"printf '%s|%s|%s' \"$(pwd -P)\" \"$HOME\" \"$(ls -A \"$HOME\" | wc -l | tr -d ' ')\"";
let answer = stand.shell(body).fetch().unwrap();
let private = std::fs::canonicalize(stand.state())
.unwrap()
.join("secret-homes/t");
let mut seen = answer.value.split('|');
assert_eq!(seen.next(), Some("/"));
let home = std::fs::canonicalize(seen.next().unwrap()).unwrap();
assert_eq!(home, private);
assert_eq!(seen.next(), Some("0"), "the private home is empty");
let mode = std::os::unix::fs::PermissionsExt::mode(
&std::fs::metadata(&private).unwrap().permissions(),
);
assert_eq!(mode & 0o777, 0o700);
let mut source = Stand::source(&["/bin/sh", "-c", body]);
source.home = openmoat_core::SecretCommandHome::User;
let answer = stand.shell_with(&source).fetch().unwrap();
assert_eq!(answer.value.as_str(), format!("/|{}|0", stand.root));
}
#[test]
fn a_planted_tool_beside_the_program_is_never_used() {
let stand = Stand::new();
let marker = format!("{}/planted-ran", stand.root);
stand.script("python3", &format!("touch '{marker}'"));
let path = stand.script(
"token",
&format!("python3 -c 'print(1)' >/dev/null 2>&1\necho {VALUE}"),
);
let answer = stand
.program(&path, &stand.policy)
.unwrap()
.fetch()
.unwrap();
assert_eq!(answer.value.as_str(), VALUE);
assert!(!Path::new(&marker).exists(), "the planted python3 ran");
}
#[test]
fn the_bytes_that_were_hashed_are_the_ones_that_run() {
let stand = Stand::new();
let path = stand.script("token", "echo \"$0\"");
let program = stand.program(&path, &stand.policy).unwrap();
let ran = PathBuf::from(program.fetch().unwrap().value.as_str());
let copies = std::fs::canonicalize(stand.state())
.unwrap()
.join("secret-programs");
assert_eq!(
std::fs::canonicalize(&ran).unwrap().parent(),
Some(copies.as_path())
);
let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777;
assert_eq!((mode(&copies), mode(&ran)), (0o700, 0o700));
std::fs::write(&ran, "#!/bin/sh\necho fake-planted-value-0000\n").unwrap();
let error = format!("{:#}", program.fetch().err().unwrap());
assert!(error.contains("private copy"), "{error}");
drop(program);
assert!(!ran.exists());
}
#[test]
fn a_child_left_holding_the_output_is_ended_at_the_limit() {
let stand = Stand::new();
let pid_file = format!("{}/child", stand.root);
let go = format!("{}/go", stand.root);
let program = stand.shell(&format!(
"if [ -e '{go}' ]; then sleep 30 & echo $! > '{pid_file}'; fi\necho {VALUE}"
));
assert_eq!(program.fetch().unwrap().value.as_str(), VALUE);
let program = program.with_time_limit(Duration::from_secs(2));
std::fs::write(&go, "").unwrap();
let started = std::time::Instant::now();
let error = format!("{:#}", program.fetch().err().unwrap());
let took = started.elapsed();
assert!(error.contains("did not finish within 2s"), "{error}");
assert!(took < Duration::from_secs(4), "{took:?}");
let pid: i32 = std::fs::read_to_string(&pid_file)
.unwrap()
.trim()
.parse()
.unwrap();
let pid = rustix::process::Pid::from_raw(pid).unwrap();
let gone = (0..50).any(|_| {
std::thread::sleep(Duration::from_millis(20));
rustix::process::test_kill_process(pid).is_err()
});
assert!(gone, "the child is still running");
}
#[test]
fn a_program_the_agent_could_change_is_not_run() {
let stand = Stand::new();
let path = stand.script("ok", &format!("echo {VALUE}"));
let open = Policy::parse("version: 1\n").unwrap();
let error = format!("{:#}", stand.program(&path, &open).err().unwrap());
assert!(error.contains("could be changed by the agent"), "{error}");
assert!(error.contains("fs.write"), "{error}");
let elsewhere = scratch();
let target = elsewhere.path().join("real");
std::fs::copy(&path, &target).unwrap();
let link = Path::new(&stand.root).join("link");
std::os::unix::fs::symlink(&target, &link).unwrap();
let error = format!("{:#}", stand.program(&link, &stand.policy).err().unwrap());
assert!(error.contains("could be changed by the agent"), "{error}");
}
#[test]
fn a_program_changed_after_the_proxy_started_is_not_run() {
let stand = Stand::new();
let path = stand.script("ok", &format!("echo {VALUE}"));
let program = stand.program(&path, &stand.policy).unwrap();
stand.script("ok", "echo fake-planted-value-0000");
let error = format!("{:#}", program.fetch().err().unwrap());
assert!(error.contains("changed since the proxy started"), "{error}");
}
#[test]
fn a_failing_hanging_or_flooding_program_gives_no_value() {
let stand = Stand::new();
for (name, body, why) in [
("fails", format!("echo {VALUE}; exit 7"), "failed"),
(
"hangs",
format!("echo {VALUE}; exec sleep 5"),
"did not finish",
),
(
"floods",
"head -c 70000 /dev/zero | tr '\\0' a".to_owned(),
"more than 64 KiB",
),
(
"control",
format!("printf '{VALUE}\\033[2J'"),
"control character",
),
] {
let mut program = stand.shell(&body);
if name == "hangs" {
program = program.with_time_limit(Duration::from_secs(2));
}
let error = format!("{:#}", program.fetch().err().unwrap());
assert!(error.contains(why), "{name}: {error}");
assert!(!error.contains(VALUE), "{name}: {error}");
}
}
}