Skip to main content

sanitize_for_terminal

Function sanitize_for_terminal 

Source
pub fn sanitize_for_terminal(s: &str) -> String
Expand description

Strips control characters (C0, DEL, C1) from a server- or writer-supplied string before it reaches the terminal.

Every CSI escape sequence starts with ESC (a C0 control character), so this neutralizes embedded ANSI escape sequences at the source without needing sequence-aware parsing; it also strips embedded newlines that could otherwise be used to spoof extra output rows. The one place a newline legitimately appears in rendered output is the record separator the renderer itself writes between rows — never a value coming from a sanitized field (#1137).

Also strips the bidirectional-control code points (LRE/RLE/PDF/LRO/RLO, LRI/RLI/FSI/PDI, LRM/RLM) — a Trojan-Source-style spoofing vector (CVE-2021-42574) distinct from escape injection, since a terminal that honors them can visually reorder rendered text (e.g. a filename crafted so evil.exe displays as exe.live). These are the only code points pulled from Unicode category Cf; the rest of Cf (e.g. ZWJ in emoji sequences, joiners in Arabic/Indic text) is left alone since stripping it would mangle otherwise-valid international text (#1552).