{"record":{"id":"42fe72cc4a0be2dc","repo":"jstedfast/MailKit","slug":"invalid-nonce-length-should-be-8-bytes-ntlmchallengemessage","errorCode":null,"errorMessage":"Invalid nonce length (should be 8 bytes).","messagePattern":"Invalid nonce length \\(should be 8 bytes\\)\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/Ntlm/NtlmChallengeMessage.cs","lineNumber":72,"sourceCode":"\n\t\t\tcached = new byte[length];\n\t\t\tBuffer.BlockCopy (message, startIndex, cached, 0, length);\n\t\t}\n\n\t\t~NtlmChallengeMessage ()\n\t\t{\n\t\t\tif (serverChallenge != null)\n\t\t\t\tArray.Clear (serverChallenge, 0, serverChallenge.Length);\n\t\t}\n\n\t\tpublic byte[] ServerChallenge {\n\t\t\tget { return serverChallenge; }\n\t\t\tset { \n\t\t\t\tif (value == null)\n\t\t\t\t\tthrow new ArgumentNullException (nameof (value));\n\n\t\t\t\tif (value.Length != 8)\n\t\t\t\t\tthrow new ArgumentException (\"Invalid nonce length (should be 8 bytes).\", nameof (value));\n\n\t\t\t\tArray.Clear (serverChallenge, 0, serverChallenge.Length);\n\t\t\t\tserverChallenge = value;\n\t\t\t}\n\t\t}\n\n\t\tpublic string? TargetName {\n\t\t\tget; set;\n\t\t}\n\n\t\tpublic NtlmTargetInfo? TargetInfo {\n\t\t\tget; set;\n\t\t}\n\n\t\tpublic byte[]? GetEncodedTargetInfo ()\n\t\t{\n\t\t\treturn TargetInfo?.Encode ((Flags & NtlmFlags.NegotiateUnicode) != 0);\n\t\t}","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/Ntlm/NtlmChallengeMessage.cs#L54-L90","documentation":"The ServerChallenge property setter throws ArgumentException \"Invalid nonce length (should be 8 bytes).\" when the supplied array is not exactly 8 bytes. The NTLM server challenge is a fixed 64-bit value from the Type2 message.","triggerScenarios":"Assigning ServerChallenge with an array of length other than 8 — e.g. slicing the raw Type2 response with wrong offsets, or a base64 decode producing 12/16 bytes.","commonSituations":"Manual Type2 message parsing with incorrect offset arithmetic; hardcoded test challenges typed with the wrong length.","solutions":["Pass exactly 8 bytes; slice the server response at the correct offset (bytes 16..24 of the Type2 message).","Check value.Length == 8 before assigning.","Prefer new NtlmChallengeMessage(buffer, startIndex) over manual property assignment so offsets are handled for you."],"exampleFix":"// before\nchallenge.ServerChallenge = Encoding.ASCII.GetBytes(\"mychallenge\"); // 11 bytes\n// after\nchallenge.ServerChallenge = challengeBytes.AsSpan(16, 8).ToArray(); // exactly 8 bytes","handlingStrategy":"validation","validationCode":"if (serverChallenge == null || serverChallenge.Length != 8) throw new ArgumentException(\"ServerChallenge must be exactly 8 bytes.\");\nchallenge.ServerChallenge = serverChallenge;","typeGuard":"static bool IsValidChallenge(byte[]? value) => value != null && value.Length == 8;","tryCatchPattern":"try {\n\tchallenge.ServerChallenge = value;\n} catch (ArgumentException ex) when (ex.Message.Contains(\"nonce length\")) {\n\t// re-slice the Type2 response at the correct offset\n\tchallenge.ServerChallenge = rawResponse.AsSpan(16, 8).ToArray();\n}","preventionTips":["The server challenge lives at bytes 16..24 of the Type2 message; slice with fixed offsets.","Verify array length before assignment.","Use the message constructors instead of hand-setting fields."],"tags":["invalid-argument-value","csharp","ntlm","nonce"],"backgroundTag":"invalid-argument-value","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}