From 9a60c5c89a944a1772e399fec300e1676314510d Mon Sep 17 00:00:00 2001 From: valoq Date: Mon, 25 May 2026 04:51:29 +0200 Subject: [PATCH] refactor: cleanup and document sanitization functions (#2589) * refactor sanitization functions * reorder functions * cleanup * document sanitize functions --- nav.go | 2 +- sixel.go | 2 +- termseq.go | 26 +++++++++++++++++++++++--- ui.go | 10 ---------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/nav.go b/nav.go index 0e377783..b5168fc9 100644 --- a/nav.go +++ b/nav.go @@ -953,7 +953,7 @@ func (nav *nav) preview(path string, win *win, mode string) { if len(gOpts.previewer) == 0 && !binary { sixel = false for i, l := range lines { - lines[i] = sanitizeForDisplay(l) + lines[i] = sanitizePreview(l) } } diff --git a/sixel.go b/sixel.go index 8ae5b48e..f8bedc84 100644 --- a/sixel.go +++ b/sixel.go @@ -42,7 +42,7 @@ func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) { break } - line = sanitizeForDisplay(line) + line = sanitizePreview(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 9cfb9ce0..19803b00 100644 --- a/termseq.go +++ b/termseq.go @@ -277,6 +277,16 @@ func applyOSC(body string, st tcell.Style) tcell.Style { } } +// Sanitation helpers for untrusted text (filenames, previews, messages). +// Pick one of these when handling untrusted input: +// +// sanitizePreview - replace control chars, keep tabs (preview content). +// sanitizeName - replace control chars and tabs (names in width/column slots). +// sanitizeMessage - replace control chars, keep lf's own SGR/OSC8 (messages). +// +// isControlChar and isPrintable are internal predicates used by the above +// and the renderer; they are not sanitation entry points. + // 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). @@ -284,10 +294,10 @@ func isControlChar(r rune) bool { return r < 0x20 || r == 0x7F || r >= 0x80 && r <= 0x9F } -// sanitizeForDisplay replaces control characters and invalid bytes with the +// sanitizePreview 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 { +func sanitizePreview(s string) string { return strings.Map(func(r rune) rune { if r == '\t' || !isControlChar(r) { return r @@ -297,7 +307,7 @@ func sanitizeForDisplay(s string) string { } // sanitizeName sanitizes a filename, path, or symlink target for display. -// Unlike sanitizeForDisplay it also replaces tabs, because tabs in names +// Unlike sanitizePreview it also replaces tabs, because tabs in names // are expanded by the renderer to tabstop width while displaywidth.String // counts them as width 1, causing column overflow. func sanitizeName(s string) string { @@ -333,3 +343,13 @@ func sanitizeMessage(s string) string { } return b.String() } + +// 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 { + r, size := utf8.DecodeRuneInString(gc) + if r == utf8.RuneError && size <= 1 { + return false + } + return !isControlChar(r) +} diff --git a/ui.go b/ui.go index a6b71401..dba09fe7 100644 --- a/ui.go +++ b/ui.go @@ -36,16 +36,6 @@ func (win *win) renew(w, h, x, y int) { win.w, win.h, win.x, win.y = w, h, x, y } -// 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 { - r, size := utf8.DecodeRuneInString(gc) - if r == utf8.RuneError && size <= 1 { - return false - } - return !isControlChar(r) -} - // firstGrapheme returns the first grapheme cluster in s and its display width. func firstGrapheme(s string) (string, int) { gr := displaywidth.StringGraphemes(s)