From 18ae1129c394f4a87406b7f0ed966c25137a457f Mon Sep 17 00:00:00 2001 From: valoq Date: Wed, 6 May 2026 07:28:06 +0200 Subject: [PATCH] fix: sanitize paths when writing to a terminal (#2540) * complete missing filename sanitation * sanitize only on stdout * improve patch --- client.go | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/client.go b/client.go index d9a388b4..cae82ea4 100644 --- a/client.go +++ b/client.go @@ -13,6 +13,7 @@ import ( "time" "github.com/gdamore/tcell/v3" + "golang.org/x/term" ) type State struct { @@ -76,26 +77,35 @@ func run() { writeSelection(gSelectionPath, app.selectionOut) } - if gPrintLastDir { - dir := app.nav.currDir().path - if strings.ContainsAny(dir, "\n\r") { - log.Printf("last-dir: path contains newline: %q", dir) - } else { - fmt.Println(dir) - } - } + if gPrintLastDir || gPrintSelection { + stdoutIsTerminal := term.IsTerminal(int(os.Stdout.Fd())) - if gPrintSelection && len(app.selectionOut) > 0 { - for _, file := range app.selectionOut { - if strings.ContainsAny(file, "\n\r") { - log.Printf("selection: skipping path with newline: %q", file) - } else { - fmt.Println(file) + if gPrintLastDir { + printPath("last-dir", app.nav.currDir().path, stdoutIsTerminal) + } + + if gPrintSelection { + for _, file := range app.selectionOut { + printPath("selection", file, stdoutIsTerminal) } } } } +// printPath prints path for -print-last-dir / -print-selection. Newlines are +// rejected unconditionally (frame integrity for line-oriented consumers); +// control bytes are stripped only when stdout is a terminal. +func printPath(label, path string, stdoutIsTerminal bool) { + if strings.ContainsAny(path, "\n\r") { + log.Printf("%s: skipping path with newline: %q", label, path) + return + } + if stdoutIsTerminal { + path = sanitizeName(path) + } + fmt.Println(path) +} + func writeLastDir(filename, lastDir string) { if strings.ContainsAny(lastDir, "\n\r") { log.Printf("last-dir: path contains newline: %q", lastDir)