Skip to content

Avoid calling cloud API if vid or pid are invalid #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"sync"

"github.com/arduino/arduino-cli/cli/globals"
Expand All @@ -35,9 +36,18 @@ var (
// ErrNotFound is returned when the API returns 404
ErrNotFound = errors.New("board not found")
m sync.Mutex
vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
)

func apiByVidPid(url string) ([]*rpc.BoardListItem, error) {
func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
// ensure vid and pid are valid before hitting the API
_, vidErr := strconv.ParseInt(vid, 0, 64)
_, pidErr := strconv.ParseInt(pid, 0, 64)
if vidErr != nil || pidErr != nil {
return nil, errors.Errorf("Invalid vid/pid value: '%s:%s'", vid, pid)
}

url := fmt.Sprintf("%s/%s/%s", vidPidURL, vid, pid)
retVal := []*rpc.BoardListItem{}
req, _ := http.NewRequest("GET", url, nil)
req.Header = globals.NewHTTPClientHeader()
Expand Down Expand Up @@ -110,10 +120,10 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
// the builder API
if len(b) == 0 {
logrus.Debug("Querying builder API for board identification...")
url := fmt.Sprintf("https://builder.arduino.cc/v3/boards/byVidPid/%s/%s",
items, err := apiByVidPid(
port.IdentificationPrefs.Get("vid"),
port.IdentificationPrefs.Get("pid"))
items, err := apiByVidPid(url)
port.IdentificationPrefs.Get("pid"),
)
if err == ErrNotFound {
// the board couldn't be detected, print a warning
logrus.Debug("Board not recognized")
Expand Down
16 changes: 10 additions & 6 deletions commands/board/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ func TestGetByVidPid(t *testing.T) {
}))
defer ts.Close()

res, err := apiByVidPid(ts.URL)
vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069")
require.Nil(t, err)
require.Len(t, res, 1)
require.Equal(t, "Arduino/Genuino MKR1000", res[0].Name)
require.Equal(t, "arduino:samd:mkr1000", res[0].FQBN)

// wrong url
res, err = apiByVidPid("http://0.0.0.0")
// wrong vid/pid
res, err = apiByVidPid("foo", "")
require.NotNil(t, err)
}

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

res, err := apiByVidPid(ts.URL)
vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069")
require.NotNil(t, err)
require.Equal(t, "board not found", err.Error())
require.Len(t, res, 0)
Expand All @@ -70,7 +72,8 @@ func TestGetByVidPid5xx(t *testing.T) {
}))
defer ts.Close()

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

res, err := apiByVidPid(ts.URL)
vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069")
require.NotNil(t, err)
require.Equal(t, "wrong format in server response", err.Error())
require.Len(t, res, 0)
Expand Down