Description: Gomuks logs by default in /tmp which opens a can of worms in terms of security and also filling tmp dir with huge and verbose logs. This patch changes that behaviour to log in ~/.local/state or XDG_STATE_DIR only when DEBUG=1 Author: Nilesh Patra Origin: upstream Bug: https://github.com/tulir/gomuks/issues/429 Forwarded: https://github.com/tulir/gomuks/pull/430 Applied-Upstream: https://github.com/tulir/gomuks/commit/2b36ee373794ae4d4df4de95ed9e1428b9157dc9 Reviewed-by: Tulir Asokan Last-Update: 2023-07-13 --- a/debug/debug.go +++ b/debug/debug.go @@ -22,7 +22,9 @@ "io" "io/ioutil" "os" + "os/user" "path/filepath" + "runtime" "runtime/debug" "time" @@ -30,11 +32,37 @@ ) var writer io.Writer -var RecoverPrettyPanic bool +var RecoverPrettyPanic bool = true var DeadlockDetection bool var WriteLogs bool var OnRecover func() -var LogDirectory = filepath.Join(os.TempDir(), "gomuks") +var LogDirectory = GetUserDebugDir() + +func GetUserDebugDir() string { + if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { + return filepath.Join(os.TempDir(), "gomuks-"+getUname()) + } + // See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html + if xdgStateHome := os.Getenv("XDG_STATE_HOME"); xdgStateHome != "" { + return filepath.Join(xdgStateHome, "gomuks") + } + home := os.Getenv("HOME") + if home == "" { + fmt.Println("XDG_STATE_HOME and HOME are both unset") + os.Exit(1) + } + return filepath.Join(home, ".local", "state", "gomuks") +} + +func getUname() string { + currUser, err := user.Current() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + return currUser.Username +} func Initialize() { err := os.MkdirAll(LogDirectory, 0750) --- a/main.go +++ b/main.go @@ -39,13 +39,10 @@ debug.LogDirectory = debugDir } debugLevel := strings.ToLower(os.Getenv("DEBUG")) - if debugLevel != "0" && debugLevel != "f" && debugLevel != "false" { - debug.WriteLogs = true - debug.RecoverPrettyPanic = true - } if debugLevel == "1" || debugLevel == "t" || debugLevel == "true" { debug.RecoverPrettyPanic = false debug.DeadlockDetection = true + debug.WriteLogs = true } debug.Initialize() defer debug.Recover()