DEV Community

takahiro hashito
takahiro hashito

Posted on

Taking the last two labels of a hostname as the site made every .co.jp domain the same party. A $third-party rule needs the Public Suffix List

Background

I run a small browser tool that checks ad-blocker filter rules (the Adblock Plus / uBlock Origin style syntax) and tells you whether a rule would match a given URL.

https://hashitosystem.com/en/tools/filterlint/

You paste rules, enter a request URL and the page it loads on, and the tool shows which rules hit.
The matcher is labelled "simplified" in the source, and I knew it cut corners.
I wanted to know exactly which corner the $third-party option was cutting, so I ran the real function in Node.js 24 with a few hostnames.

Terms

A filter rule such as ||tracker.example^$third-party means "block requests to tracker.example, but only when the page that loads them is on a different site".
"Same site" here is not "same hostname": cdn.example.com loaded by www.example.com is first-party.
So the matcher needs a function that maps a hostname to its site, usually called the registrable domain.

A public suffix is a domain under which anyone can register names, such as com, co.jp or co.uk.
The registrable domain is the public suffix plus one more label: example.com, example.co.jp.

The shortcut in my tool

This is the function the tool uses.
h is a hostname taken from new URL(...).hostname, and the result is compared between the request and the page.

function baseDomain(h) {
  var p = h.split('.');
  return p.length <= 2 ? h : p.slice(-2).join('.');   // keep the last two labels
}
function isThirdParty(host, site) { return baseDomain(host) !== baseDomain(site); }
Enter fullscreen mode Exit fullscreen mode

It keeps the last two labels and calls that the site.
For .com that is exactly right.

What it returns

I ran these pairs (request host, page host) through it:

const pairs = [
  ['cdn.example.com', 'www.example.com'],
  ['tracker.com', 'www.example.com'],
  ['ads.tracker.co.jp', 'shop.example.co.jp'],
  ['evil.github.io', 'myproject.github.io'],
];
for (const [host, site] of pairs) console.log(`${host} on ${site}: third-party=${isThirdParty(host, site)}`);
Enter fullscreen mode Exit fullscreen mode

Each line prints the request host, the page host, and the verdict of the function above:

cdn.example.com on www.example.com: third-party=false
tracker.com on www.example.com: third-party=true
ads.tracker.co.jp on shop.example.co.jp: third-party=false
evil.github.io on myproject.github.io: third-party=false
Enter fullscreen mode Exit fullscreen mode

The first two are correct.
The last two are wrong: tracker.co.jp and example.co.jp are different companies, and two GitHub Pages projects are owned by different people.
Both pairs collapse to the same "site" (co.jp and github.io), so a $third-party rule silently does not fire.
In other words, on every Japanese .co.jp site, the tool would tell you that a .co.jp tracker is first-party.

The fix: longest public suffix plus one label

The right answer is to find the longest public suffix that the hostname ends with, and keep one more label in front of it.
The list of those suffixes is the Public Suffix List, a community-maintained list started by Mozilla that browsers use, among other things, to decide where cookies may be set:

https://publicsuffix.org/

It contains both ICANN suffixes like co.jp and private ones like github.io, where a company hands out subdomains to unrelated users.

To show the logic, here is a version with a tiny hard-coded excerpt of the list.
It walks from the longest candidate suffix to the shortest, so the first hit is the longest match.

const SUFFIXES = new Set(['com', 'jp', 'co.jp', 'uk', 'co.uk', 'io', 'github.io']);  // demo excerpt only

function registrableDomain(host) {
  const labels = host.toLowerCase().split('.');
  for (let i = 0; i < labels.length; i++) {
    const suffix = labels.slice(i).join('.');
    if (SUFFIXES.has(suffix)) return i === 0 ? null : labels.slice(i - 1).join('.');
  }
  return labels.slice(-2).join('.');
}
Enter fullscreen mode Exit fullscreen mode

I printed both functions side by side:

for (const h of ['www.example.com', 'cdn.example.co.jp', 'shop.other.co.jp', 'myproject.github.io', 'evil.github.io', 'www.bbc.co.uk', 'co.jp'])
  console.log(h, '->', baseDomain(h), '|', registrableDomain(h));
Enter fullscreen mode Exit fullscreen mode

Each line shows the input host, then the two-label shortcut, then the suffix-aware result:

www.example.com -> example.com | example.com
cdn.example.co.jp -> co.jp | example.co.jp
shop.other.co.jp -> co.jp | other.co.jp
myproject.github.io -> github.io | myproject.github.io
evil.github.io -> github.io | evil.github.io
www.bbc.co.uk -> co.uk | bbc.co.uk
co.jp -> co.jp | null
Enter fullscreen mode Exit fullscreen mode

The left column is the two-label shortcut and the right column is the suffix-aware version.
Every .co.jp, .co.uk and github.io host now gets its own site, and a bare public suffix returns null because nobody can own it.

Why I have not just pasted the full list in

The real list has thousands of entries, wildcard rules and exception rules, and it changes over time.
Shipping a snapshot inside a single-page tool means it goes stale; loading it at runtime means a network request from a page that otherwise works offline.
That is a trade-off I still have to decide, so for now the tool keeps the shortcut, and I treat its $third-party verdict on multi-label suffixes as unreliable.

Conclusion

"The last two labels" is not the site.
Any code that decides first-party versus third-party, whether it is a filter matcher, a cookie check or an analytics referrer filter, should derive the registrable domain from the Public Suffix List, or it will treat every .co.jp site as one company.


This article is about my own side project. It was written with AI assistance.

Top comments (0)