From 74349e00792aad7728421f2fd4f07b17b5e3b25b Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 00:25:56 -0500 Subject: [PATCH 1/2] fix(mcpx): never overwrite same-named exec artifacts (#1605) applyFrame and fetchArtifacts renamed delivered artifacts onto the bare sanitised name, so a second artifact whose name sanitised to the same file silently destroyed the first - contradicting Store.Export's documented never-overwrite rule. Both paths now resolve the destination through the newly exported artifacts.UniquePath (the same suffixing uniquePath Export uses), and each artifact's Path points at its own file. Regression tests cover both delivery paths, including the sanitise-collision pair from the issue ("my shot.png" / "my-shot.png"). --- pkgs/mcpx/internal/artifacts/name.go | 9 ++ pkgs/mcpx/internal/cli/exec.go | 32 +++++-- pkgs/mcpx/internal/cli/exec_artifacts_test.go | 93 +++++++++++++++++++ 3 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 pkgs/mcpx/internal/cli/exec_artifacts_test.go diff --git a/pkgs/mcpx/internal/artifacts/name.go b/pkgs/mcpx/internal/artifacts/name.go index 668c9848e7..a8e551d3c5 100644 --- a/pkgs/mcpx/internal/artifacts/name.go +++ b/pkgs/mcpx/internal/artifacts/name.go @@ -74,6 +74,15 @@ func MimeForName(name string) string { return "application/octet-stream" } +// UniquePath resolves a name inside a directory without ever overwriting, +// for callers outside this package that deliver artifacts into a directory +// the caller named (the CLI's exec artifact delivery, #1605). It is the +// exported form of uniquePath, so every delivery path follows the same +// never-overwrite rule Store.Export documents. +func UniquePath(dir, name string) (string, error) { + return uniquePath(dir, name) +} + // uniquePath resolves a name inside a directory without ever overwriting. // // The suffix goes before the extension -- shot-1.png, not shot.png-1 -- so diff --git a/pkgs/mcpx/internal/cli/exec.go b/pkgs/mcpx/internal/cli/exec.go index 5f07e86d2a..5ed6cf99b5 100644 --- a/pkgs/mcpx/internal/cli/exec.go +++ b/pkgs/mcpx/internal/cli/exec.go @@ -307,18 +307,30 @@ func (a *App) applyFrame(res *execsvc.Result, f execsvc.Frame, eo execOptions, p return err } name := "artifact" + match := -1 for i := range res.Artifacts { if res.Artifacts[i].ID == f.ID { name = res.Artifacts[i].Name - dest := filepath.Join(eo.ArtifactsDir, artifacts.SanitizeName(name)) - if err := os.Rename(w.Name(), dest); err != nil { - return err - } - res.Artifacts[i].Path = dest - return nil + match = i + break } } - return os.Rename(w.Name(), filepath.Join(eo.ArtifactsDir, artifacts.SanitizeName(name))) + // Never overwrite: a second artifact whose name sanitises to the + // same file is suffixed, exactly as Store.Export does (#1605). + // Renaming onto the bare sanitised name silently destroyed the + // first artifact's body and left both Path fields pointing at + // the one surviving file. + dest, err := artifacts.UniquePath(eo.ArtifactsDir, name) + if err != nil { + return err + } + if err := os.Rename(w.Name(), dest); err != nil { + return err + } + if match >= 0 { + res.Artifacts[match].Path = dest + } + return nil } return nil } @@ -393,7 +405,11 @@ func (a *App) fetchArtifacts(res *execsvc.Result, eo execOptions) error { return err } } - dest := filepath.Join(eo.ArtifactsDir, artifacts.SanitizeName(art.Name)) + // Never overwrite, same rule as the streaming path (#1605). + dest, err := artifacts.UniquePath(eo.ArtifactsDir, art.Name) + if err != nil { + return err + } if err := writeNew(dest, body); err != nil { return err } diff --git a/pkgs/mcpx/internal/cli/exec_artifacts_test.go b/pkgs/mcpx/internal/cli/exec_artifacts_test.go new file mode 100644 index 0000000000..5f63966ea8 --- /dev/null +++ b/pkgs/mcpx/internal/cli/exec_artifacts_test.go @@ -0,0 +1,93 @@ +package cli + +import ( + "encoding/base64" + "os" + "path/filepath" + "testing" + + "github.com/dezren39/mcpx/internal/artifacts" + "github.com/dezren39/mcpx/internal/execsvc" +) + +// TestApplyFrameSameNamedArtifactsNeverOverwrite is the #1605 regression +// test for the streaming delivery path: two artifacts that sanitise to the +// same file name must both survive, suffixed the way Store.Export suffixes, +// with each artifact's Path pointing at its own body. +func TestApplyFrameSameNamedArtifactsNeverOverwrite(t *testing.T) { + dir := t.TempDir() + a := &App{} + res := &execsvc.Result{} + eo := execOptions{ArtifactsDir: dir} + pending := map[string]*os.File{} + + deliver := func(id, name, body string) { + t.Helper() + art := &execsvc.Artifact{Meta: artifacts.Meta{ID: id, Name: name}} + if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifact, Artifact: art}, eo, pending); err != nil { + t.Fatal(err) + } + data := base64.StdEncoding.EncodeToString([]byte(body)) + if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifactData, ID: id, Data: data}, eo, pending); err != nil { + t.Fatal(err) + } + if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifactEnd, ID: id}, eo, pending); err != nil { + t.Fatal(err) + } + } + // "my shot.png" and "my-shot.png" both sanitise to my-shot.png. + deliver("id1", "my shot.png", "FIRST-BODY") + deliver("id2", "my-shot.png", "SECOND-BODY") + + if len(res.Artifacts) != 2 { + t.Fatalf("artifacts = %d, want 2", len(res.Artifacts)) + } + first, second := res.Artifacts[0].Path, res.Artifacts[1].Path + if first == second { + t.Fatalf("both artifacts point at %q; the first body was overwritten", first) + } + if filepath.Base(first) != "my-shot.png" || filepath.Base(second) != "my-shot-1.png" { + t.Fatalf("paths = %q, %q; want my-shot.png and my-shot-1.png", first, second) + } + for path, want := range map[string]string{first: "FIRST-BODY", second: "SECOND-BODY"} { + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if string(got) != want { + t.Fatalf("%s holds %q, want %q", path, got, want) + } + } +} + +// TestFetchArtifactsSameNamedNeverOverwrite covers the second #1605 +// delivery path: inline bodies pulled by fetchArtifacts follow the same +// never-overwrite rule. +func TestFetchArtifactsSameNamedNeverOverwrite(t *testing.T) { + dir := t.TempDir() + a := &App{client: &Client{}, connected: true} + enc := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } + res := &execsvc.Result{Artifacts: []execsvc.Artifact{ + {Meta: artifacts.Meta{ID: "id1", Name: "shot.png"}, Data: enc("FIRST-BODY")}, + {Meta: artifacts.Meta{ID: "id2", Name: "shot.png"}, Data: enc("SECOND-BODY")}, + }} + if err := a.fetchArtifacts(res, execOptions{ArtifactsDir: dir}); err != nil { + t.Fatal(err) + } + first, second := res.Artifacts[0].Path, res.Artifacts[1].Path + if first == second { + t.Fatalf("both artifacts point at %q; the first body was overwritten", first) + } + if filepath.Base(first) != "shot.png" || filepath.Base(second) != "shot-1.png" { + t.Fatalf("paths = %q, %q; want shot.png and shot-1.png", first, second) + } + for path, want := range map[string]string{first: "FIRST-BODY", second: "SECOND-BODY"} { + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if string(got) != want { + t.Fatalf("%s holds %q, want %q", path, got, want) + } + } +} From 0230920ddab9821c3205179824b98740807fa2df Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 00:54:11 -0500 Subject: [PATCH 2/2] fix(mcpx): let UniquePath deliver into the current directory (#1605) Codex P2 on #2950: uniquePath cleaned the directory to "." and then rejected the joined bare file name with a "."+separator prefix test, so artifacts.dir "." or "./" failed both CLI delivery paths outright. Containment is now checked with filepath.Rel, which answers the real question for every base including "." and the filesystem root. Regression tests cover both delivery paths with a current-directory artifacts dir. --- pkgs/mcpx/internal/artifacts/name.go | 10 ++- pkgs/mcpx/internal/cli/exec_artifacts_test.go | 72 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/pkgs/mcpx/internal/artifacts/name.go b/pkgs/mcpx/internal/artifacts/name.go index a8e551d3c5..d3aad5d653 100644 --- a/pkgs/mcpx/internal/artifacts/name.go +++ b/pkgs/mcpx/internal/artifacts/name.go @@ -101,7 +101,15 @@ func uniquePath(dir, name string) (string, error) { candidate = fmt.Sprintf("%s-%d%s", stem, i, ext) } p := filepath.Join(base, candidate) - if !strings.HasPrefix(p, base+string(filepath.Separator)) { + // Containment via Rel, not a prefix test: Join cleans a "." (or + // "./") directory away, so "." + "shot.png" joins to the bare + // "shot.png", which a "."+separator prefix test wrongly rejects + // even though the file lands in the current directory (Codex + // finding on #2950). Rel expresses the real question -- is the + // destination at or below the directory -- for every base, + // including "." and the filesystem root. + rel, rerr := filepath.Rel(base, p) + if rerr != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { return "", fmt.Errorf("artifact name %q does not stay inside %s", name, dir) } // O_EXCL is the check and the claim in one step. Testing with Stat diff --git a/pkgs/mcpx/internal/cli/exec_artifacts_test.go b/pkgs/mcpx/internal/cli/exec_artifacts_test.go index 5f63966ea8..ec677af6ba 100644 --- a/pkgs/mcpx/internal/cli/exec_artifacts_test.go +++ b/pkgs/mcpx/internal/cli/exec_artifacts_test.go @@ -91,3 +91,75 @@ func TestFetchArtifactsSameNamedNeverOverwrite(t *testing.T) { } } } + +// TestApplyFrameCurrentDirArtifactsDir covers the Codex finding on #2950: +// with artifacts.dir set to "." the streaming path must still deliver -- +// UniquePath's containment check used to reject the joined bare file name. +func TestApplyFrameCurrentDirArtifactsDir(t *testing.T) { + dir := t.TempDir() + t.Chdir(dir) + a := &App{} + res := &execsvc.Result{} + eo := execOptions{ArtifactsDir: "."} + pending := map[string]*os.File{} + + deliver := func(id, name, body string) { + t.Helper() + art := &execsvc.Artifact{Meta: artifacts.Meta{ID: id, Name: name}} + if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifact, Artifact: art}, eo, pending); err != nil { + t.Fatal(err) + } + data := base64.StdEncoding.EncodeToString([]byte(body)) + if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifactData, ID: id, Data: data}, eo, pending); err != nil { + t.Fatal(err) + } + if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifactEnd, ID: id}, eo, pending); err != nil { + t.Fatal(err) + } + } + deliver("id1", "shot.png", "FIRST-BODY") + deliver("id2", "shot.png", "SECOND-BODY") + + first, second := res.Artifacts[0].Path, res.Artifacts[1].Path + if first == second { + t.Fatalf("both artifacts point at %q; the first body was overwritten", first) + } + for path, want := range map[string]string{first: "FIRST-BODY", second: "SECOND-BODY"} { + got, err := os.ReadFile(filepath.Join(dir, path)) + if err != nil { + t.Fatal(err) + } + if string(got) != want { + t.Fatalf("%s holds %q, want %q", path, got, want) + } + } +} + +// TestFetchArtifactsCurrentDirArtifactsDir is the fetched-path half of the +// #2950 Codex finding: artifacts.dir "./" must deliver, not error. +func TestFetchArtifactsCurrentDirArtifactsDir(t *testing.T) { + dir := t.TempDir() + t.Chdir(dir) + a := &App{client: &Client{}, connected: true} + enc := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } + res := &execsvc.Result{Artifacts: []execsvc.Artifact{ + {Meta: artifacts.Meta{ID: "id1", Name: "shot.png"}, Data: enc("FIRST-BODY")}, + {Meta: artifacts.Meta{ID: "id2", Name: "shot.png"}, Data: enc("SECOND-BODY")}, + }} + if err := a.fetchArtifacts(res, execOptions{ArtifactsDir: "./"}); err != nil { + t.Fatal(err) + } + first, second := res.Artifacts[0].Path, res.Artifacts[1].Path + if first == second { + t.Fatalf("both artifacts point at %q; the first body was overwritten", first) + } + for path, want := range map[string]string{first: "FIRST-BODY", second: "SECOND-BODY"} { + got, err := os.ReadFile(filepath.Join(dir, path)) + if err != nil { + t.Fatal(err) + } + if string(got) != want { + t.Fatalf("%s holds %q, want %q", path, got, want) + } + } +}