Skip to main content

sanitize_for_terminal

Function sanitize_for_terminal 

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

Strips ASCII control bytes (except \n/\t) — including the ESC byte that begins every ANSI escape sequence — and Unicode bidirectional-override characters from untrusted text before it is written to a terminal. This removes the trigger byte outright rather than substituting a visible placeholder, since the goal is preventing the escape sequence (or a spoofed display order) from being interpreted at all. \n/\t and the Unicode line/paragraph separators (U+2028/U+2029) are preserved, since they can be legitimate content in free text; bidi overrides are never legitimate in any context, so they are always stripped.

Use this only for genuinely free-text fields where a literal \n/\t can be legitimate content. For structured fields (node names, paths, link targets), use sanitize_name instead — those must never contain a newline, since one would let an attacker forge extra rows in table-shaped output. For free-text fields rendered inline within a single row, use sanitize_preview instead — it wraps this function and further escapes \n/\t and the Unicode line/paragraph separators to prevent them from being mistaken for separators.