Upgraded MailKit and MimeKit to v5.0 (#1226)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-24 08:35:36 +01:00
1 parent d121ec3b47
commit f6a4348820
15 files changed
+242 -85

No files matched your search

@@ -0,0 +1,10 @@
From: =?utf-8?q?M=C3=BCller=2C_J=C3=BCrgen?= <juergen@xn--mller-kva.example>
To: Team <team@example.org>
Cc: Library <info@xn--bcher-kva.example>
Subject: =?utf-8?q?Quarterly_report=C2=A0Q3_=E2=80=93_please_review=C2=A0?=
Date: Mon, 16 Nov 2026 09:30:00 +0100
Message-ID: <idn-sender@xn--mller-kva.example>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
The figures for the third quarter are ready.
@@ -1,4 +1,7 @@
using System.Globalization;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using AIStudio.Settings.DataModel;
using AIStudio.Tools.Mail;
@@ -7,6 +10,8 @@ using MailKit;
using MailKit.Net.Imap;
using MailKit.Security;
using MimeKit;
namespace AIStudio.Tests.Tools.Mail;
/// <summary>
@@ -142,4 +147,33 @@ public sealed class ImapMailboxConnectorTests
[TestCase(FolderAttributes.Trash | FolderAttributes.HasNoChildren, ExpectedResult = MailFolderSpecialUse.TRASH)]
[TestCase(FolderAttributes.Junk | FolderAttributes.Important, ExpectedResult = MailFolderSpecialUse.JUNK)]
public MailFolderSpecialUse AFolderIsForWhatTheServerAnnounces(FolderAttributes attributes) => ImapMailboxConnector.ToSpecialUse(attributes);
[Test]
public void AHeaderBlockWithAVeryLongFieldIsReadAsAWhole()
{
//
// The IMAP client parses every header block like this, with the default options of MimeKit,
// whose limit the connector raises before the first mail is fetched:
//
RuntimeHelpers.RunClassConstructor(typeof(ImapMailboxConnector).TypeHandle);
var recipients = string.Join(",\r\n ", Enumerable.Range(1, 1000).Select(number => $"recipient{number.ToString(CultureInfo.InvariantCulture)}@example.org"));
var headerBlock = Encoding.ASCII.GetBytes($"From: alice@example.org\r\nTo: {recipients}\r\nSubject: All hands\r\n\r\n");
var headers = Parse(headerBlock, ParserOptions.Default);
using (Assert.EnterMultipleScope())
{
Assert.That(() => Parse(headerBlock, new ParserOptions()), Throws.TypeOf<FormatException>(), "MimeKit reads such a field by default, so the limit needs no raising.");
Assert.That(headers[HeaderId.Subject], Is.EqualTo("All hands"));
Assert.That(MailHeaders.ReadMailboxes(headers, HeaderId.To), Has.Count.EqualTo(1000));
}
return;
static HeaderList Parse(byte[] headerBlock, ParserOptions options)
{
using var stream = new MemoryStream(headerBlock);
return new MimeParser(options, stream).ParseHeaders();
}
}
}
@@ -47,7 +47,7 @@ public sealed class MailAttachmentPiecesTests
using (Assert.EnterMultipleScope())
{
Assert.That(written, Is.EqualTo(file));
Assert.That(server.RequestedOffsets, Is.EqualTo(new[] { 0, 7, 14, 21 }));
Assert.That(server.RequestedOffsets, Is.EqualTo(new long[] { 0, 7, 14, 21 }));
}
}
@@ -93,12 +93,12 @@ public sealed class MailAttachmentPiecesTests
/// <param name="deliveredBytes">How many bytes it delivers per piece. A correct server delivers what it was asked for.</param>
private sealed class PieceServer(byte[] content, int deliveredBytes = PIECE_BYTES)
{
public List<int> RequestedOffsets { get; } = [];
public List<long> RequestedOffsets { get; } = [];
public Task<Stream> FetchAsync(int offset, CancellationToken token)
public Task<Stream> FetchAsync(long offset, CancellationToken token)
{
this.RequestedOffsets.Add(offset);
var start = Math.Min(offset, content.Length);
var start = (int)Math.Min(offset, content.Length);
var length = Math.Min(deliveredBytes, content.Length - start);
return Task.FromResult<Stream>(new MemoryStream(content, start, length, writable: false));
}
@@ -56,6 +56,32 @@ public sealed class MailEncryptionDetectionTests
}
}
[Test]
public void AStructureOfAnyDepthIsRead()
{
//
// Deep enough to overflow the stack of a recursive walk. The IMAP client reads a structure of
// any depth, so a mail nested this deeply on purpose must not end AI Studio:
//
BodyPart encrypted = Part("application", "pkcs7-mime", smimeType: "enveloped-data");
BodyPart readable = PLAIN_TEXT;
for (var depth = 0; depth < 100_000; depth++)
{
encrypted = Multipart("mixed", PLAIN_TEXT, encrypted);
readable = Multipart("mixed", readable, PLAIN_TEXT);
}
using (Assert.EnterMultipleScope())
{
Assert.That(MailEncryptionDetection.Detect(encrypted), Is.EqualTo(MailEncryptionKind.SMIME));
Assert.That(MailEncryptionDetection.Detect(readable), Is.EqualTo(MailEncryptionKind.NONE));
}
}
[Test]
public void TheFirstEncryptedPartDecides() =>
Assert.That(MailEncryptionDetection.Detect(Multipart("mixed", PLAIN_TEXT, Multipart("mixed", Part("application", "x-microsoft-rpmsg-message")), Part("application", "pkcs7-mime", smimeType: "enveloped-data"))), Is.EqualTo(MailEncryptionKind.MICROSOFT_IRM), "The parts are not visited in their order.");
[TestCase("-----BEGIN PGP MESSAGE-----\n\nhQEMA\n-----END PGP MESSAGE-----", ExpectedResult = true)]
[TestCase("<p>-----BEGIN PGP MESSAGE-----<br>hQEMA</p>", ExpectedResult = true)]
[TestCase("-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA256\n\nReadable text.", ExpectedResult = false)]
+46 -2
View File
@@ -42,6 +42,38 @@ public sealed class MailSummaryReaderTests
}
}
/// <remarks>
/// The keys come from what MimeKit reads out of the header block. A version of MimeKit which reads
/// a header in another way would give every mail it concerns a new key and embed it anew, so these
/// keys stay as MimeKit 4.18.2 made them. Change a pinned value only on purpose.
/// </remarks>
[TestCase("header-injection.eml", "mail:4ad6f665ee9a7b698d32276855cc2c8da31e5c7d25057c45d15484712ee045ad")]
[TestCase("priority-with-attachment.eml", "mail:797ac20c552b0aed439f87095a2345f38a5268a7656024b5394349652ac08e18")]
[TestCase("idn-sender.eml", "mail:e7284cb3afe9041918d344de00e01f55aebc7b29ebcd25967a8810eca13068d8")]
public void TheKeysOfIndexedMailsStayTheSame(string fixtureName, string expectedKey)
{
var summary = Summary(fixtureName);
summary.Size = 4096;
Assert.That(MailContentKey.Create(MailSummaryReader.ReadIdentity(summary)), Is.EqualTo(expectedKey));
}
[Test]
public void AnInternationalDomainIsReadInUnicode()
{
var summary = Summary("idn-sender.eml");
using (Assert.EnterMultipleScope())
{
Assert.That(MailSummaryReader.ReadIdentity(summary).FromAddresses, Is.EqualTo(new[] { "juergen@müller.example" }));
Assert.That(MailSummaryReader.ReadAddresses(summary.Headers!), Is.EqualTo(new[]
{
new MailAddressRecord(MailAddressRole.FROM, "juergen@müller.example", "Müller, Jürgen"),
new MailAddressRecord(MailAddressRole.TO, "team@example.org", "Team"),
new MailAddressRecord(MailAddressRole.CC, "info@bücher.example", "Library"),
}), "The index keeps an address in the form the mails indexed before keep it.");
}
}
[Test]
public void EveryAddressKeepsItsHeader()
{
@@ -169,8 +201,20 @@ public sealed class MailSummaryReaderTests
}
[Test]
public void AMailFetchedWithoutItsHeaderBlockIsRefused() =>
Assert.That(() => MailSummaryReader.ReadIdentity(new MessageSummary(0)), Throws.InvalidOperationException);
public void AMailFetchedWithoutItsHeaderBlockIsRefused()
{
//
// The IMAP client hands over an empty header block when it cannot parse the one the server sent:
//
var withEmptyHeaderBlock = new MessageSummary(0) { Headers = new HeaderList() };
using (Assert.EnterMultipleScope())
{
Assert.That(MailSummaryReader.ReadHeaders(new MessageSummary(0)), Is.Null);
Assert.That(MailSummaryReader.ReadHeaders(withEmptyHeaderBlock), Is.Null, "An empty header block counts as one which could be read.");
Assert.That(() => MailSummaryReader.ReadIdentity(new MessageSummary(0)), Throws.InvalidOperationException);
Assert.That(() => MailSummaryReader.ReadIdentity(withEmptyHeaderBlock), Throws.InvalidOperationException);
}
}
private static MessageSummary Summary(string fixtureName) => new(0) { Headers = MailFixtures.Load(fixtureName).Headers };
@@ -96,6 +96,15 @@ public sealed class MailTextBuilderTests
}
}
[Test]
public void AnInternationalDomainIsShownInUnicode() =>
Assert.That(Build("idn-sender.eml").HeaderBlock, Is.EqualTo(string.Join('\n',
"From: Müller, Jürgen <juergen@müller.example>",
"To: Team <team@example.org>",
"Cc: Library <info@bücher.example>",
"Subject: Quarterly report Q3 – please review",
"Date: 2026-11-16 09:30 +01:00")), "The text shows an address in another form than the mails indexed before.");
[TestCase("smime-enveloped.eml", MailEncryptionKind.SMIME)]
[TestCase("smime-opaque-signed.eml", MailEncryptionKind.SMIME_OPAQUE_SIGNED)]
[TestCase("pgp-mime.eml", MailEncryptionKind.PGP_MIME)]