Skip to content
This repository was archived by the owner on Jun 9, 2024. It is now read-only.

Commit 7c3a145

Browse files
Oliver Geiselhardt-HermsBarbarossaTM
authored andcommitted
NETDEV-5569: Fix filtering of invalid prefixes
1 parent ceea942 commit 7c3a145

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

cmd/octorpki/filter.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ func FilterInvalidPrefixLen(roalist []prefixfile.ROAJson) []prefixfile.ROAJson {
66
validROAs := make([]prefixfile.ROAJson, 0)
77
for _, roa := range roalist {
88
prefix := roa.GetPrefix()
9-
ones, _ := prefix.Mask.Size()
10-
if prefix.IP.To4() != nil && ones <= 24 {
11-
validROAs = append(validROAs, roa)
9+
prefixLen, _ := prefix.Mask.Size()
10+
if prefix.IP.To4() != nil {
11+
if prefixLen <= 24 {
12+
validROAs = append(validROAs, roa)
13+
}
14+
1215
continue
1316
}
1417

15-
if prefix.IP.To16() != nil && ones <= 48 {
18+
if prefixLen <= 48 {
1619
validROAs = append(validROAs, roa)
1720
}
1821
}

cmd/octorpki/filter_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cloudflare/gortr/prefixfile"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestFilter(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
input []prefixfile.ROAJson
14+
expected []prefixfile.ROAJson
15+
}{
16+
{
17+
name: "Invalid IPv4 prefix",
18+
input: []prefixfile.ROAJson{
19+
{
20+
Prefix: "1.1.1.0/25",
21+
ASN: 13335,
22+
Length: 32,
23+
},
24+
},
25+
expected: []prefixfile.ROAJson{},
26+
},
27+
{
28+
name: "Invalid IPv6 prefix",
29+
input: []prefixfile.ROAJson{
30+
{
31+
Prefix: "2001:db8::/64",
32+
ASN: 13335,
33+
Length: 128,
34+
},
35+
},
36+
expected: []prefixfile.ROAJson{},
37+
},
38+
{
39+
name: "All valid",
40+
input: []prefixfile.ROAJson{
41+
{
42+
Prefix: "2001:db8::/48",
43+
ASN: 13335,
44+
Length: 48,
45+
},
46+
{
47+
Prefix: "1.1.1.0/24",
48+
ASN: 13335,
49+
Length: 32,
50+
},
51+
},
52+
expected: []prefixfile.ROAJson{
53+
{
54+
Prefix: "2001:db8::/48",
55+
ASN: 13335,
56+
Length: 48,
57+
},
58+
{
59+
Prefix: "1.1.1.0/24",
60+
ASN: 13335,
61+
Length: 32,
62+
},
63+
},
64+
},
65+
}
66+
67+
for _, test := range tests {
68+
res := FilterInvalidPrefixLen(test.input)
69+
assert.Equal(t, test.expected, res, test.name)
70+
}
71+
}

0 commit comments

Comments
 (0)