Skip to content

Commit

Permalink
validate -serviceAddr on startup and cli handler (#3156)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliteprox authored Sep 5, 2024
1 parent 384b703 commit 48a92a1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/livepeer/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,11 @@ func StartLivepeer(ctx context.Context, cfg LivepeerConfig) {
if err != nil {
glog.Exit("Error getting service URI: ", err)
}

if *cfg.Network != "offchain" && !common.ValidateServiceURI(suri) {
glog.Warning("**Warning -serviceAddr is a not a public address or hostname; this is not recommended for onchain networks**")
}

n.SetServiceURI(suri)
// if http addr is not provided, listen to all ifaces
// take the port to listen to from the service URI
Expand Down
5 changes: 5 additions & 0 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"math/big"
"math/rand"
"mime"
"net/url"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -530,3 +531,7 @@ func ParseEthAddr(strJsonKey string) (string, error) {
}
return "", errors.New("Error parsing address from keyfile")
}

func ValidateServiceURI(serviceURI *url.URL) bool {
return !strings.Contains(serviceURI.Host, "0.0.0.0")
}
36 changes: 36 additions & 0 deletions common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"
"math/big"
"net/url"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -483,3 +484,38 @@ func TestParseAccelDevices_CustomSelection(t *testing.T) {
assert.Equal(ids[1], "3")
assert.Equal(ids[2], "1")
}
func TestValidateServiceURI(t *testing.T) {
// Valid service URIs
validURIs := []string{
"https://8.8.8.8:8935",
"https://127.0.0.1:8935",
}

for _, uri := range validURIs {
serviceURI, err := url.Parse(uri)
if err != nil {
t.Errorf("Failed to parse valid service URI: %v", err)
}

if !ValidateServiceURI(serviceURI) {
t.Errorf("Expected service URI to be valid, but got invalid: %v", uri)
}
}

// Invalid service URIs
invalidURIs := []string{
"http://0.0.0.0",
"https://0.0.0.0",
}

for _, uri := range invalidURIs {
serviceURI, err := url.Parse(uri)
if err != nil {
t.Errorf("Failed to parse invalid service URI: %v", err)
}

if ValidateServiceURI(serviceURI) {
t.Errorf("Expected service URI to be invalid, but got valid: %v", uri)
}
}
}
6 changes: 6 additions & 0 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ func (s *LivepeerServer) setServiceURI(client eth.LivepeerEthClient, serviceURI
return err
}

if !common.ValidateServiceURI(parsedURI) {
err = errors.New("service address must be a public IP address or hostname")
glog.Error(err)
return err
}

glog.Infof("Storing service URI %v in service registry...", serviceURI)

tx, err := client.SetServiceURI(serviceURI)
Expand Down
23 changes: 23 additions & 0 deletions server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/ethereum/go-ethereum/accounts"
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/livepeer/go-livepeer/core"
"github.com/livepeer/go-livepeer/eth"
"github.com/livepeer/go-livepeer/eth/types"
Expand Down Expand Up @@ -1570,7 +1571,29 @@ func TestMustHaveDb_Success(t *testing.T) {
assert.Equal(http.StatusOK, status)
assert.Equal("success", body)
}
func TestSetServiceURI(t *testing.T) {
s := stubServer()
client := &eth.MockClient{}
serviceURI := "https://8.8.8.8:8935"

t.Run("Valid Service URI", func(t *testing.T) {
client.On("SetServiceURI", serviceURI).Return(&ethtypes.Transaction{}, nil)
client.On("CheckTx", mock.Anything).Return(nil)

err := s.setServiceURI(client, serviceURI)

assert.NoError(t, err)
})

t.Run("Invalid Service URI", func(t *testing.T) {
invalidServiceURI := "https://0.0.0.0:8935"

err := s.setServiceURI(client, invalidServiceURI)

assert.Error(t, err)
})

}
func dummyHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down

0 comments on commit 48a92a1

Please sign in to comment.