diff --git a/component/trie/domain.go b/component/trie/domain.go index 574a59caa..6930fcd68 100644 --- a/component/trie/domain.go +++ b/component/trie/domain.go @@ -23,10 +23,17 @@ type DomainTrie[T any] struct { root *Node[T] } +// ValidAndSplitDomain lower-cases and splits a domain into its dot-separated +// parts, reporting whether it is a well-formed pattern. It returns false for a +// trailing dot ("a.com."), leading/trailing whitespace, or an empty segment, +// as well as for misplaced wildcards (see below). func ValidAndSplitDomain(domain string) ([]string, bool) { + // A trailing dot would produce an empty final segment; reject it up front. if domain != "" && domain[len(domain)-1] == '.' { return nil, false } + // Reject leading/trailing whitespace (a common copy-paste artifact) so it + // isn't silently baked into a label. if domain != "" { if r, _ := utf8.DecodeRuneInString(domain); unicode.IsSpace(r) { return nil, false @@ -37,16 +44,36 @@ func ValidAndSplitDomain(domain string) ([]string, bool) { } domain = strings.ToLower(domain) parts := strings.Split(domain, domainStep) + // A single part must be non-empty (rejects ""); for multi-part domains every + // segment after the first must be non-empty (rejects "a..b", "a.", ".."), + // while an empty first segment is allowed as the ".example.com" dot-wildcard. if len(parts) == 1 { if parts[0] == "" { return nil, false } - - return parts, true + } else { + for _, part := range parts[1:] { + if part == "" { + return nil, false + } + } } - for _, part := range parts[1:] { - if part == "" { + // Validate wildcard placement so that DomainTrie.Search (treats a stray + // wildcard as a literal label) and DomainSet.Has (treats the wildcard byte + // as a wildcard) can never disagree: + // - complexWildcard "+" is only valid as a whole first segment of a + // multi-part domain, i.e. the "+.example.com" form. A bare "+", or a + // "+" anywhere else, is rejected. + // - wildcard "*" is only valid as a whole segment; a partial wildcard such + // as "*a" or "a*b" is rejected. + for i, part := range parts { + if strings.Contains(part, complexWildcard) { + if part != complexWildcard || i != 0 || len(parts) == 1 { + return nil, false + } + } + if strings.Contains(part, wildcard) && part != wildcard { return nil, false } } diff --git a/component/trie/domain_test.go b/component/trie/domain_test.go index 6aab72d3a..6be9acc61 100644 --- a/component/trie/domain_test.go +++ b/component/trie/domain_test.go @@ -107,6 +107,37 @@ func TestTrie_WildcardBoundary(t *testing.T) { assert.NotNil(t, tree.Search("example.com")) } +func TestTrie_InvalidWildcardPlacement(t *testing.T) { + // "+" is only valid as a whole first segment ("+.example.com"); "*" is only + // valid as a whole segment. Anything else must be rejected so that + // DomainTrie.Search (treats a stray wildcard as a literal label) and + // DomainSet.Has (treats the wildcard byte as a wildcard) can never disagree. + valid := []string{"+.example.com", "*.example.com", "+.*", "stun.*.*.*", "*", "a.*", "*.a"} + for _, d := range valid { + tree := trie.New[netip.Addr]() + assert.NoErrorf(t, tree.Insert(d, localIP), "should accept %q", d) + } + + invalid := []string{"stun.+", "a.+.b", "a.+", "+", "+.+.com", "a*b.com", "*a.com", "a*.com"} + for _, d := range invalid { + tree := trie.New[netip.Addr]() + assert.ErrorIsf(t, tree.Insert(d, localIP), trie.ErrInvalidDomain, "should reject %q", d) + } + + // Accepted patterns must stay consistent between Search and DomainSet.Has. + queries := []string{"example.com", "a.example.com", "com", "x.com", "za.com", "anything.at.all"} + for _, d := range valid { + tree := trie.New[netip.Addr]() + assert.NoError(t, tree.Insert(d, localIP)) + set := tree.NewDomainSet() + for _, q := range queries { + searchHit := tree.Search(q) != nil + setHit := set != nil && set.Has(q) + assert.Equalf(t, searchHit, setHit, "pattern %q query %q: Search=%v Has=%v", d, q, searchHit, setHit) + } + } +} + func TestTrie_Foreach(t *testing.T) { tree := trie.New[netip.Addr]() domainList := []string{