@@ -18,6 +18,7 @@ package main
18
18
import (
19
19
"bytes"
20
20
"crypto/x509"
21
+ "encoding/base64"
21
22
"encoding/json"
22
23
"encoding/pem"
23
24
"fmt"
@@ -56,6 +57,11 @@ func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
56
57
r .POST ("/" , uploadHandler )
57
58
ts := httptest .NewServer (r )
58
59
60
+ fmt .Println (base64 .StdEncoding .EncodeToString ([]byte ("test" )))
61
+
62
+ //Padding: dGVzdA==
63
+ //Raw: dGVzdA
64
+
59
65
uploadEvilFileName := Upload {
60
66
Port : "/dev/ttyACM0" ,
61
67
Board : "arduino:avr:uno" ,
@@ -87,6 +93,30 @@ func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
87
93
}
88
94
}
89
95
96
+ func TestUploadHandlerAgainstBase64WithoutPaddingMustFail (t * testing.T ) {
97
+ r := gin .New ()
98
+ r .POST ("/" , uploadHandler )
99
+ ts := httptest .NewServer (r )
100
+ defer ts .Close ()
101
+
102
+ // When calling the `BindJSON` func, when a json field will be Unmarshaled
103
+ // in a []byte type, we expect to receive a base64 padded string in input.
104
+ // In case we receive a base64 unpadded string BindJSON fails.
105
+ // The expectation here is that the upload handler won't continue with the
106
+ // upload operation.
107
+ base64ContentWithoutPadding := base64 .RawStdEncoding .EncodeToString ([]byte ("test" ))
108
+ payload := fmt .Sprintf (`{"hex": "%s"}` , base64ContentWithoutPadding )
109
+
110
+ resp , err := http .Post (ts .URL , "encoding/json" , bytes .NewBufferString (payload ))
111
+ require .NoError (t , err )
112
+ require .Equal (t , http .StatusBadRequest , resp .StatusCode )
113
+
114
+ defer resp .Body .Close ()
115
+ body , err := io .ReadAll (resp .Body )
116
+ require .NoError (t , err )
117
+ require .Contains (t , string (body ), "err with the payload. illegal base64 data at input" )
118
+ }
119
+
90
120
func TestInstallToolV2 (t * testing.T ) {
91
121
92
122
indexURL := "https://downloads.arduino.cc/packages/package_index.json"
0 commit comments