{"record":{"id":"0496ccc57f531b7f","repo":"affaan-m/ECC","slug":"download-declares-an-invalid-or-excessive-size-0496cc","errorCode":null,"errorMessage":"download declares an invalid or excessive size","messagePattern":"download declares an invalid or excessive size","errorType":"exception","errorClass":"FalError","httpStatus":null,"severity":"error","filePath":"skills/taste-distillation/scripts/taste/falapi.py","lineNumber":701,"sourceCode":"    if is_dry_run():\n        dest.parent.mkdir(parents=True, exist_ok=True)\n        dest.write_bytes(b\"taste-forge dry-run placeholder\\n\")\n        log.info(\"[dry-run] would download from %s\", safe_url(url))\n        return dest\n\n    require_live()\n    _validate_download_url(url)\n    dest.parent.mkdir(parents=True, exist_ok=True)\n    log.info(\"downloading from %s\", safe_url(url))\n    req = urllib.request.Request(url, headers={\"User-Agent\": \"taste-forge\"})\n    opener = urllib.request.build_opener(_SafeRedirect())\n    temporary = None\n    try:\n        with opener.open(req, timeout=300) as resp:\n            declared = getattr(resp, \"headers\", {}).get(\"Content-Length\")\n            expected = int(declared) if declared is not None else None\n            if expected is not None and not 0 <= expected <= MAX_DOWNLOAD_BYTES:\n                raise FalError(\"download declares an invalid or excessive size\")\n            with tempfile.NamedTemporaryFile(dir=dest.parent, prefix=\".taste-download-\",\n                                             delete=False) as fh:\n                temporary = Path(fh.name)\n                total = 0\n                while True:\n                    chunk = resp.read(min(1024 * 1024, MAX_DOWNLOAD_BYTES - total + 1))\n                    if not chunk:\n                        break\n                    total += len(chunk)\n                    if total > MAX_DOWNLOAD_BYTES:\n                        raise FalError(\"download exceeds maximum allowed size\")\n                    fh.write(chunk)\n                if expected is not None and total != expected:\n                    raise FalError(\"download length does not match declared size\")\n        os.replace(temporary, dest)\n        temporary = None\n    except FalError:\n        raise","sourceCodeStart":683,"sourceCodeEnd":719,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/skills/taste-distillation/scripts/taste/falapi.py#L683-L719","documentation":"download() reads the Content-Length header and rejects the transfer before writing anything if the declared size is negative, non-numeric, or larger than MAX_DOWNLOAD_BYTES. This guards against servers lying about size and against allocating for absurdly large payloads.","triggerScenarios":"Server sends a Content-Length greater than MAX_DOWNLOAD_BYTES, or a malformed value (e.g. '12 MB', negative) that fails int() or range validation.","commonSituations":"Proxied/misconfigured servers emitting non-numeric Content-Length headers; downloading an unexpectedly huge generation result; a broken intermediary reporting a padded length.","solutions":["Verify the requested asset is the intended one (small media file, not a bulk export)","Retry the download; a transient proxy may have injected a bogus header","If the file is legitimately larger, raise MAX_DOWNLOAD_BYTES consciously and re-check disk space","Serve via a host that returns accurate Content-Length headers"],"exampleFix":"// before\n FalError: download declares an invalid or excessive size\n// after\n resp = urllib.request.urlopen(url)\n size = int(resp.headers[\"Content-Length\"])\n assert 0 <= size <= MAX_DOWNLOAD_BYTES","handlingStrategy":"try-catch","validationCode":"import urllib.request\nreq = urllib.request.Request(url, method=\"HEAD\")\ncl = urllib.request.urlopen(req, timeout=30).headers.get(\"Content-Length\")\nif cl is not None and int(cl) > MAX_DOWNLOAD_BYTES: skip_download()","typeGuard":null,"tryCatchPattern":"try:\n    download(url, dest)\nexcept FalError as e:\n    if \"invalid or excessive size\" in str(e):\n        log.warning(\"bad Content-Length from %s\", host); handle_unreachable(url)","preventionTips":["HEAD-check Content-Length before downloading","Only download from fal.media hosts","Keep MAX_DOWNLOAD_BYTES configured above your largest expected output"],"tags":["network","http","validation"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}