(new MemoryStream(content, start, length, writable: false));
}
diff --git a/app/Tests/Tools/Mail/MailEncryptionDetectionTests.cs b/app/Tests/Tools/Mail/MailEncryptionDetectionTests.cs
index 6816c26a4..c840f37fd 100644
--- a/app/Tests/Tools/Mail/MailEncryptionDetectionTests.cs
+++ b/app/Tests/Tools/Mail/MailEncryptionDetectionTests.cs
@@ -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("-----BEGIN PGP MESSAGE-----
hQEMA
", ExpectedResult = true)]
[TestCase("-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA256\n\nReadable text.", ExpectedResult = false)]
diff --git a/app/Tests/Tools/Mail/MailSummaryReaderTests.cs b/app/Tests/Tools/Mail/MailSummaryReaderTests.cs
index 9b2977e81..a30b7168f 100644
--- a/app/Tests/Tools/Mail/MailSummaryReaderTests.cs
+++ b/app/Tests/Tools/Mail/MailSummaryReaderTests.cs
@@ -42,6 +42,38 @@ public sealed class MailSummaryReaderTests
}
}
+ ///
+ /// 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.
+ ///
+ [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 };
diff --git a/app/Tests/Tools/Mail/MailTextBuilderTests.cs b/app/Tests/Tools/Mail/MailTextBuilderTests.cs
index 3a0aa626e..34e119604 100644
--- a/app/Tests/Tools/Mail/MailTextBuilderTests.cs
+++ b/app/Tests/Tools/Mail/MailTextBuilderTests.cs
@@ -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 ",
+ "To: Team ",
+ "Cc: Library ",
+ "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)]