{"record":{"id":"cebe056738f479f2","repo":"babalae/better-genshin-impact","slug":"maxpayloadlength","errorCode":null,"errorMessage":"命名管道消息超过 {MaxPayloadLength} 字节限制。","messagePattern":"命名管道消息超过 (.+?) 字节限制。","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Service/Instance/InstanceIpcProtocol.cs","lineNumber":298,"sourceCode":"            span[sizeof(ulong)] == 1);\n    }\n\n    internal static async ValueTask<InstanceIpcFrame?> ReadFrameAsync(\n        Stream stream,\n        CancellationToken cancellationToken)\n    {\n        var header = new byte[FrameHeaderLength];\n        var firstRead = await stream.ReadAsync(header.AsMemory(0, 1), cancellationToken).ConfigureAwait(false);\n        if (firstRead == 0)\n        {\n            return null;\n        }\n\n        await stream.ReadExactlyAsync(header.AsMemory(1), cancellationToken).ConfigureAwait(false);\n        var payloadLength = BinaryPrimitives.ReadUInt32LittleEndian(header);\n        if (payloadLength > MaxPayloadLength)\n        {\n            throw new InvalidDataException($\"命名管道消息超过 {MaxPayloadLength} 字节限制。\");\n        }\n\n        var payloadType = (InstanceIpcPayloadType)header[sizeof(uint)];\n        if (!Enum.IsDefined(payloadType))\n        {\n            throw new InvalidDataException($\"未知命名管道载荷类型：{header[sizeof(uint)]}。\");\n        }\n\n        var payload = new byte[checked((int)payloadLength)];\n        if (payload.Length > 0)\n        {\n            await stream.ReadExactlyAsync(payload, cancellationToken).ConfigureAwait(false);\n        }\n\n        return new InstanceIpcFrame(payloadType, payload);\n    }\n\n    private static async ValueTask WriteFrameAsync(","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Service/Instance/InstanceIpcProtocol.cs#L280-L316","documentation":"Thrown by ReadFrameAsync (the receive side of the IPC framing layer). After reading the 5-byte frame header, it interprets the first 4 bytes as a little-endian uint32 payload length. If that value exceeds MaxPayloadLength (1,048,576 bytes = 1 MiB), the frame is rejected before any payload allocation occurs, protecting against denial-of-service via oversized length fields.","triggerScenarios":"The peer (or a corrupt/malicious stream) sent a frame whose 4-byte length header decodes to a value greater than 1 MiB. In normal operation this should never happen — JSON envelopes and mouse frames are far smaller. A genuinely oversized JSON envelope (e.g., a WebView message embedding a multi-megabyte data payload) could trigger it, but more commonly the length field is garbage from stream misalignment or a crashed peer leaving partial data in the pipe buffer.","commonSituations":"A previous frame was misread (wrong byte count consumed), shifting the stream so the parser interprets payload data or a JSON string as a length header; an older/newer protocol version uses a different header layout; an attempt to send a very large embedded data blob through SendWebViewMessageAsync that serialized to > 1 MiB of JSON.","solutions":["Catch InvalidDataException in the connection receive loop and close the pipe so the accept/reconnect loops establish a fresh stream.","If sending large data through WebView messages, chunk or externalize the data (write to a temp file and pass a path) instead of embedding it in the IPC envelope.","Verify protocol version match between peers — a header layout change will cause every length field to be misread.","Add logging of the raw header bytes and decoded length at the throw site to distinguish genuine oversize from stream corruption."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// In the receive loop:\ntry\n{\n    var frame = await InstanceIpcProtocol.ReadFrameAsync(stream, ct);\n}\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"字节限制\"))\n{\n    _logger.LogError(ex, \"收到超大帧，关闭连接\");\n    await connection.DisposeAsync();\n}","preventionTips":["Never embed payloads > 1 MiB in IPC envelopes — use file-based transfer for large data.","Ensure protocol version match so header layout is consistent.","Close the connection on any frame validation failure to avoid cascade corruption."],"tags":["ipc","named-pipe","binary-protocol","frame-validation","dos-protection"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}