Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Src/SmtpServer/Protocol/SmtpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,23 +1099,35 @@ 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;
}

return false;

static Mailbox CreateMailbox(ReadOnlySequence<byte> localpart, ReadOnlySequence<byte> 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);
}
}

Expand Down
19 changes: 8 additions & 11 deletions Src/SmtpServer/Text/StringUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ internal static string Create(ReadOnlySequence<byte> sequence)
return Create(sequence, Encoding.ASCII);
}

internal static unsafe string Create(ReadOnlySequence<byte> sequence, Encoding encoding)
internal static string Create(ReadOnlySequence<byte> 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
{
Expand All @@ -43,10 +43,7 @@ internal static unsafe string Create(ReadOnlySequence<byte> sequence, Encoding e
}
}

fixed (byte* ptr = buffer)
{
return encoding.GetString(ptr, buffer.Length);
}
return encoding.GetString(buffer);
}
}

Expand Down
Loading