Skip to content

fix: use registrable domain for isSameDomain/isSameSubdomain (multi-part TLDs) - #4443

Open
eeshsaxena wants to merge 1 commit into
firecrawl:mainfrom
eeshsaxena:fix/same-domain-multipart-tld
Open

fix: use registrable domain for isSameDomain/isSameSubdomain (multi-part TLDs)#4443
eeshsaxena wants to merge 1 commit into
firecrawl:mainfrom
eeshsaxena:fix/same-domain-multipart-tld

Conversation

@eeshsaxena

@eeshsaxena eeshsaxena commented Aug 28, 2026

Copy link
Copy Markdown

Problem

isSameDomain and isSameSubdomain derive the domain from the last two hostname labels (hostname.split(".").slice(-2)). That is wrong for multi-part public suffixes:

  • foo.co.uk and bar.co.uk both reduce to co.uk → reported as the same domain
  • a.github.io and b.github.io → same
  • x.vercel.app / y.vercel.app, *.com.au, *.pages.dev, etc. → same

These two functions gate the /map endpoint's same-domain filter (lib/map-utils.ts and the v1 map controller filter mapResults/links with isSameDomain/isSameSubdomain). So a map of a site hosted on such a suffix (e.g. a *.github.io project page, a .co.uk site) can include URLs from other people's sites sharing the suffix. isSameSubdomain has the same defect in both its domain and subdomain computation (sub.example.co.uk split its domain as co.uk and its subdomain as sub.example).

Fix

Use parseHostname (tldts, public-suffix aware) for the registrable domain and subdomain — the helper the rest of the codebase already uses for exactly this (see lib/url-utils.ts, which documents the allowPrivateDomains option so hosting suffixes like vercel.app are handled). A leading www. is still ignored in the subdomain comparison.

isSameDomain("https://foo.co.uk", "https://bar.co.uk")   // before: true   after: false
isSameDomain("https://a.github.io", "https://b.github.io")// before: true   after: false
isSameDomain("https://sub.example.com", "https://example.com") // true (unchanged)

Testing

All existing isSameDomain/isSameSubdomain cases still pass, and I added tests for the multi-part-suffix cases. I verified the fixed logic against every existing test input plus the suffix cases by running it against tldts directly (the resolver the fix uses); the full API test suite runs in CI.


Summary by cubic

Fixes isSameDomain and isSameSubdomain to use the public-suffix-aware registrable domain instead of the last two hostname labels, so the /map endpoint no longer treats foo.co.uk and bar.co.uk (or a.github.io and b.github.io) as the same site. Both functions now use parseHostname from lib/url-utils, and a leading www. is still ignored in subdomain comparisons. Adds tests for multi-part suffix cases.

Written for commit c973250. Summary will update on new commits.

Review in cubic

isSameDomain and isSameSubdomain derived the domain from the last two hostname
labels, which is wrong for multi-part public suffixes: foo.co.uk and bar.co.uk
both reduced to co.uk and compared equal, as did a.github.io and b.github.io.
These gate the /map endpoint's same-domain filter (map-utils.ts, v1 map
controller), so a map of a site on such a suffix could include URLs from other
sites sharing the suffix.

Use parseHostname (tldts, public-suffix aware) for the registrable domain and
subdomain, which is already how the rest of the codebase resolves domains. A
leading www. is still ignored. Adds tests for the multi-part-suffix cases.
@eeshsaxena

Copy link
Copy Markdown
Author

The user-facing impact is on /map: because these gate the same-domain filter, mapping a site on a shared suffix (a github.io project page, a co.uk or com.au site, a vercel.app deployment) can surface URLs from other sites under that suffix. The fix reuses parseHostname/tldts, which the codebase already relies on for registrable domains, so it stays consistent with url-utils.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/api/src/lib/validateUrl.ts">

<violation number="1" location="apps/api/src/lib/validateUrl.ts:85">
P2: The new `domain1 !== null` guard makes `isSameDomain` (and `isSameSubdomain`) return `false` for identical IP-address or single-label hosts. tldts returns `domain: null` for IPs (e.g. `127.0.0.1`) and for hosts with no public-suffix match (e.g. `localhost`), so `isSameDomain("http://127.0.0.1", "http://127.0.0.1")` flips from `true` (old last-two-labels logic: `"1.1" === "1.1"`) to `false`. Since these functions gate the `/map` same-domain filter, a site hosted on a raw IP would no longer match its own links. Consider falling back to comparing the full hostnames when the registrable domain is null.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

.join(".");

return domain1 === domain2;
return domain1 !== null && domain1 === domain2;

@cubic-dev-ai cubic-dev-ai Bot Aug 28, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The new domain1 !== null guard makes isSameDomain (and isSameSubdomain) return false for identical IP-address or single-label hosts. tldts returns domain: null for IPs (e.g. 127.0.0.1) and for hosts with no public-suffix match (e.g. localhost), so isSameDomain("http://127.0.0.1", "http://127.0.0.1") flips from true (old last-two-labels logic: "1.1" === "1.1") to false. Since these functions gate the /map same-domain filter, a site hosted on a raw IP would no longer match its own links. Consider falling back to comparing the full hostnames when the registrable domain is null.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/api/src/lib/validateUrl.ts, line 85:

<comment>The new `domain1 !== null` guard makes `isSameDomain` (and `isSameSubdomain`) return `false` for identical IP-address or single-label hosts. tldts returns `domain: null` for IPs (e.g. `127.0.0.1`) and for hosts with no public-suffix match (e.g. `localhost`), so `isSameDomain("http://127.0.0.1", "http://127.0.0.1")` flips from `true` (old last-two-labels logic: `"1.1" === "1.1"`) to `false`. Since these functions gate the `/map` same-domain filter, a site hosted on a raw IP would no longer match its own links. Consider falling back to comparing the full hostnames when the registrable domain is null.</comment>

<file context>
@@ -73,23 +74,15 @@ export function isSameDomain(url: string, baseUrl: string) {
-    .join(".");
-
-  return domain1 === domain2;
+  return domain1 !== null && domain1 === domain2;
 }
 
</file context>
Fix with cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant