{"record":{"id":"c6049491838b9047","repo":"JuliusBrussee/caveman","slug":"oversized-response","errorCode":null,"errorMessage":"oversized_response","messagePattern":"oversized_response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/sdk/typescript/src/index.ts","lineNumber":879,"sourceCode":"      }\n      return response;\n    }\n  };\n}\n\n// ─── Runtime policy client ────────────────────────────────────────────────────\n\n/** Schema version this client accepts; anything else is rejected (fail closed). */\nconst RUNTIME_POLICY_SCHEMA_VERSION = \"caveman.runtime-policy.v1\";\n\n/** Runtime-policy envelopes are KB-sized; stop a hostile body after 1 MiB. */\nconst RUNTIME_POLICY_MAX_RESPONSE_BYTES = 1024 * 1024;\n\nasync function readRuntimePolicyResponseText(response: Response): Promise<string> {\n  const contentLength = response.headers?.get?.(\"content-length\");\n  if (typeof contentLength === \"string\" && /^[0-9]+$/.test(contentLength) && Number(contentLength) > RUNTIME_POLICY_MAX_RESPONSE_BYTES) {\n    await Promise.resolve(response.body?.cancel?.()).catch(() => undefined);\n    throw new Error(\"oversized_response\");\n  }\n\n  const reader = response.body?.getReader?.();\n  if (!reader) throw new Error(\"bounded_response_unavailable\");\n  const chunks: Uint8Array[] = [];\n  let total = 0;\n  try {\n    while (true) {\n      const { done, value } = await reader.read();\n      if (done) break;\n      if (!(value instanceof Uint8Array)) throw new Error(\"runtime policy response stream returned invalid bytes\");\n      total += value.byteLength;\n      if (total > RUNTIME_POLICY_MAX_RESPONSE_BYTES) {\n        await Promise.resolve(reader.cancel()).catch(() => undefined);\n        throw new Error(\"oversized_response\");\n      }\n      chunks.push(value);\n    }","sourceCodeStart":861,"sourceCodeEnd":897,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/typescript/src/index.ts#L861-L897","documentation":"readRuntimePolicyResponseText enforces a 1 MiB cap on runtime policy responses. If the Content-Length header is present, numeric, and exceeds RUNTIME_POLICY_MAX_RESPONSE_BYTES (1024*1024), the body is cancelled and this error is thrown before any bytes are read.","triggerScenarios":"Calling the runtime policy fetch path when the policy endpoint returns a Content-Length header greater than 1,048,576 bytes.","commonSituations":"Pointing runtimePolicy at a bulk/policy-bundle URL instead of the intended small policy JSON; a proxy appending large payloads; misconfigured endpoint serving an entire dataset.","solutions":["Verify the runtimePolicy URL points to the intended small policy endpoint, not a bundle or dump","Check server-side that the policy document is under 1 MiB and trim it","Serve the policy with compression so the wire Content-Length stays under the cap","Inspect any proxy/gateway that may inflate the response size"],"exampleFix":"// before\nconst policyUrl = \"https://example.com/policies/all.json\"; // 4 MB bundle\n// after\nconst policyUrl = \"https://example.com/policy/active.json\"; // small active policy","handlingStrategy":"validation","validationCode":"const head = await fetch(policyUrl, { method: \"HEAD\" });\nconst len = Number(head.headers.get(\"content-length\") ?? \"0\");\nif (len > 1024 * 1024) throw new Error(`policy response too large: ${len} bytes`);","typeGuard":"function hasBoundedBody(res: Response): boolean {\n  const len = res.headers?.get?.(\"content-length\");\n  return !(typeof len === \"string\" && /^\\d+$/.test(len) && Number(len) > 1024 * 1024);\n}","tryCatchPattern":"try {\n  const text = await readRuntimePolicyResponseText(response);\n} catch (e) {\n  if (e.message === \"oversized_response\") fallbackToCachedPolicy();\n  else throw e;\n}","preventionTips":["Keep policy documents small and serve only the active policy","Set correct Content-Length and enable compression","Monitor policy endpoint payload size in CI"],"tags":["network","http","response-size-limit","validation"],"backgroundTag":"payload-too-large","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}