fix: bounded streaming body read + update stale fallback comments

Replace request.text() with a streaming reader that cancels at
512 KiB — prevents a headerless or chunked-encoding request from
buffering an unbounded payload into Worker memory. No WAF rule
dependency.

Also updates the file header and wrangler.toml comments to
describe the current in-memory fallback (not the old direct-commit
behavior).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
faber
2026-07-11 07:33:30 -04:00
parent f0744d1f1b
commit 6b2cd9da5d
2 changed files with 30 additions and 11 deletions

View File

@@ -17,9 +17,9 @@
* KV Binding (Cloudflare Pages Dashboard → Settings → Bindings):
* TELEMETRY_KV — KV namespace for buffering
*
* 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.
* Without TELEMETRY_KV bound, telemetry accumulates in an
* in-memory isolate buffer and flushes to HF in batches.
* KV is preferred (survives isolate eviction) but not required.
*
* Cloudflare-level protections (configure in Dashboard → Security):
* - WAF rule: block POST /api/telemetry with Content-Length > 524288
@@ -200,18 +200,36 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
return jsonResponse({ error: 'Rate limit exceeded — try again later' }, 429)
}
// ── Read body with size cap ───────────────────────────────────────
// ── Bounded body read (cancels stream at 512 KiB) ──────────────────
// Content-Length can be spoofed or absent (chunked), so we enforce
// the cap at the byte level while streaming — never buffer more than
// MAX_REQUEST_BYTES regardless of what the client claims.
let bodyText: string
try {
bodyText = await request.text()
const reader = request.body?.getReader()
if (!reader) {
return jsonResponse({ error: 'No body' }, 400)
}
const chunks: Uint8Array[] = []
let totalBytes = 0
while (true) {
const { done, value } = await reader.read()
if (done) break
totalBytes += value.byteLength
if (totalBytes > MAX_REQUEST_BYTES) {
reader.cancel()
return jsonResponse({ error: 'Request too large', limit: MAX_REQUEST_BYTES }, 413)
}
chunks.push(value)
}
const merged = new Uint8Array(totalBytes)
let offset = 0
for (const c of chunks) { merged.set(c, offset); offset += c.byteLength }
bodyText = new TextDecoder().decode(merged)
} 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 = JSON.parse(bodyText) as TelemetryPayload

View File

@@ -8,8 +8,9 @@
# LOCAL DEV: Create a preview namespace and set the id below,
# then run `wrangler pages dev` to test locally.
#
# If TELEMETRY_KV is not bound, telemetry falls back to direct
# per-request HF commits (the old behavior).
# If TELEMETRY_KV is not bound, telemetry buffers in the Worker
# isolate's memory and flushes to HF in batches (zero-config fallback).
# KV is preferred because it survives isolate eviction.
# Uncomment and set a real namespace ID for local dev:
# [[kv_namespaces]]