From 43d7addc15b99c65db09bd59bce415c8ec61608e Mon Sep 17 00:00:00 2001 From: "Brandon McGinty (chatgpt)" Date: Mon, 10 Aug 2026 08:50:56 -0400 Subject: [PATCH] Protect regular files from FIFO setup --- main.go | 11 ++++++++++- main_fifo_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 main_fifo_test.go diff --git a/main.go b/main.go index f82139c..0fb1fa4 100644 --- a/main.go +++ b/main.go @@ -87,7 +87,16 @@ func setup_fifo(fn string) (chan string, error) { if fn == "" { return t, nil } - os.Remove(fn) + if info, err := os.Lstat(fn); err == nil { + if info.Mode()&os.ModeNamedPipe == 0 { + return t, fmt.Errorf("FIFO path %q already exists and is not a FIFO", fn) + } + if err := os.Remove(fn); err != nil { + return t, err + } + } else if !os.IsNotExist(err) { + return t, err + } err := syscall.Mkfifo(fn, 0600) if err != nil { return t, err diff --git a/main_fifo_test.go b/main_fifo_test.go new file mode 100644 index 0000000..82b1461 --- /dev/null +++ b/main_fifo_test.go @@ -0,0 +1,24 @@ +package main + +import ( + "os" + "path/filepath" + "testing" +) + +func TestSetupFIFORefusesToReplaceRegularFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "not-a-fifo") + if err := os.WriteFile(path, []byte("keep"), 0600); err != nil { + t.Fatal(err) + } + if _, err := setup_fifo(path); err == nil { + t.Fatal("setup_fifo replaced a regular file") + } + contents, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if string(contents) != "keep" { + t.Fatalf("regular file was modified: %q", contents) + } +}