Handle malformed and IPv6 config addresses
This commit is contained in:
committed by
Brandon McGinty
parent
4aa4fd834f
commit
501943c8f7
+11
-5
@@ -6,6 +6,7 @@ import (
|
||||
"git.stormux.org/storm/barnard/uiterm"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -367,7 +368,7 @@ func readFile(path string) []byte {
|
||||
|
||||
func fileExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
@@ -389,11 +390,16 @@ func resolvePath(path string) string {
|
||||
}
|
||||
|
||||
func makeHostPort(addr string) (string, int) {
|
||||
parts := strings.Split(addr, ":")
|
||||
host := parts[0]
|
||||
port, err := strconv.Atoi(parts[1])
|
||||
// SplitHostPort correctly handles bracketed IPv6. Invalid or portless
|
||||
// addresses stay usable as a host with Mumble's default port instead of
|
||||
// crashing configuration operations.
|
||||
host, portText, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return strings.Trim(addr, "[]"), 64738
|
||||
}
|
||||
port, err := strconv.Atoi(portText)
|
||||
if err != nil || port < 1 || port > 65535 {
|
||||
return host, 64738
|
||||
}
|
||||
return host, port
|
||||
}
|
||||
|
||||
@@ -37,6 +37,19 @@ func TestConfigBackfillsRecordingDefaults(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Regression: malformed and IPv6 addresses were split at every colon and
|
||||
// could panic while merely reading a saved user preference.
|
||||
func TestMakeHostPortHandlesIPv6AndMalformedAddress(t *testing.T) {
|
||||
host, port := makeHostPort("[2001:db8::1]:64739")
|
||||
if host != "2001:db8::1" || port != 64739 {
|
||||
t.Fatalf("got %q:%d", host, port)
|
||||
}
|
||||
host, port = makeHostPort("not-a-host-port")
|
||||
if host != "not-a-host-port" || port != 64738 {
|
||||
t.Fatalf("got %q:%d", host, port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigUsesHomeEnvironmentForDefaultPath(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("HOME", dir)
|
||||
|
||||
Reference in New Issue
Block a user