refactor: cleanup and document sanitization functions (#2589)
* refactor sanitization functions * reorder functions * cleanup * document sanitize functions
This commit is contained in:
1 parent
3bf2f25374
commit
9a60c5c89a
4 files changed
+25
-15
No files matched your search
@@ -953,7 +953,7 @@ func (nav *nav) preview(path string, win *win, mode string) {
|
|||||||
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] = sanitizeForDisplay(l)
|
lines[i] = sanitizePreview(l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
line = sanitizeForDisplay(line)
|
line = sanitizePreview(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)
|
||||||
|
|||||||
+23
-3
@@ -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
|
// isControlChar reports whether a rune is a control character or otherwise
|
||||||
// unsafe to display in a terminal.
|
// unsafe to display in a terminal.
|
||||||
// Covers C0 (0x00-0x1F), DEL (0x7F), and C1 (0x80-0x9F).
|
// 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
|
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
|
// Unicode replacement character (U+FFFD). Tabs are preserved for content
|
||||||
// where tab expansion is handled by the renderer (e.g. preview panes).
|
// 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 {
|
return strings.Map(func(r rune) rune {
|
||||||
if r == '\t' || !isControlChar(r) {
|
if r == '\t' || !isControlChar(r) {
|
||||||
return r
|
return r
|
||||||
@@ -297,7 +307,7 @@ func sanitizeForDisplay(s string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sanitizeName sanitizes a filename, path, or symlink target for display.
|
// 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
|
// are expanded by the renderer to tabstop width while displaywidth.String
|
||||||
// counts them as width 1, causing column overflow.
|
// counts them as width 1, causing column overflow.
|
||||||
func sanitizeName(s string) string {
|
func sanitizeName(s string) string {
|
||||||
@@ -333,3 +343,13 @@ func sanitizeMessage(s string) string {
|
|||||||
}
|
}
|
||||||
return b.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)
|
||||||
|
}
|
||||||
@@ -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
|
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.
|
// firstGrapheme returns the first grapheme cluster in s and its display width.
|
||||||
func firstGrapheme(s string) (string, int) {
|
func firstGrapheme(s string) (string, int) {
|
||||||
gr := displaywidth.StringGraphemes(s)
|
gr := displaywidth.StringGraphemes(s)
|
||||||
|
|||||||
Reference in new issue
Block a user