From 452e073836247cd5464b85cc95a867a64eaa3463 Mon Sep 17 00:00:00 2001 From: valoq Date: Sat, 25 Apr 2026 16:07:02 +0200 Subject: [PATCH] fix!: change fallback socket location to a user-specific directory (#2532) * fix: enforce correct owner of socket * improve socket handling * simplify socket name --- doc.md | 2 +- doc.txt | 2 +- lf.1 | 2 +- os.go | 18 ++++++++++++++++-- os_windows.go | 4 ++-- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/doc.md b/doc.md index a6c5df8d..b8d40d74 100644 --- a/doc.md +++ b/doc.md @@ -1744,7 +1744,7 @@ To use this feature, you need to use a client which supports communicating with OpenBSD implementation of netcat (nc) is one such example. You can use it to send a command to the socket file: - echo 'send echo hello world' | nc -U ${XDG_RUNTIME_DIR:-/tmp}/lf.${USER}.sock + echo 'send echo hello world' | nc -U ${XDG_RUNTIME_DIR:-/tmp/lf-$(id -u)}/lf.sock Since such a client may not be available everywhere, lf comes bundled with a command line flag to be used as such. When using lf, you do not need to specify the address of the socket file. diff --git a/doc.txt b/doc.txt index fd53fe01..1c7321de 100644 --- a/doc.txt +++ b/doc.txt @@ -1927,7 +1927,7 @@ communicating with a Unix domain socket. OpenBSD implementation of netcat (nc) is one such example. You can use it to send a command to the socket file: - echo 'send echo hello world' | nc -U ${XDG_RUNTIME_DIR:-/tmp}/lf.${USER}.sock + echo 'send echo hello world' | nc -U ${XDG_RUNTIME_DIR:-/tmp/lf-$(id -u)}/lf.sock Since such a client may not be available everywhere, lf comes bundled with a command line flag to be used as such. When using lf, you do not diff --git a/lf.1 b/lf.1 index 998c8c2e..c4b70fca 100644 --- a/lf.1 +++ b/lf.1 @@ -1797,7 +1797,7 @@ OpenBSD implementation of netcat (nc) is one such example. You can use it to send a command to the socket file: .IP .EX -echo \(aqsend echo hello world\(aq | nc \-U ${XDG_RUNTIME_DIR:\-/tmp}/lf.${USER}.sock +echo \(aqsend echo hello world\(aq | nc \-U ${XDG_RUNTIME_DIR:\-/tmp/lf\-$(id \-u)}/lf.sock .EE .PP Since such a client may not be available everywhere, lf comes bundled diff --git a/os.go b/os.go index 791a83cc..27076063 100644 --- a/os.go +++ b/os.go @@ -115,9 +115,23 @@ func init() { gTagsPath = filepath.Join(data, "lf", "tags") gHistoryPath = filepath.Join(data, "lf", "history") - runtime := cmp.Or(os.Getenv("XDG_RUNTIME_DIR"), os.TempDir()) + // Use a private per-user dir when XDG_RUNTIME_DIR is unset + runtimeDir := os.Getenv("XDG_RUNTIME_DIR") + if runtimeDir == "" { + runtimeDir = filepath.Join(os.TempDir(), fmt.Sprintf("lf-%s", gUser.Uid)) + if err := os.MkdirAll(runtimeDir, 0700); err != nil { + log.Fatalf("creating runtime dir: %s", err) + } + st, err := os.Stat(runtimeDir) + if err != nil { + log.Fatalf("stat runtime dir: %s", err) + } + if stat, ok := st.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Geteuid()) { + log.Fatalf("runtime dir not owned by current user: %s", runtimeDir) + } + } - gDefaultSocketPath = filepath.Join(runtime, fmt.Sprintf("lf.%s.sock", gUser.Username)) + gDefaultSocketPath = filepath.Join(runtimeDir, "lf.sock") } func detachedCommand(name string, arg ...string) *exec.Cmd { diff --git a/os_windows.go b/os_windows.go index 5ed13d07..ed1b5385 100644 --- a/os_windows.go +++ b/os_windows.go @@ -104,8 +104,8 @@ func init() { gTagsPath = filepath.Join(data, "lf", "tags") gHistoryPath = filepath.Join(data, "lf", "history") - runtime := os.TempDir() - gDefaultSocketPath = filepath.Join(runtime, fmt.Sprintf("lf.%s.sock", gUser.Username)) + runtimeDir := os.TempDir() + gDefaultSocketPath = filepath.Join(runtimeDir, "lf.sock") s := cmp.Or(os.Getenv("PATHEXT"), ".COM;.EXE;.BAT;.CMD") for ext := range strings.SplitSeq(s, ";") {