Files
openclaw/src/terminal/ansi.ts
2026-03-07 22:50:27 +00:00

28 lines
956 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const ANSI_SGR_PATTERN = "\\x1b\\[[0-9;]*m";
// OSC-8 hyperlinks: ESC ] 8 ; ; url ST ... ESC ] 8 ; ; ST
const OSC8_PATTERN = "\\x1b\\]8;;.*?\\x1b\\\\|\\x1b\\]8;;\\x1b\\\\";
const ANSI_REGEX = new RegExp(ANSI_SGR_PATTERN, "g");
const OSC8_REGEX = new RegExp(OSC8_PATTERN, "g");
export function stripAnsi(input: string): string {
return input.replace(OSC8_REGEX, "").replace(ANSI_REGEX, "");
}
/**
* Sanitize a value for safe interpolation into log messages.
* Strips ANSI escape sequences, C0 control characters (U+0000U+001F),
* and DEL (U+007F) to prevent log forging / terminal escape injection (CWE-117).
*/
export function sanitizeForLog(v: string): string {
let out = stripAnsi(v);
for (let c = 0; c <= 0x1f; c++) {
out = out.replaceAll(String.fromCharCode(c), "");
}
return out.replaceAll(String.fromCharCode(0x7f), "");
}
export function visibleWidth(input: string): number {
return Array.from(stripAnsi(input)).length;
}