Skip to content

Commit

Permalink
filtering: fix letter case in cname matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Jul 12, 2021
1 parent 1a693f7 commit ff55c11
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ released by then.

### Fixed

- Letter case mismatches in `CNAME` filtering ([#3335]).
- Occasional breakages on network errors with DNS-over-HTTP upstreams ([#3217]).
- Errors when setting static IP on Linux ([#3257]).
- Treatment of domain names and FQDNs in custom rules with `$dnsrewrite` that
Expand Down Expand Up @@ -101,6 +102,7 @@ released by then.
[#3217]: https://github.com/AdguardTeam/AdGuardHome/issues/3217
[#3256]: https://github.com/AdguardTeam/AdGuardHome/issues/3256
[#3257]: https://github.com/AdguardTeam/AdGuardHome/issues/3257
[#3335]: https://github.com/AdguardTeam/AdGuardHome/issues/3335



Expand Down
31 changes: 17 additions & 14 deletions internal/dnsforward/dnsforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func createTestServer(
t.Helper()

rules := `||nxdomain.example.org
||null.example.org^
||NULL.example.org^
127.0.0.1 host.example.org
@@||whitelist.example.org^
||127.0.0.255`
Expand Down Expand Up @@ -581,13 +581,13 @@ func TestServerCustomClientUpstream(t *testing.T) {

// testCNAMEs is a map of names and CNAMEs necessary for the TestUpstream work.
var testCNAMEs = map[string]string{
"badhost.": "null.example.org.",
"whitelist.example.org.": "null.example.org.",
"badhost.": "NULL.example.org.",
"whitelist.example.org.": "NULL.example.org.",
}

// testIPv4 is a map of names and IPv4s necessary for the TestUpstream work.
var testIPv4 = map[string][]net.IP{
"null.example.org.": {{1, 2, 3, 4}},
"NULL.example.org.": {{1, 2, 3, 4}},
"example.org.": {{127, 0, 0, 255}},
}

Expand All @@ -609,7 +609,7 @@ func TestBlockCNAMEProtectionEnabled(t *testing.T) {

addr := s.dnsProxy.Addr(proxy.ProtoUDP)

// 'badhost' has a canonical name 'null.example.org' which should be
// 'badhost' has a canonical name 'NULL.example.org' which should be
// blocked by filters, but protection is disabled so it is not.
req := createTestMessage("badhost.")

Expand Down Expand Up @@ -644,13 +644,13 @@ func TestBlockCNAME(t *testing.T) {
want bool
}{{
host: "badhost.",
// 'badhost' has a canonical name 'null.example.org' which is
// 'badhost' has a canonical name 'NULL.example.org' which is
// blocked by filters: response is blocked.
want: true,
}, {
host: "whitelist.example.org.",
// 'whitelist.example.org' has a canonical name
// 'null.example.org' which is blocked by filters
// 'NULL.example.org' which is blocked by filters
// but 'whitelist.example.org' is in a whitelist:
// response isn't blocked.
want: false,
Expand All @@ -671,8 +671,11 @@ func TestBlockCNAME(t *testing.T) {
assert.Equal(t, dns.RcodeSuccess, reply.Rcode)
if tc.want {
require.Len(t, reply.Answer, 1)
a, ok := reply.Answer[0].(*dns.A)
require.True(t, ok)

ans := reply.Answer[0]
a, ok := ans.(*dns.A)
require.Truef(t, ok, "got %T", ans)

assert.True(t, a.A.IsUnspecified())
}
})
Expand Down Expand Up @@ -701,7 +704,7 @@ func TestClientRulesForCNAMEMatching(t *testing.T) {

addr := s.dnsProxy.Addr(proxy.ProtoUDP)

// 'badhost' has a canonical name 'null.example.org' which is blocked by
// 'badhost' has a canonical name 'NULL.example.org' which is blocked by
// filters: response is blocked.
req := dns.Msg{
MsgHdr: dns.MsgHdr{
Expand Down Expand Up @@ -742,7 +745,7 @@ func TestNullBlockedRequest(t *testing.T) {
RecursionDesired: true,
},
Question: []dns.Question{{
Name: "null.example.org.",
Name: "NULL.example.org.",
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}},
Expand All @@ -757,7 +760,7 @@ func TestNullBlockedRequest(t *testing.T) {
}

func TestBlockedCustomIP(t *testing.T) {
rules := "||nxdomain.example.org^\n||null.example.org^\n127.0.0.1 host.example.org\n@@||whitelist.example.org^\n||127.0.0.255\n"
rules := "||nxdomain.example.org^\n||NULL.example.org^\n127.0.0.1 host.example.org\n@@||whitelist.example.org^\n||127.0.0.255\n"
filters := []filtering.Filter{{
ID: 0,
Data: []byte(rules),
Expand Down Expand Up @@ -802,7 +805,7 @@ func TestBlockedCustomIP(t *testing.T) {

addr := s.dnsProxy.Addr(proxy.ProtoUDP)

req := createTestMessageWithType("null.example.org.", dns.TypeA)
req := createTestMessageWithType("NULL.example.org.", dns.TypeA)
reply, err := dns.Exchange(req, addr.String())
require.NoError(t, err)

Expand All @@ -813,7 +816,7 @@ func TestBlockedCustomIP(t *testing.T) {

assert.True(t, net.IP{0, 0, 0, 1}.Equal(a.A))

req = createTestMessageWithType("null.example.org.", dns.TypeAAAA)
req = createTestMessageWithType("NULL.example.org.", dns.TypeAAAA)
reply, err = dns.Exchange(req, addr.String())
require.NoError(t, err)

Expand Down
2 changes: 2 additions & 0 deletions internal/filtering/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ func (d *DNSFilter) CheckHostRules(host string, qtype uint16, setts *Settings) (
return Result{}, nil
}

host = strings.ToLower(host)

return d.matchHost(host, qtype, setts)
}

Expand Down

0 comments on commit ff55c11

Please sign in to comment.