sanitize input data (#2467)
* sanitize input data * fix test * relax filter on customInfo
This commit is contained in:
1 parent
779bffb3dc
commit
7a3591c834
6 files changed
+61
-23
No files matched your search
@@ -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)
|
||||
|
||||
+3
-3
@@ -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},
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
+26
-6
@@ -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)
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user