From f427012e6fd8472aeba14bdadb0a473790f24db5 Mon Sep 17 00:00:00 2001 From: Annika Hannig Date: Wed, 31 Jan 2024 16:26:24 +0100 Subject: [PATCH] validate ext. community in search query --- pkg/api/search_filters_parsers.go | 15 ++++++++++++--- pkg/api/search_filters_parsers_test.go | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pkg/api/search_filters_parsers.go b/pkg/api/search_filters_parsers.go index 3a9841e..2e73529 100644 --- a/pkg/api/search_filters_parsers.go +++ b/pkg/api/search_filters_parsers.go @@ -1,11 +1,16 @@ package api import ( - "fmt" + "errors" "strconv" "strings" ) +// Errors +var ( + ErrExtCommunityIncomplete = errors.New("incomplete extended community") +) + // FilterQueryParser parses a filter value into a search filter type FilterQueryParser func(value string) (*SearchFilter, error) @@ -64,10 +69,14 @@ func parseExtCommunityValue(value string) (*SearchFilter, error) { community := make(ExtCommunity, len(components)) if len(community) != 3 { - return nil, fmt.Errorf("malformed ext. community: %s", value) + return nil, ErrExtCommunityIncomplete } - // Communities are not stringly typed, but a mix of string and int + // Check if the community is incomplete + if components[0] == "" || components[1] == "" || components[2] == "" { + return nil, ErrExtCommunityIncomplete + } + // TODO: Mixing strings and integers is not a good idea community[0] = components[0] community[1], _ = strconv.Atoi(components[1]) community[2], _ = strconv.Atoi(components[2]) diff --git a/pkg/api/search_filters_parsers_test.go b/pkg/api/search_filters_parsers_test.go index cb8240f..ca9c46e 100644 --- a/pkg/api/search_filters_parsers_test.go +++ b/pkg/api/search_filters_parsers_test.go @@ -59,3 +59,25 @@ func TestParseExtCommunityValue(t *testing.T) { } } + +func TestPartialParseExtCommunityValue(t *testing.T) { + filter, err := parseExtCommunityValue("rt:23") + if err == nil { + t.Error("Expected error, result:", filter) + } + + filter, err = parseExtCommunityValue("rt:23:") + if err == nil { + t.Error("Expected error, result:", filter) + } + + filter, err = parseExtCommunityValue("rt::") + if err == nil { + t.Error("Expected error, result:", filter) + } + + filter, err = parseExtCommunityValue("::") + if err == nil { + t.Error("Expected error, result:", filter) + } +}