{"record":{"id":"4e23313c12493652","repo":"jstedfast/MailKit","slug":"startindex","errorCode":null,"errorMessage":"startIndex","messagePattern":"startIndex","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/Ntlm/NtlmMessageBase.cs","lineNumber":84,"sourceCode":"\t\t}\n\n\t\tbool CheckSignature (byte[] message, int startIndex)\n\t\t{\n\t\t\tfor (int i = 0; i < Signature.Length; i++) {\n\t\t\t\tif (message[startIndex + i] != Signature[i])\n\t\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\treturn BitConverterLE.ToUInt32 (message, startIndex + 8) == Type;\n\t\t}\n\n\t\tprotected void ValidateArguments (byte[] message, int startIndex, int length)\n\t\t{\n\t\t\tif (message == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (message));\n\n\t\t\tif (startIndex < 0 || startIndex > message.Length)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (startIndex));\n\n\t\t\tif (length < 12 || length > (message.Length - startIndex))\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (length));\n\n\t\t\tif (!CheckSignature (message, startIndex))\n\t\t\t\tthrow new ArgumentException (string.Format (CultureInfo.InvariantCulture, \"Invalid Type{0} message.\", Type), nameof (message));\n\n\t\t\tvar messageType = BitConverterLE.ToUInt32 (message, 8);\n\t\t\tif (messageType != Type)\n\t\t\t\tthrow new ArgumentException (string.Format (CultureInfo.InvariantCulture, \"Invalid Type{0} message.\", Type), nameof (message));\n\t\t}\n\n\t\tpublic abstract byte[] Encode ();\n\t}\n}\n","sourceCodeStart":66,"sourceCodeEnd":100,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/Ntlm/NtlmMessageBase.cs#L66-L100","documentation":"NtlmMessageBase.ValidateArguments throws ArgumentOutOfRangeException when startIndex is negative or greater than message.Length. It bounds-checks the offset into the raw NTLM message before reading the signature and type fields.","triggerScenarios":"Passing a startIndex outside [0, message.Length] — e.g. a hardcoded nonzero offset on a short buffer, or an offset advanced past the response length after parsing a concatenated stream.","commonSituations":"Parsing SPNEGO-wrapped NTLM messages where the inner-message offset was computed wrongly; reusing an offset from a previous larger message.","solutions":["Use startIndex 0 for a standalone NTLM message, or compute the offset from the actual SPNEGO header length.","Clamp/verify: 0 <= startIndex && startIndex <= message.Length before calling.","Prefer the constructor overloads without an explicit startIndex when the message starts at byte 0."],"exampleFix":"// before\nvar challenge = new NtlmChallengeMessage(buffer, 12); // buffer.Length is 8\n// after\nint start = Math.Min(12, buffer.Length);\nvar challenge = new NtlmChallengeMessage(buffer, start >= 0 ? start : 0); // or just use 0 for a raw Type2","handlingStrategy":"validation","validationCode":"if (startIndex < 0 || startIndex > message.Length) throw new ArgumentOutOfRangeException(nameof(startIndex));\nvar msg = new NtlmChallengeMessage(message, startIndex);","typeGuard":"static bool IsValidStartIndex(byte[] message, int startIndex) =>\n\tmessage != null && startIndex >= 0 && startIndex <= message.Length;","tryCatchPattern":"try {\n\tmsg = new NtlmChallengeMessage(buffer, startIndex);\n} catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"startIndex\") {\n\t// bad SPNEGO offset; retry from the start of the buffer\n\tstartIndex = 0;\n\tmsg = new NtlmChallengeMessage(buffer, startIndex);\n}","preventionTips":["Compute SPNEGO inner-message offsets from the actual header lengths, not hardcoded values.","Reset offsets to 0 when parsing a fresh standalone message.","Bound-check every offset against buffer.Length before use."],"tags":["argument-out-of-range","csharp","ntlm","message-parsing"],"backgroundTag":"argument-out-of-range","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}