diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 0000000..c5299f1 --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2025-02-14 - Predictable Temporary Filename Symlink Vulnerability +**Vulnerability:** Predictable temporary files were created using hardcoded extensions (e.g. `path + ".tmp"`) in shared directories like `/tmp`. This allows an attacker to pre-create a symlink at the predicted location, tricking the application into overwriting an arbitrary file. +**Learning:** Atomic file writes often involve creating a temporary file and renaming it. If the temporary filename is predictable and located in a shared directory, it is vulnerable to symlink attacks. +**Prevention:** Always use `os.CreateTemp` to generate unpredictable temporary filenames. Explicitly set permissions using `Chmod` if matching the original file permissions is necessary, and ensure `Close()` is called before `Rename()`. diff --git a/internal/burnrate/burnrate.go b/internal/burnrate/burnrate.go index 8cb680c..adcd534 100644 --- a/internal/burnrate/burnrate.go +++ b/internal/burnrate/burnrate.go @@ -64,10 +64,29 @@ func LoadOrCreateSnapshotAt(sessionID string, currentCost float64, now time.Time return nil, false, err } - tmpPath := path + ".tmp" - if err := os.WriteFile(tmpPath, data, 0644); err != nil { + f, err := os.CreateTemp(filepath.Dir(path), "prism-burn-tmp-*") + if err != nil { + return nil, false, err + } + tmpPath := f.Name() + + if _, err := f.Write(data); err != nil { + f.Close() + os.Remove(tmpPath) + return nil, false, err + } + + if err := f.Chmod(0644); err != nil { + f.Close() + os.Remove(tmpPath) return nil, false, err } + + if err := f.Close(); err != nil { + os.Remove(tmpPath) + return nil, false, err + } + if err := os.Rename(tmpPath, path); err != nil { os.Remove(tmpPath) return nil, false, err diff --git a/internal/sparkline/sparkline.go b/internal/sparkline/sparkline.go index f4c755c..110d823 100644 --- a/internal/sparkline/sparkline.go +++ b/internal/sparkline/sparkline.go @@ -138,10 +138,30 @@ func Save(sessionID, metric string, b *Buffer) { if err != nil { return } - tmp := path + ".tmp" - if err := os.WriteFile(tmp, data, 0644); err != nil { + + f, err := os.CreateTemp(filepath.Dir(path), "prism-spark-tmp-*") + if err != nil { + return + } + tmp := f.Name() + + if _, err := f.Write(data); err != nil { + f.Close() + os.Remove(tmp) + return + } + + if err := f.Chmod(0644); err != nil { + f.Close() + os.Remove(tmp) + return + } + + if err := f.Close(); err != nil { + os.Remove(tmp) return } + os.Rename(tmp, path) }