28 lines
956 B
TypeScript
28 lines
956 B
TypeScript
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+0000–U+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;
|
||
}
|