Skip to content

Commit 272a8ee

Browse files
committed
refactor: deduplicate terminal sanitizer in read.rs
Replace the local sanitize_terminal_output function with the existing crate::error::sanitize_for_terminal via import alias. This eliminates code duplication and provides consistent sanitization across the codebase. The crate-wide sanitizer also correctly strips CR (carriage return) which can be abused for terminal overwrite attacks.
1 parent 24561a7 commit 272a8ee

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/helpers/gmail/read.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,8 @@ fn format_mailbox_list(mailboxes: &[Mailbox]) -> String {
102102
.join(", ")
103103
}
104104

105-
/// Sanitizes a string for terminal output by filtering out control characters
106-
/// to prevent terminal injection attacks. Safe control characters like
107-
/// newline, carriage return, and tab are preserved.
108-
fn sanitize_terminal_output(s: &str) -> String {
109-
s.chars()
110-
.filter(|c| !c.is_control() || matches!(c, '\n' | '\r' | '\t'))
111-
.collect()
112-
}
105+
/// Re-export the crate-wide terminal sanitizer for use in this module.
106+
use crate::error::sanitize_for_terminal as sanitize_terminal_output;
113107

114108
#[cfg(test)]
115109
mod tests {
@@ -122,10 +116,11 @@ mod tests {
122116
// ANSI escape sequences (control chars) should be removed
123117
assert!(!sanitized.contains('\x1b'));
124118
assert!(!sanitized.contains('\x07'));
125-
// Whitespace and formatting should be preserved
119+
// CR is also stripped (can be abused for terminal overwrite attacks)
120+
assert!(!sanitized.contains('\r'));
121+
// Newline and tab should be preserved
126122
assert!(sanitized.contains("Hello"));
127123
assert!(sanitized.contains('\n'));
128-
assert!(sanitized.contains('\r'));
129124
assert!(sanitized.contains('\t'));
130125
}
131126

0 commit comments

Comments
 (0)