Skip to content

Commit 7bcb63a

Browse files
committed
fix: use char::is_control() in reject_dangerous_chars for C1 coverage
Address PR review: the manual (c as u32) < 0x20 check missed C1 control characters (U+0080-U+009F), including CSI (U+009B) which can inject terminal escape sequences. Using char::is_control() covers both C0 and C1 ranges. Add test for CSI rejection.
1 parent 1d169b5 commit 7bcb63a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/output.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ pub(crate) fn sanitize_for_terminal(text: &str) -> String {
6262
.collect()
6363
}
6464

65-
/// Rejects strings containing null bytes, ASCII control characters
66-
/// (including DEL, 0x7F), or dangerous Unicode characters such as
67-
/// zero-width chars, bidi overrides, and Unicode line/paragraph separators.
65+
/// Rejects strings containing control characters (C0: U+0000–U+001F,
66+
/// C1: U+0080–U+009F, and DEL: U+007F) or dangerous Unicode characters
67+
/// such as zero-width chars, bidi overrides, and line/paragraph separators.
6868
///
6969
/// Used for validating CLI argument values at the parse boundary.
7070
pub(crate) fn reject_dangerous_chars(value: &str, flag_name: &str) -> Result<(), GwsError> {
7171
for c in value.chars() {
72-
if (c as u32) < 0x20 || c as u32 == 0x7F {
72+
if c.is_control() {
7373
return Err(GwsError::Validation(format!(
7474
"{flag_name} contains invalid control characters"
7575
)));
@@ -246,6 +246,13 @@ mod tests {
246246
assert!(reject_dangerous_chars("αβγ", "test").is_ok());
247247
}
248248

249+
#[test]
250+
fn reject_c1_control_csi() {
251+
// U+009B is the C1 "Control Sequence Introducer" — can inject
252+
// terminal escape sequences just like ESC+[
253+
assert!(reject_dangerous_chars("foo\u{009B}bar", "test").is_err());
254+
}
255+
249256
// ── colorize ──────────────────────────────────────────────────
250257

251258
#[test]

0 commit comments

Comments
 (0)