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"
|
"git.stormux.org/storm/barnard/uiterm"
|
||||||
"github.com/pelletier/go-toml/v2"
|
"github.com/pelletier/go-toml/v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -367,7 +368,7 @@ func readFile(path string) []byte {
|
|||||||
|
|
||||||
func fileExists(path string) bool {
|
func fileExists(path string) bool {
|
||||||
info, err := os.Stat(path)
|
info, err := os.Stat(path)
|
||||||
if os.IsNotExist(err) {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return !info.IsDir()
|
return !info.IsDir()
|
||||||
@@ -389,11 +390,16 @@ func resolvePath(path string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeHostPort(addr string) (string, int) {
|
func makeHostPort(addr string) (string, int) {
|
||||||
parts := strings.Split(addr, ":")
|
// SplitHostPort correctly handles bracketed IPv6. Invalid or portless
|
||||||
host := parts[0]
|
// addresses stay usable as a host with Mumble's default port instead of
|
||||||
port, err := strconv.Atoi(parts[1])
|
// crashing configuration operations.
|
||||||
|
host, portText, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
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
|
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) {
|
func TestConfigUsesHomeEnvironmentForDefaultPath(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
t.Setenv("HOME", dir)
|
t.Setenv("HOME", dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user