Skip to content

Commit

Permalink
Fix edge case where input matched wildcard rule
Browse files Browse the repository at this point in the history
This is an edge case resulting from #143. Once again, the match of wildcard rules is tricky as I also mentioned in #143 (comment)
  • Loading branch information
weppos committed Aug 23, 2018
1 parent b6e96f6 commit f8afde6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
55 changes: 55 additions & 0 deletions publicsuffix/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,58 @@ func TestIDNA(t *testing.T) {
}
}
}

func TestFindRuleIANA(t *testing.T) {
testCases := []struct {
input, want string
}{
// TLD with only 1 rule.
{"biz", "biz"},
{"input.biz", "biz"},
{"b.input.biz", "biz"},

// The relevant {kobe,kyoto}.jp rules are:
// jp
// *.kobe.jp
// !city.kobe.jp
// kyoto.jp
// ide.kyoto.jp
{"jp", "jp"},
{"kobe.jp", "jp"},
{"c.kobe.jp", "c.kobe.jp"},
{"b.c.kobe.jp", "c.kobe.jp"},
{"a.b.c.kobe.jp", "c.kobe.jp"},
{"city.kobe.jp", "kobe.jp"},
{"www.city.kobe.jp", "kobe.jp"},
{"kyoto.jp", "kyoto.jp"},
{"test.kyoto.jp", "kyoto.jp"},
{"ide.kyoto.jp", "ide.kyoto.jp"},
{"b.ide.kyoto.jp", "ide.kyoto.jp"},
{"a.b.ide.kyoto.jp", "ide.kyoto.jp"},

// Domain with a private public suffix should return the ICANN public suffix.
{"foo.compute-1.amazonaws.com", "com"},
// Domain equal to a private public suffix should return the ICANN public suffix.
{"cloudapp.net", "net"},
}

for _, tc := range testCases {
rule := DefaultList.Find(tc.input, &FindOptions{IgnorePrivate: true, DefaultRule: nil})

if rule == nil {
t.Errorf("TestFindRuleIANA(%v) nil rule", tc.input)
continue
}

suffix := rule.Decompose(tc.input)[1]
// If the TLD is empty, it means name is actually a suffix.
// In fact, decompose returns an array of empty strings in this case.
if suffix == "" {
suffix = tc.input
}

if suffix != tc.want {
t.Errorf("TestFindRuleIANA(%v) = %v, want %v", tc.input, suffix, tc.want)
}
}
}
12 changes: 8 additions & 4 deletions publicsuffix/publicsuffix.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,20 @@ func (l *List) Find(name string, options *FindOptions) *Rule {
options = DefaultFindOptions
}

part := name
for {
rule, ok := l.rules[name]
if ok && (!options.IgnorePrivate || !rule.Private) {
rule, ok := l.rules[part]

if ok && rule.Match(name) && !(options.IgnorePrivate && rule.Private) {
return rule
}
i := strings.IndexRune(name, '.')

i := strings.IndexRune(part, '.')
if i < 0 {
return options.DefaultRule
}
name = name[i+1:]

part = part[i+1:]
}

return nil
Expand Down
22 changes: 21 additions & 1 deletion publicsuffix/publicsuffix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ com
// io
io
// jp
jp
*.kawasaki.jp
*.kitakyushu.jp
*.kobe.jp
*.nagoya.jp
*.sapporo.jp
*.sendai.jp
*.yokohama.jp
!city.kawasaki.jp
!city.kitakyushu.jp
!city.kobe.jp
!city.nagoya.jp
!city.sapporo.jp
!city.sendai.jp
!city.yokohama.jp
// ===END ICANN DOMAINS===
// ===BEGIN PRIVATE DOMAINS===
Expand All @@ -221,7 +238,7 @@ blogspot.com
p1.Private = true

testCases := []listFindTestCase{
// match IANA
// match standard
{"example.com", MustNewRule("com")},
{"foo.example.com", MustNewRule("com")},

Expand All @@ -242,6 +259,9 @@ blogspot.com
// match private
{"blogspot.com", p1},
{"foo.blogspot.com", p1},

// input is wildcard rule
{"kobe.jp", MustNewRule("jp")},
}

list, err := NewListFromString(src, nil)
Expand Down

0 comments on commit f8afde6

Please sign in to comment.