Backend: fix IPv6 address parsing erroneous (#28585)

* Backend: Fix parsing of client IP address

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
taciomcosta
2020-11-25 03:55:22 -03:00
committed by GitHub
parent 3d33de1751
commit 10ff4eecef
15 changed files with 228 additions and 147 deletions

View File

@@ -8,30 +8,6 @@ import (
"github.com/grafana/grafana/pkg/util/errutil"
)
// ParseIPAddress parses an IP address and removes port and/or IPV6 format
func ParseIPAddress(input string) (string, error) {
addr, err := SplitHostPort(input)
if err != nil {
return "", errutil.Wrapf(err, "failed to split network address %q by host and port",
input)
}
ip := net.ParseIP(addr.Host)
if ip == nil {
return addr.Host, nil
}
if ip.IsLoopback() {
if strings.Contains(addr.Host, ":") {
// IPv6
return "::1", nil
}
return "127.0.0.1", nil
}
return ip.String(), nil
}
type NetworkAddress struct {
Host string
Port string
@@ -79,11 +55,3 @@ func SplitHostPortDefault(input, defaultHost, defaultPort string) (NetworkAddres
return addr, nil
}
// SplitHostPort splits ip address/hostname string by host and port
func SplitHostPort(input string) (NetworkAddress, error) {
if len(input) == 0 {
return NetworkAddress{}, fmt.Errorf("input is empty")
}
return SplitHostPortDefault(input, "", "")
}

View File

@@ -4,52 +4,8 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseIPAddress_Valid(t *testing.T) {
tests := []struct {
input string
expected string
}{
{input: "127.0.0.1", expected: "127.0.0.1"},
{input: "192.168.0.140:456", expected: "192.168.0.140"},
{input: "192.168.0.140", expected: "192.168.0.140"},
{input: "[::1]:456", expected: "::1"},
{input: "[::1]", expected: "::1"},
}
for _, testcase := range tests {
addr, err := ParseIPAddress(testcase.input)
require.NoError(t, err)
assert.Equal(t, testcase.expected, addr)
}
}
func TestParseIPAddress_Invalid(t *testing.T) {
tests := []struct {
input string
err string
}{
{
input: "[::1",
err: "failed to split network address \"[::1\" by host and port: malformed IPv6 address: '[::1'",
},
{
input: "::1]",
err: "failed to split network address \"::1]\" by host and port: net.SplitHostPort failed for '::1]': address ::1]: too many colons in address",
},
{
input: "",
err: "failed to split network address \"\" by host and port: input is empty",
},
}
for _, testcase := range tests {
addr, err := ParseIPAddress(testcase.input)
assert.EqualError(t, err, testcase.err)
assert.Empty(t, addr)
}
}
func TestSplitHostPortDefault_Valid(t *testing.T) {
tests := []struct {
input string
@@ -76,25 +32,3 @@ func TestSplitHostPortDefault_Valid(t *testing.T) {
assert.Equal(t, testcase.port, addr.Port)
}
}
func TestSplitHostPort_Valid(t *testing.T) {
tests := []struct {
input string
host string
port string
}{
{input: "192.168.0.140:456", host: "192.168.0.140", port: "456"},
{input: "192.168.0.140", host: "192.168.0.140", port: ""},
{input: "[::1]:456", host: "::1", port: "456"},
{input: "[::1]", host: "::1", port: ""},
{input: ":456", host: "", port: "456"},
{input: "xyz.rds.amazonaws.com", host: "xyz.rds.amazonaws.com", port: ""},
{input: "xyz.rds.amazonaws.com:123", host: "xyz.rds.amazonaws.com", port: "123"},
}
for _, testcase := range tests {
addr, err := SplitHostPort(testcase.input)
require.NoError(t, err)
assert.Equal(t, testcase.host, addr.Host)
assert.Equal(t, testcase.port, addr.Port)
}
}