Skip to content

Commit 343fe90

Browse files
authored
perf(terminal): reuse control scans and remove repeated sanitization work (#133527)
1 parent 9830acc commit 343fe90

2 files changed

Lines changed: 13 additions & 34 deletions

File tree

‎packages/terminal-core/src/ansi.ts‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,19 @@ export function splitGraphemes(input: string): string[] {
163163
}
164164
}
165165

166+
// Construct once without embedding literal controls; DEL and C1 form one range.
167+
const LOG_CONTROL_CHARS_REGEX = new RegExp(
168+
`[${String.fromCharCode(0x00)}-${String.fromCharCode(0x1f)}${String.fromCharCode(0x7f)}-${String.fromCharCode(0x9f)}]`,
169+
"g",
170+
);
171+
166172
/**
167173
* Sanitize a value for safe interpolation into log messages.
168174
* Strips ANSI escape sequences, C0/C1 control characters, and DEL to
169175
* prevent log forging / terminal escape injection (CWE-117).
170176
*/
171177
export function sanitizeForLog(v: string): string {
172-
// Pattern built at runtime so the source file stays free of literal control
173-
// characters AND the linter cannot statically detect them (no-control-regex).
174-
const c0Start = String.fromCharCode(0x00);
175-
const c0End = String.fromCharCode(0x1f);
176-
const del = String.fromCharCode(0x7f);
177-
const c1Start = String.fromCharCode(0x80);
178-
const c1End = String.fromCharCode(0x9f);
179-
const controlCharsRegex = new RegExp(`[${c0Start}-${c0End}${del}${c1Start}-${c1End}]`, "g");
180-
return stripAnsi(v).replace(controlCharsRegex, "");
178+
return stripAnsi(v).replace(LOG_CONTROL_CHARS_REGEX, "");
181179
}
182180

183181
function textWidth(text: string): number {
@@ -316,10 +314,7 @@ export function truncateToVisibleWidth(input: string, maxWidth: number): string
316314
out += segment.value;
317315
used += controlWidth;
318316
} else if (controlWidth > 0) {
319-
out += widthControls.reduce(
320-
(value, control) => value.replaceAll(control, ""),
321-
segment.value,
322-
);
317+
out += segment.value.replaceAll("\t", "");
323318
budgetSpent = true;
324319
} else {
325320
out += segment.value;

‎packages/terminal-core/src/safe-text.ts‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,15 @@ import { stripAnsi } from "./ansi.js";
33

44
/** Return whether text contains C0 or C1 terminal control characters. */
55
export function hasTerminalControl(input: string): boolean {
6-
for (const char of input) {
7-
const codePoint = char.codePointAt(0);
8-
if (
9-
codePoint !== undefined &&
10-
(codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f))
11-
) {
12-
return true;
13-
}
14-
}
15-
return false;
6+
return input.search(/\p{Cc}/u) !== -1;
167
}
178

189
/**
1910
* Normalize untrusted text for single-line terminal/log rendering.
2011
*/
2112
export function sanitizeTerminalText(input: string): string {
22-
const normalized = stripAnsi(input)
23-
.replace(/\r/g, "\\r")
24-
.replace(/\n/g, "\\n")
25-
.replace(/\t/g, "\\t");
26-
let sanitized = "";
27-
for (const char of normalized) {
28-
if (!hasTerminalControl(char)) {
29-
sanitized += char;
30-
}
31-
}
32-
return sanitized;
13+
// Strip escapes first so removed bytes cannot become a new ANSI sequence.
14+
return stripAnsi(input).replace(/\p{Cc}/gu, (control) =>
15+
control === "\r" ? "\\r" : control === "\n" ? "\\n" : control === "\t" ? "\\t" : "",
16+
);
3317
}

0 commit comments

Comments
 (0)