pub fn sanitize(input: &str) -> String {
let mut output = String::with_capacity(input.len());
let _ = sanitize_into(input, &mut output);
output
}
pub fn sanitize_into(input: &str, output: &mut impl std::fmt::Write) -> std::fmt::Result {
let mut i = 0usize;
while let Some((ch, next)) = next_char(input, i) {
match ch {
'\t' | '\n' => {
output.write_char(ch)?;
i = next;
}
'\r' => {
if next_char(input, next).is_some_and(|(next_ch, _)| next_ch == '\n') {
i = next; } else {
output.write_char('\n')?;
i = next;
}
}
'\x1B' => {
i = skip_escape_sequence(input, next);
}
'\u{80}'..='\u{9F}' => {
i = next;
if matches!(ch, '\u{90}' | '\u{9B}' | '\u{9D}' | '\u{9E}' | '\u{9F}') {
i = skip_c1_sequence(input, i, ch);
}
}
'\x7F' => {
i = next;
}
'\x00'..='\x08' | '\x0B'..='\x0C' | '\x0E'..='\x1A' | '\x1C'..='\x1F' => {
i = next;
}
ch if is_deceptive_format(ch) => {
i = next;
}
_ => {
output.write_char(ch)?;
i = next;
}
}
}
Ok(())
}
fn is_deceptive_format(ch: char) -> bool {
matches!(
ch,
'\u{061C}' | '\u{200E}' | '\u{200F}'
| '\u{202A}'..='\u{202E}'
| '\u{2066}'..='\u{2069}'
| '\u{200B}' | '\u{2060}'..='\u{2064}'
| '\u{FFF9}'..='\u{FFFB}'
| '\u{FEFF}'
)
}
fn skip_escape_sequence(input: &str, start: usize) -> usize {
let mut i = start;
let Some((introducer, next)) = next_char(input, i) else {
return input.len();
};
match introducer {
'[' => {
i = next;
while let Some((ch, after)) = next_char(input, i) {
i = after;
if ('\x40'..='\x7E').contains(&ch) {
break;
}
}
}
']' => {
i = skip_until_st_or_bel(input, next);
}
'P' => {
i = skip_until_st_or_bel(input, next);
}
'^' => {
i = skip_until_st_or_bel(input, next);
}
'_' => {
i = skip_until_st_or_bel(input, next);
}
_ => {
i = next;
}
}
i
}
fn skip_c1_sequence(input: &str, start: usize, introducer: char) -> usize {
let mut i = start;
match introducer {
'\u{9B}' => {
while let Some((ch, after)) = next_char(input, i) {
i = after;
if ('\x40'..='\x7E').contains(&ch) {
break;
}
}
}
'\u{90}' | '\u{9D}' | '\u{9E}' | '\u{9F}' => {
i = skip_until_st_or_bel(input, i);
}
_ => {}
}
i
}
fn skip_until_st_or_bel(input: &str, start: usize) -> usize {
let mut i = start;
while let Some((ch, next)) = next_char(input, i) {
if ch == '\x07' {
i = next;
break;
}
if ch == '\u{9C}' {
i = next;
break;
}
if ch == '\x1B' && next_char(input, next).is_some_and(|(next_ch, _)| next_ch == '\\') {
i = next_char(input, next).map_or(next, |(_, after)| after);
break;
}
i = next;
}
i
}
fn next_char(input: &str, index: usize) -> Option<(char, usize)> {
let ch = input.get(index..)?.chars().next()?;
Some((ch, index + ch.len_utf8()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plain_text_passes_through() {
assert_eq!(sanitize("hello world"), "hello world");
}
#[test]
fn tabs_and_newlines_preserved() {
assert_eq!(sanitize("a\tb\nc"), "a\tb\nc");
}
#[test]
fn carriage_return_normalized() {
assert_eq!(sanitize("a\r\nb"), "a\nb");
assert_eq!(sanitize("a\rb"), "a\nb");
}
#[test]
fn strips_esc_csi_sgr() {
assert_eq!(sanitize("hello\x1b[31mred\x1b[0mworld"), "helloredworld");
}
#[test]
fn strips_esc_osc_title() {
assert_eq!(sanitize("before\x1b]0;HACKED\x07after"), "beforeafter");
}
#[test]
fn strips_esc_osc_with_st() {
assert_eq!(sanitize("before\x1b]0;HACKED\x1b\\after"), "beforeafter");
}
#[test]
fn strips_csi_8bit() {
assert_eq!(sanitize("hello\u{9b}31mworld"), "helloworld");
}
#[test]
fn strips_osc_8bit() {
assert_eq!(sanitize("hello\u{9d}0;HACKED\x07world"), "helloworld");
}
#[test]
fn strips_dcs() {
assert_eq!(sanitize("before\x1bPsome data\x1b\\after"), "beforeafter");
}
#[test]
fn strips_null_and_c0() {
assert_eq!(sanitize("a\x00b\x01c\x02d"), "abcd");
assert_eq!(sanitize("a\x0Eb\x1Ac"), "abc");
}
#[test]
fn strips_del() {
assert_eq!(sanitize("a\x7Fb"), "ab");
}
#[test]
fn strips_remaining_c1_controls() {
assert_eq!(sanitize("a\u{80}b\u{8F}c"), "abc");
}
#[test]
fn unicode_passes_through() {
assert_eq!(sanitize("こんにちは 🌍 café"), "こんにちは 🌍 café");
}
#[test]
fn box_drawing_passes_through() {
assert_eq!(sanitize("╔═══╗\n║ ║\n╚═══╝"), "╔═══╗\n║ ║\n╚═══╝");
}
#[test]
fn embedded_esc_in_normal_text() {
assert_eq!(sanitize("abc\x1b7def\x1b8ghi"), "abcdefghi");
}
#[test]
fn truncated_escape_at_end() {
assert_eq!(sanitize("hello\x1b"), "hello");
assert_eq!(sanitize("hello\x1b["), "hello");
assert_eq!(sanitize("hello\x1b]"), "hello");
}
#[test]
fn complex_csi_parameters() {
assert_eq!(sanitize("a\x1b[38;2;255;100;0mb"), "ab");
}
#[test]
fn empty_input() {
assert_eq!(sanitize(""), "");
}
#[test]
fn only_control_chars() {
assert_eq!(sanitize("\x00\x01\x1b[31m\x7f"), "");
}
#[test]
fn strips_bidi_override_from_text() {
assert_eq!(
sanitize("atp://\u{202e}moc.live\u{202c}/"),
"atp://moc.live/"
);
assert_eq!(
sanitize("a\u{202a}b\u{202b}c\u{202d}d\u{2066}e\u{2067}f\u{2068}g\u{2069}h"),
"abcdefgh"
);
assert_eq!(sanitize("a\u{200e}b\u{200f}c\u{061c}d"), "abcd");
}
#[test]
fn strips_zero_width_formatting() {
assert_eq!(sanitize("pay\u{200b}pal.com"), "paypal.com");
assert_eq!(
sanitize("a\u{2060}b\u{2061}c\u{2062}d\u{2063}e\u{2064}f"),
"abcdef"
);
assert_eq!(sanitize("a\u{feff}b"), "ab");
assert_eq!(
sanitize("a\u{fff9}hidden\u{fffa}gloss\u{fffb}b"),
"ahiddenglossb"
);
}
#[test]
fn preserves_joiners_that_carry_meaning() {
assert_eq!(
sanitize("\u{1f469}\u{200d}\u{1f4bb}"),
"\u{1f469}\u{200d}\u{1f4bb}"
);
assert_eq!(sanitize("a\u{200c}b"), "a\u{200c}b");
}
#[test]
fn strips_escapes_and_deceptive_formats_together() {
assert_eq!(
sanitize("\x1b[31m\u{202e}danger\u{202c}\u{200b}\x1b[0m"),
"danger"
);
}
#[test]
fn sanitize_into_propagates_destination_rejection() {
struct Reject;
impl std::fmt::Write for Reject {
fn write_str(&mut self, _text: &str) -> std::fmt::Result {
Err(std::fmt::Error)
}
}
assert!(sanitize_into("remote", &mut Reject).is_err());
}
}