Skip to content

Commit

Permalink
fix: Add IP address validation from paramtable
Browse files Browse the repository at this point in the history
See also milvus-io#37404 milvus-io#37402

IP address in paramtable need validation and fail fast with reasonable
error message

Signed-off-by: Congqi Xia <[email protected]>
  • Loading branch information
congqixia committed Nov 4, 2024
1 parent 9fe90bf commit 142d82d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
12 changes: 12 additions & 0 deletions pkg/util/funcutil/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func GetIP(ip string) string {
if len(ip) == 0 {
return GetLocalIP()
}
netIP := net.ParseIP(ip)
// not a valid ip addr
if netIP == nil {
panic(errors.Newf(`"%s" in param table is not a valid IP address`, ip))
}
// only localhost or unicast is acceptable
if netIP.IsUnspecified() {
panic(errors.Newf(`"%s" in param table is Unspecified IP address and cannot be used`))
}
if netIP.IsMulticast() || netIP.IsLinkLocalMulticast() || netIP.IsInterfaceLocalMulticast() {
panic(errors.Newf(`"%s" in param table is Multicast IP address and cannot be used`))
}
return ip
}

Expand Down
31 changes: 26 additions & 5 deletions pkg/util/funcutil/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,32 @@ func Test_GetLocalIP(t *testing.T) {
}

func Test_GetIP(t *testing.T) {
ip := GetIP("")
assert.NotNil(t, ip)
assert.NotZero(t, len(ip))
ip = GetIP("127.0.0")
assert.Equal(t, ip, "127.0.0")
t.Run("empty_fallback_auto", func(t *testing.T) {
ip := GetIP("")
assert.NotNil(t, ip)
assert.NotZero(t, len(ip))
})

t.Run("valid_ip", func(t *testing.T) {
assert.NotPanics(t, func() {
ip := GetIP("8.8.8.8")
assert.Equal(t, "8.8.8.8", ip)
})
})

t.Run("invalid_ip", func(t *testing.T) {
assert.Panics(t, func() {
GetIP("null")
}, "null is invalid ip address, panicking")

assert.Panics(t, func() {
GetIP("0.0.0.0")
}, "null is unspecified ip address, panicking")

assert.Panics(t, func() {
GetIP("224.0.0.1")
}, "null is multicast ip address, panicking")
})
}

func Test_ParseIndexParamsMap(t *testing.T) {
Expand Down

0 comments on commit 142d82d

Please sign in to comment.