Skip to content

Commit 202d7c9

Browse files
committed
use path.Join for cross-platform abs paths
Signed-off-by: Praful <[email protected]>
1 parent cf500f6 commit 202d7c9

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

pkg/mcp/toolset/toolset.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111
"os/exec"
12+
"path"
1213
"path/filepath"
1314
"slices"
1415
"strings"
@@ -107,7 +108,7 @@ func (ts *ToolSet) TranslateHostPath(hostPath string) (guestPath, warnings strin
107108
if hostPath == "" {
108109
return "", "", errors.New("path is empty")
109110
}
110-
if !filepath.IsAbs(hostPath) {
111+
if !filepath.IsAbs(hostPath) && !strings.HasPrefix(hostPath, "/") {
111112
return "", "", fmt.Errorf("expected an absolute path, got a relative path: %q", hostPath)
112113
}
113114

@@ -130,7 +131,8 @@ func (ts *ToolSet) translateToGuestPath(hostPath string) (string, bool) {
130131

131132
rel, err := filepath.Rel(location, cleanPath)
132133
if err == nil && !strings.HasPrefix(rel, "..") && rel != ".." {
133-
guestPath := filepath.Join(*mount.MountPoint, rel)
134+
rel = filepath.ToSlash(rel)
135+
guestPath := path.Join(*mount.MountPoint, rel)
134136
return guestPath, true
135137
}
136138
}

pkg/mcp/toolset/toolset_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,38 @@ func TestTranslateHostPath(t *testing.T) {
8888
wantWarnings: false,
8989
wantErr: false,
9090
},
91+
{
92+
name: "relative path should error",
93+
hostPath: "relative/path",
94+
toolSet: ToolSet{
95+
inst: &limatype.Instance{
96+
Config: &limatype.LimaYAML{
97+
Mounts: []limatype.Mount{
98+
{Location: "/home/user", MountPoint: &mountPoint1},
99+
},
100+
},
101+
},
102+
},
103+
wantGuestPath: "",
104+
wantWarnings: false,
105+
wantErr: true,
106+
},
107+
{
108+
name: "empty path should error",
109+
hostPath: "",
110+
toolSet: ToolSet{
111+
inst: &limatype.Instance{
112+
Config: &limatype.LimaYAML{
113+
Mounts: []limatype.Mount{
114+
{Location: "/home/user", MountPoint: &mountPoint1},
115+
},
116+
},
117+
},
118+
},
119+
wantGuestPath: "",
120+
wantWarnings: false,
121+
wantErr: true,
122+
},
91123
}
92124

93125
for _, test := range tests {

0 commit comments

Comments
 (0)