fix(router): drop empty rules parsed from empty TOML arrays

aconfig parses an empty TOML array (rules = []) as a slice holding one
empty string, which the router then treated as a real rule. Empty rules
can never match a real domain; Add and Replace now drop them.
This commit is contained in:
wweir
2026-08-09 18:56:45 +08:00
parent edb665f34c
commit 759794e897
2 changed files with 29 additions and 1 deletions
+9 -1
View File
@@ -28,7 +28,9 @@ func NewRuleSet(rules ...string) *RuleSet {
}
// Add appends rules that are not already present, using the raw rule string
// as the membership key. Duplicates are ignored.
// as the membership key. Duplicates are ignored. Empty rules are dropped:
// aconfig parses an empty TOML array as [""], which would otherwise surface
// as a rule that can never match a real domain.
func (rs *RuleSet) Add(rules ...string) {
if len(rules) == 0 {
return
@@ -37,6 +39,9 @@ func (rs *RuleSet) Add(rules ...string) {
rs.mu.Lock()
defer rs.mu.Unlock()
for _, rule := range rules {
if rule == "" {
continue
}
if _, ok := rs.set[rule]; ok {
continue
}
@@ -59,6 +64,9 @@ func (rs *RuleSet) Replace(rules ...string) {
nextSet := make(map[string]struct{}, len(rules))
nextTree := suffixtree.NewNodeFromRules()
for _, rule := range rules {
if rule == "" {
continue
}
if _, ok := nextSet[rule]; ok {
continue
}
+20
View File
@@ -210,3 +210,23 @@ func TestListFilteredPaginationAndSearch(t *testing.T) {
t.Fatalf("expected empty page with total 5, got %v (total %d)", rules, total)
}
}
func TestRuleSetDropsEmptyRules(t *testing.T) {
rs := NewRuleSet("a.com", "", "b.com")
list := rs.List()
if len(list) != 2 || list[0] != "a.com" || list[1] != "b.com" {
t.Fatalf("Add should drop empty rules: %q", list)
}
rs.Add("", "c.com")
list = rs.List()
if len(list) != 3 || list[2] != "c.com" {
t.Fatalf("Add should drop empty rules: %q", list)
}
rs.Replace("", "d.com")
list = rs.List()
if len(list) != 1 || list[0] != "d.com" {
t.Fatalf("Replace should drop empty rules: %q", list)
}
}