{"record":{"id":"5f1ec056cadbb576","repo":"jstedfast/MailKit","slug":"literal-token-length-literallength-bytes-exceeds-the-maximum","errorCode":null,"errorMessage":"Literal token length ({literalLength} bytes) exceeds the maximum allowed size ({MaxLiteralTokenLength} bytes).","messagePattern":"Literal token length \\((.+?) bytes\\) exceeds the maximum allowed size \\((.+?) bytes\\)\\.","errorType":"exception","errorClass":"ImapProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapEngine.cs","lineNumber":1215,"sourceCode":"\t\t/// <param name=\"cancellationToken\">The cancellation token.</param>\n\t\t/// <exception cref=\"System.InvalidOperationException\">\n\t\t/// The <see cref=\"Stream\"/> is not in literal mode.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation was canceled via the cancellation token.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.IO.IOException\">\n\t\t/// An I/O error occurred.\n\t\t/// </exception>\n\t\tpublic string ReadLiteral (CancellationToken cancellationToken)\n\t\t{\n\t\t\tif (Stream!.Mode != ImapStreamMode.Literal)\n\t\t\t\tthrow new InvalidOperationException ();\n\n\t\t\tint literalLength = Stream.LiteralLength;\n\n\t\t\tif (literalLength > MaxLiteralTokenLength)\n\t\t\t\tthrow new ImapProtocolException ($\"Literal token length ({literalLength} bytes) exceeds the maximum allowed size ({MaxLiteralTokenLength} bytes).\");\n\n\t\t\tvar buf = ArrayPool<byte>.Shared.Rent (literalLength);\n\n\t\t\ttry {\n\t\t\t\tint n, nread = 0;\n\n\t\t\t\tdo {\n\t\t\t\t\tif ((n = Stream.Read (buf, nread, literalLength - nread, cancellationToken)) == 0)\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tnread += n;\n\t\t\t\t} while (nread < literalLength);\n\n\t\t\t\treturn TextEncodings.GetString (buf, 0, nread);\n\t\t\t} finally {\n\t\t\t\tArrayPool<byte>.Shared.Return (buf);\n\t\t\t}\n\t\t}","sourceCodeStart":1197,"sourceCodeEnd":1233,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapEngine.cs#L1197-L1233","documentation":"While reading a literal token (ImapStreamMode.Literal) in the synchronous token loop, ImapEngine compares the declared literal length against MaxLiteralTokenLength and throws ImapProtocolException if exceeded. It guards against absurdly large literals from a malicious/broken server, preventing huge memory allocations via ArrayPool rent.","triggerScenarios":"Server sends a literal ({NNNN}) whose byte count exceeds MaxLiteralTokenLength during response parsing — e.g. fetching a message/filename literal larger than the configured cap.","commonSituations":"Fetching very large attachments or header literals from a server while the client's literal cap is low (MaxLiteralTokenLength default tied to max line/literal settings); hostile or misbehaving server sending bogus large literal sizes.","solutions":["Increase ImapEngine's MaxLiteralTokenLength (client client.MaxLiteralTokenLength / engine property) to accommodate your workload.","Avoid fetching the large parts: use BodyStructure/peek and fetch specific body parts instead of whole-message literals.","If the literal size is bogus, treat it as a server bug — validate/capture the server response and report it.","Raise a fresh connection with a larger cap for the specific large fetch."],"exampleFix":"// before\nclient.Connect(host, 993, true);\nvar msg = folder.GetMessage(uid); // large literal aborts\n\n// after\nclient.Connect(host, 993, true);\nclient.MaxLiteralTokenLength = 50 * 1024 * 1024; // allow larger literals\nvar msg = folder.GetMessage(uid);","handlingStrategy":"try-catch","validationCode":"// before large fetches, compare expected size against the cap\nif (expectedSize > client.MaxLiteralTokenLength) client.MaxLiteralTokenLength = (int)(expectedSize * 1.5);","typeGuard":null,"tryCatchPattern":"try { msg = folder.GetMessage(uid); } catch (ImapProtocolException ex) when (ex.Message.Contains(\"Literal token length\")) { /* raise cap or fetch body parts instead */ }","preventionTips":["Set MaxLiteralTokenLength deliberately for your attachment sizes.","Fetch large messages by body part, not whole-message literals.","Enable protocol logging to detect servers declaring bogus literal sizes."],"tags":["imap","protocol","literal","size-limit"],"backgroundTag":"payload-too-large","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}