1use gadk_platform::go_quote_bytes;
23
24pub fn center_string(s: &str, w: usize) -> String {
27 let sw = w as isize - s.len() as isize;
28 let lw = sw / 2;
29 let rw = sw - lw;
30 " ".repeat(lw.max(0) as usize) + s + &" ".repeat(rw.max(0) as usize)
33}
34
35pub fn validate_dockerfile_safe(s: impl AsRef<[u8]>, label: &str) -> Result<(), String> {
40 let s = s.as_ref();
41 if s.iter()
43 .any(|&c| c == b'"' || c == b'`' || c == b'\\' || c < 0x20)
44 {
45 return Err(format!(
46 "{label} {} contains a character (quote, backtick, backslash, or a control character) that is not allowed in a value embedded in generated Dockerfile content",
47 go_quote_bytes(s)
48 ));
49 }
50 if std::str::from_utf8(s).is_err() {
51 return Err(format!(
52 "{label} {} is not valid UTF-8; the invalid bytes would reach the container as U+FFFD rather than as written",
53 go_quote_bytes(s)
54 ));
55 }
56 Ok(())
57}
58
59pub fn validate_shell_arg_safe(s: impl AsRef<[u8]>, label: &str) -> Result<(), String> {
63 let s = s.as_ref();
64 let allowed =
67 |c: u8| c.is_ascii_alphanumeric() || matches!(c, b'_' | b'.' | b'/' | b'-') || c >= 0x80;
68 if !s.iter().all(|&c| allowed(c)) {
69 return Err(format!(
70 "{label} {} contains a character not allowed in a value embedded in generated Dockerfile content; whitespace splits COPY operands and shell metacharacters are live in a RUN line, so only letters, digits, '.', '/', '-', '_', and non-ASCII characters are permitted",
71 go_quote_bytes(s)
72 ));
73 }
74 if std::str::from_utf8(s).is_err() {
75 return Err(format!(
76 "{label} {} is not valid UTF-8; a COPY operand containing an invalid byte does not match the file it names",
77 go_quote_bytes(s)
78 ));
79 }
80 Ok(())
81}
82
83#[cfg(test)]
84mod tests {
85 use pretty_assertions::assert_eq;
86
87 use super::*;
88
89 #[test]
91 fn center_string_pads_like_go() {
92 assert_eq!(center_string("", 4), " ");
93 assert_eq!(center_string("ab", 5), " ab ");
94 assert_eq!(center_string("abc", 6), " abc ");
95 assert_eq!(center_string("abcd", 4), "abcd");
96 assert_eq!(center_string("Press Ctrl-C to stop", 80).len(), 80);
97 assert_eq!(
98 center_string("Press Ctrl-C to stop", 80),
99 format!("{}Press Ctrl-C to stop{}", " ".repeat(30), " ".repeat(30))
100 );
101 assert_eq!(center_string("é", 4), " é ");
103 assert_eq!(center_string("toolong", 3), "toolong");
104 }
105
106 #[test]
107 fn rejection_texts_are_go_verbatim() {
108 assert_eq!(
109 validate_dockerfile_safe("x\"y", "--a2a_agent_url").unwrap_err(),
110 r#"--a2a_agent_url "x\"y" contains a character (quote, backtick, backslash, or a control character) that is not allowed in a value embedded in generated Dockerfile content"#
111 );
112 assert_eq!(
113 validate_dockerfile_safe(b"http://x\xffy", "--a2a_agent_url").unwrap_err(),
114 r#"--a2a_agent_url "http://x\xffy" is not valid UTF-8; the invalid bytes would reach the container as U+FFFD rather than as written"#
115 );
116 assert_eq!(
117 validate_shell_arg_safe("main go", "label:").unwrap_err(),
118 r#"label: "main go" contains a character not allowed in a value embedded in generated Dockerfile content; whitespace splits COPY operands and shell metacharacters are live in a RUN line, so only letters, digits, '.', '/', '-', '_', and non-ASCII characters are permitted"#
119 );
120 assert_eq!(
121 validate_shell_arg_safe(b"agent\xff", "--source_dir").unwrap_err(),
122 r#"--source_dir "agent\xff" is not valid UTF-8; a COPY operand containing an invalid byte does not match the file it names"#
123 );
124 assert_eq!(
125 validate_dockerfile_safe("http://127.0.0.1:8081", "x"),
126 Ok(())
127 );
128 assert_eq!(validate_shell_arg_safe("agentes/café/main", "x"), Ok(()));
129 }
130}