Skip to content

Commit c08a59d

Browse files
authored
fix: keep 8-bit color codes, strip them, and drop other 8-bit sequences whole (sirmalloc#704)
* fix(ansi): strip 8-bit color codes as well as ESC [ ones The ANSI parser reads the 8-bit CSI (0x9b) as an escape sequence, but stripSgrCodes only matched color codes written with ESC [. Text colored with \x9b31m kept its colors where they should go: a global foreground override left a preserveColors widget's own colors in place, and a Custom Command cut by its max width never got its closing reset, so its color ran into the separators and widgets after it. Match both introducers. restoreBackgroundAfterResets shares the pattern, so an 8-bit reset now gets the background re-applied after it too; its parameter parsing skips a one-character introducer for those. * fix(sanitize): keep 8-bit color codes and drop other 8-bit sequences whole The terminal sanitizer dropped 8-bit C1 controls one character at a time. For a sequence started by one, that removed only the introducer and left the rest as text: a command printing \x9b31mred\x9b39m showed "31mred39m", and an 8-bit hyperlink or title string showed its URL or payload. Read each 8-bit introducer as the ESC sequence it stands for: CSI (0x9b) as ESC [, OSC (0x9d) as ESC ], and DCS, SOS, PM and APC likewise, with ST (0x9c) ending strings. Color codes and safe hyperlinks are kept, in their 7-bit form, which every terminal reads and the rest of the renderer parses; any other sequence is dropped whole. Lone 8-bit controls that start no sequence, such as NEL or a stray ST, are still dropped.
1 parent 6e148cb commit c08a59d

6 files changed

Lines changed: 104 additions & 21 deletions

File tree

‎src/utils/__tests__/ansi.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getVisibleWidth,
1010
restoreBackgroundAfterResets,
1111
stripOscCodes,
12+
stripSgrCodes,
1213
truncateStyledText
1314
} from '../ansi';
1415

@@ -112,6 +113,19 @@ describe('truncateStyledText', () => {
112113
});
113114
});
114115

116+
describe('stripSgrCodes', () => {
117+
it('removes 7-bit and 8-bit color codes', () => {
118+
expect(stripSgrCodes('\x9b31mred\x9b39m')).toBe('red');
119+
expect(stripSgrCodes('\x1b[1m\x9b38;5;208mbold\x9b0m\x1b[22m')).toBe('bold');
120+
});
121+
122+
it('leaves other CSI sequences and hyperlinks alone', () => {
123+
const text = `\x1b[2K\x9b2J${OSC8_OPEN}link${OSC8_CLOSE}${C1_OSC8_OPEN}c1${C1_OSC8_CLOSE}`;
124+
125+
expect(stripSgrCodes(text)).toBe(text);
126+
});
127+
});
128+
115129
describe('restoreBackgroundAfterResets', () => {
116130
const BG = '\x1b[44m';
117131

@@ -120,6 +134,11 @@ describe('restoreBackgroundAfterResets', () => {
120134
.toBe(`\x1b[31ma\x1b[0m${BG}b\x1b[m${BG}c\x1b[00m${BG}d\x1b[;1m${BG}e\x1b[39;49m${BG}f`);
121135
});
122136

137+
it('re-applies the background after an 8-bit reset', () => {
138+
expect(restoreBackgroundAfterResets('\x9b31ma\x9b0mb\x9b49mc', BG))
139+
.toBe(`\x9b31ma\x9b0m${BG}b\x9b49m${BG}c`);
140+
});
141+
123142
it('leaves other styling, and the arguments of extended colors, alone', () => {
124143
const text = '\x1b[1;31ma\x1b[38;5;0mb\x1b[48;2;0;49;0mc\x1b[58;5;49md\x1b[39me';
125144

‎src/utils/__tests__/renderer-sanitize.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,13 @@ describe('preRenderAllWidgets', () => {
2020
expect(line[0]?.content).toBe('safe text');
2121
expect(line[0]?.plainLength).toBe('safe text'.length);
2222
});
23+
24+
it('keeps 8-bit colors in widget output, in their 7-bit form', () => {
25+
const widget: WidgetItem = { id: 'text', type: 'custom-text', customText: '\x9b31mred\x9b39m text' };
26+
27+
const [line = []] = preRenderAllWidgets([[widget]], DEFAULT_SETTINGS, context);
28+
29+
expect(line[0]?.content).toBe('\x1b[31mred\x1b[39m text');
30+
expect(line[0]?.plainLength).toBe('red text'.length);
31+
});
2332
});

‎src/utils/__tests__/terminal-sanitize.test.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,37 @@ describe('sanitizeTerminalText', () => {
8484
['shift out', '\x0e'],
8585
['form feed', '\f'],
8686
['DEL', '\x7f'],
87-
['8-bit CSI', '\x9b'],
88-
['8-bit OSC', '\x9d'],
8987
['8-bit ST', '\x9c'],
90-
['8-bit DCS', '\x90'],
9188
['NEL', '\x85']
9289
])('drops the control character %s', (_label, control) => {
9390
expect(sanitizeTerminalText(`a${control}b`)).toBe('ab');
9491
});
9592

96-
it('drops an 8-bit CSI sequence\'s introducer, leaving its harmless parameters as text', () => {
97-
expect(sanitizeTerminalText('a\x9b2Jb')).toBe('a2Jb');
93+
// 8-bit C1 introducers stand for ESC and a character: CSI (0x9b) is ESC [,
94+
// OSC (0x9d) is ESC ], and ST (0x9c) is ESC \
95+
it('keeps 8-bit color codes, written in their 7-bit form', () => {
96+
expect(sanitizeTerminalText('\x9b38;5;208mred\x9b39m')).toBe(`${ESC}[38;5;208mred${ESC}[39m`);
97+
});
98+
99+
it('keeps an 8-bit hyperlink with a safe URL, written in its 7-bit form', () => {
100+
const link = `\x9d8;;https://github.com/o/r\x9ctext\x9d8;;${BEL}`;
101+
102+
expect(sanitizeTerminalText(link)).toBe(`${ESC}]8;;https://github.com/o/r${ST}text${ESC}]8;;${BEL}`);
103+
});
104+
105+
it.each([
106+
['clearing the screen', '\x9b2J'],
107+
['moving the cursor', '\x9b10;20H'],
108+
['repeating a character', '\x9bb'],
109+
['an OSC 52 clipboard write', `\x9d52;c;ZWNobyBwd25lZA==${BEL}`],
110+
['an OSC 0 title change', '\x9d0;hacked\x9c'],
111+
['a hyperlink with an unsafe URL', '\x9d8;;https://x/a b\x9c'],
112+
['a DCS string', '\x90q#0;2;0;0;0\x9c'],
113+
['an APC string', `\x9fpayload${ST}`],
114+
['a PM string', '\x9epayload\x9c'],
115+
['a SOS string', '\x98payload\x9c']
116+
])('drops an 8-bit sequence entirely: %s', (_label, sequence) => {
117+
expect(sanitizeTerminalText(`a${sequence}b`)).toBe('ab');
98118
});
99119
});
100120

‎src/utils/ansi.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const VARIATION_SELECTOR_SUPPLEMENT_END = 0xe01ef;
1919
const REGIONAL_INDICATOR_START = 0x1f1e6;
2020
const REGIONAL_INDICATOR_END = 0x1f1ff;
2121

22-
const SGR_REGEX = /\x1b\[[0-9;]*m/g;
22+
// Color codes, written with ESC [ or the 8-bit CSI
23+
const SGR_REGEX = /(?:\x1b\[|\x9b)[0-9;]*m/g;
2324
// Lowest code point that can extend a display cluster (U+0300 is the first
2425
// \p{Mark}; ZWJ, variation selectors, the keycap and emoji modifiers are all
2526
// higher), so a printable ASCII character followed by anything below it is a
@@ -399,7 +400,8 @@ export function stripSgrCodes(text: string): string {
399400
// reset (0, an empty parameter, or 49). Skip extended color arguments so their
400401
// values aren't mistaken for resets or background operations.
401402
function sgrClearsBackground(sequence: string): boolean {
402-
const params = sequence.slice(2, -1).split(';').map(param => Number.parseInt(param || '0', 10));
403+
const introducerLength = sequence.startsWith(C1_CSI) ? 1 : 2;
404+
const params = sequence.slice(introducerLength, -1).split(';').map(param => Number.parseInt(param || '0', 10));
403405
let clearsBackground = false;
404406
for (let i = 0; i < params.length; i++) {
405407
const param = params[i];

‎src/utils/terminal-sanitize.ts‎

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,29 @@ const ESC = '\x1b';
99
const BEL = '\x07';
1010
const C1_ST = '\x9c';
1111

12-
// Colors and text attributes: ESC [ <digits, ; and :> m
13-
const SGR = /\x1b\[[0-9;:]*m/y;
14-
// A hyperlink: ESC ] 8 ; <parameters> ; <URI> then ESC \ or BEL. The URI may not
15-
// hold control characters or spaces, so it can't end the sequence early or start
16-
// another.
17-
const OSC8 = /\x1b\]8;[A-Za-z0-9=:_.-]*;[^\x00-\x20\x7f-\x9f]*(?:\x1b\\|\x07)/y;
12+
// Colors and text attributes: ESC [ (or 8-bit CSI) <digits, ; and :> m
13+
const SGR = /(?:\x1b\[|\x9b)[0-9;:]*m/y;
14+
// A hyperlink: ESC ] (or 8-bit OSC) 8 ; <parameters> ; <URI> then ESC \, ST or
15+
// BEL. The URI may not hold control characters or spaces, so it can't end the
16+
// sequence early or start another.
17+
const OSC8 = /(?:\x1b\]|\x9d)8;[A-Za-z0-9=:_.-]*;[^\x00-\x20\x7f-\x9f]*(?:\x1b\\|\x9c|\x07)/y;
1818
const SAFE_URL = /^[^\x00-\x20\x7f-\x9f]*$/;
1919
// C0 controls other than tab and newline, DEL, and the 8-bit C1 controls, which
2020
// some terminals read like ESC sequences
2121
const CONTROL = /[\x00-\x08\x0b-\x1f\x7f-\x9f]/;
2222
const CONTROL_GLOBAL = /[\x00-\x08\x0b-\x1f\x7f-\x9f]/g;
2323
// Strings the terminal swallows up to ST: OSC, DCS, SOS, PM and APC
2424
const STRING_INTRODUCERS = new Set([']', 'P', 'X', '^', '_']);
25+
// The 8-bit C1 controls that start a sequence, and the character that follows
26+
// ESC in their 7-bit form: CSI is ESC [, OSC is ESC ], and so on
27+
const C1_INTRODUCERS = new Map([
28+
['\x90', 'P'],
29+
['\x98', 'X'],
30+
['\x9b', '['],
31+
['\x9d', ']'],
32+
['\x9e', '^'],
33+
['\x9f', '_']
34+
]);
2535

2636
function matchAt(pattern: RegExp, text: string, index: number): string | null {
2737
pattern.lastIndex = index;
@@ -36,15 +46,31 @@ function isIntermediate(code: number): boolean {
3646
return code >= 0x20 && code <= 0x2f;
3747
}
3848

39-
// Where the escape sequence starting at `index` ends
49+
function startsSequence(char: string | undefined): boolean {
50+
return char === ESC || (char !== undefined && C1_INTRODUCERS.has(char));
51+
}
52+
53+
// A kept sequence in its 7-bit form, which every terminal reads and the rest of
54+
// the renderer parses
55+
function toSevenBit(sequence: string): string {
56+
const introducer = C1_INTRODUCERS.get(sequence.charAt(0));
57+
const sevenBit = introducer ? ESC + introducer + sequence.slice(1) : sequence;
58+
return sevenBit.endsWith(C1_ST) ? `${sevenBit.slice(0, -1)}${ESC}\\` : sevenBit;
59+
}
60+
61+
// Where the escape sequence starting at `index`, with ESC or an 8-bit
62+
// introducer, ends
4063
function skipEscapeSequence(text: string, index: number): number {
41-
const introducer = text[index + 1];
64+
const c1Introducer = C1_INTRODUCERS.get(text.charAt(index));
65+
const introducer = c1Introducer ?? text[index + 1];
4266
if (introducer === undefined) {
4367
return index + 1;
4468
}
69+
// Where the sequence's parameters or string start
70+
const bodyStart = c1Introducer ? index + 1 : index + 2;
4571

4672
if (introducer === '[') {
47-
let end = index + 2;
73+
let end = bodyStart;
4874
while (end < text.length && isCsiParameterOrIntermediate(text.charCodeAt(end))) {
4975
end++;
5076
}
@@ -53,7 +79,7 @@ function skipEscapeSequence(text: string, index: number): number {
5379
}
5480

5581
if (STRING_INTRODUCERS.has(introducer)) {
56-
for (let end = index + 2; end < text.length; end++) {
82+
for (let end = bodyStart; end < text.length; end++) {
5783
const char = text[end];
5884
if (char === BEL || char === C1_ST) {
5985
return end + 1;
@@ -83,6 +109,7 @@ function skipEscapeSequence(text: string, index: number): number {
83109
/**
84110
* The text with every control character and escape sequence removed, except
85111
* color codes (SGR) and hyperlinks (OSC 8) whose URL holds no control characters or spaces.
112+
* Those are kept in their 7-bit form, even when written with 8-bit C1 controls.
86113
*/
87114
export function sanitizeTerminalText(text: string): string {
88115
if (!CONTROL.test(text)) {
@@ -92,11 +119,10 @@ export function sanitizeTerminalText(text: string): string {
92119
let result = '';
93120
let index = 0;
94121
while (index < text.length) {
95-
const char = text[index];
96-
if (char === ESC) {
122+
if (startsSequence(text[index])) {
97123
const kept = matchAt(SGR, text, index) ?? matchAt(OSC8, text, index);
98124
if (kept) {
99-
result += kept;
125+
result += toSevenBit(kept);
100126
index += kept.length;
101127
} else {
102128
index = skipEscapeSequence(text, index);
@@ -105,7 +131,7 @@ export function sanitizeTerminalText(text: string): string {
105131
}
106132

107133
let end = index;
108-
while (end < text.length && text[end] !== ESC) {
134+
while (end < text.length && !startsSequence(text[end])) {
109135
end++;
110136
}
111137
result += text.slice(index, end).replace(CONTROL_GLOBAL, '');

‎src/widgets/__tests__/CustomCommand.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ describe('CustomCommandWidget', () => {
216216
expect(output.endsWith(SGR_RESET)).toBe(true);
217217
});
218218

219+
it('closes an 8-bit colour the truncation cut off', () => {
220+
const output = renderWith('\x9b38;5;208mfeature/login-refactor\x9b0m', { preserveColors: true, maxWidth: 20 }) ?? '';
221+
222+
expect(getVisibleText(output)).toBe('feature/login-ref...');
223+
expect(output.endsWith(SGR_RESET)).toBe(true);
224+
});
225+
219226
it('never cuts preserveColors output in the middle of an escape sequence', () => {
220227
const output = renderWith(`${SGR_ORANGE}feature/login-refactor${SGR_RESET}`, { preserveColors: true, maxWidth: 10 }) ?? '';
221228

0 commit comments

Comments
 (0)