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

@@ -0,0 +1,44 @@
package network
import (
"fmt"
"net"
"regexp"
)
var reIPv4AndPort = regexp.MustCompile(`^(\d+\.\d+\.\d+\.\d+):\d+$`)
// An IPv6 address/port pair can consist of the IP address enclosed in square brackets followed by a colon and
// a port, although the colon/port component is actually optional in practice (e.g., we may receive [::1], where
// we should just strip off the square brackets).
var reIPv6AndPort = regexp.MustCompile(`^\[(.+)\](:\d+)?$`)
// GetIPFromAddress tries to get an IPv4 or IPv6 address from a host address, potentially including a port.
func GetIPFromAddress(input string) (net.IP, error) {
if a := net.ParseIP(input); len(a) > 0 {
return a, nil
}
err := fmt.Errorf("not a valid IP address or IP address/port pair: %q", input)
// It could potentially be an IP address/port pair
var addr string
ms := reIPv4AndPort.FindStringSubmatch(input)
if len(ms) == 0 {
ms := reIPv6AndPort.FindStringSubmatch(input)
if len(ms) == 0 {
return nil, err
}
addr = ms[1]
} else {
// Strip off port
addr = ms[1]
}
if a := net.ParseIP(addr); len(a) > 0 {
return a, nil
}
return nil, err
}

View File

@@ -0,0 +1,82 @@
package network
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetIPFromAddress(t *testing.T) {
testCases := []struct {
desc string
input string
exp string
expErr string
}{
{
desc: "Valid IPv4",
input: "192.168.2.1",
exp: "192.168.2.1",
},
{
desc: "Valid IPv6",
input: "2001:0db8:0000:0000:0000:ff00:0042:8329",
exp: "2001:db8::ff00:42:8329",
},
{
desc: "Valid IPv6 enclosed in square brackets",
input: "[2001:0db8:0000:0000:0000:ff00:0042:8329]",
exp: "2001:db8::ff00:42:8329",
},
{
desc: "Valid IPv4/port pair",
input: "192.168.2.1:5000",
exp: "192.168.2.1",
},
{
desc: "Valid IPv6/port pair",
input: "[2001:0db8:0000:0000:0000:ff00:0042:8329]:5000",
exp: "2001:db8::ff00:42:8329",
},
{
desc: "Invalid IPv6/port pair",
input: "[2001:0db8:0000:0000:0000:ff00:0042:8329]:5000:2000",
expErr: `not a valid IP address or IP address/port pair: "[2001:0db8:0000:0000:0000:ff00:0042:8329]:5000:2000"`,
},
{
desc: "IPv6 with too many parts",
input: "2001:0db8:0000:0000:0000:ff00:0042:8329:1234",
expErr: `not a valid IP address or IP address/port pair: "2001:0db8:0000:0000:0000:ff00:0042:8329:1234"`,
},
{
desc: "IPv6 with too few parts",
input: "2001:0db8:0000:0000:0000:ff00:0042",
expErr: `not a valid IP address or IP address/port pair: "2001:0db8:0000:0000:0000:ff00:0042"`,
},
{
desc: "Valid shortened IPv6",
input: "2001:db8::ff00:42:8329",
exp: "2001:db8::ff00:42:8329",
},
{
desc: "IPv6 loopback address",
input: "::1",
exp: "::1",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
ip, err := GetIPFromAddress(tc.input)
if tc.expErr == "" {
exp := net.ParseIP(tc.exp)
require.NotNil(t, exp)
require.NoError(t, err)
assert.Equal(t, exp, ip)
} else {
require.EqualError(t, err, tc.expErr)
}
})
}
}