Skip to content

Commit 0fd0394

Browse files
committed
Added tests for path traversal in Resources
1 parent ee5be70 commit 0fd0394

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

arduino/resources/helpers_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,54 @@ func (h *EchoHandler) ServeHTTP(writer http.ResponseWriter, request *http.Reques
3636
request.Write(writer)
3737
}
3838

39+
func TestResourcesSanityChecks(t *testing.T) {
40+
tmp, err := paths.MkTempDir("", "")
41+
require.NoError(t, err)
42+
defer tmp.RemoveAll()
43+
44+
{
45+
testArchiveFileNames := []string{
46+
"test.txt",
47+
"/test.txt",
48+
"somepath/to/test.txt",
49+
"/../test.txt",
50+
"some/../test.txt",
51+
"../test.txt",
52+
}
53+
for _, testArchiveFileName := range testArchiveFileNames {
54+
r := &DownloadResource{
55+
ArchiveFileName: testArchiveFileName,
56+
CachePath: "cache",
57+
}
58+
archivePath, err := r.ArchivePath(tmp)
59+
require.NoError(t, err)
60+
expectedArchivePath := tmp.Join("cache", "test.txt")
61+
require.Equal(t, expectedArchivePath.String(), archivePath.String())
62+
}
63+
}
64+
65+
{
66+
r := &DownloadResource{
67+
ArchiveFileName: "/test.txt",
68+
CachePath: "cache",
69+
}
70+
archivePath, err := r.ArchivePath(tmp)
71+
require.NoError(t, err)
72+
expectedArchivePath := tmp.Join("cache", "test.txt")
73+
require.Equal(t, expectedArchivePath.String(), archivePath.String())
74+
}
75+
76+
{
77+
r := &DownloadResource{
78+
ArchiveFileName: "..",
79+
CachePath: "cache",
80+
}
81+
archivePath, err := r.ArchivePath(tmp)
82+
require.Error(t, err)
83+
require.Nil(t, archivePath)
84+
}
85+
}
86+
3987
func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
4088
goldUserAgentValue := fmt.Sprintf("arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019-06-12 11:11:11.111")
4189
goldUserAgentString := "User-Agent: " + goldUserAgentValue

0 commit comments

Comments
 (0)