@@ -18,6 +18,7 @@ package main
1818import (
1919 "bytes"
2020 "crypto/x509"
21+ "encoding/base64"
2122 "encoding/json"
2223 "encoding/pem"
2324 "fmt"
@@ -87,6 +88,30 @@ func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
8788 }
8889}
8990
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+
90115func TestInstallToolV2 (t * testing.T ) {
91116
92117 indexURL := "https://downloads.arduino.cc/packages/package_index.json"
0 commit comments