Skip to content

Commit

Permalink
Fix IPv6 validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pschork committed Apr 30, 2024
1 parent ae643c1 commit d7f5a4b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
19 changes: 7 additions & 12 deletions disperser/dataapi/operator_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package dataapi
import (
"context"
"errors"
"fmt"
"net"
"sort"
"strings"
"time"

"github.com/Layr-Labs/eigenda/core"
Expand Down Expand Up @@ -107,27 +105,24 @@ func checkIsOnlineAndProcessOperator(operatorStatus OperatorOnlineStatus, operat
}

// Check that the socketString is not private/unspecified
func ValidOperatorIP(socketString string, logger logging.Logger) bool {
host := strings.Split(socketString, ":")[0]
func ValidOperatorIP(address string, logger logging.Logger) bool {
host, _, err := net.SplitHostPort(address)
if err != nil {
logger.Error("Failed to split host port", "address", address, "error", err)
return false
}
ips, err := net.LookupIP(host)
fmt.Printf(" Check Socket %s\n", socketString)
if err != nil {
fmt.Printf(" IP error - %s\n", err)
logger.Error("Error resolving operator host IP", "host", host, "error", err)
return false
}
ipAddr := ips[0]
if ipAddr == nil {
fmt.Printf(" Nil error - %s\n", err)
logger.Error("IP address is nil", "host", host, "ips", ips)
return false
}
fmt.Printf(" IPS %v\n", ips)
fmt.Printf(" IP %v\n", ipAddr)
fmt.Printf(" isPrivate %v\n", ipAddr.IsPrivate())
fmt.Printf(" isUnspecified %v\n", ipAddr.IsUnspecified())
isValid := !ipAddr.IsPrivate() && !ipAddr.IsUnspecified()
logger.Debug("Operator IP validation", "socketString", socketString, "host", host, "ips", ips, "ipAddr", ipAddr, "isValid", isValid)
logger.Debug("Operator IP validation", "address", address, "host", host, "ips", ips, "ipAddr", ipAddr, "isValid", isValid)

return isValid
}
Expand Down
5 changes: 3 additions & 2 deletions disperser/dataapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,13 @@ func TestPortCheckIpValidation(t *testing.T) {
assert.Equal(t, false, dataapi.ValidOperatorIP("0.0.0.0:32005", mockLogger))
assert.Equal(t, false, dataapi.ValidOperatorIP("10.0.0.1:32005", mockLogger))
assert.Equal(t, false, dataapi.ValidOperatorIP("::ffff:192.0.2.1:32005", mockLogger))
assert.Equal(t, false, dataapi.ValidOperatorIP("google.com", mockLogger))
assert.Equal(t, true, dataapi.ValidOperatorIP("localhost:32005", mockLogger))
assert.Equal(t, true, dataapi.ValidOperatorIP("127.0.0.1:32005", mockLogger))
assert.Equal(t, true, dataapi.ValidOperatorIP("23.93.76.1:32005", mockLogger))
assert.Equal(t, true, dataapi.ValidOperatorIP("google.com:32005", mockLogger))
assert.Equal(t, true, dataapi.ValidOperatorIP("google.com", mockLogger))
assert.Equal(t, true, dataapi.ValidOperatorIP("2606:4700:4400::ac40:98f1:32005", mockLogger))
assert.Equal(t, true, dataapi.ValidOperatorIP("[2606:4700:4400::ac40:98f1]:32005", mockLogger))
assert.Equal(t, false, dataapi.ValidOperatorIP("2606:4700:4400::ac40:98f1:32005", mockLogger))
}

func TestPortCheck(t *testing.T) {
Expand Down

0 comments on commit d7f5a4b

Please sign in to comment.