16
16
package main
17
17
18
18
import (
19
+ "bytes"
19
20
"crypto/x509"
21
+ "encoding/json"
20
22
"encoding/pem"
23
+ "io"
24
+ "net/http"
25
+ "net/http/httptest"
21
26
"path/filepath"
22
27
"testing"
23
28
29
+ "github.com/arduino/arduino-create-agent/upload"
30
+ "github.com/gin-gonic/gin"
24
31
"github.com/stretchr/testify/require"
25
32
)
26
33
@@ -38,3 +45,39 @@ func TestValidSignatureKey(t *testing.T) {
38
45
require .NoError (t , err )
39
46
require .NotNil (t , key )
40
47
}
48
+
49
+ func TestUploadHandlerAgainstEvilFileNames (t * testing.T ) {
50
+ r := gin .New ()
51
+ r .POST ("/" , uploadHandler )
52
+ ts := httptest .NewServer (r )
53
+
54
+ uploadEvilFileName := Upload {
55
+ Port : "/dev/ttyACM0" ,
56
+ Board : "arduino:avr:uno" ,
57
+ Extra : upload.Extra {Network : true },
58
+ Hex : []byte ("test" ),
59
+ Filename : "../evil.txt" ,
60
+ ExtraFiles : []additionalFile {{Hex : []byte ("test" ), Filename : "../evil.txt" }},
61
+ }
62
+ uploadEvilExtraFile := Upload {
63
+ Port : "/dev/ttyACM0" ,
64
+ Board : "arduino:avr:uno" ,
65
+ Extra : upload.Extra {Network : true },
66
+ Hex : []byte ("test" ),
67
+ Filename : "file.txt" ,
68
+ ExtraFiles : []additionalFile {{Hex : []byte ("test" ), Filename : "../evil.txt" }},
69
+ }
70
+
71
+ for _ , request := range []Upload {uploadEvilFileName , uploadEvilExtraFile } {
72
+ payload , err := json .Marshal (request )
73
+ require .NoError (t , err )
74
+
75
+ resp , err := http .Post (ts .URL , "encoding/json" , bytes .NewBuffer (payload ))
76
+ require .NoError (t , err )
77
+ require .Equal (t , http .StatusBadRequest , resp .StatusCode )
78
+
79
+ body , err := io .ReadAll (resp .Body )
80
+ require .NoError (t , err )
81
+ require .Contains (t , string (body ), "unsafe path join" )
82
+ }
83
+ }
0 commit comments