@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"io/ioutil"
24
24
"net/http"
25
+ "regexp"
25
26
"sync"
26
27
27
28
"github.com/arduino/arduino-cli/cli/globals"
35
36
// ErrNotFound is returned when the API returns 404
36
37
ErrNotFound = errors .New ("board not found" )
37
38
m sync.Mutex
39
+ vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
40
+ validVidPid = regexp .MustCompile (`0[xX][a-fA-F\d]{4}` )
38
41
)
39
42
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 )
41
53
retVal := []* rpc.BoardListItem {}
42
54
req , _ := http .NewRequest ("GET" , url , nil )
43
55
req .Header = globals .NewHTTPClientHeader ()
@@ -110,10 +122,10 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
110
122
// the builder API
111
123
if len (b ) == 0 {
112
124
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 (
114
126
port .IdentificationPrefs .Get ("vid" ),
115
- port .IdentificationPrefs .Get ("pid" ))
116
- items , err := apiByVidPid ( url )
127
+ port .IdentificationPrefs .Get ("pid" ),
128
+ )
117
129
if err == ErrNotFound {
118
130
// the board couldn't be detected, print a warning
119
131
logrus .Debug ("Board not recognized" )
0 commit comments