From 7a3591c834dc6f7bea78c1d776cb53132c3e1b8e Mon Sep 17 00:00:00 2001 From: valoq Date: Wed, 8 Apr 2026 02:04:30 +0200 Subject: [PATCH] sanitize input data (#2467) * sanitize input data * fix test * relax filter on customInfo --- misc.go | 16 ++++++++++++---- misc_test.go | 6 +++--- nav.go | 5 +++-- sixel.go | 1 + termseq.go | 32 ++++++++++++++++++++++++++------ ui.go | 24 ++++++++++++++++-------- 6 files changed, 61 insertions(+), 23 deletions(-) diff --git a/misc.go b/misc.go index b8c14eee..7b1535e0 100644 --- a/misc.go +++ b/misc.go @@ -431,12 +431,12 @@ func getFileExtension(file fs.FileInfo) string { // The file extension is not affected by truncation, however it will be clipped // if it exceeds the allowed width. func truncateFilename(file fs.FileInfo, maxWidth, truncatePct int, truncateChar string) string { - filename := file.Name() + filename := sanitizeName(file.Name()) if uniseg.StringWidth(filename) <= maxWidth { return filename } - ext := getFileExtension(file) + ext := sanitizeName(getFileExtension(file)) avail := maxWidth - uniseg.StringWidth(truncateChar) - uniseg.StringWidth(ext) if avail < 0 { return truncateRight(truncateChar+ext, maxWidth) @@ -527,9 +527,17 @@ func readLines(reader io.ByteReader, maxLines int) (lines []string, binary bool, currState = stateNormal } case stateSixel: - buf.WriteByte(b) - if b == '\033' { + // Accept printable bytes (0x20-0x7E) and ESC inside the + // DCS frame. Reject everything else (C0, DEL, 0x80+). + switch { + case b == '\033': + buf.WriteByte(b) currState = stateSixelEsc + case b >= 0x20 && b <= 0x7E: + buf.WriteByte(b) + default: + buf.Reset() + currState = stateNormal } case stateSixelEsc: buf.WriteByte(b) diff --git a/misc_test.go b/misc_test.go index 27628da6..13793127 100644 --- a/misc_test.go +++ b/misc_test.go @@ -529,9 +529,9 @@ func TestReadLines(t *testing.T) { {"foo\r\n\000\r\nbar\r\n", 10, nil, true, false}, {"\033P\033\\", 10, []string{"\033P\033\\"}, false, true}, {"\033Pq\"1;1;1;1#0@\033\\", 10, []string{"\033Pq\"1;1;1;1#0@\033\\"}, false, true}, - {"\033P\000\033\\", 10, []string{"\033P\000\033\\"}, false, true}, - {"\033P\n\033\\", 10, []string{"\033P\n\033\\"}, false, true}, - {"\033P\r\n\033\\", 10, []string{"\033P\r\n\033\\"}, false, true}, + {"\033P\000\033\\", 10, []string{"\033\\"}, false, false}, + {"\033P\n\033\\", 10, []string{"\033\\"}, false, false}, + {"\033P\r\n\033\\", 10, []string{"", "\033\\"}, false, false}, {"\033P\033\\\033P\033\\", 10, []string{"\033P\033\\", "\033P\033\\"}, false, true}, {"foo\033P\033\\bar", 10, []string{"foo", "\033P\033\\", "bar"}, false, true}, {"foo\033P\033\\bar\033P\033\\baz", 10, []string{"foo", "\033P\033\\", "bar", "\033P\033\\", "baz"}, false, true}, diff --git a/nav.go b/nav.go index 2074029a..7664ca46 100644 --- a/nav.go +++ b/nav.go @@ -937,11 +937,12 @@ func (nav *nav) preview(path string, win *win, mode string) { // The internal previewer reads raw file content which may contain // escape sequences that corrupt the display or enable code execution - // (e.g. OSC 52 clipboard writes). Strip all control characters. + // (e.g. OSC 52 clipboard writes). Replace control characters with + // U+FFFD so they are visible but cannot form escape sequences. if len(gOpts.previewer) == 0 && !binary { sixel = false for i, l := range lines { - lines[i] = stripAllSequences(l) + lines[i] = sanitizeForDisplay(l) } } diff --git a/sixel.go b/sixel.go index 5ef5c18e..8ae5b48e 100644 --- a/sixel.go +++ b/sixel.go @@ -42,6 +42,7 @@ func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) { break } + line = sanitizeForDisplay(line) screen.LockRegion(win.x, y, printLength(line), 1, true) fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1) b.WriteString(line) diff --git a/termseq.go b/termseq.go index 26d9fde1..bcd924b7 100644 --- a/termseq.go +++ b/termseq.go @@ -277,14 +277,34 @@ func applyOSC(body string, st tcell.Style) tcell.Style { } } -// stripAllSequences removes all control characters from a string, keeping -// only printable characters and tabs. This is used to -// sanitize raw file content for the internal previewer. -func stripAllSequences(s string) string { +// isControlChar reports whether a rune is a control character or otherwise +// unsafe to display in a terminal. +// Covers C0 (0x00-0x1F), DEL (0x7F), and C1 (0x80-0x9F). +func isControlChar(r rune) bool { + return r < 0x20 || r == 0x7F || r >= 0x80 && r <= 0x9F +} + +// sanitizeForDisplay replaces control characters and invalid bytes with the +// Unicode replacement character (U+FFFD). Tabs are preserved for content +// where tab expansion is handled by the renderer (e.g. preview panes). +func sanitizeForDisplay(s string) string { return strings.Map(func(r rune) rune { - if r >= 0x20 && r != 0x7F || r == '\t' { + if r == '\t' || !isControlChar(r) { return r } - return -1 + return '\uFFFD' + }, s) +} + +// sanitizeName sanitizes a filename, path, or symlink target for display. +// Unlike sanitizeForDisplay it also replaces tabs, because tabs in names +// are expanded by the renderer to tabstop width while uniseg.StringWidth +// counts them as width 1, causing column overflow. +func sanitizeName(s string) string { + return strings.Map(func(r rune) rune { + if !isControlChar(r) { + return r + } + return '\uFFFD' }, s) } diff --git a/ui.go b/ui.go index 770d5001..440bed83 100644 --- a/ui.go +++ b/ui.go @@ -36,10 +36,14 @@ func (win *win) renew(w, h, x, y int) { win.w, win.h, win.x, win.y = w, h, x, y } -// isPrintable reports whether sequence is safe to display. -// It rejects C0 control characters (0x00-0x1F) and DEL (0x7F). +// isPrintable reports whether a grapheme cluster is safe to display. +// It rejects C0/C1 controls, DEL, and invalid UTF-8. func isPrintable(gc string) bool { - return gc[0] >= 0x20 && gc[0] != 0x7F + r, size := utf8.DecodeRuneInString(gc) + if r == utf8.RuneError && size <= 1 { + return false + } + return !isControlChar(r) } // printLength returns the display width of s in terminal cells. @@ -64,6 +68,8 @@ func printLength(s string) int { length += gOpts.tabstop - length%gOpts.tabstop } else if isPrintable(gc) { length += w + } else { + length++ // U+FFFD replacement has width 1 } } @@ -98,6 +104,8 @@ func (win *win) print(screen tcell.Screen, x, y int, st tcell.Style, s string) t b.WriteString(strings.Repeat(" ", w)) } else if isPrintable(gc) { b.WriteString(gc) + } else { + b.WriteString("\uFFFD") } i += len(gc) @@ -648,7 +656,7 @@ func (ui *ui) drawPromptLine(nav *nav) { var fname string if curr := nav.currFile(); curr != nil { - fname = filepath.Base(curr.path) + fname = sanitizeName(filepath.Base(curr.path)) } var prompt string @@ -746,7 +754,7 @@ func (ui *ui) drawStat(nav *nav) { replace("%s", humanize(curr.Size())) replace("%S", fmt.Sprintf("%5s", humanize(curr.Size()))) replace("%t", curr.ModTime().Format(gOpts.timefmt)) - replace("%l", curr.linkTarget) + replace("%l", sanitizeName(curr.linkTarget)) var fileInfo strings.Builder for section := range strings.SplitSeq(statfmt, "\x1f") { @@ -882,8 +890,8 @@ func (ui *ui) drawRulerFile(nav *nav) { if curr != nil { if curr.err == nil { stat = &statData{ - Path: curr.path, - Name: curr.Name(), + Path: sanitizeName(curr.path), + Name: sanitizeName(curr.Name()), Extension: curr.ext, Size: curr.Size(), DirSize: curr.dirSize, @@ -896,7 +904,7 @@ func (ui *ui) drawRulerFile(nav *nav) { LinkCount: linkCount(curr), User: userName(curr), Group: groupName(curr), - Target: curr.linkTarget, + Target: sanitizeName(curr.linkTarget), CustomInfo: curr.customInfo, } } else {