filtering: wildcard interference

This commit is contained in:
Stanislav Chzhen
2023-05-12 12:25:33 +03:00
parent c77b2a0ce5
commit 1f2ba07eae
2 changed files with 53 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package filtering
import (
"github.com/AdguardTeam/urlfilter/rules"
"github.com/miekg/dns"
"golang.org/x/exp/slices"
)
// DNSRewriteResult is the result of application of $dnsrewrite rules.
@@ -24,7 +25,13 @@ func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
Response: DNSRewriteResultResponse{},
}
slices.SortFunc(dnsr, rewriteSortsBefore)
for _, nr := range dnsr {
if containsWildcard(nr) {
break
}
dr := nr.DNSRewrite
if dr.NewCNAME != "" {
// NewCNAME rules have a higher priority than other rules.
@@ -73,3 +80,19 @@ func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
Reason: RewrittenRule,
}
}
func rewriteSortsBefore(a, b *rules.NetworkRule) (sortsBefore bool) {
return len(a.Shortcut) > len(b.Shortcut)
}
func containsWildcard(r *rules.NetworkRule) (ok bool) {
for _, c := range r.RuleText {
if c == '*' {
return true
} else if c == '^' {
break
}
}
return false
}