diff --git a/aidlc-docs/features/identity-email-notifications/construction/mail-foundation/infrastructure-design/infrastructure-design.md b/aidlc-docs/features/identity-email-notifications/construction/mail-foundation/infrastructure-design/infrastructure-design.md index 7a13737..78e1db9 100644 --- a/aidlc-docs/features/identity-email-notifications/construction/mail-foundation/infrastructure-design/infrastructure-design.md +++ b/aidlc-docs/features/identity-email-notifications/construction/mail-foundation/infrastructure-design/infrastructure-design.md @@ -54,3 +54,10 @@ Read-only checks done from the developer PC (by "CMS & API"): `mail.andreaslop.n - Mail is switched on only by setting `Email__Smtp__Host`, and it only turns ON when the whole mail configuration is valid: `App__PublicBaseUrl` (https, no path), `Email__FromAddress`/`Email__FromName` (committed defaults), `Email__Smtp__Username` together with `Email__Smtp__Password`, and sane port, timeout, colour and logo values. **A half-finished or wrong mail configuration never stops the host from starting** (decided by Bryan after the 2026-11-08 incident): the host starts with mail OFF (`emailEnabled=false`, all mail UI hidden, the copy-link flow keeps working) and logs ONE Error line, `mail_configuration_invalid`, that names each missing or invalid setting (names only, never values or secrets; Sentry sees it). Check the log after changing mail settings. Set `App__PublicBaseUrl` together with `Email__Smtp__Host`. - Optional: `Identity__Invitation__ExpiryHours` (24), `Identity__PasswordReset__TokenLifetimeMinutes` (60), `Identity__PasswordReset__CooldownMinutes` (5), `App__DefaultLanguage` (nl), `Email__AllowedFromAddresses__0..n`, `Website__LogLevel`. - Incident 2026-11-08: ASF test returned 502 after a restart because `Email__Smtp__Host` was set but `App__PublicBaseUrl` was not (the start-up validation at that time refused a half-configured mail setup). Not a deploy without mail configuration. Since changed: the soft behaviour above. + +## Which sender is used, and what the server must accept (update 2026-11-08) +The `From:` address of every mail is decided in this order: (1) the sender chosen in the site settings (Mail card), if it is still on the allowlist; otherwise (2) `Email:FromAddress` (environment variable `Email__FromAddress`, else the committed default of that host's `appsettings.json`). The committed default of the ASF host is `no-reply@slpsoftware.nl` (copied from SimpleDeckApp); the SlpSoftware host has the same. The SMTP server checks the `MAIL FROM` against the logged-in account (the envelope sender is always the `From:` address here, MailKit uses it). + +**So for `mail.andreaslop.nl` the ASF host must set**, in `shared/env`: `Email__FromAddress=` and `Email__AllowedFromAddresses__0=` (plus `Email__FromName`), where that address is one the SMTP account is allowed to use as sender. With the default `no-reply@slpsoftware.nl` the server is likely to refuse the sender (error code `rejected_sender`, SMTP 5xx at MAIL FROM) or not relay it. A change of the committed ASF default to an `@andreaslop.nl` address is a decision for Bryan (not made). + +**Diagnosis in the log** (Pi-Main journal): at start `mail_status enabled=true smtp_host=... smtp_port=... starttls=... default_from_domain=...`; per failed mail `identity_mail_failed code=... detail=... from_domain=... smtp_port=...`, where `detail` for a refused command is `SmtpCommandException phase=SenderNotAccepted|RecipientNotAccepted|MessageNotAccepted smtp_status=() reply=""`. The codes shown to the administrator: `rejected_sender`, `rejected_recipient`, `rejected`, `auth_failed`, `tls_failed`, `connection_failed`, `timeout`, `not_configured`, `unknown`. Never logged: credentials, addresses, tokens, links, the rest of a server reply. diff --git a/frontend/src/features/users/components/RegenerateInviteDialog.tsx b/frontend/src/features/users/components/RegenerateInviteDialog.tsx new file mode 100644 index 0000000..1c829c0 --- /dev/null +++ b/frontend/src/features/users/components/RegenerateInviteDialog.tsx @@ -0,0 +1,75 @@ +import { useTranslation } from 'react-i18next'; +import { Button } from '@/components/ui/button'; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from '@/components/ui/dialog'; +import type { UserListItem } from '@/features/users/services/types'; + +interface RegenerateInviteDialogProps { + user: UserListItem | null; + /** Whether this site can send mail; without it there is nothing to choose, only the link. */ + emailEnabled: boolean; + isPending: boolean; + onOpenChange: (open: boolean) => void; + /** Creates the new link (the old one stops working) and copies it; mails it too when asked. */ + onConfirm: (sendEmail: boolean) => void; +} + +/** Asks whether the new invitation link should also be e-mailed. Nothing is created or sent before a choice. */ +export function RegenerateInviteDialog({ user, emailEnabled, isPending, onOpenChange, onConfirm }: RegenerateInviteDialogProps) { + const { t } = useTranslation(); + if (!user) return null; + + return ( + + + + {t('users.regenerate.title')} + + {emailEnabled + ? t('users.regenerate.descriptionMail', { email: user.email }) + : t('users.regenerate.description', { email: user.email })} + + + + {emailEnabled ? ( + <> + + + + ) : ( + + )} + + + + + ); +} diff --git a/frontend/src/features/users/mailErrors.test.ts b/frontend/src/features/users/mailErrors.test.ts new file mode 100644 index 0000000..0a7c8d3 --- /dev/null +++ b/frontend/src/features/users/mailErrors.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, it } from 'vitest'; +import { mailErrorText } from '@/features/users/mailErrors'; + +const t = (key: string) => key; + +describe('mailErrorText', () => { + it.each([ + 'timeout', 'auth_failed', 'rejected', 'rejected_sender', 'rejected_recipient', 'tls_failed', 'connection_failed', 'not_configured', 'unknown', + ])('has its own text for %s', (code) => { + expect(mailErrorText(t, code)).toBe(`users.invite.mailErrors.${code}`); + }); + + it('reads an unknown or missing code as the generic one', () => { + expect(mailErrorText(t, 'something_new')).toBe('users.invite.mailErrors.unknown'); + expect(mailErrorText(t, null)).toBe('users.invite.mailErrors.unknown'); + expect(mailErrorText(t, undefined)).toBe('users.invite.mailErrors.unknown'); + }); +}); diff --git a/frontend/src/features/users/mailErrors.ts b/frontend/src/features/users/mailErrors.ts index a4c463c..ab272b1 100644 --- a/frontend/src/features/users/mailErrors.ts +++ b/frontend/src/features/users/mailErrors.ts @@ -1,5 +1,8 @@ /** A short, human reason for a failed mail (codes come from the server; unknown codes read as generic). */ export function mailErrorText(t: (key: string) => string, error: string | null | undefined): string { - const known = ['timeout', 'auth_failed', 'rejected', 'not_configured', 'unknown']; + const known = [ + 'timeout', 'auth_failed', 'rejected', 'rejected_sender', 'rejected_recipient', + 'tls_failed', 'connection_failed', 'not_configured', 'unknown', + ]; return t(`users.invite.mailErrors.${known.includes(error ?? '') ? error : 'unknown'}`); } diff --git a/frontend/src/features/users/mailFlows.test.tsx b/frontend/src/features/users/mailFlows.test.tsx index bcde246..f010bb2 100644 --- a/frontend/src/features/users/mailFlows.test.tsx +++ b/frontend/src/features/users/mailFlows.test.tsx @@ -196,6 +196,72 @@ describe('users list: resend', () => { }); }); +describe('new invitation link with a mail choice', () => { + async function openRegenerate() { + renderApp('/users'); + await screen.findByTestId('users-table'); + const pending = getMockUsers().find((u) => u.invitationPending)!; + await userEvent.click(screen.getByTestId(`user-actions-${pending.id}`)); + await userEvent.click(await screen.findByTestId(`user-regenerate-invite-${pending.id}`)); + return pending; + } + + function capture() { + const state: { body?: Record } = {}; + server.use( + http.post(`${USERS}/:id/regenerate-invite`, async ({ request }) => { + state.body = (await request.json()) as Record; + return HttpResponse.json({ + inviteLink: '/invite/complete?token=fresh', + mail: state.body.sendEmail ? { sent: true, error: null } : null, + }); + }), + ); + return state; + } + + it('offers both choices when the site has mail, and sends nothing before one is chosen', async () => { + mockAuthenticated(); + mailOn(); + const state = capture(); + + await openRegenerate(); + + expect(await screen.findByTestId('regenerate-copy-and-mail')).toBeInTheDocument(); + expect(screen.getByTestId('regenerate-copy-only')).toBeInTheDocument(); + expect(state.body).toBeUndefined(); + }); + + it('"copy only" asks the server NOT to mail, and still copies the link', async () => { + mockAuthenticated(); + mailOn(); + const writeText = vi.fn().mockResolvedValue(undefined); + Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true }); + const state = capture(); + await openRegenerate(); + + await userEvent.click(await screen.findByTestId('regenerate-copy-only')); + + await vi.waitFor(() => expect(writeText).toHaveBeenCalled()); + expect(state.body).toEqual({ sendEmail: false }); + }); + + it('"copy and mail" asks the server to mail, and copies the link as well', async () => { + mockAuthenticated(); + mailOn(); + const writeText = vi.fn().mockResolvedValue(undefined); + Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true }); + const state = capture(); + await openRegenerate(); + + await userEvent.click(await screen.findByTestId('regenerate-copy-and-mail')); + + await vi.waitFor(() => expect(writeText).toHaveBeenCalled()); + expect(state.body).toEqual({ sendEmail: true }); + expect(writeText.mock.calls[0][0]).toContain('/invite/complete?token=fresh'); + }); +}); + describe('language preference in the profile', () => { it('pre-fills the saved language and sends the choice, an empty value clears it', async () => { server.use( diff --git a/frontend/src/features/users/pages/UsersPage.test.tsx b/frontend/src/features/users/pages/UsersPage.test.tsx index 0025679..3ed28df 100644 --- a/frontend/src/features/users/pages/UsersPage.test.tsx +++ b/frontend/src/features/users/pages/UsersPage.test.tsx @@ -139,15 +139,15 @@ describe('UsersPage', () => { expect(screen.queryByTestId('user-actions-a3')).not.toBeInTheDocument(); }); - it('creates a new invitation link for a pending invitation and copies it', async () => { + it('asks before creating a new invitation link, and without mail offers only the link', async () => { mockAuthenticated(); const writeText = vi.fn().mockResolvedValue(undefined); Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true }); - let called = false; + let body: Record | undefined; server.use( - http.post(`${USERS_URL}/:id/regenerate-invite`, () => { - called = true; - return HttpResponse.json({ inviteLink: '/invite/complete?token=fresh' }); + http.post(`${USERS_URL}/:id/regenerate-invite`, async ({ request }) => { + body = (await request.json()) as Record; + return HttpResponse.json({ inviteLink: '/invite/complete?token=fresh', mail: null }); }), ); renderApp('/users'); @@ -157,11 +157,39 @@ describe('UsersPage', () => { await userEvent.click(screen.getByTestId(`user-actions-${pending.id}`)); await userEvent.click(screen.getByTestId(`user-regenerate-invite-${pending.id}`)); + // Nothing has been created yet; the dialog has no mail choice on a site without mail. + expect(await screen.findByTestId('regenerate-invite-dialog')).toBeInTheDocument(); + expect(body).toBeUndefined(); + expect(screen.queryByTestId('regenerate-copy-and-mail')).not.toBeInTheDocument(); + + await userEvent.click(screen.getByTestId('regenerate-copy-only')); + await vi.waitFor(() => expect(writeText).toHaveBeenCalled()); - expect(called).toBe(true); + expect(body).toEqual({ sendEmail: false }); expect(writeText.mock.calls[0][0]).toContain('/invite/complete?token=fresh'); }); + it('cancelling the dialog creates nothing', async () => { + mockAuthenticated(); + let called = false; + server.use( + http.post(`${USERS_URL}/:id/regenerate-invite`, () => { + called = true; + return HttpResponse.json({ inviteLink: '/x' }); + }), + ); + renderApp('/users'); + + await screen.findByTestId('users-table'); + const pending = getMockUsers().find((u) => u.invitationPending)!; + await userEvent.click(screen.getByTestId(`user-actions-${pending.id}`)); + await userEvent.click(screen.getByTestId(`user-regenerate-invite-${pending.id}`)); + await userEvent.click(await screen.findByTestId('regenerate-cancel')); + + expect(screen.queryByTestId('regenerate-invite-dialog')).not.toBeInTheDocument(); + expect(called).toBe(false); + }); + it('shows an error message when the API fails', async () => { mockAuthenticated(); server.use(http.get(USERS_URL, () => HttpResponse.json({}, { status: 500 }))); diff --git a/frontend/src/features/users/pages/UsersPage.tsx b/frontend/src/features/users/pages/UsersPage.tsx index c1039f3..5e8e7b5 100644 --- a/frontend/src/features/users/pages/UsersPage.tsx +++ b/frontend/src/features/users/pages/UsersPage.tsx @@ -34,6 +34,7 @@ import { } from '@/components/ui/dropdown-menu'; import { InviteUserDialog } from '@/features/invitation/components/InviteUserDialog'; import { DeleteUserDialog } from '@/features/users/components/DeleteUserDialog'; +import { RegenerateInviteDialog } from '@/features/users/components/RegenerateInviteDialog'; import { RoleBadge } from '@/components/shared/RoleBadge'; import { useUsers, useChangeRole, useSetUserActive, useDeleteUser, useRegenerateInvite, useResendInvite } from '@/features/users/services/useUsers'; import { useAuth } from '@/features/auth/context/auth-context'; @@ -79,6 +80,7 @@ export function UsersPage() { const setUserActive = useSetUserActive(); const deleteUser = useDeleteUser(); const regenerateInvite = useRegenerateInvite(); + const [regenerateTarget, setRegenerateTarget] = useState(null); const resendInvite = useResendInvite(); const { data: capabilities } = useSystemCapabilities(); const emailEnabled = capabilities?.emailEnabled ?? false; @@ -119,13 +121,15 @@ export function UsersPage() { toast.success(t('users.actions.linkCopied')); }; - const handleRegenerateInvite = async (user: UserListItem) => { + // The administrator chooses first (dialog); the link is always copied, mailed only when asked. + const handleRegenerateInvite = async (user: UserListItem, sendEmail: boolean) => { try { - const { inviteLink, mail } = await regenerateInvite.mutateAsync(user.id); + const { inviteLink, mail } = await regenerateInvite.mutateAsync({ invitationId: user.id, sendEmail }); await navigator.clipboard.writeText(buildAppUrl(inviteLink)); - if (mail?.sent) toast.success(t('users.actions.inviteRegeneratedMailed', { email: user.email })); - else if (mail) toast.warning(t('users.invite.mailFailed', { reason: mailErrorText(t, mail.error) })); + if (sendEmail && mail?.sent) toast.success(t('users.actions.inviteRegeneratedMailed', { email: user.email })); + else if (sendEmail && mail) toast.warning(t('users.invite.mailFailed', { reason: mailErrorText(t, mail.error) })); else toast.success(t('users.actions.inviteRegenerated')); + setRegenerateTarget(null); } catch { toast.error(t('errors.generic')); } @@ -340,7 +344,7 @@ export function UsersPage() { <> handleRegenerateInvite(user)} + onClick={() => setRegenerateTarget(user)} data-testid={`user-regenerate-invite-${user.id}`} > @@ -424,6 +428,14 @@ export function UsersPage() { + { if (!open) setRegenerateTarget(null); }} + onConfirm={(sendEmail) => regenerateTarget && void handleRegenerateInvite(regenerateTarget, sendEmail)} + /> + { if (!open) setSelfRoleChangeTarget(null); }} diff --git a/frontend/src/features/users/services/useUsers.ts b/frontend/src/features/users/services/useUsers.ts index 3918305..8c4fca6 100644 --- a/frontend/src/features/users/services/useUsers.ts +++ b/frontend/src/features/users/services/useUsers.ts @@ -50,9 +50,10 @@ export function useDeleteUser() { export function useRegenerateInvite() { const queryClient = useQueryClient(); - return useMutation({ - mutationFn: (invitationId) => - api.post(`/api/v1/Users/${invitationId}/regenerate-invite`, {}), + return useMutation({ + // The server mails the new link only when asked to: sendEmail defaults to false there too. + mutationFn: ({ invitationId, sendEmail }) => + api.post(`/api/v1/Users/${invitationId}/regenerate-invite`, { sendEmail }), onSuccess: () => queryClient.invalidateQueries({ queryKey: ['users'] }), }); } diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index 7099259..29bcc52 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -138,6 +138,14 @@ "successDeactivated": "{{name}} has been deactivated.", "successActivated": "{{name}} has been activated." }, + "regenerate": { + "title": "Create new invite link", + "description": "A new link is created for {{email}}. The old link stops working. The new link is copied so you can share it yourself.", + "descriptionMail": "A new link is created for {{email}}. The old link stops working. Do you also want to send the new link by e-mail?", + "copyOnly": "Copy link only", + "copyAndMail": "Copy link and send e-mail", + "create": "Create and copy link" + }, "delete": { "title": "Delete user", "description": "Are you sure you want to delete {{name}} ({{email}})? This action cannot be undone.", @@ -161,6 +169,10 @@ "timeout": "the mail server did not respond in time", "auth_failed": "signing in to the mail server failed", "rejected": "the mail server or the address refused the message", + "rejected_sender": "the mail server does not accept this sender address for this account", + "rejected_recipient": "the mail server will not deliver to this address", + "tls_failed": "the secure connection to the mail server failed", + "connection_failed": "no connection to the mail server", "not_configured": "e-mail is not set up", "unknown": "unknown error" }, diff --git a/frontend/src/i18n/locales/nl/translation.json b/frontend/src/i18n/locales/nl/translation.json index a35019c..c3284aa 100644 --- a/frontend/src/i18n/locales/nl/translation.json +++ b/frontend/src/i18n/locales/nl/translation.json @@ -138,6 +138,14 @@ "successDeactivated": "{{name}} is gedeactiveerd.", "successActivated": "{{name}} is geactiveerd." }, + "regenerate": { + "title": "Nieuwe uitnodigingslink maken", + "description": "Er wordt een nieuwe link gemaakt voor {{email}}. De oude link werkt daarna niet meer. De nieuwe link wordt gekopieerd, zodat je hem zelf kunt delen.", + "descriptionMail": "Er wordt een nieuwe link gemaakt voor {{email}}. De oude link werkt daarna niet meer. Wil je de nieuwe link ook per e-mail versturen?", + "copyOnly": "Alleen link kopiëren", + "copyAndMail": "Link kopiëren en mailen", + "create": "Link maken en kopiëren" + }, "delete": { "title": "Gebruiker verwijderen", "description": "Weet je zeker dat je {{name}} ({{email}}) wilt verwijderen? Deze actie kan niet ongedaan worden gemaakt.", @@ -162,6 +170,10 @@ "timeout": "de mailserver reageerde niet op tijd", "auth_failed": "inloggen op de mailserver mislukt", "rejected": "de mailserver of het adres weigerde het bericht", + "rejected_sender": "de mailserver accepteert dit afzenderadres niet voor dit account", + "rejected_recipient": "de mailserver wil niet naar dit adres versturen", + "tls_failed": "beveiligde verbinding met de mailserver mislukt", + "connection_failed": "geen verbinding met de mailserver", "not_configured": "e-mail is niet ingesteld", "unknown": "onbekende fout" }, diff --git a/src/SlpModularCms.Core.Tests/Email/SmtpEmailSenderTests.cs b/src/SlpModularCms.Core.Tests/Email/SmtpEmailSenderTests.cs index 464fc32..d3ca184 100644 --- a/src/SlpModularCms.Core.Tests/Email/SmtpEmailSenderTests.cs +++ b/src/SlpModularCms.Core.Tests/Email/SmtpEmailSenderTests.cs @@ -19,10 +19,17 @@ internal sealed class StubSmtpServer : IDisposable public List Recipients { get; } = []; public int Port => ((IPEndPoint)_listener.LocalEndpoint).Port; + private readonly string? _rejectMailFrom; + private readonly string? _rejectRcpt; + /// Accept connections but never answer (to provoke the timeout). - public StubSmtpServer(bool silent = false) + /// Reply to MAIL FROM with this full line (for example "553 5.7.1 ..."). + /// Reply to RCPT TO with this full line. + public StubSmtpServer(bool silent = false, string? rejectMailFrom = null, string? rejectRcpt = null) { _silent = silent; + _rejectMailFrom = rejectMailFrom; + _rejectRcpt = rejectRcpt; _listener.Start(); _loop = Task.Run(Serve); } @@ -41,8 +48,8 @@ internal sealed class StubSmtpServer : IDisposable while (await reader.ReadLineAsync() is { } line) { if (line.StartsWith("EHLO", StringComparison.OrdinalIgnoreCase)) await writer.WriteLineAsync("250 stub"); - else if (line.StartsWith("MAIL FROM", StringComparison.OrdinalIgnoreCase)) { MailFrom = line; await writer.WriteLineAsync("250 ok"); } - else if (line.StartsWith("RCPT TO", StringComparison.OrdinalIgnoreCase)) { Recipients.Add(line); await writer.WriteLineAsync("250 ok"); } + else if (line.StartsWith("MAIL FROM", StringComparison.OrdinalIgnoreCase)) { MailFrom = line; await writer.WriteLineAsync(_rejectMailFrom ?? "250 ok"); } + else if (line.StartsWith("RCPT TO", StringComparison.OrdinalIgnoreCase)) { Recipients.Add(line); await writer.WriteLineAsync(_rejectRcpt ?? "250 ok"); } else if (line.StartsWith("DATA", StringComparison.OrdinalIgnoreCase)) { await writer.WriteLineAsync("354 go"); diff --git a/src/SlpModularCms.Core.Tests/Email/SmtpFailureTests.cs b/src/SlpModularCms.Core.Tests/Email/SmtpFailureTests.cs new file mode 100644 index 0000000..cee31b6 --- /dev/null +++ b/src/SlpModularCms.Core.Tests/Email/SmtpFailureTests.cs @@ -0,0 +1,159 @@ +using System.Net.Sockets; +using FluentAssertions; +using FsCheck; +using FsCheck.Fluent; +using MailKit.Net.Smtp; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SlpModularCms.Core.Email; +using Xunit.Abstractions; + +namespace SlpModularCms.Core.Tests.Email; + +public class SmtpFailureTests(ITestOutputHelper output) +{ + [Theory] + [InlineData("553 5.7.1 : Sender address rejected", "[adres]")] + [InlineData("550 relay denied for jan@example.org", "relay denied for [adres]")] + [InlineData("554 user@host.example.nl: not allowed", "[adres]: not allowed")] + public void SafeReply_ReplacesEveryAddress(string reply, string expectedFragment) + { + var safe = SmtpFailure.SafeReply(reply); + + safe.Should().Contain(expectedFragment); + safe.Should().NotContain("@"); + } + + [Fact] + public void SafeReply_ControlCharactersCannotGlueAnAddressTogether() + { + // Found by the property test: "x-@" CR "host.example.nl" becomes an address once the CR is removed. + var safe = SmtpFailure.SafeReply("550 x-@\rhost.example.nl y"); + + safe.Should().NotContain("@"); + safe.Should().Contain("[adres]"); + } + + [Fact] + public void SafeReply_KeepsOnlyTheFirstLine_DropsControlCharacters_AndCutsLongText() + { + SmtpFailure.SafeReply("550 first line\r\n550-second line with secret").Should().Be("550 first line"); + SmtpFailure.SafeReply("550 bad\u0000char\u0007").Should().Be("550 badchar"); + var cut = SmtpFailure.SafeReply("550 " + new string('x', 400)); + cut.Length.Should().BeLessThanOrEqualTo(SmtpFailure.MaxReplyLength + 3); + cut.Should().EndWith("..."); + SmtpFailure.SafeReply(null).Should().BeEmpty(); + SmtpFailure.SafeReply(" ").Should().BeEmpty(); + } + + [Fact] + public void Classify_IsAsSpecificAsTheFailureAllows() + { + var none = CancellationToken.None; + SmtpFailure.Classify(new SmtpCommandException(SmtpErrorCode.SenderNotAccepted, SmtpStatusCode.MailboxUnavailable, "x"), none) + .Should().Be(MailSendResult.RejectedSender); + SmtpFailure.Classify(new SmtpCommandException(SmtpErrorCode.RecipientNotAccepted, SmtpStatusCode.MailboxUnavailable, "x"), none) + .Should().Be(MailSendResult.RejectedRecipient); + SmtpFailure.Classify(new SmtpCommandException(SmtpErrorCode.MessageNotAccepted, SmtpStatusCode.TransactionFailed, "x"), none) + .Should().Be(MailSendResult.Rejected); + SmtpFailure.Classify(new AuthenticationException("bad pw-secret"), none).Should().Be(MailSendResult.AuthFailed); + SmtpFailure.Classify(new TimeoutException(), none).Should().Be(MailSendResult.Timeout); + SmtpFailure.Classify(new OperationCanceledException(), none).Should().Be(MailSendResult.Timeout); + SmtpFailure.Classify(new SocketException(), none).Should().Be(MailSendResult.ConnectionFailed); + SmtpFailure.Classify(new InvalidOperationException(), none).Should().Be(MailSendResult.Unknown); + } + + [Fact] + public void Describe_ForACommandNamesPhaseAndStatus_ForAuthNeverTheReply() + { + var command = new SmtpCommandException(SmtpErrorCode.SenderNotAccepted, SmtpStatusCode.MailboxNameNotAllowed, "553 no for jan@example.org"); + var describedCommand = SmtpFailure.Describe(command); + describedCommand.Should().Contain("SenderNotAccepted").And.Contain("MailboxNameNotAllowed(553)").And.Contain("[adres]"); + describedCommand.Should().NotContain("jan@example.org"); + + var auth = SmtpFailure.Describe(new AuthenticationException("535 wrong password pw-secret-123")); + auth.Should().Contain("AuthenticationException").And.Contain("AUTH"); + auth.Should().NotContain("pw-secret-123").And.NotContain("535"); + } + + // PBT-03: whatever a server says, the reply that reaches the log has no address, no control character and a bounded length. + [Fact] + public void Property_TheLoggedReply_NeverHasAnAddressOrControlCharacters_AndIsBounded() + { + string[] pieces = ["550 ", " ", "a", "Z9", "@", ".", "-", "_", "<", ">", "\r", "\n", "\0", "\t", "\"", "example", "nl", "user", "x", "😀", "é"]; + var arb = Gen.Elements(pieces).ArrayOf().Select(p => string.Concat(p)).ToArbitrary(); + Prop.ForAll(arb, reply => + { + var safe = SmtpFailure.SafeReply(reply); + return !safe.Any(char.IsControl) + && safe.Length <= SmtpFailure.MaxReplyLength + 3 + && !System.Text.RegularExpressions.Regex.IsMatch(safe, @"[A-Za-z0-9._-]+@[A-Za-z0-9.-]+"); + }).Check(Config.QuickThrowOnFailure.WithMaxTest(1500)); + output.WriteLine("Property held for 1500 generated server replies."); + } +} + +public class SmtpSenderDiagnosticsTests +{ + private static (SmtpEmailSender Sender, LogCapture Logs) Sender(StubSmtpServer server) + { + var logs = new LogCapture(); + var factory = LoggerFactory.Create(b => b.AddProvider(logs)); + var sender = new SmtpEmailSender( + Options.Create(new EmailOptions + { + FromAddress = "no-reply@slpsoftware.nl", FromName = "Site", + Smtp = { Host = "127.0.0.1", Port = server.Port, EnableSsl = false, Username = "", Password = "", TimeoutSeconds = 5 } + }), + factory.CreateLogger()); + return (sender, logs); + } + + [Fact] + public async Task ARefusedSender_IsNamedAsSuch_AndTheLogShowsPhaseStatusAndARedactedReply_NoAddresses() + { + using var server = new StubSmtpServer(rejectMailFrom: "553 5.7.1 : Sender address rejected: not owned by user jan@andreaslop.nl"); + var (sender, logs) = Sender(server); + + var result = await sender.SendAsync(new OutgoingMail("someone@gmail.com", "Subject", "

x

")); + + result.Should().Be(MailSendResult.Fail(MailSendResult.RejectedSender)); + var line = logs.Entries.Should().ContainSingle(e => e.Level == LogLevel.Warning).Subject.Message; + line.Should().Contain("rejected_sender").And.Contain("SenderNotAccepted").And.Contain("(553)") + .And.Contain("[adres]").And.Contain("from_domain=slpsoftware.nl").And.Contain($"smtp_port={server.Port}"); + // No address of the sender, the recipient or the account, and not the server reply beyond the redacted first line. + line.Should().NotContain("no-reply@").And.NotContain("someone@").And.NotContain("jan@").And.NotContain("gmail.com"); + } + + [Fact] + public async Task ARefusedRecipient_IsNamedAsSuch() + { + using var server = new StubSmtpServer(rejectRcpt: "550 5.7.1 : Relay access denied"); + var (sender, logs) = Sender(server); + + var result = await sender.SendAsync(new OutgoingMail("someone@gmail.com", "Subject", "

x

")); + + result.Should().Be(MailSendResult.Fail(MailSendResult.RejectedRecipient)); + logs.Entries.Single(e => e.Level == LogLevel.Warning).Message.Should().Contain("RecipientNotAccepted").And.NotContain("someone@"); + } + + [Fact] + public async Task TheLog_NeverContainsTheCredentials() + { + using var server = new StubSmtpServer(rejectMailFrom: "553 5.7.1 rejected"); + var logs = new LogCapture(); + var factory = LoggerFactory.Create(b => b.AddProvider(logs)); + var sender = new SmtpEmailSender( + Options.Create(new EmailOptions + { + FromAddress = "no-reply@slpsoftware.nl", FromName = "Site", + Smtp = { Host = "127.0.0.1", Port = server.Port, EnableSsl = false, Username = "user-secret-1", Password = "pw-secret-2", TimeoutSeconds = 5 } + }), + factory.CreateLogger()); + + await sender.SendAsync(new OutgoingMail("someone@gmail.com", "Subject", "

x

")); + + string.Join("\n", logs.Entries.Select(e => e.Message)).Should().NotContain("user-secret-1").And.NotContain("pw-secret-2"); + } +} diff --git a/src/SlpModularCms.Core/Email/MailAvailability.cs b/src/SlpModularCms.Core/Email/MailAvailability.cs index c9d826b..89f2ce8 100644 --- a/src/SlpModularCms.Core/Email/MailAvailability.cs +++ b/src/SlpModularCms.Core/Email/MailAvailability.cs @@ -50,11 +50,24 @@ public sealed class MailAvailability : IMailAvailability } /// Makes the decision (and its Error log line) happen at start-up instead of at the first mail. -public sealed class MailStartupCheck(IMailAvailability availability, ILogger logger) : IHostedService +public sealed class MailStartupCheck(IMailAvailability availability, IOptions email, ILogger logger) : IHostedService { public Task StartAsync(CancellationToken cancellationToken) { - logger.LogInformation("mail_status enabled={Enabled}", availability.IsEnabled); + var o = email.Value; + if (availability.IsEnabled) + { + // Where mail goes through and with which default sender DOMAIN (not the address): what a refused + // MAIL FROM is checked against. A sender chosen in the site settings can differ from this default. + var at = o.FromAddress.LastIndexOf('@'); + logger.LogInformation( + "mail_status enabled=true smtp_host={Host} smtp_port={Port} starttls={StartTls} default_from_domain={FromDomain}", + o.Smtp.Host, o.Smtp.Port, o.Smtp.EnableSsl, at >= 0 ? o.FromAddress[(at + 1)..] : "?"); + } + else + { + logger.LogInformation("mail_status enabled=false"); + } return Task.CompletedTask; } diff --git a/src/SlpModularCms.Core/Email/MailTypes.cs b/src/SlpModularCms.Core/Email/MailTypes.cs index 09b06bd..290d1d3 100644 --- a/src/SlpModularCms.Core/Email/MailTypes.cs +++ b/src/SlpModularCms.Core/Email/MailTypes.cs @@ -8,6 +8,12 @@ public readonly record struct MailSendResult(bool Sent, string? ErrorCode) public const string Timeout = "timeout"; public const string AuthFailed = "auth_failed"; public const string Rejected = "rejected"; + /// The server refused the sender address (MAIL FROM): the From address is not allowed for this account. + public const string RejectedSender = "rejected_sender"; + /// The server refused the recipient (RCPT TO), for instance because it does not relay for that domain. + public const string RejectedRecipient = "rejected_recipient"; + public const string TlsFailed = "tls_failed"; + public const string ConnectionFailed = "connection_failed"; public const string Unknown = "unknown"; public static MailSendResult Ok { get; } = new(true, null); diff --git a/src/SlpModularCms.Core/Email/SmtpEmailSender.cs b/src/SlpModularCms.Core/Email/SmtpEmailSender.cs index b5bf6a4..1095203 100644 --- a/src/SlpModularCms.Core/Email/SmtpEmailSender.cs +++ b/src/SlpModularCms.Core/Email/SmtpEmailSender.cs @@ -62,21 +62,15 @@ public sealed class SmtpEmailSender(IOptions options, ILogger ex switch - { - OperationCanceledException when !callerToken.IsCancellationRequested => MailSendResult.Timeout, - TimeoutException => MailSendResult.Timeout, - OperationCanceledException => MailSendResult.Unknown, - AuthenticationException => MailSendResult.AuthFailed, - SmtpCommandException or SmtpProtocolException or SslHandshakeException => MailSendResult.Rejected, - ServiceNotConnectedException or ServiceNotAuthenticatedException => MailSendResult.Rejected, - _ => MailSendResult.Unknown - }; + private static string DomainOf(string address) => + address.Contains('@') ? address[(address.LastIndexOf('@') + 1)..] : "?"; } diff --git a/src/SlpModularCms.Core/Email/SmtpFailure.cs b/src/SlpModularCms.Core/Email/SmtpFailure.cs new file mode 100644 index 0000000..1a7b014 --- /dev/null +++ b/src/SlpModularCms.Core/Email/SmtpFailure.cs @@ -0,0 +1,69 @@ +using System.Net.Sockets; +using System.Text.RegularExpressions; +using MailKit; +using MailKit.Net.Smtp; +using MailKit.Security; + +namespace SlpModularCms.Core.Email; + +/// +/// What went wrong with a mail, in two forms: a short code for the screen () and a +/// careful one-line description for the log (). +/// +/// +/// The exception text is still not logged as a whole (it can quote the recipient, the sender and credentials). The +/// description is explicit and limited: status code and phase, then at most the first line of the server's reply, +/// with every e-mail address replaced by [adres] and cut to characters. Credentials +/// never appear in a server reply to MAIL/RCPT, and the AUTH reply text is not included at all. +/// +public static partial class SmtpFailure +{ + public const int MaxReplyLength = 120; + + [GeneratedRegex(@"[^\s@<>""'(),;:]+@[^\s@<>""'(),;:]+")] + private static partial Regex AddressPattern(); + + /// The short code shown to the administrator; the most specific one the failure allows. + public static string Classify(Exception ex, CancellationToken callerToken) => ex switch + { + OperationCanceledException when !callerToken.IsCancellationRequested => MailSendResult.Timeout, + TimeoutException => MailSendResult.Timeout, + OperationCanceledException => MailSendResult.Unknown, + AuthenticationException => MailSendResult.AuthFailed, + SslHandshakeException => MailSendResult.TlsFailed, + SmtpCommandException c => c.ErrorCode switch + { + SmtpErrorCode.SenderNotAccepted => MailSendResult.RejectedSender, + SmtpErrorCode.RecipientNotAccepted => MailSendResult.RejectedRecipient, + _ => MailSendResult.Rejected + }, + SmtpProtocolException => MailSendResult.Rejected, + ServiceNotConnectedException or ServiceNotAuthenticatedException => MailSendResult.Rejected, + SocketException => MailSendResult.ConnectionFailed, + _ => MailSendResult.Unknown + }; + + /// The log description of a failure: the type, and for a refused command its phase, status code and a redacted first line. + public static string Describe(Exception ex) + { + var type = ex.GetType().Name; + if (ex is SmtpCommandException c) + { + var status = (int)c.StatusCode; + return $"{type} phase={c.ErrorCode} smtp_status={c.StatusCode}({status}) reply=\"{SafeReply(c.Message)}\""; + } + if (ex is AuthenticationException) return $"{type} phase=AUTH"; // the reply text is deliberately left out + return type; + } + + /// The first line of a server reply: addresses replaced, control characters dropped, cut short. + public static string SafeReply(string? reply) + { + if (string.IsNullOrWhiteSpace(reply)) return ""; + var firstLine = reply.Split('\n', 2)[0].Trim(); + // Control characters go FIRST: removed afterwards they could glue the parts of an address together again. + var printable = new string(firstLine.Where(ch => !char.IsControl(ch)).ToArray()); + var cleaned = AddressPattern().Replace(printable, "[adres]").Replace('"', '\''); + return cleaned.Length <= MaxReplyLength ? cleaned : cleaned[..MaxReplyLength] + "..."; + } +} diff --git a/src/SlpModularCms.Core/Identity/Models/IdentityRequests.cs b/src/SlpModularCms.Core/Identity/Models/IdentityRequests.cs index d5c7cfa..edea921 100644 --- a/src/SlpModularCms.Core/Identity/Models/IdentityRequests.cs +++ b/src/SlpModularCms.Core/Identity/Models/IdentityRequests.cs @@ -8,6 +8,8 @@ namespace SlpModularCms.Core.Identity.Models; [ExcludeFromCodeCoverage] public record CompleteSetupRequest(string Token, string Password, string? Name = null); [ExcludeFromCodeCoverage] public record ChangeRoleRequest(string NewRole); [ExcludeFromCodeCoverage] public record ForgotPasswordRequest(string Email); +/// SendEmail is false unless the caller says so: a new link is never mailed by surprise. +[ExcludeFromCodeCoverage] public record RegenerateInviteRequest(bool SendEmail = false); [ExcludeFromCodeCoverage] public record ResetPasswordRequest(Guid UserId, string Token, string NewPassword); [ExcludeFromCodeCoverage] public record UpdateProfileRequest(string Name, string Email, string? Language = null); [ExcludeFromCodeCoverage] public record ChangePasswordRequest(string CurrentPassword, string NewPassword); diff --git a/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersApiWithRealTokensTests.cs b/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersApiWithRealTokensTests.cs index dc0bada..07cb927 100644 --- a/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersApiWithRealTokensTests.cs +++ b/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersApiWithRealTokensTests.cs @@ -174,6 +174,30 @@ public class UsersApiWithRealTokensTests : IAsyncLifetime (await RoleOfAsync(user.Id)).Should().Be("User"); } + // ---- regenerate-invite: the body is optional and the mail is only sent on request + + [Fact] + public async Task RegenerateInvite_OverHttp_MailsOnlyWhenAskedTo_WhateverTheBodyLooksLike() + { + await AddUserAsync("andrea@test.nl", "Owner"); + var id = Guid.NewGuid(); + _invitations.GetAllPendingInvitationsAsync().Returns(new List + { + new(id, "invited@test.com", "User", "old", DateTimeOffset.UtcNow.AddDays(-2), true, "nl") + }); + _invitations.RegenerateInvitationAsync(id).Returns("fresh"); + _mail.SendInvitationAsync(default!, default!, default, default, default).ReturnsForAnyArgs(MailSendResult.Ok); + var token = await TokenAsync("andrea@test.nl"); + + (await SendAsync(token, HttpMethod.Post, $"/Users/{id}/regenerate-invite")).Should().Be(HttpStatusCode.OK); // no body at all + (await SendAsync(token, HttpMethod.Post, $"/Users/{id}/regenerate-invite", new { })).Should().Be(HttpStatusCode.OK); // {} + (await SendAsync(token, HttpMethod.Post, $"/Users/{id}/regenerate-invite", new { sendEmail = false })).Should().Be(HttpStatusCode.OK); + await _mail.DidNotReceiveWithAnyArgs().SendInvitationAsync(default!, default!, default, default, default); + + (await SendAsync(token, HttpMethod.Post, $"/Users/{id}/regenerate-invite", new { sendEmail = true })).Should().Be(HttpStatusCode.OK); + await _mail.Received(1).SendInvitationAsync("invited@test.com", "fresh", "nl", true, Arg.Any()); + } + // ---- the Administrator is held to the hierarchy [Fact] diff --git a/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersControllerMailTests.cs b/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersControllerMailTests.cs index 77b97ef..327a9d8 100644 --- a/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersControllerMailTests.cs +++ b/src/SlpModularCms.Modules.Identity.Tests/Controllers/UsersControllerMailTests.cs @@ -154,19 +154,81 @@ public class UsersControllerMailTests : IDisposable // --- regenerate / resend --- [Fact] - public async Task Regenerate_SendsTheNewLinkMail_InTheLanguageOfTheInvitation() + public async Task Regenerate_WithTheFlag_SendsTheNewLinkMail_InTheLanguageOfTheInvitation() { var owner = await Add("owner@test.com", "Owner"); var id = Guid.NewGuid(); Pending(id, language: "en"); _invitations.RegenerateInvitationAsync(id).Returns("new-token"); - var result = await As(owner).RegenerateInvite(id); + var result = await As(owner).RegenerateInvite(id, new RegenerateInviteRequest(SendEmail: true)); - Prop(result.Should().BeOfType().Subject.Value, "inviteLink").Should().Contain("new-token"); + var body = result.Should().BeOfType().Subject.Value; + Prop(body, "inviteLink").Should().Contain("new-token"); + Prop(Prop(body, "mail"), "sent").Should().BeTrue(); await _mail.Received(1).SendInvitationAsync("invited@test.com", "new-token", "en", true, Arg.Any()); } + [Fact] + public async Task Regenerate_WithoutTheFlag_CreatesTheLink_ButSendsNoMail() + { + var owner = await Add("owner@test.com", "Owner"); + var id = Guid.NewGuid(); + Pending(id); + _invitations.RegenerateInvitationAsync(id).Returns("new-token"); + + var result = await As(owner).RegenerateInvite(id, new RegenerateInviteRequest()); + + var body = result.Should().BeOfType().Subject.Value; + Prop(body, "inviteLink").Should().Contain("new-token"); + Prop(body, "mail").Should().BeNull(); + await _invitations.Received(1).RegenerateInvitationAsync(id); + await _mail.DidNotReceiveWithAnyArgs().SendInvitationAsync(default!, default!, default, default, default); + } + + [Fact] + public async Task Regenerate_WithNoBodyAtAll_IsTheSameAsWithoutTheFlag() + { + var owner = await Add("owner@test.com", "Owner"); + var id = Guid.NewGuid(); + Pending(id); + _invitations.RegenerateInvitationAsync(id).Returns("new-token"); + + var result = await As(owner).RegenerateInvite(id); // an older client sends no flag + + Prop(result.Should().BeOfType().Subject.Value, "mail").Should().BeNull(); + await _mail.DidNotReceiveWithAnyArgs().SendInvitationAsync(default!, default!, default, default, default); + } + + [Fact] + public async Task Regenerate_WithTheFlag_OnASiteWithoutMail_HasNoMailResult() + { + var owner = await Add("owner@test.com", "Owner"); + var id = Guid.NewGuid(); + Pending(id); + _invitations.RegenerateInvitationAsync(id).Returns("new-token"); + _mail.SendInvitationAsync(default!, default!, default, default, default).ReturnsForAnyArgs(MailSendResult.Fail(MailSendResult.Disabled)); + + var result = await As(owner).RegenerateInvite(id, new RegenerateInviteRequest(true)); + + Prop(result.Should().BeOfType().Subject.Value, "mail").Should().BeNull(); + } + + [Fact] + public async Task Regenerate_WithTheFlag_ReportsAFailedMail_NextToTheLink() + { + var owner = await Add("owner@test.com", "Owner"); + var id = Guid.NewGuid(); + Pending(id); + _invitations.RegenerateInvitationAsync(id).Returns("new-token"); + _mail.SendInvitationAsync(default!, default!, default, default, default).ReturnsForAnyArgs(MailSendResult.Fail(MailSendResult.RejectedSender)); + + var body = (await As(owner).RegenerateInvite(id, new RegenerateInviteRequest(true))).Should().BeOfType().Subject.Value; + + Prop(Prop(body, "mail"), "error").Should().Be("rejected_sender"); + Prop(body, "inviteLink").Should().Contain("new-token"); + } + [Fact] public async Task Resend_OfAValidInvitation_SendsTheExistingLink_WithoutCreatingANewOne() { diff --git a/src/SlpModularCms.Modules.Identity/Controllers/UsersController.cs b/src/SlpModularCms.Modules.Identity/Controllers/UsersController.cs index 268df49..b9d6c70 100644 --- a/src/SlpModularCms.Modules.Identity/Controllers/UsersController.cs +++ b/src/SlpModularCms.Modules.Identity/Controllers/UsersController.cs @@ -166,10 +166,11 @@ public class UsersController : ControllerBase /// /// Issues a fresh link for an open (or expired) invitation: new token and a new lifetime. The - /// old link stops working. Until e-mail exists this is how an administrator re-sends one. + /// old link stops working. It is mailed only when the request says sendEmail: true (the administrator + /// is asked); otherwise the link is just returned so it can be copied and shared. /// [HttpPost("{invitationId:guid}/regenerate-invite")] - public async Task RegenerateInvite(Guid invitationId) + public async Task RegenerateInvite(Guid invitationId, [FromBody] RegenerateInviteRequest? request = null) { var invitation = await GetInvitationAsync(invitationId); if (invitation is null) return NotFound(); @@ -180,8 +181,12 @@ public class UsersController : ControllerBase var token = await _invitationService.RegenerateInvitationAsync(invitationId); if (token is null) return NotFound(); + var link = $"/invite/complete?token={Uri.EscapeDataString(token)}"; + if (request?.SendEmail != true) + return Ok(new { inviteLink = link, mail = (object?)null }); + var mail = await _mail.SendInvitationAsync(invitation.Email, token, invitation.Language, isNewLink: true, HttpContext.RequestAborted); - return Ok(new { inviteLink = $"/invite/complete?token={Uri.EscapeDataString(token)}", mail = MailBody(mail) }); + return Ok(new { inviteLink = link, mail = MailBody(mail) }); } ///