adblock-rust icon indicating copy to clipboard operation
adblock-rust copied to clipboard

Fix #479: Smart www-stripping for hostname anchor filters

Open R0h1tAnand opened this issue 10 months ago • 1 comments

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 .be websites to be blocked.

Expected Behavior (uBlock Origin):

  • ||www.com^ should only block www.com and its subdomains (e.g., sub.www.com)
  • ||www.com^ should NOT block example.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 work
  • is_anchored_by_hostname_works: Hostname matching logic preserved
  • check_pattern_hostname_anchor_filter_works: All existing hostname anchor tests pass

Manual Testing Verified:

  • ??? ||www.com^ now only blocks www.com (not example.com, github.com, etc.)
  • ??? ||www.be^ now only blocks www.be (not all .be domains)
  • ??? ||www.youjizz.com^ correctly blocks cdne-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

R0h1tAnand avatar Jan 10 '26 19:01 R0h1tAnand

This issue has been fixed in https://github.com/brave/adblock-rust/pull/618. It can be closed.

agadiffe avatar Apr 23 '26 07:04 agadiffe