0 byte (\000) seems to cause domain matchig error
Prerequisites
-
[x] I have checked the Wiki and Discussions and found no answer
-
[x] I have searched other issues and found no duplicates
-
[x] I want to report a bug and not ask a question or ask for help
-
[x] I have set up AdGuard Home correctly and configured clients to use it. (Use the Discussions for help with installing and configuring clients.)
Platform (OS and CPU architecture)
Linux, AMD64 (aka x86_64)
Installation
Docker
Setup
On one machine
AdGuard Home version
v0.107.72
Action
So I just saw in my logs that iahwpf-dnsotls-ds.metric.gstatic.com got blocked by the rule ||metric.gstatic.com^. But then it requested q\000enhk-dnsotls-ds.metric.gstatic.com and no block no nothing, resolved ip, maybe this is a bug caused by the \000 (0 byte) or the backslash in general.
Expected result
It should have blocked q\000enhk-dnsotls-ds.metric.gstatic.com like normal.
Actual result
q\000enhk-dnsotls-ds.metric.gstatic.com was not blocked.
Additional information and/or screenshots
I can reproduce this. I’ll forward it to the dev team.
I can reproduce this on current master (a308a743) with a focused test through filtering.DNSFilter.CheckHost:
x.metric.gstatic.com-> blocked by||metric.gstatic.com^(control)q\000enhk-dnsotls-ds.metric.gstatic.com(the escaped form miekg/dns produces from the wire) -> not filtered- same miss for a raw 0x00 byte, and for labels containing
\, space, or%;_still matches.
Root cause: urlfilter compiles ||domain^ into a regex anchored with ^(http|https|ws|wss)://([a-z0-9-_.]+\.)? (rules/regexp.go, RegexStartURL). The subdomain group only accepts [a-z0-9-_.], so a label containing any other byte fails the anchored match and the engine fails open. Since DNS labels on the wire can carry arbitrary bytes, any client can bypass every ||domain^ block rule this way, not just this one list.
I'd like to work on a fix. Two directions I see:
- In urlfilter: make hostname matching tolerant of arbitrary label bytes - match the domain suffix per label instead of relying on the anchored
([a-z0-9-_.]+\.)?prefix, keeping current behavior for ordinary names. - In AdGuard Home: validate or normalize the qname in
dnsforward.filterDNSRequestbeforeCheckHost. Rewriting names risks changing lookup semantics, and rejecting them outright would drop weird-but-real client traffic like the one in this report.
My inclination is (1), since the rules express domain-suffix semantics and the library is shared with other AdGuard products. Happy to hear which layer you'd prefer before any code gets written, per the contributing guide.