From 745f0b220390a201bdb460adcbdf686a80a1f5f1 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Sun, 8 Nov 2026 18:22:26 +0100 Subject: [PATCH] U1 mail-foundation (backend): verzender, opties, sjabloon, SiteSettings met toegestane afzenderlijst IEmailSender met MailKit-implementatie (StartTls, timeout, fouten als waarde), opties met startvalidatie, nl/en sjabloon, taalkeuze, tabel SiteSettings (additieve migratie), SiteSettings-API (Beheerder leest, Eigenaar wijzigt) en emailEnabled/defaultLanguage in de capabilities. Zonder SMTP-host verandert er niets. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 15 + .../HostConfigurationTests.cs | 12 + .../appsettings.json | 25 + .../appsettings.json | 25 + .../Email/EmailRulesAndRendererTests.cs | 264 ++++++++++ .../Email/MigrationRoundTripTests.cs | 59 +++ .../Email/SiteSettingsAndRegistrationTests.cs | 154 ++++++ .../Email/SmtpEmailSenderTests.cs | 160 ++++++ .../Data/ApplicationDbContext.cs | 12 + .../Email/EmailAddressRules.cs | 38 ++ src/SlpModularCms.Core/Email/EmailOptions.cs | 65 +++ .../Email/EmailOptionsValidator.cs | 63 +++ .../Email/EmailServiceCollectionExtensions.cs | 37 ++ src/SlpModularCms.Core/Email/IEmailSender.cs | 26 + .../Email/MailTemplateRenderer.cs | 201 ++++++++ src/SlpModularCms.Core/Email/MailTypes.cs | 63 +++ .../Email/SmtpEmailSender.cs | 82 +++ .../Hosting/ServiceCollectionExtensions.cs | 3 + .../Hosting/SystemController.cs | 17 +- ...20261108170420_AddSiteSettings.Designer.cs | 466 ++++++++++++++++++ .../20261108170420_AddSiteSettings.cs | 38 ++ .../ApplicationDbContextModelSnapshot.cs | 25 + .../SiteSettings/SiteSettingsEntity.cs | 19 + .../SiteSettings/SiteSettingsService.cs | 107 ++++ .../SlpModularCms.Core.csproj | 1 + .../SiteSettingsControllerTests.cs | 56 +++ .../Controllers/SiteSettingsController.cs | 38 ++ 27 files changed, 2069 insertions(+), 2 deletions(-) create mode 100644 src/SlpModularCms.Core.Tests/Email/EmailRulesAndRendererTests.cs create mode 100644 src/SlpModularCms.Core.Tests/Email/MigrationRoundTripTests.cs create mode 100644 src/SlpModularCms.Core.Tests/Email/SiteSettingsAndRegistrationTests.cs create mode 100644 src/SlpModularCms.Core.Tests/Email/SmtpEmailSenderTests.cs create mode 100644 src/SlpModularCms.Core/Email/EmailAddressRules.cs create mode 100644 src/SlpModularCms.Core/Email/EmailOptions.cs create mode 100644 src/SlpModularCms.Core/Email/EmailOptionsValidator.cs create mode 100644 src/SlpModularCms.Core/Email/EmailServiceCollectionExtensions.cs create mode 100644 src/SlpModularCms.Core/Email/IEmailSender.cs create mode 100644 src/SlpModularCms.Core/Email/MailTemplateRenderer.cs create mode 100644 src/SlpModularCms.Core/Email/MailTypes.cs create mode 100644 src/SlpModularCms.Core/Email/SmtpEmailSender.cs create mode 100644 src/SlpModularCms.Core/Migrations/20261108170420_AddSiteSettings.Designer.cs create mode 100644 src/SlpModularCms.Core/Migrations/20261108170420_AddSiteSettings.cs create mode 100644 src/SlpModularCms.Core/SiteSettings/SiteSettingsEntity.cs create mode 100644 src/SlpModularCms.Core/SiteSettings/SiteSettingsService.cs create mode 100644 src/SlpModularCms.Modules.Identity.Tests/Controllers/SiteSettingsControllerTests.cs create mode 100644 src/SlpModularCms.Modules.Identity/Controllers/SiteSettingsController.cs diff --git a/README.md b/README.md index 0d5e758..502bf68 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,21 @@ De API gebruikt een beveiligde flow voor authenticatie: - **Rate Limiting**: Login endpoints hebben rate limiting (Fixed window 5/min, Sliding window 20/min). De limieten gelden **per clientadres**. Achter een reverse proxy is dat alleen het echte bezoekersadres als `ForwardedHeaders__KnownProxies__0` (en volgende) het IP van de proxy bevat; zonder die instelling worden `X-Forwarded-*`-headers genegeerd en delen alle bezoekers het adres van de proxy (één bucket, het gedrag van vóór deze optie). Alleen de opgegeven proxies worden vertrouwd (één hop); een onparseerbaar of wildcard-adres (`0.0.0.0`, `::`) laat de host bij **opstarten** falen. - **Error Handling**: Foutmeldingen volgen de RFC 9457 `ProblemDetails` standaard. +## E-mail (uitnodigingen, wachtwoord opnieuw instellen, meldingen) + +E-mail staat standaard UIT: zolang `Email:Smtp:Host` leeg is, werkt alles zoals voorheen (de beheerder kopieert de uitnodigingslink). Aanzetten per omgeving met omgevingsvariabelen (geheimen nooit in een bestand in de repo): + +| Variabele | Voorbeeld | +|---|---| +| `Email__Smtp__Host` | `mail.slpsoftware.nl` (zet e-mail aan) | +| `Email__Smtp__Port` / `Email__Smtp__EnableSsl` | `587` / `true` (STARTTLS) | +| `Email__Smtp__Username` / `Email__Smtp__Password` | geheimen, alleen in `shared/env` (mode 600) | +| `App__PublicBaseUrl` | `https://test.andreaslop.nl` (https, zonder pad; http alleen voor localhost) | +| `Email__FromAddress` / `Email__FromName` | `no-reply@slpsoftware.nl` / sitenaam | +| `Email__AllowedFromAddresses__0` | extra afzenderadressen die de eigenaar bij Instellingen mag kiezen | + +Lokaal: smtp4dev op `localhost:25` (`Email__Smtp__Host=localhost`, `Email__Smtp__Port=25`, `Email__Smtp__EnableSsl=false`). De opstart weigert een onjuiste mailconfiguratie met een duidelijke melding (zonder geheimen). Afzenderadres en -naam stelt de eigenaar in bij Instellingen, Mail; het adres kan alleen uit de toegestane lijst worden gekozen (SPF/DKIM). Details: `aidlc-docs/features/identity-email-notifications/`. + ## Frontend Development (CMS Admin UI) De frontend is een Vite + React 19 + TypeScript single-page application in de map `frontend/`. Hij gebruikt TanStack Router, Tailwind v4 (hoofdkleur `#ac0000`) met shadcn/ui-stijl componenten, react-i18next (NL/EN), en MSW voor mocking in tests. diff --git a/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/HostConfigurationTests.cs b/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/HostConfigurationTests.cs index 0faefb5..cfc1588 100644 --- a/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/HostConfigurationTests.cs +++ b/src/SlpModularCms.Api.AndreaSlopFotografie.Tests/HostConfigurationTests.cs @@ -74,4 +74,16 @@ public class HostConfigurationTests referenced.Should().NotContain("SlpModularCms.Modules.Offerings") .And.NotContain("SlpModularCms.Modules.Master"); } + + [Fact] + public void Mail_IsOffByDefault_AndNoCredentialIsCommitted() + { + var config = Load(); + + config["Email:Smtp:Host"].Should().BeNullOrEmpty("mail is switched on per environment"); + config["Email:Smtp:Username"].Should().BeNullOrEmpty(); + config["Email:Smtp:Password"].Should().BeNullOrEmpty(); + config["Email:FromAddress"].Should().NotBeNullOrEmpty(); + config["Email:LogoUrl"].Should().Be("/admin/brand-logo-on-light.png"); + } } diff --git a/src/SlpModularCms.Api.AndreaSlopFotografie/appsettings.json b/src/SlpModularCms.Api.AndreaSlopFotografie/appsettings.json index e47d5f1..6343757 100644 --- a/src/SlpModularCms.Api.AndreaSlopFotografie/appsettings.json +++ b/src/SlpModularCms.Api.AndreaSlopFotografie/appsettings.json @@ -48,6 +48,31 @@ // production instance must set Website__NoIndex=false explicitly. "NoIndex": true }, + // Mail (invitations, password reset, notices). Mail is OFF while Email:Smtp:Host is empty and every + // flow then works as before (copy the link). Enable per environment with environment variables: + // Email__Smtp__Host, Email__Smtp__Port, Email__Smtp__EnableSsl, Email__Smtp__Username, + // Email__Smtp__Password (secrets only there, never in this file) and App__PublicBaseUrl. + // Extra sender addresses a site may choose in its settings go in Email:AllowedFromAddresses. + "Email": { + "FromAddress": "no-reply@slpsoftware.nl", + "FromName": "Andrea Slop Fotografie", + "AllowedFromAddresses": [], + "LogoUrl": "/admin/brand-logo-on-light.png", + "BrandColor": "#5f6a58", + "Smtp": { + "Host": "", + "Port": 587, + "EnableSsl": true, + "Username": "", + "Password": "", + "TimeoutSeconds": 10 + } + }, + "App": { + // Public https address of this site, used for links in mails, e.g. https://test.andreaslop.nl + "PublicBaseUrl": "", + "DefaultLanguage": "nl" + }, "Cors": { "AllowedOrigins": [] }, diff --git a/src/SlpModularCms.Api.SlpSoftware/appsettings.json b/src/SlpModularCms.Api.SlpSoftware/appsettings.json index 72f469d..7d4bf4d 100644 --- a/src/SlpModularCms.Api.SlpSoftware/appsettings.json +++ b/src/SlpModularCms.Api.SlpSoftware/appsettings.json @@ -33,6 +33,31 @@ "FailOpenAfterMinutes": 5, "HttpTimeoutSeconds": 5 }, + // Mail (invitations, password reset, notices). Mail is OFF while Email:Smtp:Host is empty and every + // flow then works as before (copy the link). Enable per environment with environment variables: + // Email__Smtp__Host, Email__Smtp__Port, Email__Smtp__EnableSsl, Email__Smtp__Username, + // Email__Smtp__Password (secrets only there, never in this file) and App__PublicBaseUrl. + // Extra sender addresses a site may choose in its settings go in Email:AllowedFromAddresses. + "Email": { + "FromAddress": "no-reply@slpsoftware.nl", + "FromName": "SLP Software", + "AllowedFromAddresses": [], + "LogoUrl": "", + "BrandColor": "", + "Smtp": { + "Host": "", + "Port": 587, + "EnableSsl": true, + "Username": "", + "Password": "", + "TimeoutSeconds": 10 + } + }, + "App": { + // Public https address of this site, used for links in mails, e.g. https://test.andreaslop.nl + "PublicBaseUrl": "", + "DefaultLanguage": "nl" + }, "Cors": { "AllowedOrigins": [] }, diff --git a/src/SlpModularCms.Core.Tests/Email/EmailRulesAndRendererTests.cs b/src/SlpModularCms.Core.Tests/Email/EmailRulesAndRendererTests.cs new file mode 100644 index 0000000..5c6d6b3 --- /dev/null +++ b/src/SlpModularCms.Core.Tests/Email/EmailRulesAndRendererTests.cs @@ -0,0 +1,264 @@ +using FluentAssertions; +using FsCheck; +using FsCheck.Fluent; +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Email; +using Xunit.Abstractions; + +namespace SlpModularCms.Core.Tests.Email; + +public class EmailOptionsValidatorTests +{ + private static EmailOptionsValidator Validator(string baseUrl = "https://test.example.nl", string lang = "nl") => + new(Options.Create(new AppMailOptions { PublicBaseUrl = baseUrl, DefaultLanguage = lang })); + + private static EmailOptions Valid() => new() + { + FromAddress = "no-reply@example.nl", + FromName = "Example", + Smtp = { Host = "mail.example.nl", Port = 587, Username = "u", Password = "p" } + }; + + [Fact] + public void WithoutAHost_MailIsDisabled_AndNothingIsValidated() => + Validator("", "xx").Validate(null, new EmailOptions()).Succeeded.Should().BeTrue(); + + [Fact] + public void AValidConfiguration_Passes() => + Validator().Validate(null, Valid()).Succeeded.Should().BeTrue(); + + [Theory] + [InlineData("http://example.nl")] // http only allowed for localhost + [InlineData("https://example.nl/admin")] // no path + [InlineData("https://example.nl/?a=1")] + [InlineData("https://user:pw@example.nl")] + [InlineData("example.nl")] + [InlineData("")] + public void ABadPublicBaseUrl_FailsStartup(string url) => + Validator(url).Validate(null, Valid()).Failed.Should().BeTrue(); + + [Theory] + [InlineData("http://localhost:5200")] + [InlineData("https://test.andreaslop.nl")] + [InlineData("https://test.andreaslop.nl/")] + public void GoodPublicBaseUrls_Pass(string url) => + Validator(url).Validate(null, Valid()).Succeeded.Should().BeTrue(); + + [Fact] + public void EachProblem_IsReported_AndNeverEchoesTheCredentials() + { + var o = Valid(); + o.FromAddress = "not-an-address"; + o.FromName = ""; + o.Smtp.Port = 0; + o.Smtp.TimeoutSeconds = 0; + o.Smtp.Password = ""; // username set, password empty + o.BrandColor = "red"; + o.LogoUrl = "javascript:alert(1)"; + o.AllowedFromAddresses = ["bad"]; + + var result = Validator("http://x.nl", "xx").Validate(null, o); + + result.Failed.Should().BeTrue(); + var text = string.Join(" | ", result.Failures!); + text.Should().Contain("FromAddress").And.Contain("FromName").And.Contain("Port").And.Contain("TimeoutSeconds") + .And.Contain("Password").And.Contain("BrandColor").And.Contain("LogoUrl") + .And.Contain("AllowedFromAddresses").And.Contain("PublicBaseUrl").And.Contain("DefaultLanguage"); + text.Should().NotContain("mail.example.nl"); + } + + [Fact] + public void ToString_NeverContainsTheCredentials() + { + var o = Valid(); + o.Smtp.Username = "user-secret-1"; + o.Smtp.Password = "pw-secret-2"; + + o.ToString().Should().NotContain("user-secret-1").And.NotContain("pw-secret-2"); + } +} + +public class EmailAddressRulesTests +{ + [Theory] + [InlineData("no-reply@slpsoftware.nl", true)] + [InlineData("a.b+c@sub.example.org", true)] + [InlineData("", false)] + [InlineData("a@b", false)] + [InlineData("a b@example.nl", false)] + [InlineData("a@example.nl\r\nBcc: x@y.nl", false)] + [InlineData("\"quoted\"@example.nl", false)] + [InlineData("a@exa mple.nl", false)] + [InlineData("", false)] + public void Address_IsAnAllowlistMatch(string value, bool expected) => + EmailAddressRules.IsValidAddress(value).Should().Be(expected); + + [Theory] + [InlineData("Andrea Slop Fotografie", true)] + [InlineData("Décor & Co", true)] + [InlineData("", false)] + [InlineData(" ", false)] + [InlineData("two\nlines", false)] + [InlineData("tab\there", false)] + public void DisplayName_RejectsControlCharacters(string value, bool expected) => + EmailAddressRules.IsValidDisplayName(value).Should().Be(expected); + + [Fact] + public void DisplayName_HasALengthLimit() + { + EmailAddressRules.IsValidDisplayName(new string('a', 80)).Should().BeTrue(); + EmailAddressRules.IsValidDisplayName(new string('a', 81)).Should().BeFalse(); + } + + [Fact] + public void Sender_MustBeTheDefaultOrOnTheAllowlist() + { + string[] allowed = ["info@andreaslop.nl"]; + EmailAddressRules.IsAllowedSender("no-reply@slpsoftware.nl", "no-reply@slpsoftware.nl", allowed).Should().BeTrue(); + EmailAddressRules.IsAllowedSender("INFO@AndreaSlop.nl", "no-reply@slpsoftware.nl", allowed).Should().BeTrue(); + EmailAddressRules.IsAllowedSender("evil@other.nl", "no-reply@slpsoftware.nl", allowed).Should().BeFalse(); + } +} + +public class MailTemplateRendererTests +{ + private readonly MailTemplateRenderer _renderer = new(); + private static MailModel Model(string site = "Andrea Slop Fotografie", string? action = "https://t.nl/admin/invite/complete?token=abc%2Fd", string? name = null) => + new(site, name, action, ValidForMinutes: 1440, Detail: "Administrator", LogoUrl: "https://t.nl/admin/brand-logo-on-light.png", BrandColor: "#2b6cb0"); + + [Fact] + public void Invitation_IsRenderedInDutchWithLinkLogoAndColour() + { + var mail = _renderer.Render(MailKind.Invitation, "nl", Model()); + + mail.Subject.Should().Be("Uitnodiging voor Andrea Slop Fotografie"); + mail.Html.Should().Contain("Account activeren").And.Contain("https://t.nl/admin/invite/complete?token=abc%2Fd") + .And.Contain("brand-logo-on-light.png").And.Contain("#2b6cb0").And.Contain("24 uur"); + mail.Text.Should().Contain("https://t.nl/admin/invite/complete?token=abc%2Fd").And.Contain("24 uur"); + } + + [Fact] + public void English_IsSupported_AndAnUnknownLanguageFallsBackToDutch() + { + _renderer.Render(MailKind.Invitation, "en", Model()).Subject.Should().Be("Invitation to Andrea Slop Fotografie"); + _renderer.Render(MailKind.Invitation, "fr", Model()).Subject.Should().StartWith("Uitnodiging"); + _renderer.Render(MailKind.Invitation, "EN-gb", Model()).Subject.Should().StartWith("Invitation"); + } + + [Fact] + public void RoleNotice_UsesTheLocalisedRoleName() + { + _renderer.Render(MailKind.RoleChanged, "nl", Model(action: null)).Text.Should().Contain("Beheerder"); + _renderer.Render(MailKind.RoleChanged, "en", Model(action: null)).Text.Should().Contain("Administrator"); + } + + [Fact] + public void ANoticeWithoutAButton_HasNoLink() + { + var mail = _renderer.Render(MailKind.AccountDeactivated, "nl", Model(action: "https://t.nl/x")); + + mail.Html.Should().NotContain("https://t.nl/x"); + mail.Text.Should().NotContain("https://t.nl/x"); + } + + [Fact] + public void ADisplayNameWithMarkup_IsEncoded_NotInterpreted() + { + var mail = _renderer.Render(MailKind.Invitation, "nl", Model(site: " & \"Co\"", name: "x")); + + mail.Html.Should().NotContain("