Make a string that originates in an attacker-controlled response body safe to put
into an error message printed raw to stderr by run.ts — an error detail snippet,
an object type, a link, a Content-Type:
Whole escape sequences (CSI, OSC and other control strings) are dropped first, then any
remaining control character (all C0 and C1 but the whitespace ones, plus DEL). Without
this, a hostile or spoofed server could drive ANSI/OSC escape sequences (display
spoofing, terminal title changes) into the user's terminal.
Unicode format characters (General Category Cf) are dropped: the bidi controls, so
server text cannot reorder the visible message, and the invisible ones (zero-width
space and joiners, word joiner, soft hyphen, BOM), so two messages that look identical
are identical.
Every run of whitespace — newlines and Unicode line separators included — becomes
a single space, so the result is one line and a server cannot forge a second
log record of its own next to ours.
The result is capped at maxLength characters, so a 3 KB "message" cannot bury
the diagnostic the CLI printed. The cut never splits a surrogate pair (cutText),
so the message stays well-formed.
Only the first MAX_SERVER_TEXT (16 384) units are read, and every step is a linear regex
replace: a hostile error body costs neither seconds nor the heap. A caller that scrubs
credentials does so first, so a cut never splits one.
Every server-supplied string that reaches a message goes through here. The CLI's
JSON output is escaped separately (escapeControlChars in cli/shared.ts):
JSON.stringify alone leaves DEL and the C1 range raw. Written with escapes so no raw
control byte appears in this source.
Make a string that originates in an attacker-controlled response body safe to put into an error message printed raw to stderr by run.ts — an error
detailsnippet, an objecttype, a link, aContent-Type:maxLengthcharacters, so a 3 KB "message" cannot bury the diagnostic the CLI printed. The cut never splits a surrogate pair (cutText), so the message stays well-formed.Every server-supplied string that reaches a message goes through here. The CLI's JSON output is escaped separately (escapeControlChars in cli/shared.ts): JSON.stringify alone leaves DEL and the C1 range raw. Written with escapes so no raw control byte appears in this source.