Skip to content

Commit 4f340fd

Browse files
authored
fix(dev): escape ANSI escape sequence for server.forwardConsole (#23933)
1 parent cce559a commit 4f340fd

2 files changed

Lines changed: 90 additions & 7 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { escapeUnsupportedAnsiSequences } from '../../plugins/forwardConsole'
3+
4+
describe('escapeUnsupportedAnsiSequences', () => {
5+
test('escapes standalone BEL characters', () => {
6+
expect(escapeUnsupportedAnsiSequences('before\x07middle\x07after')).toBe(
7+
'before\\x07middle\\x07after',
8+
)
9+
})
10+
11+
test('escapes OSC 52 with a BEL terminator', () => {
12+
expect(escapeUnsupportedAnsiSequences('\x1b]52;c;Y2xpcGJvYXJk\x07')).toBe(
13+
'\\x1b]52;c;Y2xpcGJvYXJk\\x07',
14+
)
15+
})
16+
17+
test('escapes OSC 52 with an ST terminator', () => {
18+
expect(escapeUnsupportedAnsiSequences('\x1b]52;c;Y2xpcGJvYXJk\x1b\\')).toBe(
19+
'\\x1b]52;c;Y2xpcGJvYXJk\\x1b\\',
20+
)
21+
})
22+
23+
test('escapes C1 CSI screen controls', () => {
24+
expect(escapeUnsupportedAnsiSequences('\u009b2J\u009bH')).toBe(
25+
'\\x9b2J\\x9bH',
26+
)
27+
})
28+
29+
test('escapes C1 OSC and ST controls', () => {
30+
expect(
31+
escapeUnsupportedAnsiSequences('\u009d52;c;Y2xpcGJvYXJk\u009c'),
32+
).toBe('\\x9d52;c;Y2xpcGJvYXJk\\x9c')
33+
})
34+
35+
test('escapes the entire C1 control range', () => {
36+
const controls = Array.from({ length: 32 }, (_, i) =>
37+
String.fromCharCode(0x80 + i),
38+
).join('')
39+
40+
expect(escapeUnsupportedAnsiSequences(controls)).toBe(
41+
'\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87' +
42+
'\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f' +
43+
'\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97' +
44+
'\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f',
45+
)
46+
})
47+
48+
test('preserves Chrome DevTools SGR syntax', () => {
49+
const message =
50+
'\x1b[31mred\x1b[0m \x1b[1;4;93mbold underline\x1b[m ' +
51+
'\x1b[38;2;255;0;128mRGB\x1b[39m \x1b[48;2;0;128;255mbackground\x1b[49m ' +
52+
'\x1b[;32;mempty parameters\x1b[;m'
53+
54+
expect(escapeUnsupportedAnsiSequences(message)).toBe(message)
55+
})
56+
57+
test('escapes unsupported and incomplete sequences alongside SGR', () => {
58+
const message =
59+
'\x1b]0;title\x07 \x1b[2J\x1b[H \x1b[?25l \x1b[31:1m \x1bc ' +
60+
'\x1b[31mred\x1b[m \x1b[31 \x1b'
61+
const escaped =
62+
'\\x1b]0;title\\x07 \\x1b[2J\\x1b[H \\x1b[?25l \\x1b[31:1m \\x1bc ' +
63+
'\x1b[31mred\x1b[m \\x1b[31 \\x1b'
64+
65+
expect(escapeUnsupportedAnsiSequences(message)).toBe(escaped)
66+
})
67+
})

‎packages/vite/src/node/plugins/forwardConsole.ts‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ export function forwardConsolePlugin(pluginOpts: {
2828
payload.type === 'error' ||
2929
payload.type === 'unhandled-rejection'
3030
) {
31-
const output = formatError(
32-
payload,
33-
environment,
34-
sourceMapCache,
35-
bundledDevOutDir,
31+
const output = escapeUnsupportedAnsiSequences(
32+
formatError(
33+
payload,
34+
environment,
35+
sourceMapCache,
36+
bundledDevOutDir,
37+
),
3638
)
3739
environment.config.logger.error(output, {
3840
timestamp: true,
3941
})
4042
} else {
41-
const output =
42-
c.dim(`[console.${payload.data.level}] `) + payload.data.message
43+
const output = escapeUnsupportedAnsiSequences(
44+
c.dim(`[console.${payload.data.level}] `) + payload.data.message,
45+
)
4346
if (payload.data.level === 'error') {
4447
environment.config.logger.error(output, {
4548
timestamp: true,
@@ -60,6 +63,19 @@ export function forwardConsolePlugin(pluginOpts: {
6063
}
6164
}
6265

66+
export function escapeUnsupportedAnsiSequences(message: string): string {
67+
// Browser logs may contain terminal controls that write to the clipboard (OSC 52)
68+
// or ring the terminal bell (BEL). Escape them to prevent side effects, preserving
69+
// only SGR sequences recognized by Chrome DevTools.
70+
// C1 controls can invoke terminal commands without an ESC character.
71+
// https://developer.chrome.com/docs/devtools/console/format-style#style-ansi
72+
return message.replace(
73+
// eslint-disable-next-line no-control-regex
74+
/[\x07\x80-\x9f]|\x1b(?!\[[\d;]*m)/g,
75+
(char) => `\\x${char.charCodeAt(0).toString(16).padStart(2, '0')}`,
76+
)
77+
}
78+
6379
function formatError(
6480
payload: Extract<
6581
ForwardConsolePayload,

0 commit comments

Comments
 (0)