Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
feat: update terminal
Sergey Kozyrenkof49d9cba428e
diff --git a/frontend/src/components/shared/terminal/terminal-sanitizer.test.ts b/frontend/src/components/shared/terminal/terminal-sanitizer.test.tsindex 47b1a82c..90ced26e 100644--- a/frontend/src/components/shared/terminal/terminal-sanitizer.test.ts+++ b/frontend/src/components/shared/terminal/terminal-sanitizer.test.ts@@ -117,52 +117,55 @@ describe('sanitizeTerminalOutput', () => {});});- describe('CSI non-SGR — blocked', () => {- it('strips ED: erase display', () => {- expect(hasEsc(processLog('a\x1b[2Jb'))).toBe(false);+ describe('CSI non-SGR — blocked with body consumed', () => {+ it('consumes ED: erase display', () => {+ expect(processLog('a\x1b[2Jb')).toBe('a.b');});- it('strips ED: erase scrollback', () => {- expect(hasEsc(processLog('a\x1b[3Jb'))).toBe(false);+ it('consumes ED: erase scrollback', () => {+ expect(processLog('a\x1b[3Jb')).toBe('a.b');});- it('strips CUP: cursor home', () => {- expect(hasEsc(processLog('a\x1b[Hb'))).toBe(false);+ it('consumes CUP: cursor home', () => {+ expect(processLog('a\x1b[Hb')).toBe('a.b');});- it('strips SU: scroll up', () => {- expect(hasEsc(processLog('a\x1b[999Sb'))).toBe(false);+ it('consumes SU: scroll up', () => {+ expect(processLog('a\x1b[999Sb')).toBe('a.b');});- it('strips SD: scroll down', () => {- expect(hasEsc(processLog('a\x1b[999Tb'))).toBe(false);+ it('consumes SD: scroll down', () => {+ expect(processLog('a\x1b[999Tb')).toBe('a.b');});- it('strips DECSET: alternate buffer', () => {- expect(hasEsc(processLog('a\x1b[?1049hb'))).toBe(false);+ it('consumes DECSET: alternate buffer', () => {+ expect(processLog('a\x1b[?1049hb')).toBe('a.b');});- it('strips DECSET: hide cursor', () => {- expect(hasEsc(processLog('a\x1b[?25lb'))).toBe(false);+ it('consumes DECSET: hide cursor', () => {+ expect(processLog('a\x1b[?25lb')).toBe('a.b');});- it('strips DL: delete lines', () => {- expect(hasEsc(processLog('a\x1b[10Mb'))).toBe(false);+ it('consumes DL: delete lines', () => {+ expect(processLog('a\x1b[10Mb')).toBe('a.b');});- it('strips IL: insert lines', () => {- expect(hasEsc(processLog('a\x1b[10Lb'))).toBe(false);+ it('consumes IL: insert lines', () => {+ expect(processLog('a\x1b[10Lb')).toBe('a.b');});- it('strips ICH: insert characters', () => {- expect(hasEsc(processLog('a\x1b[10@b'))).toBe(false);+ it('consumes ICH: insert characters', () => {+ expect(processLog('a\x1b[10@b')).toBe('a.b');});- it('preserves surrounding text when stripping CSI', () => {- const out = processLog('before\x1b[2Jafter');+ it('preserves surrounding text when consuming CSI', () => {+ expect(processLog('before\x1b[2Jafter')).toBe('before.after');+ });++ it('does not consume invalid CSI (no final byte)', () => {+ const out = processLog('\x1b[');- expect(out).toContain('before');- expect(out).toContain('after');+ expect(out).not.toContain('\x1b');});});@@ -235,79 +238,85 @@ describe('sanitizeTerminalOutput', () => {});describe('OSC 8 hyperlinks — dangerous protocols blocked', () => {- it('strips javascript: URI', () => {+ it('consumes javascript: URI with body', () => {const input = '\x1b]8;;javascript:alert(1)\x07Click\x1b]8;;\x07';const out = processLog(input);- expect(out).not.toContain('\x1b]8;;javascript:');+ expect(out).not.toContain('javascript:');+ expect(out).toContain('Click');+ expect(out).toContain('\x1b]8;;\x07');});- it('strips data: URI', () => {+ it('consumes data: URI with body', () => {const input = '\x1b]8;;data:text/html,<h1>XSS</h1>\x07Click\x1b]8;;\x07';const out = processLog(input);- expect(out).not.toContain('\x1b]8;;data:');+ expect(out).not.toContain('data:');});- it('strips ftp: URI', () => {+ it('consumes ftp: URI with body', () => {const input = '\x1b]8;;ftp://evil.com/shell\x07Click\x1b]8;;\x07';const out = processLog(input);- expect(out).not.toContain('\x1b]8;;ftp:');+ expect(out).not.toContain('ftp:');});- it('strips file: URI', () => {+ it('consumes file: URI with body', () => {const input = '\x1b]8;;file:///etc/passwd\x07Click\x1b]8;;\x07';const out = processLog(input);- expect(out).not.toContain('\x1b]8;;file:');+ expect(out).not.toContain('file:');});- it('preserves safe close tag even when open tag is stripped', () => {+ it('preserves safe close tag even when open tag is consumed', () => {const input = '\x1b]8;;javascript:x\x07Click\x1b]8;;\x07';const out = processLog(input);expect(out).toContain('\x1b]8;;\x07');});- it('strips OSC 8 with missing second semicolon', () => {+ it('consumes OSC 8 with missing second semicolon', () => {const input = '\x1b]8;https://example.com\x07Click\x1b]8;;\x07';const out = processLog(input);- expect(out).not.toContain('\x1b]8;https:');+ expect(out).not.toContain('8;https:');});- it('strips OSC 8 with unknown protocol', () => {+ it('consumes OSC 8 with unknown protocol', () => {const input = '\x1b]8;;custom://something\x07Click\x1b]8;;\x07';const out = processLog(input);- expect(out).not.toContain('\x1b]8;;custom:');+ expect(out).not.toContain('custom:');});});- describe('OSC non-hyperlink — blocked', () => {- it('strips OSC 0: window title', () => {- expect(hasEsc(processLog('\x1b]0;HACKED\x07'))).toBe(false);+ describe('OSC non-hyperlink — blocked with body consumed', () => {+ it('consumes OSC 0: window title', () => {+ expect(processLog('\x1b]0;HACKED\x07')).toBe('.');+ });++ it('consumes OSC 2: window title', () => {+ expect(processLog('\x1b]2;HACKED\x07')).toBe('.');});- it('strips OSC 2: window title', () => {- expect(hasEsc(processLog('\x1b]2;HACKED\x07'))).toBe(false);+ it('consumes OSC 10: foreground color', () => {+ expect(processLog('\x1b]10;#ff0000\x07')).toBe('.');});- it('strips OSC 10: foreground color', () => {- expect(hasEsc(processLog('\x1b]10;#ff0000\x07'))).toBe(false);+ it('consumes OSC 11: background color', () => {+ expect(processLog('\x1b]11;#00ff00\x07')).toBe('.');});- it('strips OSC 11: background color', () => {- expect(hasEsc(processLog('\x1b]11;#00ff00\x07'))).toBe(false);+ it('consumes OSC 12: cursor color', () => {+ expect(processLog('\x1b]12;#0000ff\x07')).toBe('.');});- it('strips OSC 12: cursor color', () => {- expect(hasEsc(processLog('\x1b]12;#0000ff\x07'))).toBe(false);+ it('consumes OSC 4: palette color', () => {+ expect(processLog('\x1b]4;1;#ff0000\x07')).toBe('.');});- it('strips OSC 4: palette color', () => {- expect(hasEsc(processLog('\x1b]4;1;#ff0000\x07'))).toBe(false);+ it('preserves surrounding text when consuming OSC', () => {+ expect(processLog('before\x1b]0;TITLE\x07after')).toBe('before.after');});});@@ -596,6 +605,103 @@ describe('sanitizeTerminalOutput', () => {});});+ describe('bidi override characters — stripped', () => {+ it('strips RTL override U+202E', () => {+ expect(processLog('hello\u202Eworld')).toBe('hello.world');+ });++ it('strips LTR mark U+200E', () => {+ expect(processLog('a\u200Eb')).toBe('a.b');+ });++ it('strips RTL mark U+200F', () => {+ expect(processLog('a\u200Fb')).toBe('a.b');+ });++ it('strips LTR embedding U+202A', () => {+ expect(processLog('a\u202Ab')).toBe('a.b');+ });++ it('strips first-strong isolate U+2068', () => {+ expect(processLog('a\u2068b')).toBe('a.b');+ });++ it('strips pop directional isolate U+2069', () => {+ expect(processLog('a\u2069b')).toBe('a.b');+ });++ it('strips LTR isolate U+2066', () => {+ expect(processLog('a\u2066b')).toBe('a.b');+ });++ it('strips multiple bidi controls in sequence', () => {+ expect(processLog('a\u202E\u202D\u200Fb')).toBe('a...b');+ });++ it('preserves normal Unicode alongside bidi stripping', () => {+ expect(processLog('Привет\u202Eмир')).toBe('Привет.мир');+ });++ it('preserves SGR alongside bidi stripping', () => {+ const out = processLog('\x1b[31m\u202Etext\x1b[0m');++ expect(out).toBe('\x1b[31m.text\x1b[0m');+ });+ });++ describe('needsSanitization — bidi detection', () => {+ it('returns true for RTL override U+202E', () => {+ expect(needsSanitization('hello\u202Eworld')).toBe(true);+ });++ it('returns true for LTR mark U+200E', () => {+ expect(needsSanitization('text\u200E')).toBe(true);+ });++ it('returns true for RTL mark U+200F', () => {+ expect(needsSanitization('text\u200F')).toBe(true);+ });++ it('returns true for isolate U+2066', () => {+ expect(needsSanitization('text\u2066')).toBe(true);+ });++ it('returns false for non-bidi Unicode above 0xA0', () => {+ expect(needsSanitization('\u2010\u2014\u2026')).toBe(false);+ });+ });++ describe('OSC 8 with Unicode URI', () => {+ it('preserves OSC 8 link with Cyrillic path', () => {+ const input = '\x1b]8;;https://example.com/путь\x07Click\x1b]8;;\x07';+ const out = processLog(input);++ expect(out).toContain('https://example.com/путь');+ expect(out).toContain('\x1b]8;;https://example.com/путь\x07');+ });++ it('preserves OSC 8 link with CJK path', () => {+ const input = '\x1b]8;;https://example.com/文档\x07Link\x1b]8;;\x07';+ const out = processLog(input);++ expect(out).toContain('https://example.com/文档');+ });++ it('rejects OSC with C1 control in content', () => {+ const input = '\x1b]8;;\x90https://x\x07Click\x1b]8;;\x07';+ const out = processLog(input);++ expect(out).not.toContain('\x1b]8;;\x90');+ });++ it('rejects OSC with DEL in content', () => {+ const input = '\x1b]8;;\x7fhttps://x\x07Click\x1b]8;;\x07';+ const out = processLog(input);++ expect(out).not.toContain('\x1b]8;;\x7f');+ });+ });+describe('performance', () => {it('processes 1M chars in under 500ms', () => {const input = '\x1b[31m' + 'A'.repeat(100_000) + '\x1b[0m';diff --git a/frontend/src/components/shared/terminal/terminal-sanitizer.ts b/frontend/src/components/shared/terminal/terminal-sanitizer.tsindex 5922090b..f7c965d9 100644--- a/frontend/src/components/shared/terminal/terminal-sanitizer.ts+++ b/frontend/src/components/shared/terminal/terminal-sanitizer.ts@@ -4,7 +4,7 @@ const MAX_CSI_LENGTH = 256;const MAX_OSC_LENGTH = 2048;const MAX_STRING_SEQUENCE_LENGTH = 100_000;-const SAFE_OSC8_PROTOCOLS = ['http://', 'https://', 'mailto:', 'ssh://', 'telnet://'];+export const SAFE_PROTOCOLS = ['http://', 'https://', 'mailto:', 'ssh://', 'telnet://'];/*** Quick scan to determine if a string needs full sanitization.@@ -45,6 +45,10 @@ export function needsSanitization(input: string): boolean {return true;}+ if (isBidiControl(code)) {+ return true;+ }+continue;}@@ -102,7 +106,7 @@ export function sanitizeTerminalOutput(input: string): string {continue;}- // Valid Unicode (>= 0xA0, excluding lone surrogates and U+FFFD)+ // Valid Unicode (>= 0xA0, excluding lone surrogates, U+FFFD, and bidi controls)if (code >= 0xa0) {// Valid surrogate pair (emoji, supplementary chars) — skip both unitsif (code >= 0xd800 && code <= 0xdbff) {@@ -117,13 +121,13 @@ export function sanitizeTerminalOutput(input: string): string {}// Lone/invalid surrogate — falls through to replacement below- } else if (code !== 0xfffd && !(code >= 0xdc00 && code <= 0xdfff)) {+ } else if (code !== 0xfffd && !(code >= 0xdc00 && code <= 0xdfff) && !isBidiControl(code)) {i++;continue;}- // Lone surrogates and U+FFFD fall through to replacement below+ // Lone surrogates, U+FFFD, and bidi controls fall through to replacement below}// We hit a character that needs handling — flush the safe range@@ -155,6 +159,10 @@ export function sanitizeTerminalOutput(input: string): string {continue;}+ if (parsed.end !== -1) {+ i = parsed.end;+ }+result.push('.');safeStart = i;@@ -176,6 +184,8 @@ export function sanitizeTerminalOutput(input: string): string {continue;}++ i = end;}result.push('.');@@ -214,6 +224,12 @@ export function sanitizeTerminalOutput(input: string): string {return result.join('');}+function isBidiControl(code: number): boolean {+ return (+ (code >= 0x200e && code <= 0x200f) || (code >= 0x202a && code <= 0x202e) || (code >= 0x2066 && code <= 0x2069)+ );+}+/*** Heuristic detection of binary content in a JS string.* Binary data (e.g. JPEG) interpreted as UTF-16 produces null bytes,@@ -301,7 +317,7 @@ function isOscSafe(content: string): boolean {// Validate protocol against whitelist (blocks javascript:, data:, ftp:, file:, etc.)const uriLower = uri.toLowerCase();- return SAFE_OSC8_PROTOCOLS.some((protocol) => uriLower.startsWith(protocol));+ return SAFE_PROTOCOLS.some((protocol) => uriLower.startsWith(protocol));}/**@@ -313,10 +329,7 @@ function isOscSafe(content: string): boolean {* - Intermediate bytes: 0x20-0x2F (space, !, ", etc.)* - Final byte: 0x40-0x7E (letters — 'm' for SGR, 'H' for CUP, etc.)*/-function parseCsiSequence(- input: string,- start: number,-): { end: number; final: string } {+function parseCsiSequence(input: string, start: number): { end: number; final: string } {let i = start;while (i < input.length && i - start < MAX_CSI_LENGTH) {@@ -341,7 +354,9 @@ function parseCsiSequence(* Returns end index (after terminator), or -1 if invalid.** OSC format: ESC ] content BEL or ESC ] content ESC \- * Only ASCII printables and BEL are allowed in content per xterm.js parser.+ * Allows ASCII printables (0x20-0x7E) and Unicode >= 0xA0 per xterm.js parser+ * (which accepts "any codepoint greater than C1 as printable").+ * Blocks C0 controls (except BEL as terminator), DEL, and C1 controls (0x80-0x9F).*/function parseOscSequence(input: string, start: number): number {let i = start;@@ -357,7 +372,11 @@ function parseOscSequence(input: string, start: number): number {return i + 2;}- if (code > 0x7e || (code < 0x20 && code !== 0x07)) {+ if (code < 0x20 && code !== 0x07) {+ return -1;+ }++ if (code === 0x7f || (code >= 0x80 && code <= 0x9f)) {return -1;}diff --git a/frontend/src/components/shared/terminal/use-xterm.ts b/frontend/src/components/shared/terminal/use-xterm.tsindex 3dec839b..5153a97f 100644--- a/frontend/src/components/shared/terminal/use-xterm.ts+++ b/frontend/src/components/shared/terminal/use-xterm.ts@@ -13,10 +13,11 @@ import { Log } from '@/lib/log';import { isMac } from '@/lib/utils/platform';import { getTerminalTheme, isDarkMode, TERMINAL_OPTIONS } from './terminal-config';+import { SAFE_PROTOCOLS } from './terminal-sanitizer';const FLOW_CONTROL_CHUNK_SIZE = 64 * 1024;const DEBOUNCE_DELAY = 150;-const TOOLTIP_CLASS = 'xterm-link-tooltip';+const TOOLTIP_CLASS = 'terminal-link-tooltip';export interface UseXtermResult {clear: () => void;@@ -99,7 +100,12 @@ export function useXterm({ theme }: { theme: 'dark' | 'light' | 'system' }): Useconst mac = isMac();const openLink = (event: MouseEvent, uri: string) => {- if (mac ? event.metaKey : event.ctrlKey) {+ const uriLower = uri.toLowerCase();++ if (+ (mac ? event.metaKey : event.ctrlKey) &&+ SAFE_PROTOCOLS.some((p) => uriLower.startsWith(p))+ ) {window.open(uri, '_blank', 'noopener,noreferrer');}};@@ -302,6 +308,11 @@ function showLinkTooltip(container: HTMLElement, event: MouseEvent, uri: string,* Writes data to the terminal with chunked flow control.* For large payloads, splits into chunks and uses xterm's write callback* to avoid overwhelming the parser and blocking the UI thread.+ *+ * Chunk boundaries are adjusted to avoid splitting UTF-16 surrogate pairs:+ * if the last code unit of a chunk is a high surrogate (0xD800-0xDBFF),+ * the boundary is moved back by one so the pair stays intact.+ ** See: https://xtermjs.org/docs/guides/flowcontrol/*/function writeWithFlowControl(terminal: Terminal, data: string): void {@@ -314,8 +325,18 @@ function writeWithFlowControl(terminal: Terminal, data: string): void {let offset = 0;const writeNext = () => {- const chunk = data.slice(offset, offset + FLOW_CONTROL_CHUNK_SIZE);- offset += FLOW_CONTROL_CHUNK_SIZE;+ let end = Math.min(offset + FLOW_CONTROL_CHUNK_SIZE, data.length);++ if (end < data.length) {+ const code = data.charCodeAt(end - 1);++ if (code >= 0xd800 && code <= 0xdbff) {+ end--;+ }+ }++ const chunk = data.slice(offset, end);+ offset = end;if (offset < data.length) {terminal.write(chunk, writeNext);