fix(cleanup): resolve macOS symlink mismatch causing all log files to be kept (#155)

* fix(cleanup): resolve macOS symlink mismatch causing all log files to be kept

On macOS, os.TempDir() returns /var/folders/... while filepath.EvalSymlinks
resolves to /private/var/folders/... (since /var is a symlink to /private/var).

isUnsafeFile was comparing filepath.Abs(tempDir) against EvalSymlinks(file),
causing filepath.Rel to produce a path starting with "../../../../../private/..."
which triggered the "file is outside tempDir" guard. As a result, --cleanup
kept all 1367 log files instead of deleting any.

Fix: use evalSymlinksFn on tempDir as well, so both sides of the comparison
are resolved consistently. Falls back to filepath.Abs if symlink resolution fails.

* test(logger): fix isUnsafeFile eval symlinks stubs

---------

Co-authored-by: cexll <evanxian9@gmail.com>
This commit is contained in:
cnzgray
2026-02-28 17:22:58 +08:00
committed by GitHub
parent 33a94d2bc4
commit 62309d1429
2 changed files with 73 additions and 7 deletions

View File

@@ -569,10 +569,16 @@ func isUnsafeFile(path string, tempDir string) (bool, string) {
return true, fmt.Sprintf("path resolution failed: %v", err)
}
// Get absolute path of tempDir
absTempDir, err := filepath.Abs(tempDir)
// Get canonical path of tempDir, resolving symlinks to match resolvedPath.
// On macOS, os.TempDir() returns /var/folders/... but EvalSymlinks resolves
// files to /private/var/folders/..., causing a spurious "outside tempDir" mismatch.
absTempDir, err := evalSymlinksFn(tempDir)
if err != nil {
return true, fmt.Sprintf("tempDir resolution failed: %v", err)
// Fallback to Abs if symlink resolution fails
absTempDir, err = filepath.Abs(tempDir)
if err != nil {
return true, fmt.Sprintf("tempDir resolution failed: %v", err)
}
}
// Ensure resolved path is within tempDir