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

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

‎arduino/resources/helpers.go‎

Lines changed: 5 additions & 1 deletion
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)

‎arduino/resources/helpers_test.go‎

Lines changed: 25 additions & 8 deletions
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

‎go.mod‎

Lines changed: 1 addition & 1 deletion
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

‎go.sum‎

Lines changed: 2 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /