{"record":{"id":"44f3e1ad7b635afe","repo":"github/copilot-sdk","slug":"json-rpc-frame-has-a-missing-duplicate-or-invali","errorCode":null,"errorMessage":"JSON-RPC frame has a missing, duplicate, or invalid Content-Length header.","messagePattern":"JSON-RPC frame has a missing, duplicate, or invalid Content-Length header\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"dotnet/src/JsonRpc.cs","lineNumber":431,"sourceCode":"        // A missing or unparsable Content-Length means the framing is broken — there's\n        // no safe way to resync, so throw and let the read loop terminate the connection.\n        int contentLength = -1;\n        ReadOnlySpan<byte> prefix = \"Content-Length: \"u8;\n        // headerEnd points just past the \\r\\n\\r\\n terminator. Drop only the trailing\n        // empty line's \\r\\n; each remaining header line is still \\r\\n-terminated and\n        // gets split out by the IndexOf below.\n        var headerLines = buffer.AsSpan(0, headerEnd - 2);\n        while (!headerLines.IsEmpty)\n        {\n            int lineEnd = headerLines.IndexOf(\"\\r\\n\"u8);\n            ReadOnlySpan<byte> line = lineEnd >= 0 ? headerLines.Slice(0, lineEnd) : headerLines;\n\n            if (line.StartsWith(prefix) &&\n                (contentLength >= 0 ||\n                 !int.TryParse(line.Slice(prefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out contentLength) ||\n                 contentLength < 0))\n            {\n                throw new InvalidDataException(\"JSON-RPC frame has a missing, duplicate, or invalid Content-Length header.\");\n            }\n\n            headerLines = lineEnd >= 0 ? headerLines.Slice(lineEnd + 2) : default;\n        }\n\n        if (contentLength < 0)\n        {\n            throw new InvalidDataException(\"JSON-RPC frame is missing the Content-Length header.\");\n        }\n\n        // Bytes after the header that we already have\n        int extraBytes = filled - headerEnd;\n\n        // Ensure buffer is large enough for the body and any overflow already read.\n        int needed = Math.Max(contentLength, extraBytes);\n        if (needed > buffer.Length)\n        {\n            var newBuffer = new byte[needed];","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/dotnet/src/JsonRpc.cs#L413-L449","documentation":"ReadMessageAsync validates each header line: if a line starts with the Content-Length prefix but the value was already parsed (duplicate header), fails to parse, or is negative, it throws InvalidDataException. A JSON-RPC frame must carry exactly one valid non-negative integer Content-Length.","triggerScenarios":"Peer sends two Content-Length headers; sends a non-numeric or negative value (\"Content-Length: abc\"); uses a broken hand-rolled framing writer.","commonSituations":"Custom protocol implementations that hand-format headers; locale formatting inserting separators; relays/proxies duplicating headers; testing with malformed fixture frames.","solutions":["Fix the peer to emit exactly one header: \"Content-Length: <int>\\r\\n\" with an ASCII integer.","Remove duplicate Content-Length headers from any header-building code.","Validate your client's serialization with a known-good JSON-RPC test vector before pointing it at this library.","Check for middlewares that rewrite or append headers."],"exampleFix":"// before\nsb.Append($\"Content-Length: {len:N0}\\r\\n\"); // locale grouping chars\n// after\nsb.Append($\"Content-Length: {len.ToString(CultureInfo.InvariantCulture)}\\r\\n\");","handlingStrategy":"validation","validationCode":"static void ValidateFrameHeader(string header)\n{\n    var matches = Regex.Matches(header, @\"Content-Length:\\s*(\\d+)\");\n    if (matches.Count != 1) throw new FormatException(\"Exactly one valid Content-Length required\");\n}","typeGuard":null,"tryCatchPattern":"try { await reader.ReadLoopAsync(ct); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"Content-Length header\")) {\n    logger.LogError(ex, \"Peer sent malformed Content-Length; dropping connection\");\n    transport.Close();\n}","preventionTips":["Format header ints with CultureInfo.InvariantCulture","Build headers with a shared, tested framing helper","Never duplicate Content-Length in header builders","Test senders against reference JSON-RPC fixtures"],"tags":["jsonrpc","framing","invalid-header"],"backgroundTag":"invalid-argument-format","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}