Skip to content

Commit 285ae7f

Browse files
committed
review
1 parent 4f45cee commit 285ae7f

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

agent/ls.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"os"
77
"path/filepath"
8+
"regexp"
89
"runtime"
910
"strings"
1011

@@ -59,6 +60,10 @@ func listFiles(query LSRequest) (LSResponse, error) {
5960
if len(query.Path) == 0 {
6061
return listDrives()
6162
}
63+
re := regexp.MustCompile(`^[a-zA-Z]:\\$`)
64+
if !re.MatchString(query.Path[0]) {
65+
return LSResponse{}, xerrors.Errorf("invalid drive letter %q", query.Path[0])
66+
}
6267
} else {
6368
fullPath = []string{"/"}
6469
}

agent/ls_internal_test.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ func TestListFilesSuccess(t *testing.T) {
115115
err = os.Mkdir(downloadsDir, 0o755)
116116
require.NoError(t, err)
117117

118+
textFile := filepath.Join(tmpDir, "file.txt")
119+
err = os.WriteFile(textFile, []byte("content"), 0o600)
120+
require.NoError(t, err)
121+
118122
var queryComponents []string
119123
// We can't get an absolute path relative to empty string on Windows.
120124
if runtime.GOOS == "windows" && base == "" {
@@ -133,23 +137,23 @@ func TestListFilesSuccess(t *testing.T) {
133137
require.NoError(t, err)
134138

135139
require.Equal(t, tmpDir, resp.AbsolutePathString)
136-
137-
var foundRepos, foundDownloads bool
138-
for _, file := range resp.Contents {
139-
switch file.Name {
140-
case "repos":
141-
foundRepos = true
142-
expectedPath := filepath.Join(tmpDir, "repos")
143-
require.Equal(t, expectedPath, file.AbsolutePathString)
144-
require.True(t, file.IsDir)
145-
case "Downloads":
146-
foundDownloads = true
147-
expectedPath := filepath.Join(tmpDir, "Downloads")
148-
require.Equal(t, expectedPath, file.AbsolutePathString)
149-
require.True(t, file.IsDir)
150-
}
151-
}
152-
require.True(t, foundRepos && foundDownloads, "expected to find both repos and Downloads directories, got: %+v", resp.Contents)
140+
require.ElementsMatch(t, []LSFile{
141+
{
142+
Name: "repos",
143+
AbsolutePathString: reposDir,
144+
IsDir: true,
145+
},
146+
{
147+
Name: "Downloads",
148+
AbsolutePathString: downloadsDir,
149+
IsDir: true,
150+
},
151+
{
152+
Name: "file.txt",
153+
AbsolutePathString: textFile,
154+
IsDir: false,
155+
},
156+
}, resp.Contents)
153157
})
154158
}
155159
}
@@ -186,4 +190,12 @@ func TestListFilesListDrives(t *testing.T) {
186190
}
187191
resp, err = listFiles(query)
188192
require.NoError(t, err)
193+
194+
query = LSRequest{
195+
// Network drives are not supported.
196+
Path: []string{"\\sshfs\\work"},
197+
Relativity: LSRelativityRoot,
198+
}
199+
resp, err = listFiles(query)
200+
require.ErrorContains(t, err, "drive")
189201
}

0 commit comments

Comments
 (0)