Fix #479: Smart www-stripping for hostname anchor filters
Problem
Hostname anchor filters like ||www.com^ were having the www. prefix unconditionally stripped during filter parsing (line 757 in src/filters/network.rs). This caused the filter hostname to become "com" instead of "www.com".
When matching requests, "com" would match as a suffix of any .com hostname, causing virtually all .com domains to be incorrectly blocked. The same issue affected other TLDs: ||www.be^ would block all .be websites.
Affected Filter Lists:
- Hagezi's "Multi Ultimate" list contains
||www.be^(a malware domain), causing all.bewebsites to be blocked.
Expected Behavior (uBlock Origin):
||www.com^should only blockwww.comand its subdomains (e.g.,sub.www.com)||www.com^should NOT blockexample.com,github.com, etc.
Solution
Implemented smart www-stripping logic that only removes the www. prefix when it represents a subdomain, not when it's the actual domain name:
Before Fix:
||www.com^??? hostname:"com"??? blocks ALL .com domains ???||www.youjizz.com^??? hostname:"youjizz.com"??? works correctly ???
After Fix:
||www.com^??? hostname:"www.com"??? blocks only www.com and subdomains ???||www.youjizz.com^??? hostname:"youjizz.com"??? still works correctly ???
Logic:
The fix checks if stripping www. would leave a valid domain with at least one dot:
- If
after_www.contains('.'): strip www. (it's a subdomain prefix) - Otherwise: preserve www. (it's the actual domain)
Changes
- src/filters/network.rs: Implemented smart www-stripping logic in hostname parsing
- src/filters/network_matchers.rs: Code restructuring for clarity (no functional change to matching logic)
- tests/unit/engine.rs: Updated serialization hash to reflect the fix
Testing
All existing tests pass, including:
hostname_regex_filter_works: Wildcard hostname filters still workis_anchored_by_hostname_works: Hostname matching logic preservedcheck_pattern_hostname_anchor_filter_works: All existing hostname anchor tests pass
Manual Testing Verified:
- ???
||www.com^now only blockswww.com(notexample.com,github.com, etc.) - ???
||www.be^now only blockswww.be(not all.bedomains) - ???
||www.youjizz.com^correctly blockscdne-pics.youjizz.com(existing behavior preserved)
Breaking Changes
The serialization format hash has changed. Filter lists using ||www.[tld]^ filters will now work correctly instead of over-blocking.
Fixes #479
This issue has been fixed in https://github.com/brave/adblock-rust/pull/618. It can be closed.