@@ -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 */
171177export 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
183181function 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 ;
0 commit comments