From 1c076875c2d39972c7095f43a355644e90ebb764 Mon Sep 17 00:00:00 2001 From: Dalton Hubble Date: Wed, 9 Aug 2017 10:50:25 -0700 Subject: [PATCH] matchbox/client: Validate client endpoint is a host:port * Provide better errors to clients that forget to specify the port or include a protocol scheme by mistake * grpc-go uses net.SplitHostPort to validate server listener addresses are 'host:port', but doesn't validate Dial targets --- matchbox/client/client.go | 7 +++++++ matchbox/client/client_test.go | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/matchbox/client/client.go b/matchbox/client/client.go index c7dc2dbd..08b0de9a 100644 --- a/matchbox/client/client.go +++ b/matchbox/client/client.go @@ -3,6 +3,8 @@ package client import ( "crypto/tls" "errors" + "fmt" + "net" "time" "google.golang.org/grpc" @@ -40,6 +42,11 @@ func New(config *Config) (*Client, error) { if len(config.Endpoints) == 0 { return nil, errNoEndpoints } + for _, endpoint := range config.Endpoints { + if _, _, err := net.SplitHostPort(endpoint); err != nil { + return nil, fmt.Errorf("client: invalid host:port endpoint: %v", err) + } + } return newClient(config) } diff --git a/matchbox/client/client_test.go b/matchbox/client/client_test.go index 4c79d159..d80a369a 100644 --- a/matchbox/client/client_test.go +++ b/matchbox/client/client_test.go @@ -14,3 +14,20 @@ func TestNew_MissingEndpoints(t *testing.T) { assert.Nil(t, client) assert.Equal(t, errNoEndpoints, err) } + +// gRPC expects host:port with no scheme (e.g. matchbox.example.com:8081) +func TestNew_InvalidEndpoints(t *testing.T) { + invalid := []string{ + "matchbox.example.com", + "http://matchbox.example.com:8081", + "https://matchbox.example.com:8081", + } + + for _, endpoint := range invalid { + client, err := New(&Config{ + Endpoints: []string{endpoint}, + }) + assert.Nil(t, client) + assert.Error(t, err) + } +}