Skip to content

Commit ecacef1

Browse files
Copilotnomeguy
andcommitted
fix: add "is abroad" case to IP rule operator switches
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
1 parent 466801c commit ecacef1

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

rule/rule_ip.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func (r *IpRule) checkRule(expressions []*object.Expression, req *http.Request)
6161
if !ipNet.Contains(netIp) {
6262
return &RuleResult{Reason: reason}, nil
6363
}
64+
case "is abroad":
65+
// This case should not be reached due to early continue, but adding for safety
66+
continue
6467
default:
6568
return nil, fmt.Errorf("unknown operator: %s", expression.Operator)
6669
}
@@ -74,6 +77,9 @@ func (r *IpRule) checkRule(expressions []*object.Expression, req *http.Request)
7477
if ipStr != clientIp {
7578
return &RuleResult{Reason: reason}, nil
7679
}
80+
case "is abroad":
81+
// This case should not be reached due to early continue, but adding for safety
82+
continue
7783
default:
7884
return nil, fmt.Errorf("unknown operator: %s", expression.Operator)
7985
}

rule/rule_ip_abroad_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package rule
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"testing"
7+
8+
"github.com/casbin/caswaf/ip"
9+
"github.com/casbin/caswaf/object"
10+
)
11+
12+
func init() {
13+
// Initialize IP database for tests
14+
// Check if we're in the rule directory and adjust path if needed
15+
if _, err := os.Stat("../ip/17monipdb.dat"); err == nil {
16+
os.Chdir("..")
17+
}
18+
ip.InitIpDb()
19+
}
20+
21+
func TestIpRule_IsAbroad(t *testing.T) {
22+
ipRule := &IpRule{}
23+
24+
tests := []struct {
25+
name string
26+
operator string
27+
value string
28+
clientIP string
29+
shouldMatch bool
30+
shouldError bool
31+
}{
32+
{
33+
name: "is abroad with empty value - foreign IP",
34+
operator: "is abroad",
35+
value: "",
36+
clientIP: "8.8.8.8",
37+
shouldMatch: true,
38+
shouldError: false,
39+
},
40+
{
41+
name: "is abroad with some value - foreign IP",
42+
operator: "is abroad",
43+
value: "1.1.1.1",
44+
clientIP: "8.8.8.8",
45+
shouldMatch: true,
46+
shouldError: false,
47+
},
48+
{
49+
name: "is abroad with CIDR value - foreign IP",
50+
operator: "is abroad",
51+
value: "1.1.1.0/24",
52+
clientIP: "8.8.8.8",
53+
shouldMatch: true,
54+
shouldError: false,
55+
},
56+
}
57+
58+
for _, tt := range tests {
59+
t.Run(tt.name, func(t *testing.T) {
60+
expressions := []*object.Expression{
61+
{
62+
Operator: tt.operator,
63+
Value: tt.value,
64+
},
65+
}
66+
67+
req := &http.Request{
68+
RemoteAddr: tt.clientIP + ":1234",
69+
}
70+
71+
result, err := ipRule.checkRule(expressions, req)
72+
73+
if tt.shouldError && err == nil {
74+
t.Errorf("Expected error but got none")
75+
}
76+
if !tt.shouldError && err != nil {
77+
t.Errorf("Unexpected error: %v", err)
78+
}
79+
80+
gotMatch := result != nil
81+
if gotMatch != tt.shouldMatch {
82+
t.Errorf("Expected match: %v, got: %v", tt.shouldMatch, gotMatch)
83+
}
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)