Skip to content

Commit 1ceab8b

Browse files
author
Massimiliano Pippi
authored
Avoid calling cloud API if vid or pid are invalid (#439)
* avoid calling cloud API if vid or pid are invalid * user regex
1 parent 6f1b5dd commit 1ceab8b

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

Diff for: commands/board/list.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
"net/http"
25+
"regexp"
2526
"sync"
2627

2728
"github.com/arduino/arduino-cli/cli/globals"
@@ -35,9 +36,20 @@ var (
3536
// ErrNotFound is returned when the API returns 404
3637
ErrNotFound = errors.New("board not found")
3738
m sync.Mutex
39+
vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
40+
validVidPid = regexp.MustCompile(`0[xX][a-fA-F\d]{4}`)
3841
)
3942

40-
func apiByVidPid(url string) ([]*rpc.BoardListItem, error) {
43+
func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
44+
// ensure vid and pid are valid before hitting the API
45+
if !validVidPid.MatchString(vid) {
46+
return nil, errors.Errorf("Invalid vid value: '%s'", vid)
47+
}
48+
if !validVidPid.MatchString(pid) {
49+
return nil, errors.Errorf("Invalid pid value: '%s'", pid)
50+
}
51+
52+
url := fmt.Sprintf("%s/%s/%s", vidPidURL, vid, pid)
4153
retVal := []*rpc.BoardListItem{}
4254
req, _ := http.NewRequest("GET", url, nil)
4355
req.Header = globals.NewHTTPClientHeader()
@@ -110,10 +122,10 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
110122
// the builder API
111123
if len(b) == 0 {
112124
logrus.Debug("Querying builder API for board identification...")
113-
url := fmt.Sprintf("https://builder.arduino.cc/v3/boards/byVidPid/%s/%s",
125+
items, err := apiByVidPid(
114126
port.IdentificationPrefs.Get("vid"),
115-
port.IdentificationPrefs.Get("pid"))
116-
items, err := apiByVidPid(url)
127+
port.IdentificationPrefs.Get("pid"),
128+
)
117129
if err == ErrNotFound {
118130
// the board couldn't be detected, print a warning
119131
logrus.Debug("Board not recognized")

Diff for: commands/board/list_test.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ func TestGetByVidPid(t *testing.T) {
4040
}))
4141
defer ts.Close()
4242

43-
res, err := apiByVidPid(ts.URL)
43+
vidPidURL = ts.URL
44+
res, err := apiByVidPid("0xf420", "0XF069")
4445
require.Nil(t, err)
4546
require.Len(t, res, 1)
4647
require.Equal(t, "Arduino/Genuino MKR1000", res[0].Name)
4748
require.Equal(t, "arduino:samd:mkr1000", res[0].FQBN)
4849

49-
// wrong url
50-
res, err = apiByVidPid("http://0.0.0.0")
50+
// wrong vid (too long), wrong pid (not an hex value)
51+
res, err = apiByVidPid("0xfffff", "0xDEFG")
5152
require.NotNil(t, err)
5253
}
5354

@@ -57,7 +58,8 @@ func TestGetByVidPidNotFound(t *testing.T) {
5758
}))
5859
defer ts.Close()
5960

60-
res, err := apiByVidPid(ts.URL)
61+
vidPidURL = ts.URL
62+
res, err := apiByVidPid("0x0420", "0x0069")
6163
require.NotNil(t, err)
6264
require.Equal(t, "board not found", err.Error())
6365
require.Len(t, res, 0)
@@ -70,7 +72,8 @@ func TestGetByVidPid5xx(t *testing.T) {
7072
}))
7173
defer ts.Close()
7274

73-
res, err := apiByVidPid(ts.URL)
75+
vidPidURL = ts.URL
76+
res, err := apiByVidPid("0x0420", "0x0069")
7477
require.NotNil(t, err)
7578
require.Equal(t, "the server responded with status 500 Internal Server Error", err.Error())
7679
require.Len(t, res, 0)
@@ -82,7 +85,8 @@ func TestGetByVidPidMalformedResponse(t *testing.T) {
8285
}))
8386
defer ts.Close()
8487

85-
res, err := apiByVidPid(ts.URL)
88+
vidPidURL = ts.URL
89+
res, err := apiByVidPid("0x0420", "0x0069")
8690
require.NotNil(t, err)
8791
require.Equal(t, "wrong format in server response", err.Error())
8892
require.Len(t, res, 0)

0 commit comments

Comments
 (0)