Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions util/builtin_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,23 +588,29 @@ func RegexMatchFunc(args ...interface{}) (interface{}, error) {

// IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
// For example, "192.168.2.123" matches "192.168.2.0/24".
// An invalid argument matches nothing; IPMatchFunc reports it as an error.
func IPMatch(ip1 string, ip2 string) bool {
matched, _ := ipMatch(ip1, ip2)
return matched
}

func ipMatch(ip1 string, ip2 string) (bool, error) {
objIP1 := net.ParseIP(ip1)
if objIP1 == nil {
panic("invalid argument: ip1 in IPMatch() function is not an IP address.")
return false, errors.New("invalid argument: ip1 in IPMatch() function is not an IP address")
}

_, cidr, err := net.ParseCIDR(ip2)
if err != nil {
objIP2 := net.ParseIP(ip2)
if objIP2 == nil {
panic("invalid argument: ip2 in IPMatch() function is neither an IP address nor a CIDR.")
return false, errors.New("invalid argument: ip2 in IPMatch() function is neither an IP address nor a CIDR")
}

return objIP1.Equal(objIP2)
return objIP1.Equal(objIP2), nil
}

return cidr.Contains(objIP1)
return cidr.Contains(objIP1), nil
}

// IPMatchFunc is the wrapper for IPMatch.
Expand All @@ -616,7 +622,12 @@ func IPMatchFunc(args ...interface{}) (interface{}, error) {
ip1 := args[0].(string)
ip2 := args[1].(string)

return IPMatch(ip1, ip2), nil
matched, err := ipMatch(ip1, ip2)
if err != nil {
return false, fmt.Errorf("%s: %w", "ipMatch", err)
}

return matched, nil
}

// GlobMatch determines whether key1 matches the pattern of key2 using glob pattern.
Expand Down
27 changes: 27 additions & 0 deletions util/builtin_operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,33 @@ func testIPMatch(t *testing.T, ip1 string, ip2 string, res bool) {
}
}

func TestIPMatchInvalidInput(t *testing.T) {
// An invalid argument matches nothing instead of panicking,
// mirroring the RegexMatch convention: the plain function returns
// false and the Func wrapper reports the error.
if IPMatch("192.168.1.abc", "192.168.1.0/24") {
t.Errorf("IPMatch(192.168.1.abc, 192.168.1.0/24): got true, expected false")
}
if IPMatch("192.168.2.123", "not-an-ip") {
t.Errorf("IPMatch(192.168.2.123, not-an-ip): got true, expected false")
}

_, err := ipMatch("192.168.1.abc", "192.168.1.0/24")
if err == nil {
t.Errorf("ipMatch(192.168.1.abc, ...): expected an error, got nil")
}

_, err = IPMatchFunc("192.168.1.abc", "192.168.1.0/24")
if err == nil || err.Error() != "ipMatch: invalid argument: ip1 in IPMatch() function is not an IP address" {
t.Errorf("IPMatchFunc(192.168.1.abc, ...): unexpected error: %v", err)
}

_, err = IPMatchFunc("192.168.2.123", "not-an-ip")
if err == nil || err.Error() != "ipMatch: invalid argument: ip2 in IPMatch() function is neither an IP address nor a CIDR" {
t.Errorf("IPMatchFunc(..., not-an-ip): unexpected error: %v", err)
}
}

func TestIPMatch(t *testing.T) {
testIPMatch(t, "192.168.2.123", "192.168.2.0/24", true)
testIPMatch(t, "192.168.2.123", "192.168.3.0/24", false)
Expand Down
Loading