-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_match.go
58 lines (48 loc) · 1.19 KB
/
ip_match.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0
//go:build !coraza.disabled_operators.ipMatch
package operators
import (
"net"
"strings"
"github.com/appsentinels/coraza/v3/experimental/plugins/plugintypes"
)
type ipMatch struct {
subnets []net.IPNet
}
var _ plugintypes.Operator = (*ipMatch)(nil)
func newIPMatch(options plugintypes.OperatorOptions) (plugintypes.Operator, error) {
data := options.Arguments
var subnets []net.IPNet
for _, sb := range strings.Split(data, ",") {
sb = strings.TrimSpace(sb)
if sb == "" {
continue
}
if strings.Contains(sb, ":") && !strings.Contains(sb, "/") {
// ipv6
sb += "/128"
} else if strings.Contains(sb, ".") && !strings.Contains(sb, "/") {
// ipv4
sb += "/32"
}
_, subnet, err := net.ParseCIDR(sb)
if err != nil {
continue
}
subnets = append(subnets, *subnet)
}
return &ipMatch{subnets: subnets}, nil
}
func (o *ipMatch) Evaluate(tx plugintypes.TransactionState, value string) bool {
ip := net.ParseIP(value)
for _, subnet := range o.subnets {
if subnet.Contains(ip) {
return true
}
}
return false
}
func init() {
Register("ipMatch", newIPMatch)
}