diff --git a/pkgs/mcpx/internal/artifacts/artifacts.go b/pkgs/mcpx/internal/artifacts/artifacts.go index b3bdba88f1..5871d8a5ad 100644 --- a/pkgs/mcpx/internal/artifacts/artifacts.go +++ b/pkgs/mcpx/internal/artifacts/artifacts.go @@ -17,6 +17,10 @@ // content: serving by hash would mean anyone who knows what a file looks // like can read yours. // +// Blobs are never linked to or from files outside the store: PutFile +// copies in and Export copies out (#1660), so no outside file shares a +// blob's inode in either direction. +// // An index beside it. Name, mime, run, session, expiry -- everything that is // about this particular registration rather than about the bytes. SQLite // because it is already a dependency and because "list the artifacts of this @@ -252,14 +256,15 @@ func (s *Store) PutFile(path string, p PutOptions) (Meta, error) { if err := os.MkdirAll(filepath.Dir(dest), 0o700); err != nil { return Meta{}, err } - if lerr := os.Link(path, dest); lerr != nil { - // A different device, or a filesystem with no links. Copy. - if _, serr := f.Seek(0, io.SeekStart); serr != nil { - return Meta{}, serr - } - if cerr := copyInto(dest, f); cerr != nil { - return Meta{}, cerr - } + // Copy, never link (#1660): a blob must not share an inode + // with the caller's file — rewriting that file in place + // would silently rewrite the stored bytes under their + // original hash. + if _, serr := f.Seek(0, io.SeekStart); serr != nil { + return Meta{}, serr + } + if cerr := copyInto(dest, f); cerr != nil { + return Meta{}, cerr } } if p.Name == "" { @@ -288,9 +293,8 @@ func copyInto(dest string, r io.Reader) error { func (s *Store) adopt(tmp, hash string) error { dest := s.blobPath(hash) if _, err := os.Stat(dest); err == nil { - // Already stored. Identical content by definition, so keep the one - // that is there: it may already be hardlinked into a caller's - // directory, and replacing it would break that link's meaning. + // Already stored. Identical content by definition, so keep + // the one that is there. return nil } if err := os.MkdirAll(filepath.Dir(dest), 0o700); err != nil { @@ -540,10 +544,9 @@ func (s *Store) GC(now time.Time) (int, error) { // Export places an artifact in a directory the caller named. // -// A hardlink first, because the local case should not copy megabytes to hand -// a file to something that can already see the disk it is on. A copy when -// the link fails, which is what a different device or a foreign filesystem -// looks like. +// Always a copy, never a link (#1660): a blob must not share an inode +// with a file outside the store — editing the exported file in place +// would silently rewrite the stored bytes under their original hash. // // The name is sanitised and collisions are resolved by suffixing, never by // overwriting: a script that emits two files called "shot.png" has produced @@ -557,9 +560,6 @@ func (s *Store) Export(m Meta, dir string) (string, error) { return "", err } src := s.blobPath(m.SHA256) - if err := os.Link(src, dest); err == nil { - return dest, nil - } f, err := os.Open(src) if err != nil { return "", err diff --git a/pkgs/mcpx/internal/artifacts/artifacts_test.go b/pkgs/mcpx/internal/artifacts/artifacts_test.go index ef2ee3da10..32ba7e9aaa 100644 --- a/pkgs/mcpx/internal/artifacts/artifacts_test.go +++ b/pkgs/mcpx/internal/artifacts/artifacts_test.go @@ -136,7 +136,7 @@ func TestQuotaRefusesNewContentButNotDuplicates(t *testing.T) { } } -func TestPutFileHardlinksRatherThanCopies(t *testing.T) { +func TestPutFileCopiesRatherThanLinks(t *testing.T) { dir := t.TempDir() src := filepath.Join(dir, "shot.png") if err := os.WriteFile(src, []byte("PNGDATA"), 0o600); err != nil { @@ -150,8 +150,104 @@ func TestPutFileHardlinksRatherThanCopies(t *testing.T) { if m.Name != "shot.png" { t.Errorf("the name should default to the file's, got %q", m.Name) } - if !sameInode(t, src, s.BodyPath(m)) { - t.Error("a file on the same filesystem should be linked, not copied") + // #1660: the blob must never share an inode with the source file, + // or rewriting the source in place would rewrite the stored bytes. + if sameInode(t, src, s.BodyPath(m)) { + t.Error("the blob should be a copy with its own inode, not a link to the source") + } +} + +// #1660 regression: rewriting a PutFile source in place afterwards +// must leave the stored bytes — and their agreement with the +// registered hash — untouched. +func TestPutFileSourceRewrittenInPlaceLeavesStoredBytesAlone(t *testing.T) { + dir := t.TempDir() + src := filepath.Join(dir, "shot.png") + if err := os.WriteFile(src, []byte("PNGDATA"), 0o600); err != nil { + t.Fatal(err) + } + s := open(t, artifacts.Options{}) + m, err := s.PutFile(src, artifacts.PutOptions{}) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(src, []byte("FORGED!!"), 0o600); err != nil { + t.Fatal(err) + } + body, got, err := s.Bytes(m.ID) + if err != nil { + t.Fatal(err) + } + if string(body) != "PNGDATA" { + t.Errorf("stored bytes followed the source rewrite: %q", body) + } + if got.SHA256 != m.SHA256 || int64(len(body)) != got.Size { + t.Errorf("served bytes disagree with the registration: sha %s vs %s, size %d vs %d", + got.SHA256, m.SHA256, len(body), got.Size) + } +} + +// #1660 regression: editing an exported copy in place must leave the +// stored bytes untouched — the export is a copy, not a link. +func TestExportEditedInPlaceLeavesStoredBytesAlone(t *testing.T) { + s := open(t, artifacts.Options{}) + m, err := s.Put(strings.NewReader("PNGDATA"), artifacts.PutOptions{Name: "shot.png"}) + if err != nil { + t.Fatal(err) + } + out := t.TempDir() + dest, err := s.Export(m, out) + if err != nil { + t.Fatal(err) + } + if sameInode(t, dest, s.BodyPath(m)) { + t.Fatal("the export should be a copy with its own inode, not a link to the blob") + } + if err := os.WriteFile(dest, []byte("FORGED!!"), 0o600); err != nil { + t.Fatal(err) + } + body, _, err := s.Bytes(m.ID) + if err != nil { + t.Fatal(err) + } + if string(body) != "PNGDATA" { + t.Errorf("stored bytes followed the export edit: %q", body) + } +} + +// #1660 regression: after a mutation attempt on the source, putting +// the original content again still serves the original bytes (the +// content address was never poisoned). +func TestRePutOfOriginalContentAfterMutationServesOriginal(t *testing.T) { + dir := t.TempDir() + src := filepath.Join(dir, "shot.png") + if err := os.WriteFile(src, []byte("PNGDATA"), 0o600); err != nil { + t.Fatal(err) + } + s := open(t, artifacts.Options{}) + first, err := s.PutFile(src, artifacts.PutOptions{}) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(src, []byte("FORGED!!"), 0o600); err != nil { + t.Fatal(err) + } + if _, err := s.PutFile(src, artifacts.PutOptions{}); err != nil { + t.Fatal(err) + } + again, err := s.Put(strings.NewReader("PNGDATA"), artifacts.PutOptions{Name: "shot.png"}) + if err != nil { + t.Fatal(err) + } + if again.SHA256 != first.SHA256 { + t.Fatalf("the original content should keep its address: %s vs %s", again.SHA256, first.SHA256) + } + body, _, err := s.Bytes(again.ID) + if err != nil { + t.Fatal(err) + } + if string(body) != "PNGDATA" { + t.Errorf("the original registration serves %q, not the original bytes", body) } }