Skip to content

Commit 94a637a

Browse files
martinlehouxMartin Kagamino LehouxDean Karn
authored
feat(BREAKING): Change CIDRIPv4 validation (#945)
## Fixes Or Enhances - Mentions #909 - Disable validation of cidripv4 when ip is not the begining of the block Co-authored-by: Martin Kagamino Lehoux <[email protected]> Co-authored-by: Dean Karn <[email protected]>
1 parent 8d50f2f commit 94a637a

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

baked_in.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,9 @@ func isMAC(fl FieldLevel) bool {
373373

374374
// isCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address.
375375
func isCIDRv4(fl FieldLevel) bool {
376-
ip, _, err := net.ParseCIDR(fl.Field().String())
376+
ip, net, err := net.ParseCIDR(fl.Field().String())
377377

378-
return err == nil && ip.To4() != nil
378+
return err == nil && ip.To4() != nil && net.IP.Equal(ip)
379379
}
380380

381381
// isCIDRv6 is the validation function for validating if the field's value is a valid v6 CIDR address.

validator_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,19 +2575,26 @@ func TestCIDRv4Validation(t *testing.T) {
25752575
param string
25762576
expected bool
25772577
}{
2578-
{"10.0.0.0/0", true},
2579-
{"10.0.0.1/8", true},
2580-
{"172.16.0.1/16", true},
2581-
{"192.168.0.1/24", true},
2582-
{"192.168.255.254/24", true},
2578+
{"0.0.0.0/0", true},
2579+
{"10.0.0.0/0", false},
2580+
{"10.0.0.0/8", true},
2581+
{"10.0.0.1/8", false},
2582+
{"172.16.0.0/16", true},
2583+
{"172.16.0.1/16", false},
2584+
{"192.168.0.0/24", true},
2585+
{"192.168.0.1/24", false},
2586+
{"192.168.255.0/24", true},
2587+
{"192.168.255.254/24", false},
25832588
{"192.168.255.254/48", false},
25842589
{"192.168.255.256/24", false},
2585-
{"172.16.255.254/16", true},
2590+
{"172.16.0.0/16", true},
2591+
{"172.16.255.254/16", false},
25862592
{"172.16.256.255/16", false},
25872593
{"2001:cdba:0000:0000:0000:0000:3257:9652/64", false},
25882594
{"2001:cdba:0000:0000:0000:0000:3257:9652/256", false},
25892595
{"2001:cdba:0:0:0:0:3257:9652/32", false},
25902596
{"2001:cdba::3257:9652/16", false},
2597+
{"172.56.1.0/16", false},
25912598
}
25922599

25932600
validate := New()

0 commit comments

Comments
 (0)