sanitize input data (#2467)

* sanitize input data

* fix test

* relax filter on customInfo
This commit is contained in:
valoq authored and GitHub committed 2026-04-08 02:04:30 +02:00
1 parent 779bffb3dc
commit 7a3591c834
6 files changed
+61 -23

No files matched your search

+12 -4
View File
@@ -431,12 +431,12 @@ func getFileExtension(file fs.FileInfo) string {
// The file extension is not affected by truncation, however it will be clipped // The file extension is not affected by truncation, however it will be clipped
// if it exceeds the allowed width. // if it exceeds the allowed width.
func truncateFilename(file fs.FileInfo, maxWidth, truncatePct int, truncateChar string) string { func truncateFilename(file fs.FileInfo, maxWidth, truncatePct int, truncateChar string) string {
filename := file.Name() filename := sanitizeName(file.Name())
if uniseg.StringWidth(filename) <= maxWidth { if uniseg.StringWidth(filename) <= maxWidth {
return filename return filename
} }
ext := getFileExtension(file) ext := sanitizeName(getFileExtension(file))
avail := maxWidth - uniseg.StringWidth(truncateChar) - uniseg.StringWidth(ext) avail := maxWidth - uniseg.StringWidth(truncateChar) - uniseg.StringWidth(ext)
if avail < 0 { if avail < 0 {
return truncateRight(truncateChar+ext, maxWidth) return truncateRight(truncateChar+ext, maxWidth)
@@ -527,9 +527,17 @@ func readLines(reader io.ByteReader, maxLines int) (lines []string, binary bool,
currState = stateNormal currState = stateNormal
} }
case stateSixel: case stateSixel:
buf.WriteByte(b) // Accept printable bytes (0x20-0x7E) and ESC inside the
if b == '\033' { // DCS frame. Reject everything else (C0, DEL, 0x80+).
switch {
case b == '\033':
buf.WriteByte(b)
currState = stateSixelEsc currState = stateSixelEsc
case b >= 0x20 && b <= 0x7E:
buf.WriteByte(b)
default:
buf.Reset()
currState = stateNormal
} }
case stateSixelEsc: case stateSixelEsc:
buf.WriteByte(b) buf.WriteByte(b)
+3 -3
View File
@@ -529,9 +529,9 @@ func TestReadLines(t *testing.T) {
{"foo\r\n\000\r\nbar\r\n", 10, nil, true, false}, {"foo\r\n\000\r\nbar\r\n", 10, nil, true, false},
{"\033P\033\\", 10, []string{"\033P\033\\"}, false, true}, {"\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}, {"\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\000\033\\", 10, []string{"\033\\"}, false, false},
{"\033P\n\033\\", 10, []string{"\033P\n\033\\"}, false, true}, {"\033P\n\033\\", 10, []string{"\033\\"}, false, false},
{"\033P\r\n\033\\", 10, []string{"\033P\r\n\033\\"}, false, true}, {"\033P\r\n\033\\", 10, []string{"", "\033\\"}, false, false},
{"\033P\033\\\033P\033\\", 10, []string{"\033P\033\\", "\033P\033\\"}, false, true}, {"\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", 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}, {"foo\033P\033\\bar\033P\033\\baz", 10, []string{"foo", "\033P\033\\", "bar", "\033P\033\\", "baz"}, false, true},
+3 -2
View File
@@ -937,11 +937,12 @@ func (nav *nav) preview(path string, win *win, mode string) {
// The internal previewer reads raw file content which may contain // The internal previewer reads raw file content which may contain
// escape sequences that corrupt the display or enable code execution // 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 { if len(gOpts.previewer) == 0 && !binary {
sixel = false sixel = false
for i, l := range lines { for i, l := range lines {
lines[i] = stripAllSequences(l) lines[i] = sanitizeForDisplay(l)
} }
} }
+1
View File
@@ -42,6 +42,7 @@ func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) {
break break
} }
line = sanitizeForDisplay(line)
screen.LockRegion(win.x, y, printLength(line), 1, true) screen.LockRegion(win.x, y, printLength(line), 1, true)
fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1) fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1)
b.WriteString(line) b.WriteString(line)
+26 -6
View File
@@ -277,14 +277,34 @@ func applyOSC(body string, st tcell.Style) tcell.Style {
} }
} }
// stripAllSequences removes all control characters from a string, keeping // isControlChar reports whether a rune is a control character or otherwise
// only printable characters and tabs. This is used to // unsafe to display in a terminal.
// sanitize raw file content for the internal previewer. // Covers C0 (0x00-0x1F), DEL (0x7F), and C1 (0x80-0x9F).
func stripAllSequences(s string) string { 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 { return strings.Map(func(r rune) rune {
if r >= 0x20 && r != 0x7F || r == '\t' { if r == '\t' || !isControlChar(r) {
return 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) }, s)
} }
+16 -8
View File
@@ -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 win.w, win.h, win.x, win.y = w, h, x, y
} }
// isPrintable reports whether sequence is safe to display. // isPrintable reports whether a grapheme cluster is safe to display.
// It rejects C0 control characters (0x00-0x1F) and DEL (0x7F). // It rejects C0/C1 controls, DEL, and invalid UTF-8.
func isPrintable(gc string) bool { 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. // 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 length += gOpts.tabstop - length%gOpts.tabstop
} else if isPrintable(gc) { } else if isPrintable(gc) {
length += w 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)) b.WriteString(strings.Repeat(" ", w))
} else if isPrintable(gc) { } else if isPrintable(gc) {
b.WriteString(gc) b.WriteString(gc)
} else {
b.WriteString("\uFFFD")
} }
i += len(gc) i += len(gc)
@@ -648,7 +656,7 @@ func (ui *ui) drawPromptLine(nav *nav) {
var fname string var fname string
if curr := nav.currFile(); curr != nil { if curr := nav.currFile(); curr != nil {
fname = filepath.Base(curr.path) fname = sanitizeName(filepath.Base(curr.path))
} }
var prompt string var prompt string
@@ -746,7 +754,7 @@ func (ui *ui) drawStat(nav *nav) {
replace("%s", humanize(curr.Size())) replace("%s", humanize(curr.Size()))
replace("%S", fmt.Sprintf("%5s", humanize(curr.Size()))) replace("%S", fmt.Sprintf("%5s", humanize(curr.Size())))
replace("%t", curr.ModTime().Format(gOpts.timefmt)) replace("%t", curr.ModTime().Format(gOpts.timefmt))
replace("%l", curr.linkTarget) replace("%l", sanitizeName(curr.linkTarget))
var fileInfo strings.Builder var fileInfo strings.Builder
for section := range strings.SplitSeq(statfmt, "\x1f") { for section := range strings.SplitSeq(statfmt, "\x1f") {
@@ -882,8 +890,8 @@ func (ui *ui) drawRulerFile(nav *nav) {
if curr != nil { if curr != nil {
if curr.err == nil { if curr.err == nil {
stat = &statData{ stat = &statData{
Path: curr.path, Path: sanitizeName(curr.path),
Name: curr.Name(), Name: sanitizeName(curr.Name()),
Extension: curr.ext, Extension: curr.ext,
Size: curr.Size(), Size: curr.Size(),
DirSize: curr.dirSize, DirSize: curr.dirSize,
@@ -896,7 +904,7 @@ func (ui *ui) drawRulerFile(nav *nav) {
LinkCount: linkCount(curr), LinkCount: linkCount(curr),
User: userName(curr), User: userName(curr),
Group: groupName(curr), Group: groupName(curr),
Target: curr.linkTarget, Target: sanitizeName(curr.linkTarget),
CustomInfo: curr.customInfo, CustomInfo: curr.customInfo,
} }
} else { } else {