Repository navigation
fix(pty): sanitize ANSI escape sequences in pty_read and pty_exited output #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| let optionOverride: boolean | undefined | ||
|
|
||
| /** | ||
| * Applies the `sanitizeOutput` plugin option (from the opencode plugin config). | ||
| * Precedence: explicit option > PTY_SANITIZE_OUTPUT env var > default (true). | ||
| */ | ||
| export function setAnsiSanitizationOverride(value: boolean | undefined): void { | ||
| optionOverride = value | ||
| } | ||
|
|
||
| export function isAnsiSanitizationEnabled(): boolean { | ||
| if (optionOverride !== undefined) { | ||
| return optionOverride | ||
| } | ||
| const rawValue = process.env.PTY_SANITIZE_OUTPUT ?? 'true' | ||
| return rawValue.toLowerCase() !== 'false' && rawValue !== '0' | ||
| } | ||
|
|
||
| /** | ||
| * Strips ANSI/VT escape sequences (CSI, OSC, and lone ESC) from text before | ||
| * it is returned in `pty_read` results or `<pty_exited>` notifications. | ||
| * | ||
| * The raw buffer is kept untouched so the Web UI / xterm.js stream still | ||
| * receives the original terminal output. | ||
| */ | ||
| export function sanitizeAnsi(text: string): string { | ||
| if (!isAnsiSanitizationEnabled()) { | ||
| return text | ||
| } | ||
|
|
||
| return Bun.stripANSI(text) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { describe, it, expect } from 'bun:test' | ||
| import { RingBuffer } from '../src/plugin/pty/buffer.ts' | ||
| import { OutputManager } from '../src/plugin/pty/output-manager.ts' | ||
| import type { PTYSession } from '../src/plugin/pty/types.ts' | ||
|
|
||
| function createSession(bufferContent: string): PTYSession { | ||
| const buffer = new RingBuffer() | ||
| buffer.append(bufferContent) | ||
| return { | ||
| id: 'pty_test', | ||
| title: 'Test Session', | ||
| command: 'echo', | ||
| args: ['hello'], | ||
| workdir: '/tmp', | ||
| status: 'running', | ||
| pid: 12345, | ||
| createdAt: new Date(), | ||
| parentSessionId: 'parent-session-id', | ||
| notifyOnExit: false, | ||
| timedOut: false, | ||
| buffer, | ||
| process: null, | ||
| } | ||
| } | ||
|
|
||
| describe('OutputManager', () => { | ||
| const manager = new OutputManager() | ||
|
|
||
| it('strips ANSI escape sequences from read output', () => { | ||
| const session = createSession('\x1b[31mred\x1b[0m\n\x1b[?9001h\x1b[2Jplain\n') | ||
|
|
||
| const result = manager.read(session) | ||
|
|
||
| expect(result.lines).toEqual(['red', 'plain']) | ||
| }) | ||
|
|
||
| it('strips ANSI escape sequences from search matches', () => { | ||
| const session = createSession('\x1b[31mred error\x1b[0m\n\x1b[32mgreen ok\x1b[0m\n') | ||
|
|
||
| const result = manager.search(session, /error/) | ||
|
|
||
| expect(result.matches).toHaveLength(1) | ||
| expect(result.matches[0]?.text).toBe('red error') | ||
| expect(result.matches[0]?.lineNumber).toBe(1) | ||
| }) | ||
|
|
||
| it('leaves plain text unchanged', () => { | ||
| const session = createSession('hello world\nno escapes\n') | ||
|
|
||
| const result = manager.read(session) | ||
|
|
||
| expect(result.lines).toEqual(['hello world', 'no escapes']) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { describe, it, expect } from 'bun:test' | ||
| import { sanitizeAnsi } from '../src/plugin/pty/sanitize.ts' | ||
|
|
||
| describe('sanitizeAnsi', () => { | ||
| it('strips CSI escape sequences', () => { | ||
| expect(sanitizeAnsi('\x1b[?9001h')).toBe('') | ||
| expect(sanitizeAnsi('\x1b[?1004h')).toBe('') | ||
| expect(sanitizeAnsi('\x1b[?25l')).toBe('') | ||
| expect(sanitizeAnsi('\x1b[2J')).toBe('') | ||
| expect(sanitizeAnsi('\x1b[31mred\x1b[0m')).toBe('red') | ||
| }) | ||
|
|
||
| it('strips OSC escape sequences', () => { | ||
| expect(sanitizeAnsi('\x1b]0;window title\x07')).toBe('') | ||
| expect(sanitizeAnsi('before\x1b]0;title\x07after')).toBe('beforeafter') | ||
| }) | ||
|
|
||
| it('strips a lone ESC character', () => { | ||
| expect(sanitizeAnsi('hello\x1b')).toBe('hello') | ||
| expect(sanitizeAnsi('hello\x1b\x1b')).toBe('hello') | ||
| }) | ||
|
|
||
| it('leaves plain text unchanged', () => { | ||
| expect(sanitizeAnsi('hello world')).toBe('hello world') | ||
| expect(sanitizeAnsi('line1\nline2')).toBe('line1\nline2') | ||
| expect(sanitizeAnsi('error: file not found')).toBe('error: file not found') | ||
| }) | ||
|
|
||
| it('handles mixed real-world terminal output', () => { | ||
| const raw = | ||
| '\x1b[?9001h\x1b[?1004h\x1b[?25l\x1b[2J\x1b]0;playwright\x07Running 1 test\n\x1b[32m✓ passed\x1b[0m' | ||
| expect(sanitizeAnsi(raw)).toBe('Running 1 test\n✓ passed') | ||
| }) | ||
|
|
||
| it('can be disabled via PTY_SANITIZE_OUTPUT=false', () => { | ||
| const original = process.env.PTY_SANITIZE_OUTPUT | ||
| process.env.PTY_SANITIZE_OUTPUT = 'false' | ||
| try { | ||
| expect(sanitizeAnsi('\x1b[31mred\x1b[0m')).toBe('\x1b[31mred\x1b[0m') | ||
| } finally { | ||
| process.env.PTY_SANITIZE_OUTPUT = original | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| describe('sanitizeOutput plugin option', () => { | ||
| it('plugin option overrides the env var', async () => { | ||
| const { setAnsiSanitizationOverride } = await import('../src/plugin/pty/sanitize.ts') | ||
| const original = process.env.PTY_SANITIZE_OUTPUT | ||
| process.env.PTY_SANITIZE_OUTPUT = 'true' | ||
| try { | ||
| setAnsiSanitizationOverride(false) | ||
| expect(sanitizeAnsi('\x1b[31mred\x1b[0m')).toBe('\x1b[31mred\x1b[0m') | ||
| setAnsiSanitizationOverride(true) | ||
| expect(sanitizeAnsi('\x1b[31mred\x1b[0m')).toBe('red') | ||
| } finally { | ||
| setAnsiSanitizationOverride(undefined) | ||
| process.env.PTY_SANITIZE_OUTPUT = original | ||
| } | ||
| }) | ||
|
|
||
| it('falls back to the env var when no option is set', async () => { | ||
| const { setAnsiSanitizationOverride } = await import('../src/plugin/pty/sanitize.ts') | ||
| const original = process.env.PTY_SANITIZE_OUTPUT | ||
| process.env.PTY_SANITIZE_OUTPUT = 'false' | ||
| try { | ||
| setAnsiSanitizationOverride(undefined) | ||
| expect(sanitizeAnsi('\x1b[31mred\x1b[0m')).toBe('\x1b[31mred\x1b[0m') | ||
| } finally { | ||
| process.env.PTY_SANITIZE_OUTPUT = original | ||
| } | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will that work on opencode v2? afaik opencode v2 is not using bun?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I re-checked this against the current upstream state and went through the plugin again more carefully. I'm closing this PR and will open a new one with a better-scoped fix.
The problem actually is
The plugin hands PTY output to the host as plain text in two places:
pty_readresults and the<pty_exited>notification. On Windows, ConPTY starts every session with mode, clear-screen and window-title sequences (ESC[?9001h ESC[?1004h ESC[?25l ESC[2J ESC[H ESC]0;C:\Program Files\PowerShell\7\pwsh.EXE BEL). For short commands, that preamble often ends up as the notification'sLast Line. The OpenCode TUI writes text content to the terminal verbatim, so the terminal executes these sequences. It isn't specific to V1 or to Bun:TextRenderableon both 0.4.5 (V1) and 0.5.17 (V2). An injectedESC[?7777hshows up raw inside the rendered frame.<pty_exited>messages contained escape sequences. In 21 of them, theLast Linewas nothing but the ConPTY preamble.On V2 / Bun question
You were right. V2 ships a Bun-compiled binary, but it also builds a Node SEA (
opencode2-node), and plugins run in-process. Under Node,Bun.stripANSIwould throw. The new PR doesn't add any Bun dependency.Changes in the new PR
node:utilstripVTControlCharacters, plus removal of OSC/DCS/APC strings and leftover C0/C1 control characters (e.g.\r, BEL). Tabs are kept. This works on both Bun and Node.pty_readand<pty_exited>. The raw buffer used by the Web UI is unchanged.pty_readwithpatternnow matches against the sanitized text. Previously a pattern could match bytes inside escape sequences, or fail to match the visible text.PTY_SANITIZE_OUTPUT=0escape hatch, following the existingPTY_*env vars.New PR is here