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

28 lines
983 B
TypeScript

import { describe, expect, it } from 'vitest'
import { rateKey } from './http'
const request = (ip?: string) =>
new Request('https://freeitchgames.win/api/verify', { headers: ip === undefined ? {} : { 'cf-connecting-ip': ip } })
describe('rateKey', () => {
it.each([
['203.0.113.9', '203.0.113.9'],
['2001:db8:1234:5678:9abc:def0:1:2', '2001:db8:1234:5678::/64'],
['2001:DB8:0:0012::1', '2001:db8:0:12::/64'],
['2001:db8::1', '2001:db8:0:0::/64'],
['::1', '0:0:0:0::/64'],
['::ffff:198.51.100.7', '198.51.100.7'],
])('%s → %s', (ip, key) => {
expect(rateKey(request(ip))).toBe(key)
})
it('puts every address of one IPv6 /64 in the same bucket', () => {
expect(rateKey(request('2001:db8:aa:bb:1::1'))).toBe(rateKey(request('2001:db8:aa:bb:ffff:ffff:ffff:ffff')))
})
it('uses one shared key when the address is missing', () => {
expect(rateKey(request())).toBe('unknown')
expect(rateKey(request(''))).toBe('unknown')
})
})