Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkgs/mcpx/internal/artifacts/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -92,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
Expand Down
32 changes: 24 additions & 8 deletions pkgs/mcpx/internal/cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Comment thread
dezren39 marked this conversation as resolved.
Resolved
}
if err := writeNew(dest, body); err != nil {
return err
}
Expand Down
165 changes: 165 additions & 0 deletions pkgs/mcpx/internal/cli/exec_artifacts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
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)
}
}
}

// 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)
}
}
}
Loading