@@ -29,6 +29,7 @@ import (
29
29
30
30
"github.com/arduino/arduino-create-agent/config"
31
31
"github.com/arduino/arduino-create-agent/gen/tools"
32
+ "github.com/arduino/arduino-create-agent/upload"
32
33
v2 "github.com/arduino/arduino-create-agent/v2"
33
34
"github.com/gin-gonic/gin"
34
35
"github.com/stretchr/testify/require"
@@ -114,3 +115,38 @@ func TestInstallToolV2(t *testing.T) {
114
115
})
115
116
}
116
117
}
118
+ func TestUploadHandlerAgainstEvilFileNames (t * testing.T ) {
119
+ r := gin .New ()
120
+ r .POST ("/" , uploadHandler )
121
+ ts := httptest .NewServer (r )
122
+
123
+ uploadEvilFileName := Upload {
124
+ Port : "/dev/ttyACM0" ,
125
+ Board : "arduino:avr:uno" ,
126
+ Extra : upload.Extra {Network : true },
127
+ Hex : []byte ("test" ),
128
+ Filename : "../evil.txt" ,
129
+ ExtraFiles : []additionalFile {{Hex : []byte ("test" ), Filename : "../evil.txt" }},
130
+ }
131
+ uploadEvilExtraFile := Upload {
132
+ Port : "/dev/ttyACM0" ,
133
+ Board : "arduino:avr:uno" ,
134
+ Extra : upload.Extra {Network : true },
135
+ Hex : []byte ("test" ),
136
+ Filename : "file.txt" ,
137
+ ExtraFiles : []additionalFile {{Hex : []byte ("test" ), Filename : "../evil.txt" }},
138
+ }
139
+
140
+ for _ , request := range []Upload {uploadEvilFileName , uploadEvilExtraFile } {
141
+ payload , err := json .Marshal (request )
142
+ require .NoError (t , err )
143
+
144
+ resp , err := http .Post (ts .URL , "encoding/json" , bytes .NewBuffer (payload ))
145
+ require .NoError (t , err )
146
+ require .Equal (t , http .StatusBadRequest , resp .StatusCode )
147
+
148
+ body , err := io .ReadAll (resp .Body )
149
+ require .NoError (t , err )
150
+ require .Contains (t , string (body ), "unsafe path join" )
151
+ }
152
+ }
0 commit comments