Skip to content

perf(d1): stop crawlers rendering public-dashboard filter permutations - #8

Merged
scobb merged 1 commit into
mainfrom
perf/public-crawler-guard
Oct 12, 2026
Merged

scobb merged 1 commit into
mainfrom
perf/public-crawler-guard

Conversation

@scobb

@scobb scobb commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Why

D1 read 24.0B rows in the 32 days to 2026-10-12, against 25B included on Workers Paid. It peaked at 2.1B rows on 2026-09-29. Nearly all of it was renders of Beam's own /public/ dashboard (linked from the landing page as "Live stats"). It's the only public dashboard with real data, and its 43k events and 48k pageviews make each render expensive.

A 2-minute wrangler tail showed 7 of 15 worker requests were /public/ filter permutations, for example ?range=30d&page=/for/framer&browser=Other&channel=Direct&referrer=Direct. They came from bingbot, PetalBot (Huawei), DotBot, and unverified Alibaba/residential-proxy scrapers. Every combination is a new URL that runs the full ~16-statement batch, and the filters aren't indexed, so they don't shrink the scan.

This is the robots.txt + Cache-Control follow-up identified on 2026-09-04 and never shipped, plus guards for the scrapers robots.txt won't stop.

What changed

Each guard runs before the first query:

Guard Stops
robots.txt Disallow: /public/ and /embed/, noindex meta and X-Robots-Tag, rel="nofollow" on every same-page link well-behaved crawlers
403 for filtered requests from cf.verifiedBotCategory or crawler UAs crawlers that ignore robots.txt but identify themselves
At most 2 filters: more 301s to the unfiltered view, and at the cap the page offers no link or chart click that adds a third unverified scrapers generating endless combinations
Rendered page cached in caches.default for 5 min, keyed on canonical params repeat hits and refresh loops
The 3 filter-independent custom_events statements (~8.3B rows/30d) cached per site and range filtered variants re-running identical queries

Notes:

  • No KV writes. The Cache API has no write quota, and KV writes are the tightest budget.
  • Privacy: the sites.public lookup runs before the cache read, so making a dashboard private takes effect immediately rather than after the TTL.
  • Batch indexes: the events statements were the batch's last three entries, so no batchRes[n] above them shifted.
  • Bot regex: the UA regex requires bot to be followed by / ; ) -, so phone models like "Cubot X30" aren't treated as crawlers.

Verification

  • npm run typecheck is clean
  • npm test: 130 pass, 0 fail (4 skipped as before), including 11 new tests in test/public-crawler-guard.test.ts. These assert on the SQL issued, not just the status code.
  • Every guard proven to fail when broken. Each of 14 mutations turned at least one test red: bot block, cf check, filter cap, link cap, chart cap, page cache, event cache, chip/filter/range nofollow, meta noindex, robots.txt, and loosening the bot regex. Two gaps found this way were fixed. The stub returned no rows, so the link-cap test proved nothing. And the channel chart built its own links that bypassed the cap.
  • npm run test:smoke: 128/128, against a dev server with PUBLIC_BASE_URL=http://localhost:8787, so nothing was written to production
  • On the local worker: MISS then HIT on reordered params, bingbot gets 403 on filtered URLs and 200 on the bare page, 3 filters 301 to ?range=30d, and 1 filter offers 33 add-links while 2 filters offer 0 (chart targets [null,null]). Two-filter page checked visually.

After merge

Check the Deploy job, not just this PR's checkmark. Then confirm in production: robots.txt, the 403 for a bot UA, X-Beam-Cache: HIT on a repeat request, and daily rowsRead in D1 analytics falling over the next few days.

🤖 Generated with Claude Code

…ations

D1 hit 24.0B rows read in the 32 days to 2026-10-12, against 25B included.
Nearly all of it was renders of Beam's own /public/ dashboard, with bingbot,
PetalBot, DotBot and unverified scrapers walking its filter links. Each
filter combination is a new URL that runs the full ~16-statement batch.

Each guard runs before the first query:
- robots.txt disallows /public/ and /embed/. The page sends noindex, and
  every same-page link (filters, chips, ranges) is rel=nofollow.
- Filtered requests from verified bots (cf.verifiedBotCategory) or
  self-identified crawler UAs get a 403.
- At most 2 filters. More than that 301s to the unfiltered view, and at
  the cap the page offers no link (or chart click) that adds a third.
- The rendered page is cached in caches.default for 5 minutes, keyed on
  canonical params. The visibility lookup runs first, so making a
  dashboard private takes effect immediately.
- The three filter-independent custom_events statements (~8.3B rows in
  30 days) are cached per site and range, so filtered variants share them.
  They were the batch's last entries, so no positional index shifted.

No KV writes: the Cache API has no write quota.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@scobb
scobb merged commit 9feb684 into main Oct 12, 2026
2 checks passed
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