Files
poli0981andClaude Opus 5.5 0cdc730461 feat(worker): site-wide rate limit with the branded 429 page (D32) (#49)
* feat(worker): site-wide rate limit with the branded 429 page (D32)
* docs: record how the rate-limit binding really counts (measured on the preview)

Every page, feed and data request runs the Worker first and spends one
unit of SITE_LIMITER (60 per 10 s per IPv4 or IPv6 /64); over it, the
built /errors/429 page (status 429, Retry-After 10, no-store) or JSON
under /api/. What a page loads stays asset-first. Fails open. Measured on
the preview: holds a client on one connection; many-connection floods are
for the zone rule (docs/16 §9.1). Privacy #rate-limit, LEGAL_VERSION
2026-10-26, suite v3.0, v1.3.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 06:16:14 +07:00

89 lines
4.7 KiB
JSON

{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "softharbor",
// D25 + D32 (docs/15): ONE small Worker. It runs the site-wide rate limit
// and answers POST /api/verify (the legal gate's Turnstile check); every
// page, feed and file it lets through is still served by the asset server
// (env.ASSETS). See `run_worker_first` below and worker/index.ts.
"main": "./worker/index.ts",
// ≥ 2025-04-01 still matters on the asset-first paths below: there a
// NAVIGATION that matches no asset gets dist/404.html from the asset server
// directly, and any other miss reaches the Worker, which counts it and
// hands it to ASSETS.fetch() for the same 404.
"compatibility_date": "2026-10-01",
"assets": {
"directory": "./dist",
"binding": "ASSETS",
"not_found_handling": "404-page", // serves dist/404.html with HTTP 404
// Pairs with astro's trailingSlash:'never' + build.format:'file'.
// Measured in S1 on a real deploy: "auto-trailing-slash" 307s /apps to
// /apps/, which contradicts every canonical, sitemap entry and hreflang
// alternate we emit. "drop-trailing-slash" makes the no-slash URL the one
// that serves 200 and redirects the slashed variant to it.
"html_handling": "drop-trailing-slash",
// D32: the Worker runs FIRST for every request — so each one passes the
// site-wide rate limit — EXCEPT the files below, which stay asset-first:
// never limited, never billed. A page fetches them only after it passed
// itself, so limiting them could only leave a page half-loaded, and
// content-hashed files are cached for a year anyway. Negative patterns
// win over `/*` regardless of order. Change this list only with a
// decision-log entry: every request it routes is a billed invocation.
"run_worker_first": [
"/*",
"!/_astro/*", // content-hashed JS, CSS and the self-hosted fonts
"!/icons/*",
"!/favicon.ico",
"!/favicon.svg",
"!/apple-touch-icon.png",
"!/manifest.webmanifest",
"!/sw.js",
"!/registerSW.js",
"!/workbox-*.js",
],
},
// Workers Rate Limiting, counted per IPv4 address or IPv6 /64 and per
// Cloudflare location (worker/http.ts rateKey). Generous on purpose: a
// school, an office or a mobile carrier's NAT puts many people behind one
// address. namespace_id is account-wide; 4101 and 4102 are used by no
// other Worker on this account (checked 2026-10-26).
"ratelimits": [
// Every request that runs the Worker (D32): 60 per 10 s is six a second,
// sustained — far above anyone reading the site, low enough to stop a
// flood or a runaway scraper. Over it: the branded 429 page (or JSON
// under /api/), Retry-After 10. Keep `period` equal to RETRY_AFTER_S in
// worker/limit.ts (a test holds them together).
{ "name": "SITE_LIMITER", "namespace_id": "4102", "simple": { "limit": 60, "period": 10 } },
// /api/verify on top of that (D25): 20 verifications per minute.
{ "name": "VERIFY_LIMITER", "namespace_id": "4101", "simple": { "limit": 20, "period": 60 } },
],
// Workers Logs keep the Worker's own one-line JSON events — a verification's
// outcome and duration (worker/verify.ts), a rate-limit refusal's kind
// (worker/limit.ts) — never an IP or token. A request that passes logs
// nothing. Invocation logs stay off: now that every page runs the Worker
// they would be a log line per page view, holding per-request metadata the
// Privacy Policy does not need us to hold.
"observability": {
"enabled": true,
"logs": { "enabled": true, "invocation_logs": false },
},
// No `routes` here — on purpose since commit 66c6255 (2026-07-28).
//
// softharbor.net and www.softharbor.net are attached as Custom Domains in
// the Cloudflare dashboard (Workers & Pages → softharbor → Domains). With no
// `routes` key, `wrangler deploy` leaves those triggers untouched; declaring
// them here is what previously let a deploy reconcile the hand-added `www`
// record away (observed 2026-07-28). Re-creating the Worker from a fresh
// clone therefore needs the two domains re-attached by hand (docs/16 §3).
//
// www never SERVES the site: the zone Redirect Rule (docs/16 §4 R1) runs
// before Workers and 301s it to the apex. That rule is a hard prerequisite —
// without it www would serve a duplicate of the site, which is the exact
// hazard docs/16 N1/N2 exist to prevent. Verified after every deploy in the
// launch checklist (docs/13 §2).
"workers_dev": false, // canonical origin is the custom domain only
// PR builds upload preview versions to *.workers.dev; public/_headers marks
// that host pattern `X-Robots-Tag: noindex`.
"preview_urls": true,
}