{"record":{"id":"8febae1234a6424f","repo":"microsoft/aspire","slug":"headers-too-large-limit-max-header-bytes-bytes","errorCode":null,"errorMessage":"Headers too large (limit {_MAX_HEADER_BYTES} bytes)","messagePattern":"Headers too large \\(limit (.+?) bytes\\)","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs","lineNumber":938,"sourceCode":"\n            def _read_headers(self) -> dict[str, str]:\n                '''Read HTTP-style headers until empty line.\n\n                Enforces limits on header count and total size to prevent\n                memory exhaustion from a malicious peer.\n                '''\n                headers: dict[str, str] = {}\n                total_bytes = 0\n                while True:\n                    line = self._read_line()\n                    if not line:  # Empty line signals end of headers\n                        break\n\n                    total_bytes += len(line) + 2  # account for \\r\\n\n                    if len(headers) >= _MAX_HEADER_COUNT:\n                        raise ConnectionError(f\"Too many headers (limit {_MAX_HEADER_COUNT})\")\n                    if total_bytes > _MAX_HEADER_BYTES:\n                        raise ConnectionError(f\"Headers too large (limit {_MAX_HEADER_BYTES} bytes)\")\n\n                    # Parse \"Header-Name: value\"\n                    if b\":\" in line:\n                        name, value = line.split(b\":\", 1)\n                        headers[name.decode(\"utf-8\").strip().lower()] = value.decode(\"utf-8\").strip()\n                return headers\n\n            def _receive_loop(self) -> None:\n                '''Receive and process messages from the server'''\n                try:\n                    while self._connected and self._socket:\n                        # Read HTTP-style headers (HeaderDelimitedMessageHandler format)\n                        headers = self._read_headers()\n                        content_length = int(headers.get(\"content-length\", \"0\"))\n\n                        if content_length == 0:\n                            continue\n","sourceCodeStart":920,"sourceCodeEnd":956,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Python/PythonModuleBuilder.cs#L920-L956","documentation":"The generated Python client reads HTTP-style headers from the AppHost socket connection and enforces a cumulative size budget of _MAX_HEADER_BYTES (8 KB) across all header lines (counting the trailing CRLF). If the accumulated header bytes exceed that limit before the blank terminator line, it raises ConnectionError to protect against malformed or malicious framing from the peer. This is a framing-safety guard, not a normal operational error.","triggerScenarios":"Calling any client method (ping, authenticate, invoke_capability) when the AppHost server's response header block exceeds 8 KB total, or when a corrupted/hostile peer streams header lines without a blank-line terminator.","commonSituations":"A proxy or intermediate device injecting large headers; connecting to the wrong process/port that speaks a different protocol; a version mismatch where the AppHost emits unexpectedly large header sets; a desynchronized socket stream misinterpreting body bytes as headers.","solutions":["Verify you are connecting to the correct AppHost endpoint (right port/process, not a proxy).","Check that the AppHost CLI/CLI-generated module and server versions match; rebuild the Python module against the running AppHost version.","Reset the connection (recreate the client) in case the stream is desynchronized from a prior failed read.","If legitimate headers are genuinely large, report/upgrade since the 8 KB limit is fixed in the generated module."],"exampleFix":"// before\nclient = AspireClient(host, port)  # wrong port: hit a dev proxy injecting headers\n// after\nclient = AspireClient(\"127.0.0.1\", apphost_port)  # port from AppHost output, direct connection","handlingStrategy":"try-catch","validationCode":"def check_headers_safe():\n    return client.is_connected() if hasattr(client, 'is_connected') else True","typeGuard":null,"tryCatchPattern":"try:\n    result = client.ping()\nexcept ConnectionError as e:\n    if 'Headers too large' in str(e):\n        client = recreate_client()  # resync/reconnect\n    else:\n        raise","preventionTips":["Connect directly to the AppHost port, never through a mutating proxy","Keep client module and AppHost versions in sync","Recreate the client after any protocol-level failure"],"tags":["python","connection","headers","protocol"],"backgroundTag":"payload-too-large","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}