{"record":{"id":"488cc07b4fdbde0e","repo":"chenhg5/cc-connect","slug":"content-length-too-large-d","errorCode":null,"errorMessage":"Content-Length too large: %d","messagePattern":"Content-Length too large: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agent/copilot/jsonrpc.go","lineNumber":115,"sourceCode":"\t\t\t// End of headers\n\t\t\tbreak\n\t\t}\n\t\tif strings.HasPrefix(line, \"Content-Length: \") {\n\t\t\tval := strings.TrimPrefix(line, \"Content-Length: \")\n\t\t\tn, err := strconv.Atoi(strings.TrimSpace(val))\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"invalid Content-Length: %w\", err)\n\t\t\t}\n\t\t\tcontentLength = n\n\t\t}\n\t\t// Ignore other headers (Content-Type, etc.)\n\t}\n\n\tif contentLength < 0 {\n\t\treturn nil, fmt.Errorf(\"missing Content-Length header\")\n\t}\n\tif contentLength > 10*1024*1024 {\n\t\treturn nil, fmt.Errorf(\"Content-Length too large: %d\", contentLength)\n\t}\n\n\tbody := make([]byte, contentLength)\n\tif _, err := io.ReadFull(lr.reader, body); err != nil {\n\t\treturn nil, fmt.Errorf(\"read body: %w\", err)\n\t}\n\treturn body, nil\n}\n\n// rpcClient manages JSON-RPC request IDs and pending responses.\ntype rpcClient struct {\n\twriter *lspWriter\n\tnextID atomic.Int64\n\n\tpendingMu sync.Mutex\n\tpending   map[int64]chan *jsonRPCResponse\n}\n","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/agent/copilot/jsonrpc.go#L97-L133","documentation":"readMessage enforces a 10 MB cap on the declared Content-Length to prevent huge or malicious allocations (`body := make([]byte, contentLength)` would otherwise allocate whatever the peer claims). If the header value exceeds 10*1024*1024 the message is rejected with \"Content-Length too large: %d\".","triggerScenarios":"The copilot child process (or test mock) sends a Content-Length header whose value is greater than 10485760 bytes — e.g. a corrupt/misencoded large payload, a bogus integer in a test fixture, or a malformed writer emitting an absurd length.","commonSituations":"TestLSPReader_TooLargeContentLength style fixtures; a writer that emits the body length before appending more content (counting wrong); a hostile/corrupted child process; unit confusion (bytes vs KiB) when constructing a big fake response.","solutions":["Fix the message producer to send the true byte length of its JSON body","If you legitimately need larger messages, raise the 10*1024*1024 cap in jsonrpc.go and re-test","Log and inspect the declared length vs actual bytes to find the framing bug","Split oversized payloads at the protocol level rather than in one frame"],"exampleFix":"// before (test/mock emits wrong length)\nbody := buildHugeResult()\nfmt.Fprintf(w, \"Content-Length: %d\\r\\n\\r\\n\", len(body)*2)\n// after\nfmt.Fprintf(w, \"Content-Length: %d\\r\\n\\r\\n\", len(body))","handlingStrategy":"validation","validationCode":"func plausibleContentLength(n int) bool {\n    return n > 0 && n <= 10*1024*1024\n}","typeGuard":null,"tryCatchPattern":"body, err := readMessage(lr)\nif err != nil {\n    if strings.Contains(err.Error(), \"too large\") {\n        slog.Error(\"copilot: oversized frame declared\", \"err\", err)\n        return errFatal\n    }\n    return err\n}","preventionTips":["Compute Content-Length with len(body) — never hand-write it","Split large results into multiple smaller JSON-RPC messages","Keep test payloads representative of production sizes","Review any change to the child's writer for byte-count unit mistakes"],"tags":["jsonrpc","lsp","limit","payload"],"backgroundTag":"payload-too-large","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}