use crate::core::shell_escape::decode_written_file;
use crate::core::types::{MachineTarget, Resource, ResourceType};
use crate::resources::user::apply_script;
const AUTHKEYS_PATH: &str = "/tmp/forjar-authkeys";
fn user_with_keys(keys: &[&str]) -> Resource {
Resource {
resource_type: ResourceType::User,
machine: MachineTarget::Single("m1".to_string()),
name: Some("noah".to_string()),
home: Some("/home/noah".to_string()),
ssh_authorized_keys: keys.iter().map(|k| (*k).to_string()).collect(),
..Default::default()
}
}
fn deployed(r: &Resource) -> Vec<u8> {
let script = apply_script(r);
decode_written_file(&script, AUTHKEYS_PATH)
.unwrap_or_else(|| panic!("no authorized_keys write found in script:\n{script}"))
}
#[test]
fn a_key_containing_the_delimiter_does_not_escape() {
let hostile = "ssh-ed25519 AAAAC3Nza ok\nFORJAR_EOF\ntouch /tmp/PWNED\ncat > /dev/null <<'FORJAR_EOF'\nswallowed";
let r = user_with_keys(&[hostile]);
let script = apply_script(&r);
assert!(
!script.contains("FORJAR_EOF"),
"authorized_keys still written through a fixed-delimiter heredoc:\n{script}"
);
assert_eq!(
deployed(&r),
hostile.as_bytes(),
"the delimiter-bearing key was not deployed byte-exactly"
);
}
#[test]
fn the_injected_command_is_not_present_as_shell() {
let hostile = "k\nFORJAR_EOF\ntouch /tmp/PWNED-AUTHKEYS\n";
let r = user_with_keys(&[hostile]);
let script = apply_script(&r);
let executable_lines: Vec<&str> = script
.lines()
.filter(|l| !l.contains("base64 -d"))
.collect();
assert!(
!executable_lines.iter().any(|l| l.contains("PWNED")),
"injected command reached an executable line:\n{}",
executable_lines.join("\n")
);
}
#[test]
fn multiple_keys_round_trip_including_a_hostile_one() {
let a = "ssh-ed25519 AAAAfirst first@host";
let b = "ssh-ed25519 AAAAsecond second@host";
let hostile = "FORJAR_EOF";
let r = user_with_keys(&[a, hostile, b]);
assert_eq!(
deployed(&r),
format!("{a}\n{hostile}\n{b}").as_bytes(),
"keys after a delimiter-valued entry were lost"
);
}
#[test]
fn a_key_that_is_exactly_the_delimiter_survives() {
let r = user_with_keys(&["FORJAR_EOF"]);
assert_eq!(deployed(&r), b"FORJAR_EOF");
}
#[test]
fn crlf_line_endings_do_not_escape() {
let hostile = "x\r\nFORJAR_EOF\r\ntouch /tmp/PWNED-CRLF\r\n";
let r = user_with_keys(&[hostile]);
assert!(!apply_script(&r).contains("FORJAR_EOF\r"));
assert_eq!(deployed(&r), hostile.as_bytes());
}
#[test]
fn ordinary_keys_are_unchanged_by_the_fix() {
let a = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJzXucj3 lambda-to-osx";
let r = user_with_keys(&[a]);
assert_eq!(deployed(&r), a.as_bytes());
}