Skip to content

Commit

Permalink
fix: validate relay port numbers as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ZinoKader committed Feb 24, 2023
1 parent 6fadc89 commit efde99f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions cmd/portal/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"errors"
"net"
"regexp"
"strconv"
"strings"

"github.com/spf13/viper"
"golang.org/x/net/idna"
)

var ErrInvalidRelay = errors.New("invalid relay provided")
var ErrInvalidRelay = errors.New("invalid relay address provided")

var ipv6Rex = regexp.MustCompile(`\[(.*?)\]`)

Expand All @@ -31,7 +32,22 @@ func stripPort(addr string) string {
func validateRelayInViper() error {
relayAddr := viper.GetString("relay")

if ip := net.ParseIP(stripPort(relayAddr)); ip != nil {
onlyHost := stripPort(relayAddr)
if relayAddr != onlyHost {
_, port, err := net.SplitHostPort(relayAddr)
if err != nil {
return ErrInvalidRelay
}
portNumber, err := strconv.Atoi(port)
if err != nil {
return ErrInvalidRelay
}
if portNumber < 1 || portNumber > 65535 {
return ErrInvalidRelay
}
}

if ip := net.ParseIP(onlyHost); ip != nil {
return nil
}

Expand Down

0 comments on commit efde99f

Please sign in to comment.