diff --git a/Src/SmtpServer/Protocol/SmtpParser.cs b/Src/SmtpServer/Protocol/SmtpParser.cs index 38018cb..3b57b32 100644 --- a/Src/SmtpServer/Protocol/SmtpParser.cs +++ b/Src/SmtpServer/Protocol/SmtpParser.cs @@ -1099,13 +1099,13 @@ public bool TryMakeMailbox(ref TokenReader reader, out IMailbox mailbox) if (reader.TryMake(TryMakeDomain, out var domain)) { - mailbox = CreateMailbox(localpart, domain); + mailbox = CreateMailbox(localpart, domain) ?? throw new SmtpResponseException(SmtpResponse.MailboxNameNotAllowed); return true; } if (reader.TryMake(TryMakeAddressLiteral, out var address)) { - mailbox = CreateMailbox(localpart, address); + mailbox = CreateMailbox(localpart, address) ?? throw new SmtpResponseException(SmtpResponse.MailboxNameNotAllowed); return true; } @@ -1113,9 +1113,21 @@ public bool TryMakeMailbox(ref TokenReader reader, out IMailbox mailbox) static Mailbox CreateMailbox(ReadOnlySequence localpart, ReadOnlySequence domainOrAddress) { - var user = Regex.Unescape(StringUtil.Create(localpart, Encoding.UTF8).Trim('"')); + var tempLocalpart = StringUtil.Create(localpart, Encoding.UTF8)?.Trim('"'); + if (tempLocalpart == null) + { + return null; + } + + var tempDomain = StringUtil.Create(domainOrAddress); + if (tempDomain == null) + { + return null; + } + + var unescapedLocalpart = Regex.Unescape(tempLocalpart); - return new Mailbox(user, StringUtil.Create(domainOrAddress)); + return new Mailbox(unescapedLocalpart, tempDomain); } } diff --git a/Src/SmtpServer/Text/StringUtil.cs b/Src/SmtpServer/Text/StringUtil.cs index 0e097b6..d2912ff 100644 --- a/Src/SmtpServer/Text/StringUtil.cs +++ b/Src/SmtpServer/Text/StringUtil.cs @@ -11,21 +11,21 @@ internal static string Create(ReadOnlySequence sequence) return Create(sequence, Encoding.ASCII); } - internal static unsafe string Create(ReadOnlySequence sequence, Encoding encoding) + internal static string Create(ReadOnlySequence sequence, Encoding encoding) { if (sequence.Length == 0) { return null; } - if (sequence.IsSingleSegment) + if (sequence.Length > short.MaxValue) { - var span = sequence.First.Span; + return null; + } - fixed (byte* ptr = span) - { - return encoding.GetString(ptr, span.Length); - } + if (sequence.IsSingleSegment) + { + return encoding.GetString(sequence.First.Span); } else { @@ -43,10 +43,7 @@ internal static unsafe string Create(ReadOnlySequence sequence, Encoding e } } - fixed (byte* ptr = buffer) - { - return encoding.GetString(ptr, buffer.Length); - } + return encoding.GetString(buffer); } }