diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 35a0a8e..d06cac5 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -255,7 +255,9 @@ candidate output still leaves the worktree clean. **Resolution:** Candidate and CLI delivery builds use shared system temporary storage allocated by `internal/tempdir`, with worktree containment checks and caller-owned cleanup. Regressions execute both operations with read-only -repository parents and confirm clean worktrees. Affected suites pass. +repository parents and confirm clean worktrees. Additional helper regressions +cover relative system temporary paths and cleanup after rejecting in-worktree +storage. Affected suites pass. ### R7 — Medium: Attachment origin restrictions exceed credential isolation diff --git a/internal/tempdir/tempdir.go b/internal/tempdir/tempdir.go index 0eed265..a737f66 100644 --- a/internal/tempdir/tempdir.go +++ b/internal/tempdir/tempdir.go @@ -14,6 +14,12 @@ func Outside(repositoryDir, pattern string) (string, error) { if err != nil { return "", err } + absolute, err := filepath.Abs(directory) + if err != nil { + os.RemoveAll(directory) + return "", err + } + directory = absolute repository, err := filepath.Abs(repositoryDir) if err == nil && !strings.EqualFold(filepath.VolumeName(repository), filepath.VolumeName(directory)) { return directory, nil diff --git a/internal/tempdir/tempdir_test.go b/internal/tempdir/tempdir_test.go new file mode 100644 index 0000000..fa52ea7 --- /dev/null +++ b/internal/tempdir/tempdir_test.go @@ -0,0 +1,52 @@ +package tempdir + +import ( + "os" + "path/filepath" + "runtime" + "testing" +) + +func TestOutsideHonorsRelativeTemporaryDirectory(t *testing.T) { + parent := t.TempDir() + repository := t.TempDir() + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + relative, err := filepath.Rel(cwd, parent) + if err != nil { + t.Skip("temporary directory is on another volume") + } + setTemporaryDirectory(t, relative) + directory, err := Outside(repository, "fixture-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(directory) + if !filepath.IsAbs(directory) || filepath.Dir(directory) != parent { + t.Fatalf("temporary storage=%q want parent %q", directory, parent) + } +} + +func TestOutsideRejectsAndCleansStorageInsideWorktree(t *testing.T) { + repository := t.TempDir() + setTemporaryDirectory(t, repository) + if directory, err := Outside(repository, "fixture-"); err == nil { + os.RemoveAll(directory) + t.Fatal("storage created inside worktree") + } + remaining, err := os.ReadDir(repository) + if err != nil || len(remaining) != 0 { + t.Fatalf("temporary storage left behind: %v %v", remaining, err) + } +} + +func setTemporaryDirectory(t *testing.T, directory string) { + t.Helper() + variable := "TMPDIR" + if runtime.GOOS == "windows" { + variable = "TMP" + } + t.Setenv(variable, directory) +}