Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4c37100

Browse files
committed
review
1 parent4f45cee commit4c37100

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
iferr!=nil {
29+
status:=http.StatusInternalServerError
2830
switch {
2931
caseerrors.Is(err,os.ErrNotExist):
30-
httpapi.Write(ctx,rw,http.StatusNotFound, codersdk.Response{
31-
Message:err.Error(),
32-
})
32+
status=http.StatusNotFound
3333
caseerrors.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
iflen(query.Path)==0 {
6058
returnlistDrives()
6159
}
60+
re:=regexp.MustCompile(`^[a-zA-Z]:\\$`)
61+
if!re.MatchString(query.Path[0]) {
62+
returnLSResponse{},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
varqueryComponents []string
119123
// We can't get an absolute path relative to empty string on Windows.
120124
ifruntime.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-
varfoundRepos,foundDownloadsbool
138-
for_,file:=rangeresp.Contents {
139-
switchfile.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)

[8]ページ先頭

©2009-2025 Movatter.jp