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

91 lines
3.6 KiB
TypeScript

/**
* Public "suggest a game" form: GET /api/suggest (is it enabled + Turnstile
* site key), POST /api/suggest {url, note?, token}.
*
* Guards: same-origin POST, per-IP rate limit (RL_SUGGEST), Turnstile
* siteverify (the only thing a bot cannot forge), canonical itch.io URL. A
* suggestion only enters the review queue; the answer never says more than
* the public catalog already shows.
*/
import { canonicalize } from './canonical'
import { catalogUrls, deletedGames, type DataSource } from './data'
import { missingConfig, type WorkerEnv } from './env'
import { HttpError, errorResponse, isObject, json, rateKey, readJson } from './http'
import { autoFlags, classify, type Outcome, type QueueStore } from './queue'
import { siteverify } from './turnstile'
const MAX_NOTE = 500
type SuggestStatus = 'received' | 'already_listed' | 'already_suggested' | 'not_accepted'
const PUBLIC_STATUS: Record<Exclude<Outcome, 'invalid_url' | 'duplicate_in_request'>, SuggestStatus> = {
queued: 'received',
previously_deleted: 'received', // flagged for the maintainer
duplicate_catalog: 'already_listed',
duplicate_pending: 'already_suggested',
rejected: 'not_accepted',
}
export interface SuggestDeps {
store: QueueStore | null
data: DataSource
fetch: typeof fetch
limiter: Pick<RateLimit, 'limit'> | undefined
}
function enabled(env: WorkerEnv, deps: SuggestDeps): boolean {
return missingConfig(env, ['TURNSTILE_SECRET', 'TURNSTILE_SITEKEY', 'SITE_ORIGIN']).length === 0 && deps.store !== null
}
export async function handleSuggest(request: Request, env: WorkerEnv, deps: SuggestDeps): Promise<Response> {
try {
if (request.method === 'GET') {
const on = enabled(env, deps)
return json({ enabled: on, sitekey: on ? env.TURNSTILE_SITEKEY : null })
}
if (request.method !== 'POST') throw new HttpError(405, 'method_not_allowed')
if (!enabled(env, deps) || !deps.store) throw new HttpError(503, 'unavailable')
if (request.headers.get('origin') !== env.SITE_ORIGIN) throw new HttpError(403, 'bad_origin')
if (deps.limiter && !(await deps.limiter.limit({ key: rateKey(request) })).success) {
throw new HttpError(429, 'rate_limited')
}
const body = await readJson(request, 4096)
if (!isObject(body) || typeof body.url !== 'string' || typeof body.token !== 'string') {
throw new HttpError(400, 'invalid_body')
}
const note = typeof body.note === 'string' ? body.note.trim() : ''
if (note.length > MAX_NOTE) throw new HttpError(400, 'note_too_long')
const url = canonicalize(body.url)
if (!url) throw new HttpError(400, 'invalid_url')
const verdict = await siteverify(
{
secret: env.TURNSTILE_SECRET ?? '',
token: body.token,
ip: request.headers.get('cf-connecting-ip') ?? '',
action: 'suggest',
hostname: new URL(env.SITE_ORIGIN).hostname,
},
deps.fetch,
)
if (verdict === 'unavailable') throw new HttpError(503, 'challenge_unavailable')
if (verdict === 'rejected') throw new HttpError(403, 'challenge_failed')
const [catalog, deleted, known] = await Promise.all([
catalogUrls(deps.data),
deletedGames(deps.data),
deps.store.statuses([url]),
])
const [result] = classify([url], { catalog: new Set(catalog.keys()), deleted, known })
if (result.store) {
await deps.store.insert([
{ url, source: 'form', note: note || null, submitter: 'form', flags: autoFlags(result.outcome) },
])
}
const status = PUBLIC_STATUS[result.outcome as keyof typeof PUBLIC_STATUS]
return json({ status })
} catch (e) {
return errorResponse(e)
}
}