Files
poli0981andClaude Opus 5.5 846e5ec17d Site-wide rate limit: 429 for every request the Worker sees (#106)
* feat(security): site-wide rate limit for every request the Worker sees

Only the /api groups were rate-limited; pages, /media, /ext-img and the
search index had no limit at all (a 200-request burst against production
answered 200 every time). A new RL_SITE binding (100 requests per 10 s per
client, IPv6 per /64) is checked by the Worker entry right after the IP
denylist. Over it: the 429 page, no-store, Retry-After: 10, its countdown
rewritten to 10 s. The admin host is exempt (behind Access, RL_ADMIN per
account). A throwing limiter fails open without skipping the Turnstile gate.

- tests: worker spam/keys/exemptions/fail-open; Retry-After constants are
  checked against the periods in wrangler.jsonc
- smoke: a fourth phase spams the built Worker until the limit answers
- scripts/probe-ratelimit.mjs: tell the Worker's 429 from the WAF's 1015
- docs 06/07/02/09/00: three layers (WAF → RL_SITE → /api groups) and the
  Free plan's real WAF limits; CHANGELOG 2.1.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(sonar): project version 2.1.0 for the release

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(worker): stay a 429 if the rate-limit page can't be built

Review follow-ups on the site-wide limit:
- If fetching or rewriting the 429 page threw, the error reached the edge
  fail-open path, which hands the request to the Astro app and skips the
  Turnstile gate. It now answers a plain-text 429 (no-store, Retry-After).
- Drop Last-Modified and Content-Length with the ETag, as the gate does:
  the countdown rewrite only kept the length by luck ("60" vs "10").
- Docs: the WAF rule can't filter by host on Free, so it counts admin.*
  too; 09 lists the worker entry as a 429 source and the probe checks;
  CHANGELOG says what was unlimited before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 07:34:15 +07:00

123 lines
6.6 KiB
JSON
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "poli0981-dev",
"compatibility_date": "2026-10-15",
"compatibility_flags": ["nodejs_compat"],
// Custom entry wrapping the adapter's handler: www→apex, the IP denylist, security headers,
// the Turnstile gate, /media and /ext-img for *every* page — the Astro middleware never
// sees prerendered routes. See src/worker.ts.
"main": "./src/worker.ts",
// Serve only via the custom domains — no poli0981-dev.*.workers.dev URL, and no
// per-version preview URLs either: those live on workers.dev, outside the zone's
// WAF/Transform/Access rules. Explicit, because an omitted key keeps the old setting.
"workers_dev": false,
"preview_urls": false,
"assets": {
"binding": "ASSETS",
"directory": "./dist",
"not_found_handling": "404-page",
"html_handling": "auto-trailing-slash",
// Pages go through the Worker first; files that must stay reachable without it (and
// that gain nothing from it) are served straight from the asset layer, which also
// keeps them unbilled. Globs are anchored and `!` patterns win. /pagefind/* is NOT
// excluded on purpose: its fragments contain the full text of every page.
"run_worker_first": [
"/*",
"!/_astro/*",
"!/og/*",
"!/favicon.svg",
"!/sw.js",
"!/skullhop.js",
"!/gate.js",
"!/offline*",
"!/robots.txt",
"!/sitemap*",
"!/rss.xml",
"!/blog/rss.xml",
"!/truyen/rss.xml",
"!/.well-known/*",
// Vite dev-server internals (`astro dev`): the Cloudflare Vite plugin applies these
// rules before Vite's own middleware, so without them module requests like
// /@vite/client or /src/styles/global.css reach the Worker and 404, and no island
// hydrates. None of these paths exist in a build.
"!/@*",
"!/src/*",
"!/node_modules/*",
],
},
"kv_namespaces": [
{
"binding": "KV",
"id": "8c366cd5e57c4dd1b309787d0603c490",
"preview_id": "7e710034a6a0408191fc00adb5277ae8",
},
// Astro's Cloudflare adapter enables sessions by default and injects a `SESSION`
// KV binding. This site does not use Astro.session, but wrangler deploy needs the
// binding to resolve to a real namespace — reuse the KV namespace (SESSION keys are
// never written here, so no collision with rl:/denylist: keys).
{
"binding": "SESSION",
"id": "8c366cd5e57c4dd1b309787d0603c490",
"preview_id": "7e710034a6a0408191fc00adb5277ae8",
},
],
// Workers Rate Limiting (docs 06 §2) — replaces the KV read-then-write counter. The
// period must be 10 or 60 s; counters are per Cloudflare location, so these are
// per-colo soft limits (the zone WAF rule in docs 07 §6 stays the hard outer limit).
// namespace_id only has to be unique within the account. A period here must match its
// Retry-After in src/lib/ratelimit.ts (tests/lib.test.ts checks).
"ratelimits": [
// Site-wide, per client, for every request the Worker sees (src/worker.ts): pages,
// /media, /ext-img, /pagefind and the API. A page view costs 1–20 of these (static
// assets never reach the Worker), so 100 per 10 s only stops floods and F5-spam; the
// /api groups below are the strict ones. The WAF rule sits above it (docs 07 §6).
{ "name": "RL_SITE", "namespace_id": "981005", "simple": { "limit": 100, "period": 10 } },
{ "name": "RL_REPORT", "namespace_id": "981001", "simple": { "limit": 5, "period": 60 } },
// Generous: one carrier-grade-NAT address can front many phones on VN mobile networks.
{ "name": "RL_GATE", "namespace_id": "981002", "simple": { "limit": 30, "period": 60 } },
{ "name": "RL_ADMIN", "namespace_id": "981003", "simple": { "limit": 120, "period": 60 } },
{ "name": "RL_API", "namespace_id": "981004", "simple": { "limit": 120, "period": 60 } },
],
"vars": {
// Turnstile gate in front of every page (src/lib/gate): "off" | "on" | "force".
// "on" fails open while the zone Transform Rule that sets x-verified-bot is missing;
// "force" ignores that (local testing only). Needs GATE_SECRET + TURNSTILE_SECRET.
// On since 2026-10-07: the Transform Rule's header was confirmed reaching the Worker
// (`x-verified-bot: false` for curl and for a spoofed Googlebot UA alike).
"GATE_MODE": "on",
// Cloudflare Access application protecting admin.poli0981.dev (src/lib/admin/access).
// Not secrets: the AUD tag only says which app a token must be minted for.
"ACCESS_TEAM_DOMAIN": "https://polished-hall-48fd.cloudflareaccess.com",
"ACCESS_AUD": "197ec8eae1275e465623bdb77f88238b9b01dd66eec0be2e8c13ccd7b13e48b7",
// Where /api/report files bug reports. A PRIVATE repo: reports carry the reporter's
// description, route and browser details, and the privacy policy promises they are not
// public. GITHUB_ISSUES_TOKEN must have Issues: read & write on it.
"REPORT_REPO": "poli0981/site-reports",
},
// Uploaded images (src/lib/media): public AVIF/WebP variants under v1/, private
// metadata-free masters and per-image metadata elsewhere. The bucket has no public
// URL — everything public is served by /media in the Worker. Created with
// `wrangler r2 bucket create poli0981-media --location apac`.
"r2_buckets": [{ "binding": "MEDIA", "bucket_name": "poli0981-media" }],
// Cloudflare Images, used only by the admin upload to make the AVIF/WebP variants
// (never per request). Free tier: 5,000 unique transformations a month. Local in dev
// (width/height/format only) — never `remote: true`, the build-time prerender worker
// inherits every binding and CI has no Cloudflare credentials.
"images": { "binding": "IMAGES" },
// The running version's id/tag/timestamp (src/lib/version.ts): /api/health and the admin
// report it, so a deploy can be verified by asking the live site what it runs.
"version_metadata": { "binding": "CF_VERSION_METADATA" },
"observability": {
"enabled": true,
// Query strings never reach Workers Logs or traces (admin URLs carry content paths).
"redact_query_string": true,
// Every invocation is logged: the structured JSON lines from src/lib/log.ts become
// queryable fields in the dashboard.
"logs": { "enabled": true, "invocation_logs": true, "head_sampling_rate": 1 },
// Traces are sampled — enough to see where time goes without paying per request.
"traces": { "enabled": true, "head_sampling_rate": 0.1 },
},
// Custom domains (poli0981.dev, www., admin.) are attached in the dashboard, not here:
// with no `routes` key, `wrangler deploy` leaves them untouched.
}