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
37 changes: 36 additions & 1 deletion internal/connect/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connect

import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -118,13 +119,47 @@ func (s *Service) UndoWithIntent(clientID, serverName, backupName string, intent
if matched != filepath.Base(cfgPath) {
cfgPath = filepath.Join(filepath.Dir(cfgPath), matched)
}
backupPath = filepath.Join(filepath.Dir(cfgPath), base)
// The path is built from the directory's own listing, not from the
// request: the request only selects which listed backup is meant.
listed, found, lerr := listedFileName(filepath.Dir(cfgPath), base)
if lerr != nil {
return nil, s.asAccessError(client, cfgPath, fmt.Errorf("read backup: %w", lerr))
}
if !found {
return &ConnectResult{
Success: false,
Client: client.ID,
ConfigPath: cfgPath,
ServerName: serverName,
Action: "not_found",
Message: fmt.Sprintf("Backup %s no longer exists; cannot restore. Use disconnect to remove the mcpproxy entry instead.", filepath.Join(filepath.Dir(cfgPath), base)),
}, nil
}
backupPath = filepath.Join(filepath.Dir(cfgPath), listed)
}

res, err = s.undo(client, cfgPath, serverName, backupPath, intent)
return res, s.asAccessError(client, cfgPath, err)
}

// listedFileName looks for a file called name in dir and returns the name as
// the directory lists it. A missing directory means the file is not there.
func listedFileName(dir, name string) (string, bool, error) {
entries, err := os.ReadDir(dir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", false, nil
}
return "", false, err
}
for _, e := range entries {
if e.Name() == name {
return e.Name(), true, nil
}
}
return "", false, nil
}

func (s *Service) undo(client *ClientDef, cfgPath, serverName, backupPath string, intent CredentialIntent) (*ConnectResult, error) {
unlock, lockErr := lockConfig(cfgPath)
if lockErr != nil {
Expand Down
41 changes: 41 additions & 0 deletions internal/connect/undo_listing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package connect

import (
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
"testing"
)

// The backup an Undo restores is addressed by the name the directory lists,
// and only a file that is really there is found.
func TestListedFileName(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "c.json.bak.1"), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
name, found, err := listedFileName(dir, "c.json.bak.1")
if err != nil || !found || name != "c.json.bak.1" {
t.Fatalf("listed file: name=%q found=%v err=%v", name, found, err)
}
for _, missing := range []string{"c.json.bak.2", "..", ".", ""} {
if _, found, err := listedFileName(dir, missing); err != nil || found {
t.Fatalf("%q: found=%v err=%v, want not found", missing, found, err)
}
}
if _, found, err := listedFileName(filepath.Join(dir, "absent"), "c.json.bak.1"); err != nil || found {
t.Fatalf("missing directory: found=%v err=%v, want not found", found, err)
}
}

// Records written before this change must still match: the digest is the
// plain SHA-256 of the file.
func TestFileDigestIsPlainSHA256(t *testing.T) {
for _, in := range [][]byte{nil, []byte("abc"), []byte("{\"mcpServers\":{}}\n")} {
sum := sha256.Sum256(in)
if got, want := fileDigest(in), hex.EncodeToString(sum[:]); got != want {
t.Fatalf("fileDigest(%q) = %s, want %s", in, got, want)
}
}
}
2 changes: 2 additions & 0 deletions internal/connect/undo_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func undoRecordPath(cfgPath, backupPath string) string {
return backupPath + ".undo"
}

// fileDigest is the SHA-256 of a whole config file, in hex. It is a change
// detector for the file a connect wrote, not a way to store a credential.
func fileDigest(b []byte) string {
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:])
Expand Down
Loading