Skip to content

Commit 4c37100

Browse files
committed
review
1 parent 4f45cee commit 4c37100

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

agent/ls.go

Lines changed: 11 additions & 9 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

@@ -25,20 +26,17 @@ func (*agent) HandleLS(rw http.ResponseWriter, r *http.Request) {
2526

2627
resp, err := listFiles(query)
2728
if err != nil {
29+
status := http.StatusInternalServerError
2830
switch {
2931
case errors.Is(err, os.ErrNotExist):
30-
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
31-
Message: err.Error(),
32-
})
32+
status = http.StatusNotFound
3333
case errors.Is(err, os.ErrPermission):
34-
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
35-
Message: err.Error(),
36-
})
34+
status = http.StatusForbidden
3735
default:
38-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
39-
Message: err.Error(),
40-
})
4136
}
37+
httpapi.Write(ctx, rw, status, codersdk.Response{
38+
Message: err.Error(),
39+
})
4240
return
4341
}
4442

@@ -59,6 +57,10 @@ func listFiles(query LSRequest) (LSResponse, error) {
5957
if len(query.Path) == 0 {
6058
return listDrives()
6159
}
60+
re := regexp.MustCompile(`^[a-zA-Z]:\\$`)
61+
if !re.MatchString(query.Path[0]) {
62+
return LSResponse{}, xerrors.Errorf("invalid drive letter %q", query.Path[0])
63+
}
6264
} else {
6365
fullPath = []string{"/"}
6466
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy