Skip to content
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

validate -serviceAddr on startup and cli handler #3156

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
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
9 changes: 9 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,11 @@ func ParseEthAddr(strJsonKey string) (string, error) {
}
return "", errors.New("Error parsing address from keyfile")
}

func ValidateServiceURI(serviceURI *url.URL) bool {
if strings.Contains(serviceURI.Host, "0.0.0.0") {
leszko marked this conversation as resolved.
Show resolved Hide resolved
return false
} else {
return true
}
}
leszko marked this conversation as resolved.
Show resolved Hide resolved
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
Loading