Files
poli0981andClaude Opus 5.5 2c2d990f33 feat(worker): Turnstile verification gate for the web app
Until a visitor passes a Cloudflare Turnstile check, app pages ("/" is
now Worker-first too) answer with a self-contained verification page and
/img covers with 403, so a browser loads nothing of the site before the
check. Passing sets an HttpOnly, host-only cookie (HMAC of its issue
time) good for BOT_GATE_TTL_HOURS (48). The page then reloads into the
link that was asked for, and the legal gate follows as before.

- /assets, /data (desktop/Android apps, CC BY reuse), robots, sitemap,
  og image, /api/* and /admin* stay ungated.
- Verified bots pass with the token a zone Transform Rule adds
  (x-fig-known-bot); clients that only claim to be a search crawler get
  a 503, never the noindex page.
- Fails open: it needs BOT_GATE="on", the Turnstile keys, GATE_SECRET
  and GATE_BOT_TOKEN; deleting GATE_SECRET switches it off at once.
- POST /api/verify: same-origin, RL_GATE (IPv4 / IPv6 /64), siteverify
  with action "gate". GET reports the pass for the page and the SPA.
- turnstile.ts: siteverify shared with Suggest, which now also checks
  the hostname and action (a token from one page can't be spent on the
  other) and answers 503 when Turnstile is unreachable.
- SPA: a tab whose pass ran out re-checks when shown again or when a
  cover fails, and reloads into the verification page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 17:56:33 +07:00

29 lines
1.3 KiB
TypeScript

/**
* Secrets live only in Cloudflare (`wrangler secret put`, or .dev.vars for
* `wrangler dev`) and are typed here instead of in the generated
* worker-configuration.d.ts (`npm run cf:types` ignores .dev.vars, so the
* committed file never depends on who generated it). They are optional at
* runtime: a route whose configuration is incomplete answers 503.
*/
interface Secrets {
GH_APP_PRIVATE_KEY?: string // GitHub App PEM (PKCS#1 as downloaded, or PKCS#8)
ADMIN_EMAILS?: string // comma-separated maintainer emails
TURNSTILE_SECRET?: string
/** 32+ random characters: signs the verification-gate cookie (gate.ts). */
GATE_SECRET?: string
/** 32+ random characters: the value the zone's Transform Rule sends for verified bots (gate.ts). */
GATE_BOT_TOKEN?: string
/** Local `wrangler dev` only (admin without Access); ignored off localhost. */
DEV_ADMIN_EMAIL?: string
}
export type WorkerEnv = Env & Secrets
/** Names of the settings in `keys` that are empty or unset. */
export function missingConfig<K extends keyof WorkerEnv>(env: WorkerEnv, keys: readonly K[]): K[] {
return keys.filter((k) => !env[k])
}
export const ACCESS_CONFIG = ['ACCESS_TEAM_DOMAIN', 'SITE_ORIGIN'] as const
export const GITHUB_CONFIG = ['GITHUB_REPO', 'GH_APP_ID', 'GH_APP_INSTALLATION_ID', 'GH_APP_PRIVATE_KEY'] as const