Skip to main content

gitpoulpi_hosting/markup/
sanitize.rs

1//! Untrusted text hygiene: what a terminal must never be handed.
2//!
3//! Comment bodies come from other people. A C0/C1 control character in one
4//! (ESC, BEL, CSI, OSC introducers) would be interpreted by the terminal, so
5//! they are removed here, before any parsing. Emoji presentation selectors,
6//! zero-width joiners, skin-tone modifiers and combining marks with no base
7//! are removed too: terminals disagree on their cell width, which corrupts
8//! the layout (see "emoji presentation" in `docs/developer/where-we-are.md`).
9
10/// Characters kept as they are although they are controls.
11fn keep_control(c: char) -> bool {
12    c == '\n' || c == '\t'
13}
14
15fn is_combining_mark(c: char) -> bool {
16    matches!(
17        c as u32,
18        0x0300..=0x036F | 0x1AB0..=0x1AFF | 0x1DC0..=0x1DFF | 0x20D0..=0x20FF | 0xFE20..=0xFE2F
19    )
20}
21
22/// Format characters that change width or direction in a terminal.
23fn is_unwanted_format(c: char) -> bool {
24    matches!(
25        c as u32,
26        // variation selectors 15 and 16, zero-width joiner
27        0xFE0E | 0xFE0F | 0x200D
28        // zero-width space, non-joiner, LRM, RLM
29        | 0x200B | 0x200C | 0x200E | 0x200F
30        // bidi embedding and isolate controls (spoofing)
31        | 0x202A..=0x202E | 0x2066..=0x2069
32        // BOM / word joiner
33        | 0xFEFF | 0x2060
34        // emoji skin-tone modifiers
35        | 0x1F3FB..=0x1F3FF
36    )
37}
38
39/// `text` with every character a terminal could act on removed. `\r\n`
40/// becomes `\n`; a lone `\r` is dropped.
41pub fn sanitize(text: &str) -> String {
42    let mut out = String::with_capacity(text.len());
43    for c in text.chars() {
44        if c.is_control() {
45            if keep_control(c) {
46                out.push(c);
47            }
48            continue;
49        }
50        if is_unwanted_format(c) {
51            continue;
52        }
53        if is_combining_mark(c) {
54            // Lone: nothing to attach to at the start or after a blank.
55            let lone = out.chars().next_back().is_none_or(|p| p.is_whitespace());
56            if lone {
57                continue;
58            }
59        }
60        out.push(c);
61    }
62    out
63}
64
65#[cfg(test)]
66mod tests {
67    use super::sanitize;
68
69    #[test]
70    fn table() {
71        let cases: &[(&str, &str)] = &[
72            ("plain text", "plain text"),
73            ("a\tb\nc", "a\tb\nc"),
74            ("red \u{1b}[31mtext\u{1b}[0m", "red [31mtext[0m"),
75            ("a\u{1b}]0;title\u{7}b", "a]0;titleb"),
76            ("c1\u{9b}31m\u{85}x", "c131mx"),
77            ("nul\u{0}del\u{7f}x", "nuldelx"),
78            ("crlf\r\nx\ry", "crlf\nxy"),
79            ("\u{2764}\u{fe0f} heart", "\u{2764} heart"),
80            ("text\u{fe0e}", "text"),
81            ("\u{1f468}\u{200d}\u{1f469}", "\u{1f468}\u{1f469}"),
82            ("\u{1f44d}\u{1f3fd}", "\u{1f44d}"),
83            ("e\u{301} stays", "e\u{301} stays"),
84            ("\u{301}lone", "lone"),
85            ("a \u{301}b", "a b"),
86            ("\u{202e}evil", "evil"),
87            ("caf\u{e9} \u{4e2d}\u{6587}", "caf\u{e9} \u{4e2d}\u{6587}"),
88        ];
89        for (input, want) in cases {
90            assert_eq!(sanitize(input), *want, "{input:?}");
91        }
92    }
93
94    #[test]
95    fn no_escape_survives() {
96        let nasty = "\u{1b}[2J\u{1b}]52;c;AAAA\u{7}\u{9d}x\u{90}y";
97        let out = sanitize(nasty);
98        assert!(!out.chars().any(|c| c.is_control()), "{out:?}");
99    }
100}