Skip to content

Commit

Permalink
filtering: fix hosts nodata
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Dec 14, 2023
1 parent 9241393 commit e00d1d2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -30,10 +30,13 @@ NOTE: Add new changes BELOW THIS COMMENT.

### Fixed

- Names defined in the `/etc/hosts` for a single address family wrongly
considered undefined for another family ([#6541]).
- Omitted CNAME records in safe search results, which can cause YouTube to not
work on iOS ([#6352]).

[#6352]: https://github.com/AdguardTeam/AdGuardHome/issues/6352
[#6541]: https://github.com/AdguardTeam/AdGuardHome/issues/6541

<!--
NOTE: Add new changes ABOVE THIS COMMENT.
Expand Down
22 changes: 14 additions & 8 deletions internal/filtering/dnsrewrite_test.go
Expand Up @@ -324,16 +324,22 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
wantRules: nil,
wantResps: nil,
}, {
name: "v4_mismatch",
host: "v4.host.example",
dtyp: dns.TypeAAAA,
wantRules: nil,
name: "v4_mismatch",
host: "v4.host.example",
dtyp: dns.TypeAAAA,
wantRules: []*ResultRule{{
Text: fmt.Sprintf("%s v4.host.example", addrv4),
FilterListID: SysHostsListID,
}},
wantResps: nil,
}, {
name: "v6_mismatch",
host: "v6.host.example",
dtyp: dns.TypeA,
wantRules: nil,
name: "v6_mismatch",
host: "v6.host.example",
dtyp: dns.TypeA,
wantRules: []*ResultRule{{
Text: fmt.Sprintf("%s v6.host.example", addrv6),
FilterListID: SysHostsListID,
}},
wantResps: nil,
}, {
name: "wrong_ptr",
Expand Down
28 changes: 16 additions & 12 deletions internal/filtering/filtering.go
Expand Up @@ -630,8 +630,8 @@ func (d *DNSFilter) matchSysHosts(
return Result{}, nil
}

vals, rs := hostsRewrites(qtype, host, d.conf.EtcHosts)
if len(vals) > 0 {
vals, rs, matched := hostsRewrites(qtype, host, d.conf.EtcHosts)
if matched {
res.DNSRewriteResult = &DNSRewriteResult{
Response: DNSRewriteResultResponse{
qtype: vals,
Expand All @@ -650,7 +650,7 @@ func hostsRewrites(
qtype uint16,
host string,
hs hostsfile.Storage,
) (vals []rules.RRValue, rs []*ResultRule) {
) (vals []rules.RRValue, rls []*ResultRule, matched bool) {
var isValidProto func(netip.Addr) (ok bool)
switch qtype {
case dns.TypeA:
Expand All @@ -663,37 +663,41 @@ func hostsRewrites(
if err != nil {
log.Debug("filtering: failed to parse PTR record %q: %s", host, err)

return nil, nil
return nil, nil, false
}

addr, _ := netip.AddrFromSlice(ip)

for _, name := range hs.ByAddr(addr) {
matched = true

vals = append(vals, name)
rs = append(rs, &ResultRule{
rls = append(rls, &ResultRule{
Text: fmt.Sprintf("%s %s", addr, name),
FilterListID: SysHostsListID,
})
}

return vals, rs
return vals, rls, matched
default:
log.Debug("filtering: unsupported qtype %d", qtype)

return nil, nil
return nil, nil, false
}

for _, addr := range hs.ByName(host) {
matched = true

if isValidProto(addr) {
vals = append(vals, addr)
rs = append(rs, &ResultRule{
Text: fmt.Sprintf("%s %s", addr, host),
FilterListID: SysHostsListID,
})
}
rls = append(rls, &ResultRule{
Text: fmt.Sprintf("%s %s", addr, host),
FilterListID: SysHostsListID,
})
}

return vals, rs
return vals, rls, matched
}

// processRewrites performs filtering based on the legacy rewrite records.
Expand Down

0 comments on commit e00d1d2

Please sign in to comment.