Skip to content

Commit

Permalink
ethstats: fix URL parser for '@' or ':' in node name/password (ethere…
Browse files Browse the repository at this point in the history
…um#21640)

Fixes the case (example below) where the value passed
to --ethstats flag would be parsed wrongly because the
node name and/or password value contained the special
characters '@' or ':'

    --ethstats "ETC Labs Metrics @meowsbits":mypass@ws://mordor.dash.fault.dev:3000
  • Loading branch information
meowsbits authored May 25, 2021
1 parent 49bde05 commit 10962b6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
39 changes: 30 additions & 9 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"math/big"
"net/http"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -144,21 +143,43 @@ func (w *connWrapper) Close() error {
return w.conn.Close()
}

// parseEthstatsURL parses the netstats connection url.
// URL argument should be of the form <nodename:secret@host:port>
// If non-erroring, the returned slice contains 3 elements: [nodename, pass, host]
func parseEthstatsURL(url string) (parts []string, err error) {
err = fmt.Errorf("invalid netstats url: \"%s\", should be nodename:secret@host:port", url)

hostIndex := strings.LastIndex(url, "@")
if hostIndex == -1 || hostIndex == len(url)-1 {
return nil, err
}
preHost, host := url[:hostIndex], url[hostIndex+1:]

passIndex := strings.LastIndex(preHost, ":")
if passIndex == -1 {
return []string{preHost, "", host}, nil
}
nodename, pass := preHost[:passIndex], ""
if passIndex != len(preHost)-1 {
pass = preHost[passIndex+1:]
}

return []string{nodename, pass, host}, nil
}

// New returns a monitoring service ready for stats reporting.
func New(node *node.Node, backend backend, engine consensus.Engine, url string) error {
// Parse the netstats connection url
re := regexp.MustCompile("([^:@]*)(:([^@]*))?@(.+)")
parts := re.FindStringSubmatch(url)
if len(parts) != 5 {
return fmt.Errorf("invalid netstats url: \"%s\", should be nodename:secret@host:port", url)
parts, err := parseEthstatsURL(url)
if err != nil {
return err
}
ethstats := &Service{
backend: backend,
engine: engine,
server: node.Server(),
node: parts[1],
pass: parts[3],
host: parts[4],
node: parts[0],
pass: parts[1],
host: parts[2],
pongCh: make(chan struct{}),
histCh: make(chan []uint64, 1),
}
Expand Down
67 changes: 67 additions & 0 deletions ethstats/ethstats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ethstats

import (
"strconv"
"testing"
)

func TestParseEthstatsURL(t *testing.T) {
cases := []struct {
url string
node, pass, host string
}{
{
url: `"debug meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
node: "debug meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `"debug @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
node: "debug @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `"debug: @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
node: "debug: @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `name:@ws://mordor.dash.fault.dev:3000`,
node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `name@ws://mordor.dash.fault.dev:3000`,
node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `:mypass@ws://mordor.dash.fault.dev:3000`,
node: "", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `:@ws://mordor.dash.fault.dev:3000`,
node: "", pass: "", host: "ws://mordor.dash.fault.dev:3000",
},
}

for i, c := range cases {
parts, err := parseEthstatsURL(c.url)
if err != nil {
t.Fatal(err)
}
node, pass, host := parts[0], parts[1], parts[2]

// unquote because the value provided will be used as a CLI flag value, so unescaped quotes will be removed
nodeUnquote, err := strconv.Unquote(node)
if err == nil {
node = nodeUnquote
}

if node != c.node {
t.Errorf("case=%d mismatch node value, got: %v ,want: %v", i, node, c.node)
}
if pass != c.pass {
t.Errorf("case=%d mismatch pass value, got: %v ,want: %v", i, pass, c.pass)
}
if host != c.host {
t.Errorf("case=%d mismatch host value, got: %v ,want: %v", i, host, c.host)
}
}

}

0 comments on commit 10962b6

Please sign in to comment.