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

Commit

Permalink
NETDEV-5569: Fix filtering of invalid prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Geiselhardt-Herms committed Apr 18, 2023
1 parent ceea942 commit 0458fa2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cmd/octorpki/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ func FilterInvalidPrefixLen(roalist []prefixfile.ROAJson) []prefixfile.ROAJson {
validROAs := make([]prefixfile.ROAJson, 0)
for _, roa := range roalist {
prefix := roa.GetPrefix()
ones, _ := prefix.Mask.Size()
if prefix.IP.To4() != nil && ones <= 24 {
validROAs = append(validROAs, roa)
prefixLen, _ := prefix.Mask.Size()
if prefix.IP.To4() != nil {
if prefixLen <= 24 {
validROAs = append(validROAs, roa)
}

continue
}

if prefix.IP.To16() != nil && ones <= 48 {
if prefix.IP.To16() != nil && prefixLen <= 48 {
validROAs = append(validROAs, roa)
}
}
Expand Down
71 changes: 71 additions & 0 deletions cmd/octorpki/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"testing"

"github.com/cloudflare/gortr/prefixfile"
"github.com/stretchr/testify/assert"
)

func TestFilter(t *testing.T) {
tests := []struct {
name string
input []prefixfile.ROAJson
expected []prefixfile.ROAJson
}{
{
name: "Invalid IPv4 prefix",
input: []prefixfile.ROAJson{
{
Prefix: "1.1.1.0/25",
ASN: 13335,
Length: 32,
},
},
expected: []prefixfile.ROAJson{},
},
{
name: "Invalid IPv6 prefix",
input: []prefixfile.ROAJson{
{
Prefix: "2001:db8::/64",
ASN: 13335,
Length: 128,
},
},
expected: []prefixfile.ROAJson{},
},
{
name: "All valid",
input: []prefixfile.ROAJson{
{
Prefix: "2001:db8::/48",
ASN: 13335,
Length: 48,
},
{
Prefix: "1.1.1.0/24",
ASN: 13335,
Length: 32,
},
},
expected: []prefixfile.ROAJson{
{
Prefix: "2001:db8::/48",
ASN: 13335,
Length: 48,
},
{
Prefix: "1.1.1.0/24",
ASN: 13335,
Length: 32,
},
},
},
}

for _, test := range tests {
res := FilterInvalidPrefixLen(test.input)
assert.Equal(t, test.expected, res, test.name)
}
}

0 comments on commit 0458fa2

Please sign in to comment.