Skip to content

Commit 258eacb

Browse files
committed
Use paths.SafeNew when dealing with external sources
1 parent 4b3d731 commit 258eacb

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

Diff for: arduino/resources/helpers.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ func (r *DownloadResource) ArchivePath(downloadDir *paths.Path) (*paths.Path, er
3333
}
3434

3535
// Filter out paths from file name
36-
archiveFileName := paths.New(r.ArchiveFileName).Base()
36+
archiveFile, err := paths.SafeNew(r.ArchiveFileName)
37+
if err != nil {
38+
return nil, errors.Errorf("invalid filename: %s", r.ArchiveFileName)
39+
}
40+
archiveFileName := archiveFile.Base()
3741
archivePath := staging.Join(archiveFileName).Clean()
3842
if archivePath.IsDir() {
3943
return nil, errors.Errorf("invalid filename or exinsting directory: %s", archivePath)

Diff for: arduino/resources/helpers_test.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ func TestResourcesSanityChecks(t *testing.T) {
4646
"test.txt",
4747
"/test.txt",
4848
"somepath/to/test.txt",
49-
"/../test.txt",
50-
"some/../test.txt",
49+
"/somepath/to/test.txt",
50+
"path/to/../test.txt",
51+
"/path/to/../test.txt",
5152
"../test.txt",
53+
"/../test.txt",
5254
}
5355
for _, testArchiveFileName := range testArchiveFileNames {
5456
r := &DownloadResource{
@@ -74,13 +76,28 @@ func TestResourcesSanityChecks(t *testing.T) {
7476
}
7577

7678
{
77-
r := &DownloadResource{
78-
ArchiveFileName: "..",
79-
CachePath: "cache",
79+
testArchiveFileNames := []string{
80+
"/",
81+
".",
82+
"/.",
83+
"..",
84+
"/..",
85+
"path/..",
86+
"/path/..",
87+
"path/path/..",
88+
"/path/path/..",
89+
".." + string([]byte{0xC0, 0xAF}) + "test.txt",
90+
"/.." + string([]byte{0xC0, 0xAF}) + "test.txt",
91+
}
92+
for _, testArchiveFileName := range testArchiveFileNames {
93+
r := &DownloadResource{
94+
ArchiveFileName: testArchiveFileName,
95+
CachePath: "cache",
96+
}
97+
archivePath, err := r.ArchivePath(tmp)
98+
require.Nil(t, archivePath)
99+
require.Error(t, err)
80100
}
81-
archivePath, err := r.ArchivePath(tmp)
82-
require.Error(t, err)
83-
require.Nil(t, archivePath)
84101
}
85102
}
86103

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
bou.ke/monkey v1.0.1
1010
github.com/GeertJohan/go.rice v1.0.0
1111
github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c
12-
github.com/arduino/go-paths-helper v1.2.0
12+
github.com/arduino/go-paths-helper v1.2.1-0.20200802112116-33dcc69b14ba
1313
github.com/arduino/go-properties-orderedmap v1.3.0
1414
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b
1515
github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ github.com/arduino/go-paths-helper v1.0.1 h1:utYXLM2RfFlc9qp/MJTIYp3t6ux/xM6mWje
1818
github.com/arduino/go-paths-helper v1.0.1/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
1919
github.com/arduino/go-paths-helper v1.2.0 h1:qDW93PR5IZUN/jzO4rCtexiwF8P4OIcOmcSgAYLZfY4=
2020
github.com/arduino/go-paths-helper v1.2.0/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
21+
github.com/arduino/go-paths-helper v1.2.1-0.20200802112116-33dcc69b14ba h1:rQtLTpIICgc8ad2UG/A7X1F4TpKGoazBxhKR+crsf4k=
22+
github.com/arduino/go-paths-helper v1.2.1-0.20200802112116-33dcc69b14ba/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
2123
github.com/arduino/go-properties-orderedmap v1.3.0 h1:4No/vQopB36e7WUIk6H6TxiSEJPiMrVOCZylYmua39o=
2224
github.com/arduino/go-properties-orderedmap v1.3.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
2325
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b h1:9hDi4F2st6dbLC3y4i02zFT5quS4X6iioWifGlVwfy4=

0 commit comments

Comments
 (0)