@@ -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"
@@ -87,6 +88,30 @@ func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
87
88
}
88
89
}
89
90
91
+ func TestUploadHandlerAgainstBase64WithoutPaddingMustFail (t * testing.T ) {
92
+ r := gin .New ()
93
+ r .POST ("/" , uploadHandler )
94
+ ts := httptest .NewServer (r )
95
+ defer ts .Close ()
96
+
97
+ // When calling the `BindJSON` func, when a json field will be Unmarshaled
98
+ // in a []byte type, we expect to receive a base64 padded string in input.
99
+ // In case we receive a base64 unpadded string BindJSON fails.
100
+ // The expectation here is that the upload handler won't continue with the
101
+ // upload operation.
102
+ base64ContentWithoutPadding := base64 .RawStdEncoding .EncodeToString ([]byte ("test" ))
103
+ payload := fmt .Sprintf (`{"hex": "%s"}` , base64ContentWithoutPadding )
104
+
105
+ resp , err := http .Post (ts .URL , "encoding/json" , bytes .NewBufferString (payload ))
106
+ require .NoError (t , err )
107
+ require .Equal (t , http .StatusBadRequest , resp .StatusCode )
108
+
109
+ defer resp .Body .Close ()
110
+ body , err := io .ReadAll (resp .Body )
111
+ require .NoError (t , err )
112
+ require .Contains (t , string (body ), "err with the payload. illegal base64 data at input" )
113
+ }
114
+
90
115
func TestInstallToolV2 (t * testing.T ) {
91
116
92
117
indexURL := "https://downloads.arduino.cc/packages/package_index.json"
0 commit comments