Skip to content

Commit e54d409

Browse files
fix: strip all ANSI escape seqs in terminal command output
When sudo (or other interactive programs) run via run_terminal_command, they can emit cursor-movement, scroll-region, alternate-screen and other VT/ANSI control sequences that bleed into the TUI and corrupt the display (garbage characters, broken scrolling). Changes: - Switch stripColors -> stripAnsi so ALL escape sequences are removed from captured stdout/stderr, not just SGR color codes (\x1B[...m). - Strip bare \r characters that sudo uses to erase its password-prompt line after credentials are accepted. - Set SUDO_PROMPT to a plain ASCII string so sudo never emits decorated (colored/formatted) prompts to /dev/tty. - Default TERM=dumb (when not overridden by the caller) so child programs like git, less, and man suppress their own color/cursor output.
1 parent 088a57e commit e54d409

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

sdk/src/tools/run-terminal-command.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from 'path'
66
import type { ChildProcess } from 'child_process'
77

88
import {
9-
stripColors,
9+
stripAnsi,
1010
truncateStringWithMessage,
1111
} from '../../../common/src/util/string'
1212
import { getSystemProcessEnv } from '../env'
@@ -175,6 +175,14 @@ export function runTerminalCommand({
175175
const processEnv = {
176176
...getSystemProcessEnv(),
177177
...(env ?? {}),
178+
// Use a plain-text sudo prompt so sudo doesn't emit ANSI color/cursor
179+
// sequences when asking for a password. Without this, sudo writes
180+
// decorated prompts to /dev/tty that can bleed into the TUI.
181+
SUDO_PROMPT: '[sudo] password: ',
182+
// Signal to child programs that this is a dumb non-interactive terminal
183+
// so they suppress color output and cursor-movement sequences.
184+
// (Only set if the caller hasn't explicitly overridden it.)
185+
...((env ?? {}).TERM === undefined && { TERM: 'dumb' }),
178186
} as NodeJS.ProcessEnv
179187

180188
if (signal?.aborted) {
@@ -241,7 +249,12 @@ export function runTerminalCommand({
241249

242250
const truncateOutput = (str: string) =>
243251
truncateStringWithMessage({
244-
str: stripColors(str),
252+
// Strip all ANSI/VT escape sequences (not just color codes) so that
253+
// cursor-movement, scroll-region, alternate-screen and other control
254+
// sequences emitted by sudo and interactive programs don't bleed into
255+
// the TUI and corrupt its scroll state or render garbage characters.
256+
// Also normalise bare \r that sudo uses to overwrite its prompt line.
257+
str: stripAnsi(str).replace(/\r/g, ''),
245258
maxLength: COMMAND_OUTPUT_LIMIT,
246259
remove: 'MIDDLE',
247260
})

0 commit comments

Comments
 (0)