diff --git a/util/builtin_operators.go b/util/builtin_operators.go index 87ed875b..3b7706eb 100644 --- a/util/builtin_operators.go +++ b/util/builtin_operators.go @@ -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. @@ -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. diff --git a/util/builtin_operators_test.go b/util/builtin_operators_test.go index c9445d33..6fc9eaa5 100644 --- a/util/builtin_operators_test.go +++ b/util/builtin_operators_test.go @@ -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)