Skip to content

feat: manage bot WAF rules in terraform and upsize the prod database - #1601

Merged
piscisaureus merged 2 commits into
mainfrom
ops/bot-rules-db-tier
Nov 28, 2026
Merged

piscisaureus merged 2 commits into
mainfrom
ops/bot-rules-db-tier

Conversation

@piscisaureus

@piscisaureus piscisaureus commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor
  • terraform/bots.tf: manages the production zone's custom WAF
    ruleset (http_request_firewall_custom). Staging isn't managed.
    • Two rules challenge unverified bots on jsr.io requests whose Accept
      header starts with text/html: one for bot scores under 10 and for
      the Lightpanda headless browser (user agent and JA4), one for
      scores 10-29. The LB never serves module files to such requests,
      so module downloads, the Deno CLI, and npm clients aren't matched.
      The e2e tests (Deno user agent) and fediverse link-preview fetchers
      are exempt.
    • The existing "Singapore/China/Vietnam scrape" rule is carried over
      unchanged.
    • The rules, their order, and their refs are identical to what's live
      now, so the apply doesn't change behavior or rule IDs.
    • The zone already has this entrypoint ruleset (created in the
      dashboard), so it's adopted with an import block. The ruleset ID
      is set in prod.tfvars. The resource has prevent_destroy, since
      replacing it would delete every custom rule in the zone.
    • To stop managing the ruleset, use a removed block with
      destroy = false. Reverting this PR as is would delete the rules.
  • terraform/db.tf: prod Cloud SQL tier db-custom-2-6144 ->
    db-custom-4-6144. It was raised to 4 vCPUs by hand on Nov 27 and
    reverted to 2 by the terraform apply on Nov 28 03:54 UTC. Changing the
    tier restarts the instance (about 30-60 seconds of database downtime).
  • CI: Terraform 1.4.2 -> 1.9.8, since import blocks with for_each
    need 1.7 or later.

Context: on Nov 28 01:45-02:00 UTC a distributed headless-browser crawl
pushed Hyperdrive past its origin connection limit and the site hung.
Since the challenge rules went live, challenge solve rates are
0.08-0.1% and database load is down from 140-260k to 20-50k queries
per 5 minutes.

This overlaps with #1597; that PR will need to build on
cloudflare_ruleset.custom_firewall instead of creating its own.

Codify the custom WAF rules on the production zone, which were added in
the dashboard while a headless-browser crawl overloaded the database,
and bring the Cloud SQL tier back to 4 vCPUs. The tier was raised by
hand earlier and then reverted by the next terraform apply.

The zone's entrypoint ruleset already exists, so it's imported. Import
blocks with for_each need Terraform 1.7 or later, so CI moves to 1.9.8.
* Import into cloudflare_ruleset.custom_firewall[0]; import targets
  can't call functions
* Set each rule's ref to its current ID so rule IDs stay stable
* Guard the ruleset with prevent_destroy
* Match Mastodon's older http.rb-first user agent too
* Correct the comment on which requests the rules match
@piscisaureus

Copy link
Copy Markdown
Contributor Author

Live incident, landing without review

@piscisaureus
piscisaureus merged commit 6c4e03b into main Nov 28, 2026
10 of 11 checks passed
@piscisaureus
piscisaureus deleted the ops/bot-rules-db-tier branch November 28, 2026 05:46
CaffeeLake pushed a commit to CaffeeLake/jsr that referenced this pull request Nov 30, 2026
…o#1597)

Most HTML traffic on jsr.io is unverified automation: about 3M
requests a day with a bot score under 30, against roughly 0.4M from
likely humans. The scrapers spread across thousands of IPs, so the
busiest single IP makes about 16 page requests a minute and never trips
the per-IP Worker limits.

This builds on the challenge rules that are already live (see jsr-io#1601),
adds a challenge for scraping networks, and adds log-only rules to
measure the rest of the website before anything there is challenged.
Module files served from R2, api.jsr.io and npm.jsr.io are never
matched.

* `terraform/bots.tf`, custom rules, in evaluation order:
  * Log: Cloudflare's scraping detection IDs on website requests.
  * Log: website requests without an HTML Accept header that score
    under 30. The challenge rules only see `text/html` requests, so
    this covers what `website_requests` adds.
  * The two existing `managed_challenge` rules, unchanged apart from
    the e2e exemption below.
  * Challenge: page requests carrying Bot Management detection
    50331656 (networks that scrape many zones), whatever their score.
    It catches a crawl of doc and diff pages from 1,000+ proxy and VPN
    IPs, at most about 11 requests each, that scores 30 or more.
* `website_requests` mirrors the LB's routing: requests on jsr.io
  except API, login and sitemap routes, badge endpoints (shields.io
  fetches them server-side), the OG image route, static resources, and
  module-file requests the LB would serve from R2. A new test in
  `lb/main_test.ts` checks that the module path regex matches
  `isModuleFilePath`, and CI now runs the LB tests.
* Rate limiting, log only: more than 20 website 404s a minute per IP
  and colo. The zone's plan doesn't allow JA4 as a characteristic,
  throttling (a mitigation timeout of 0), or request headers in rate
  limiting expressions, so the rule matches by path and leaves out
  module-file paths for every client.
* The post-deploy e2e tests send an `x-jsr-e2e` header carrying the
  `E2E_BYPASS_TOKEN` secret, which replaces the `Deno/` user agent
  exemption. `e2e_bypass_token` is sensitive, so the plan shows the
  custom rules as `(sensitive value)`.
* Removes the `@font-face` for the missing `DMMono-Bold.woff2` (about
  2.3k 404s an hour) and declares the existing `DMMono-Medium.woff2` at
  weight 500.

The LB's `FRONTEND_RATELIMIT` and `DOCS_RATELIMIT` stay for now; they
can go once the WAF rules have run for a while.

Bot Management settings aren't managed here: prod already has
JavaScript detections and model auto-updates on.

The new rules and the `http_ratelimit` entrypoint were created on the
prod zone through the API with the same refs, so the first apply updates
them in place. The hand-applied rules exempt `Deno/` user agents instead
of the e2e header; the apply switches them over. `E2E_BYPASS_TOKEN` was
reset to a hex value in both environments so it passes the variable
validation.

The live ruleset also has two temporary log rules measuring detection
50331656. They aren't in terraform, so applying this removes them.

---------

Co-authored-by: Bert Belder <bbelder@cloudflare.com>

This branch had an error being deployed

1 failed deployment
staging — 5b88d1fa Deployed Nov 28, 2026 by piscisaureus via staging #3913
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