Skip to content

Commit

Permalink
chore: disallow space at begin or end in DomainTrie
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 21, 2024
1 parent d80e8bb commit 7dafe78
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions component/trie/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package trie
import (
"errors"
"strings"
"unicode"
"unicode/utf8"
)

const (
Expand All @@ -25,6 +27,14 @@ func ValidAndSplitDomain(domain string) ([]string, bool) {
if domain != "" && domain[len(domain)-1] == '.' {
return nil, false
}
if domain != "" {
if r, _ := utf8.DecodeRuneInString(domain); unicode.IsSpace(r) {
return nil, false
}
if r, _ := utf8.DecodeLastRuneInString(domain); unicode.IsSpace(r) {
return nil, false
}
}
domain = strings.ToLower(domain)
parts := strings.Split(domain, domainStep)
if len(parts) == 1 {
Expand Down
11 changes: 11 additions & 0 deletions component/trie/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,14 @@ func TestTrie_Foreach(t *testing.T) {
})
assert.Equal(t, 7, count)
}

func TestTrie_Space(t *testing.T) {
validDomain := func(domain string) bool {
_, ok := trie.ValidAndSplitDomain(domain)
return ok
}
assert.True(t, validDomain("google.com"))
assert.False(t, validDomain(" google.com"))
assert.False(t, validDomain(" google.com "))
assert.True(t, validDomain("Mijia Cloud"))
}

0 comments on commit 7dafe78

Please sign in to comment.