StackKit

Verify Googlebot with Forward-Confirmed Reverse DNS in Node.js

By · · 7 min read

Any client can send User-Agent: Googlebot. If your logs or your firewall trust that header, a scraper can pass as Google. Google's documented way to check is a forward-confirmed reverse DNS lookup. This post shows the version I run in my own Next.js app, line by line, and where it can't help.

The idea

A real Googlebot request comes from an IP address that:

  1. reverse-resolves (PTR record) to a hostname ending in googlebot.com or google.com, and
  2. that hostname resolves forward (A/AAAA record) back to the same IP.

Both steps are needed. An attacker who controls the reverse DNS of their own IP can make it say anything, but they can't make crawl-66-249-66-1.googlebot.com resolve to their address, because they don't control Google's DNS zone.

The implementation

import { promises as dns } from "node:dns";

const verdicts = new Map<string, boolean>();

async function verifyIp(ip: string, domains: string[]): Promise<boolean | null> {
  if (!domains.length || !ip) return null;
  const key = `${ip}|${domains[0]}`;
  const cached = verdicts.get(key);
  if (cached !== undefined) return cached;

  let ok = false;
  try {
    for (const host of await dns.reverse(ip)) {
      if (!domains.some((d) => host.endsWith(d))) continue;
      const fwd = await dns.lookup(host, { all: true });
      if (fwd.some((r) => r.address === ip)) {
        ok = true;
        break;
      }
    }
  } catch {
    ok = false;
  }
  if (verdicts.size > 500) verdicts.clear();
  verdicts.set(key, ok);
  return ok;
}

Why it returns boolean | null

There are three outcomes, not two:

  • true: verified.
  • false: the bot claimed to be Googlebot and failed the check. This is the interesting case.
  • null: verification isn't possible. The function returns null immediately when the bot has no domains list.

That third case matters. Many crawlers do not publish a reverse-DNS domain. In my bot table, OpenAI, Anthropic, Perplexity, Common Crawl, Bytespider and the SEO tools all have an empty domains array, so they can never be "verified" this way. They publish IP ranges (or nothing) instead. In my dashboard those rows show a dash instead of a failed check, because "can't verify" and "failed verification" are different facts.

endsWith and the leading dot

The domain list contains .googlebot.com, with the leading dot. Without it, evilgooglebot.com would pass an endsWith("googlebot.com") check. The dot forces a label boundary. The forward-confirm step also protects against this, since the attacker still can't make the forward lookup return their IP, but the dot is a cheap second guard.

Checking every PTR record

dns.reverse can return several hostnames. The loop skips any that don't match the allowed suffixes and forward-confirms the first one that does. dns.lookup(host, { all: true }) returns all addresses, so it works for hosts with both IPv4 and IPv6 records.

The cache

DNS lookups add latency and Google crawls many pages from the same few IPs. The cache key is ip|domain. The cache is cleared when it passes 500 entries, which is crude but keeps memory bounded. On serverless hosting each instance has its own cache, so it resets on cold starts; that costs a few extra lookups, nothing more.

Caching failures as false has a downside: a temporary DNS error marks a real Googlebot IP as unverified until that instance restarts. For a log that's acceptable. For a firewall rule it would not be.

Running it off the request path

I don't verify inside the request. The proxy hands the job to event.waitUntil, so the DNS round-trips happen after the response is sent (see logging crawlers with the Next.js proxy). The page never waits for DNS.

What it showed me

After a few months, every Google-family row in my logs (Googlebot, Googlebot Image, GoogleOther and the AdSense crawler) showed verified counts equal to their hit counts, for example 268 of 268 for the AdSense crawler. So I know those visits were real Google. The rows for the AI crawlers and SEO tools stayed unverifiable, as expected.

Limitations

  • It trusts x-forwarded-for. I take the first address from that header. That is correct behind Vercel's proxy, which sets it, but if your app is reachable directly, a client can send its own header and fool the check. Know what sits in front of your server.
  • It only verifies crawlers that publish domains. Google, Bing, Yandex, Baidu and Apple do. Others need IP range matching, which I haven't implemented.
  • Verification isn't authorization. A verified Googlebot is still just a crawler. It doesn't get access to anything a visitor couldn't see.

If you'd rather not run DNS yourself, Google also publishes its crawler IP ranges as JSON, which you can match against instead.