diff --git a/functions/api/telemetry.ts b/functions/api/telemetry.ts index 8ffc77b..4b532b6 100644 --- a/functions/api/telemetry.ts +++ b/functions/api/telemetry.ts @@ -3,21 +3,29 @@ * * Receives metadata events from the frontend and buffers them in * Cloudflare KV. When enough batches accumulate (or enough time - * passes), all pending batches are flushed as a single JSONL commit - * to HuggingFace. This prevents the 10k+ commit buildup that + * passes), pending batches are flushed as JSONL commits to + * HuggingFace. This prevents the 10k+ commit buildup that * happens with per-request commits. * * URL: POST /api/telemetry * * Setup (Cloudflare Pages Dashboard → Settings → Environment Variables): - * HF_TOKEN — HuggingFace write token (hf_...) - * HF_DATASET_REPO — Target dataset repo (e.g. "pliny-the-prompter/g0dm0d3") + * HF_TOKEN — HuggingFace write token (hf_...) + * HF_DATASET_REPO — Target dataset repo (e.g. "pliny-the-prompter/g0dm0d3") + * TELEMETRY_FLUSH_SECRET — Bearer token for authenticated cron flush * * KV Binding (Cloudflare Pages Dashboard → Settings → Bindings): - * TELEMETRY_KV — KV namespace for buffering (optional; falls back to direct commit) + * TELEMETRY_KV — KV namespace for buffering * - * File layout in the HF repo: - * telemetry/batch__.jsonl + * IMPORTANT: TELEMETRY_KV must be bound in production. Without it, + * every request falls back to a direct HF commit — which will + * recreate the commit-explosion problem at scale. + * + * Cloudflare-level protections (configure in Dashboard → Security): + * - WAF rule: block POST /api/telemetry with Content-Length > 524288 + * - Rate limiting rule: 20 req/min per IP to /api/telemetry + * - Restrict Origin header to godmod3.ai (optional) + * - Monthly usage alert on Workers & KV (paid plan required at scale) */ interface Env { @@ -48,22 +56,29 @@ const CORS_HEADERS = { 'Access-Control-Max-Age': '86400', } -// KV batching thresholds -const FLUSH_BATCH_COUNT = 50 -const FLUSH_AGE_MS = 30 * 60 * 1000 // 30 minutes +// ── Size & batching limits ────────────────────────────────────────── +// Workers have 128 MiB memory. Keep individual requests small and +// cap the total data loaded during a flush. +const MAX_REQUEST_BYTES = 512 * 1024 // 512 KiB per inbound POST +const MAX_BATCH_EVENTS = 100 // events per request +const FLUSH_BATCH_COUNT = 50 // keys before triggering flush +const FLUSH_MAX_KEYS = 50 // max keys consumed per flush cycle +const FLUSH_MAX_BYTES = 5 * 1024 * 1024 // 5 MiB combined JSONL per HF commit +const FLUSH_AGE_MS = 30 * 60 * 1000 // 30 minutes -// ── Rate Limiter (in-memory, per-isolate) ──────────────────────────── +// ── Rate Limiter (dual: session + IP, in-memory per-isolate) ──────── const RATE_LIMIT_WINDOW_MS = 60_000 -const RATE_LIMIT_MAX = 10 +const RATE_LIMIT_MAX_SESSION = 10 // per session_id +const RATE_LIMIT_MAX_IP = 20 // per IP — catches session_id rotation const rateLimitMap = new Map() -function isRateLimited(sessionId: string): boolean { +function isRateLimited(key: string, max: number): boolean { const now = Date.now() - let timestamps = rateLimitMap.get(sessionId) + let timestamps = rateLimitMap.get(key) if (!timestamps) { timestamps = [] - rateLimitMap.set(sessionId, timestamps) + rateLimitMap.set(key, timestamps) } const cutoff = now - RATE_LIMIT_WINDOW_MS @@ -71,11 +86,22 @@ function isRateLimited(sessionId: string): boolean { timestamps.shift() } - if (timestamps.length >= RATE_LIMIT_MAX) { + if (timestamps.length >= max) { return true } timestamps.push(now) + + // Prevent unbounded memory growth across isolate lifetime + if (rateLimitMap.size > 10_000) { + const it = rateLimitMap.keys() + for (let i = 0; i < 1_000; i++) { + const k = it.next() + if (k.done) break + rateLimitMap.delete(k.value) + } + } + return false } @@ -110,9 +136,6 @@ export const onRequestOptions: PagesFunction = async () => { } // GET ?flush=force — authenticated cron trigger for guaranteed flush. -// Requires Authorization: Bearer header. -// Set TELEMETRY_FLUSH_SECRET in CF Pages env vars, then configure a -// CF Worker scheduled event or external cron to call this every 30 min. export const onRequestGet: PagesFunction = async (context) => { const { request, env } = context const url = new URL(request.url) @@ -143,6 +166,12 @@ export const onRequestGet: PagesFunction = async (context) => { export const onRequestPost: PagesFunction = async (context) => { const { request, env } = context + // ── Early size rejection (before reading body into memory) ──────── + const contentLength = parseInt(request.headers.get('Content-Length') || '', 10) + if (contentLength > MAX_REQUEST_BYTES) { + return jsonResponse({ error: 'Request too large', limit: MAX_REQUEST_BYTES }, 413) + } + if (!env.HF_TOKEN || !env.HF_DATASET_REPO) { const missing = [] if (!env.HF_TOKEN) missing.push('HF_TOKEN') @@ -151,9 +180,27 @@ export const onRequestPost: PagesFunction = async (context) => { return jsonResponse({ error: `Telemetry not configured (missing: ${missing.join(', ')})` }, 503) } + // ── IP-based rate limiting (catches session_id rotation attacks) ─── + const clientIP = request.headers.get('CF-Connecting-IP') || request.headers.get('X-Forwarded-For') || 'unknown' + if (isRateLimited(`ip:${clientIP}`, RATE_LIMIT_MAX_IP)) { + return jsonResponse({ error: 'Rate limit exceeded — try again later' }, 429) + } + + // ── Read body with size cap ─────────────────────────────────────── + let bodyText: string + try { + bodyText = await request.text() + } catch { + return jsonResponse({ error: 'Failed to read body' }, 400) + } + + if (new TextEncoder().encode(bodyText).byteLength > MAX_REQUEST_BYTES) { + return jsonResponse({ error: 'Request too large', limit: MAX_REQUEST_BYTES }, 413) + } + let payload: TelemetryPayload try { - payload = await request.json() as TelemetryPayload + payload = JSON.parse(bodyText) as TelemetryPayload } catch { return jsonResponse({ error: 'Invalid JSON' }, 400) } @@ -162,40 +209,33 @@ export const onRequestPost: PagesFunction = async (context) => { return jsonResponse({ error: 'No events provided' }, 400) } - const MAX_BATCH = 500 - const events = payload.events.slice(0, MAX_BATCH) + const events = payload.events.slice(0, MAX_BATCH_EVENTS) const invalid = events.filter(e => !validateEvent(e)) if (invalid.length > 0) { return jsonResponse({ error: `${invalid.length} event(s) failed schema validation` }, 400) } + // ── Session-based rate limiting ─────────────────────────────────── const firstEvent = events[0] const sessionKey = firstEvent.session_id ? firstEvent.session_id : deriveSessionKey(firstEvent) - if (isRateLimited(sessionKey)) { + if (isRateLimited(`session:${sessionKey}`, RATE_LIMIT_MAX_SESSION)) { return jsonResponse({ error: 'Rate limit exceeded — try again later' }, 429) } const sanitized = events.map(stripPII) const jsonl = sanitized.map(e => JSON.stringify(e)).join('\n') - // KV values max out at 25 MiB; reject oversized batches cleanly - const KV_VALUE_LIMIT = 24 * 1024 * 1024 // 24 MiB with margin - const jsonlBytes = new TextEncoder().encode(jsonl).byteLength - if (jsonlBytes > KV_VALUE_LIMIT) { - return jsonResponse({ error: 'Batch too large', bytes: jsonlBytes, limit: KV_VALUE_LIMIT }, 413) - } - // ── KV buffered path (preferred) ────────────────────────────────── if (env.TELEMETRY_KV) { const now = Date.now() const kvKey = `batch:${now}:${shortHash(jsonl)}` await env.TELEMETRY_KV.put(kvKey, jsonl, { - expirationTtl: 604800, // 7 days — long enough for any low-volume period + expirationTtl: 604800, // 7 days }) // Check flush conditions @@ -213,10 +253,6 @@ export const onRequestPost: PagesFunction = async (context) => { } if (shouldFlush) { - // Flush is idempotent: concurrent flushes may produce duplicate - // JSONL files on HF, which is harmless (extra telemetry rows). - // KV is eventually consistent and can't do atomic locks, so we - // accept rare duplicates rather than pretend we can prevent them. context.waitUntil(flushKVToHF(env)) } @@ -229,6 +265,10 @@ export const onRequestPost: PagesFunction = async (context) => { } // ── Direct commit fallback (KV not configured) ──────────────────── + // WARNING: At scale this creates one HF commit per request. + // Bind TELEMETRY_KV in production to avoid commit explosion. + console.warn('[Telemetry] TELEMETRY_KV not bound — falling back to direct HF commit') + const ts = new Date().toISOString().replace(/[:.]/g, '-') const hash = shortHash(jsonl) const filePath = `telemetry/batch_${ts}_${hash}.jsonl` @@ -243,48 +283,63 @@ export const onRequestPost: PagesFunction = async (context) => { return jsonResponse({ error: 'Failed to publish to HuggingFace' }, 502) } -// ── KV Flush: combine all pending batches into one HF commit ──────── +// ── KV Flush: read up to FLUSH_MAX_KEYS, cap at FLUSH_MAX_BYTES ──── async function flushKVToHF(env: Env): Promise { const kv = env.TELEMETRY_KV! - const allKeys: { name: string }[] = [] - let cursor: string | undefined - do { - const list = await kv.list({ prefix: 'batch:', cursor }) - allKeys.push(...list.keys) - cursor = list.list_complete ? undefined : list.cursor - } while (cursor) + // Only list up to FLUSH_MAX_KEYS — leave the rest for the next cycle. + // This bounds both KV read ops and memory consumption. + const list = await kv.list({ prefix: 'batch:', limit: FLUSH_MAX_KEYS }) + const keys = list.keys - if (allKeys.length === 0) return 0 + if (keys.length === 0) return 0 + // Read values with a cumulative size cap + const flushedKeys: { name: string }[] = [] const chunks: string[] = [] - for (const key of allKeys) { + let totalBytes = 0 + + for (const key of keys) { const value = await kv.get(key.name) - if (value) chunks.push(value) + if (!value) { + flushedKeys.push(key) // empty key — still delete it + continue + } + + const valueBytes = new TextEncoder().encode(value).byteLength + if (totalBytes + valueBytes > FLUSH_MAX_BYTES && chunks.length > 0) { + // Would exceed cap — stop here, remaining keys stay for next flush + break + } + + chunks.push(value) + flushedKeys.push(key) + totalBytes += valueBytes } - if (chunks.length === 0) return 0 + if (chunks.length === 0) { + // Only empty keys found — clean them up + if (flushedKeys.length > 0) { + await Promise.all(flushedKeys.map(key => kv.delete(key.name))) + } + return 0 + } const combined = chunks.join('\n') - // Filename is derived entirely from the key set — no timestamp. - // Concurrent flushes reading identical keys produce the exact same - // filename, so HF overwrites idempotently (no duplicate rows). - const keyDigest = await sha256Hex(allKeys.map(k => k.name).sort().join(',')) + const keyDigest = await sha256Hex(flushedKeys.map(k => k.name).sort().join(',')) const filePath = `telemetry/flush_${keyDigest}.jsonl` const branch = env.HF_DATASET_BRANCH || 'main' const ok = await commitToHF(env.HF_TOKEN, env.HF_DATASET_REPO, branch, filePath, combined) if (ok) { - // Delete flushed keys. If a concurrent flush already deleted some, - // the delete is a no-op — KV delete on missing keys is safe. - await Promise.all(allKeys.map(key => kv.delete(key.name))) - console.log(`[Telemetry] Flushed ${allKeys.length} batches → ${filePath}`) - return allKeys.length + await Promise.all(flushedKeys.map(key => kv.delete(key.name))) + console.log(`[Telemetry] Flushed ${flushedKeys.length} batches (${(totalBytes / 1024).toFixed(0)} KiB) → ${filePath}`) + return flushedKeys.length } - console.error(`[Telemetry] Flush failed — ${allKeys.length} batches remain in KV`) + console.error(`[Telemetry] Flush failed — ${flushedKeys.length} batches remain in KV`) return 0 } @@ -299,10 +354,7 @@ async function commitToHF( ): Promise { const url = `${HF_API}/datasets/${repo}/commit/${branch}` - // btoa() only handles Latin-1; telemetry JSONL can contain Unicode - const bytes = new TextEncoder().encode(content) - const binStr = Array.from(bytes, b => String.fromCodePoint(b)).join('') - const contentBase64 = btoa(binStr) + const contentBase64 = utf8ToBase64(content) const ndjson = [ JSON.stringify({ key: 'header', value: { summary: `[telemetry] ${filePath}` } }), JSON.stringify({ key: 'file', value: { content: contentBase64, path: filePath, encoding: 'base64' } }), @@ -406,6 +458,21 @@ async function sha256Hex(str: string): Promise { return [...new Uint8Array(buf)].map(b => b.toString(16).padStart(2, '0')).join('') } +// Chunked UTF-8-safe base64: processes 32 KiB at a time to avoid +// creating a single enormous string via Array.from() on the full input. +function utf8ToBase64(content: string): string { + const bytes = new TextEncoder().encode(content) + const CHUNK = 32 * 1024 // 32 KiB — 3-byte aligned (32768 % 3 = 0) + let binStr = '' + for (let i = 0; i < bytes.length; i += CHUNK) { + const slice = bytes.subarray(i, Math.min(i + CHUNK, bytes.length)) + for (let j = 0; j < slice.length; j++) { + binStr += String.fromCharCode(slice[j]) + } + } + return btoa(binStr) +} + function jsonResponse(data: unknown, status: number): Response { return new Response(JSON.stringify(data), { status,