Skip to content

Commit d36d0e1

Browse files
upload: don't ignore BindJSON errors (#999)
* upload: don't ignore BindJSON errors
1 parent 63ef111 commit d36d0e1

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

conn.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ type Upload struct {
8080
var uploadStatusStr = "ProgrammerStatus"
8181

8282
func uploadHandler(c *gin.Context) {
83-
8483
data := new(Upload)
85-
c.BindJSON(data)
84+
if err := c.BindJSON(data); err != nil {
85+
c.String(http.StatusBadRequest, fmt.Sprintf("err with the payload. %v", err.Error()))
86+
return
87+
}
8688

8789
log.Printf("%+v %+v %+v %+v %+v %+v", data.Port, data.Board, data.Rewrite, data.Commandline, data.Extra, data.Filename)
8890

main_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818
import (
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+
90115
func TestInstallToolV2(t *testing.T) {
91116

92117
indexURL := "https://downloads.arduino.cc/packages/package_index.json"

0 commit comments

Comments
 (0)