Contact-mail: vervolg op review (notitie weg, tekstdeel ingesprongen, nieuwe Core-tests voor details en Reply-To)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 15:31:49 +01:00
co-authored by Claude Sonnet 5.5
parent bc7936034c
commit 7974d22912
3 changed files with 122 additions and 4 deletions
@@ -21,3 +21,8 @@
## Found by the real-database pipeline test
`datetime` drops fractions, which made the claim's concurrency check fail on the outcome save; claim time is now truncated to whole seconds.
## Follow-up (review by AndreaSlopFotografie)
- The "you can reply directly" note is removed from the ContactMessage catalog (Reply-To may be left out when the visitor address is invalid).
- Plain text: every line of a visitor value is indented two spaces, so a visitor's own `--` line or fake headings cannot imitate the layout.
- NEW Core tests (next to the unchanged existing ones): `ContactMessageMailRenderingTests` (encoding of script/quotes/line breaks, labels encoded, null/empty Details give no block, fixed subject, text indent, no reply hint) and `SmtpReplyToTests` (valid Reply-To with name, invalid/CRLF address left out, control characters in the name dropped, none when absent), using the existing in-process stub SMTP server. Core: 639 green (625 existing + 14 new).
@@ -479,3 +479,109 @@ public class MailPropertyTests(ITestOutputHelper output)
return value == token;
});
}
/// <summary>NEW tests for the contact-message mail (details block, text part); the existing tests above are unchanged.</summary>
public class ContactMessageMailRenderingTests
{
private readonly MailTemplateRenderer _renderer = new();
private static MailModel Model(params MailDetail[]? details) => new("De Site", Details: details);
[Fact]
public void Details_AreHtmlEncoded_BeforeLineBreaksBecomeBr()
{
var mail = _renderer.Render(MailKind.ContactMessage, "nl",
Model(new MailDetail("Bericht", "<script>alert(\"x\")</script> & 'q'\r\nregel2\nregel3")));
mail.Html.Should().NotContain("<script>").And.Contain("&lt;script&gt;")
.And.Contain("&amp;").And.Contain("regel2").And.Contain("<br>regel3");
mail.Html.Should().Contain("&lt;/script&gt; &amp; ");
}
[Fact]
public void ALabelIsEncodedToo()
=> _renderer.Render(MailKind.ContactMessage, "en", Model(new MailDetail("<b>L</b>", "v"))).Html
.Should().NotContain("<b>L</b>").And.Contain("&lt;b&gt;");
[Theory]
[InlineData(false)]
[InlineData(true)]
public void NullOrEmptyDetails_GiveNoBlock(bool empty)
{
var model = empty ? new MailModel("De Site", Details: []) : new MailModel("De Site");
var mail = _renderer.Render(MailKind.ContactMessage, "nl", model);
mail.Html.Should().NotContain("class=\"mail-rule\"");
mail.Text.Should().NotContain(" ");
}
[Fact]
public void TheSubject_IsFixed_AndNeverContainsADetail()
{
var mail = _renderer.Render(MailKind.ContactMessage, "nl", Model(new MailDetail("Onderwerp", "ZXQ-geheim")));
mail.Subject.Should().Be("Nieuw bericht via De Site");
_renderer.Render(MailKind.ContactMessage, "en", Model()).Subject.Should().Be("New message via De Site");
}
[Fact]
public void TheText_IndentsEveryVisitorLine_SoFakeHeadingsCannotImitateTheLayout()
{
var mail = _renderer.Render(MailKind.ContactMessage, "nl",
Model(new MailDetail("Bericht", "hallo\n--\nNieuw bericht via nep")));
var lines = mail.Text.Split('\n').Select(l => l.TrimEnd('\r')).ToList();
lines.Should().Contain("Bericht:").And.Contain(" hallo").And.Contain(" --").And.Contain(" Nieuw bericht via nep");
lines.Count(l => l == "--").Should().Be(1, "only the real footer separator starts at column 0");
}
[Fact]
public void NoReplyHintIsInTheMail_BecauseReplyToMayHaveBeenLeftOut()
=> _renderer.Render(MailKind.ContactMessage, "nl", Model()).Html.Should().NotContain("antwoorden");
}
public class SmtpReplyToTests
{
private static SmtpEmailSender Sender(int port) => new(
Options.Create(new EmailOptions
{
FromAddress = "no-reply@example.nl",
FromName = "Example Site",
Smtp = { Host = "127.0.0.1", Port = port, EnableSsl = false, TimeoutSeconds = 5 }
}),
Microsoft.Extensions.Logging.Abstractions.NullLogger<SmtpEmailSender>.Instance);
private static async Task<string> SendAsync(string? replyAddress, string? replyName)
{
using var server = new StubSmtpServer();
var result = await Sender(server.Port).SendAsync(
new OutgoingMail("to@example.org", "Hi", "<p>x</p>", "x", ReplyToAddress: replyAddress, ReplyToName: replyName));
result.Sent.Should().BeTrue();
return server.ReceivedData!;
}
[Fact]
public async Task AValidReplyTo_IsSet_WithItsName()
=> (await SendAsync("jan@example.nl", "Jan Jansen")).Should().Contain("Reply-To: Jan Jansen <jan@example.nl>");
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("not-an-address")]
[InlineData("a@b.nl\r\nBcc: evil@example.com")]
public async Task AnInvalidReplyTo_IsLeftOut_AndTheMailIsStillSent(string? address)
=> (await SendAsync(address, "Jan")).Should().NotContain("Reply-To:").And.NotContain("evil@example.com");
[Fact]
public async Task ANameWithControlCharacters_IsDropped_NeverReachingAHeader()
{
var data = await SendAsync("jan@example.nl", "Jan\r\nBcc: evil@example.com");
data.Should().Contain("Reply-To: jan@example.nl").And.NotContain("evil@example.com");
}
[Fact]
public async Task WithoutAReplyTo_ThereIsNoHeader()
=> (await SendAsync(null, null)).Should().NotContain("Reply-To:");
}
@@ -108,11 +108,11 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
{
["nl"] = new("Nieuw bericht via {0}", "Nieuw bericht via het contactformulier",
"Er is een bericht verstuurd via het contactformulier van {0}.",
null, "Je kunt direct op deze mail antwoorden: het antwoord gaat naar het e-mailadres van de afzender.",
null, null,
"Deze mail is automatisch verstuurd door {0}."),
["en"] = new("New message via {0}", "New message from the contact form",
"A message was sent through the contact form of {0}.",
null, "You can reply to this message directly: the reply goes to the sender's e-mail address.",
null, null,
"This message was sent automatically by {0}.")
}
};
@@ -287,8 +287,15 @@ public sealed class MailTemplateRenderer : IMailTemplateRenderer
{
foreach (var detail in details)
{
// Plain text: a label line, then the value as written (the message keeps its own line breaks).
sb.Append(detail.Label).AppendLine(":").AppendLine(detail.Value.Replace("\r\n", "\n").Replace('\r', '\n')).AppendLine();
// Plain text: a label line, then the value indented two spaces per line, so a visitor's own
// '--' line or fake headings cannot imitate the layout of the mail.
sb.Append(detail.Label).AppendLine(":");
foreach (var line in detail.Value.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n'))
{
sb.Append(" ").AppendLine(line);
}
sb.AppendLine();
}
}
if (url is not null) sb.AppendLine(url).AppendLine();