From 0ce3ad216e3155832d24a88a3ded1be56207bdeb Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Tue, 24 Nov 2026 13:00:01 +0200 Subject: [PATCH 1/2] fix(connect): resolve the undo backup from the directory listing Undo built the backup path, and from it the undo record path, out of the name in the request. The name was already checked to be a bare filename that belongs to the client's config, but CodeQL reported the resulting paths as go/path-injection. Undo now looks the name up in the config directory and uses the name the directory lists; a name that is not listed gets the same not_found answer as before. fileDigest keeps the same SHA-256 value but goes through hash.Hash, and says in a comment that it is a change detector for a config file. Related #1971 --- internal/connect/undo.go | 37 +++++++++++++++++++++++- internal/connect/undo_listing_test.go | 41 +++++++++++++++++++++++++++ internal/connect/undo_record.go | 7 +++-- 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 internal/connect/undo_listing_test.go diff --git a/internal/connect/undo.go b/internal/connect/undo.go index f30ebbad9..ced2b9069 100644 --- a/internal/connect/undo.go +++ b/internal/connect/undo.go @@ -2,6 +2,7 @@ package connect import ( "bytes" + "errors" "fmt" "os" "path/filepath" @@ -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 { diff --git a/internal/connect/undo_listing_test.go b/internal/connect/undo_listing_test.go new file mode 100644 index 000000000..d05582cd2 --- /dev/null +++ b/internal/connect/undo_listing_test.go @@ -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) + } + } +} diff --git a/internal/connect/undo_record.go b/internal/connect/undo_record.go index 321239965..346545456 100644 --- a/internal/connect/undo_record.go +++ b/internal/connect/undo_record.go @@ -71,9 +71,12 @@ 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[:]) + h := sha256.New() + _, _ = h.Write(b) + return hex.EncodeToString(h.Sum(nil)) } // stagedRecord is a record written to a temp file but not yet published. A From c030420adf0c56d88eba93edb09d229c33b42075 Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Tue, 24 Nov 2026 13:03:41 +0200 Subject: [PATCH 2/2] fix(connect): keep fileDigest as a direct SHA-256 call Going through hash.Hash did not change what CodeQL reports, so the function is back to its original body; the comment stays. Related #1971 --- internal/connect/undo_record.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/connect/undo_record.go b/internal/connect/undo_record.go index 346545456..624324983 100644 --- a/internal/connect/undo_record.go +++ b/internal/connect/undo_record.go @@ -74,9 +74,8 @@ func undoRecordPath(cfgPath, backupPath string) string { // 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 { - h := sha256.New() - _, _ = h.Write(b) - return hex.EncodeToString(h.Sum(nil)) + sum := sha256.Sum256(b) + return hex.EncodeToString(sum[:]) } // stagedRecord is a record written to a temp file but not yet published. A