1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Untrusted-text neutralization (CR-19, threat model T-1/T-3). Every string that can originate in a
//! source repository — a path, a name, an email, a message, a ref name, a drop's `what`, an error text
//! carrying a path — is routed through one of these two functions before it reaches stdout or stderr, in
//! both human and machine form. Neither function can be bypassed per-call-site by construction: there is
//! no third way to write source-derived text.
//!
//! - [`human`] makes control characters and invisible/bidirectional Unicode formatting characters visible
//! as `\u{XXXX}`, so source text can never drive or disguise the terminal.
//! - [`machine_value`] percent-encodes anything outside a safe ASCII set, so source text can never forge a
//! machine-format key or line.
use Cow;
use Write as _;
/// True for a character `human` must escape: the C0/C1 control ranges, and the Unicode bidirectional,
/// invisible, or line-breaking formatting characters an attacker could use to disguise, reorder, or split
/// terminal or log-viewer output. Extended after review 003 R-2: the first pass (handoff §3.6) missed
/// U+00AD (soft hyphen, invisible), U+061C (Arabic letter mark, a bidi control), U+180E (Mongolian vowel
/// separator, invisible), U+2028/U+2029 (line/paragraph separators — these can forge a line), U+206A–
/// U+206F (deprecated format controls), and U+FFF9–U+FFFB (interlinear annotation controls).
/// Escape `s` for safe display on a terminal (human output). Every control character and every
/// bidirectional/invisible Unicode format character becomes `\u{XXXX}`; a literal backslash becomes `\\`
/// so that an escape sequence in the *output* is never ambiguous with one that arrived in the *input* —
/// only this function's own escaping can produce a lone backslash followed by `u{`.
/// True for an ASCII byte that may pass through [`machine_value`] unescaped.
/// Encode `s` for safe use as a machine-format *value* (never a key — untrusted text never appears in a
/// key at all, CR-19). Every byte outside `A-Za-z0-9-._~/@:+` — including `%`, `=`, space, and every byte
/// of a non-ASCII UTF-8 sequence — becomes `%XX` (uppercase hex). The result can never contain `=` or a
/// newline, so it can never forge a line or a key in the line-oriented machine format.
/// The byte-level form of [`machine_value`], for text that is not necessarily UTF-8 (a commit message is
/// bytes, RFC 011 D-4). Percent-encoded once: a consumer percent-decodes to the **exact** bytes, whatever
/// they are, and no display escaping is applied first (that belongs to the human form only).