Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🔒️ Stop logging recipient email addresses on send failure (#3264)
Luke Vella5415b3060243
diff --git a/apps/docs/self-hosting/configuration.mdx b/apps/docs/self-hosting/configuration.mdxindex bad7b676c..43e3823d7 100644--- a/apps/docs/self-hosting/configuration.mdx+++ b/apps/docs/self-hosting/configuration.mdx@@ -74,12 +74,8 @@ An SMTP server is required to send magic-link sign-in emails and notifications.<Note>Available from v4.8.0 and later.</Note></ParamField>-<ParamField path="SMTP_DEBUG" default="false">- Set to `true` to log the full SMTP conversation. Useful for troubleshooting email delivery issues.-- <Warning>Debug output includes the authentication exchange, where credentials are only base64 encoded. Enable this only while troubleshooting and redact your logs before sharing them.</Warning>-- <Note>Available from v4.12.0 and later.</Note>+<ParamField path="SMTP_DEBUG" deprecated>+ No longer supported since v4.16.0 and can be removed from your configuration. Send failures are now logged with the SMTP server's response (`errorResponse`, `errorResponseCode`, `errorCommand`), which covers the cases this option was used for. For connection level diagnosis, run `openssl s_client -starttls smtp -connect <host>:<port>` for STARTTLS (`SMTP_SECURE=false`), or `openssl s_client -connect <host>:<port>` for implicit TLS (`SMTP_SECURE=true`, usually port 465).</ParamField>## Authdiff --git a/apps/web/src/env.ts b/apps/web/src/env.tsindex 8388379bd..df8e02c1f 100644--- a/apps/web/src/env.ts+++ b/apps/web/src/env.ts@@ -49,9 +49,10 @@ export const env = createEnv({SMTP_SECURE: z.enum(["true", "false"]).optional(),SMTP_PORT: z.string().optional(),SMTP_REJECT_UNAUTHORIZED: z.enum(["true", "false"]).optional(),- SMTP_DEBUG: z.enum(["true", "false"]).optional(),/** @deprecated Use SMTP_REJECT_UNAUTHORIZED instead */SMTP_TLS_ENABLED: z.enum(["true", "false"]).optional(),+ /** @deprecated No longer supported; send failures log the server response */+ SMTP_DEBUG: z.enum(["true", "false"]).optional(),/*** AWS SES Configuration*/@@ -253,8 +254,8 @@ export const env = createEnv({SMTP_SECURE: process.env.SMTP_SECURE,SMTP_PORT: process.env.SMTP_PORT,SMTP_REJECT_UNAUTHORIZED: process.env.SMTP_REJECT_UNAUTHORIZED,- SMTP_DEBUG: process.env.SMTP_DEBUG,SMTP_TLS_ENABLED: process.env.SMTP_TLS_ENABLED,+ SMTP_DEBUG: process.env.SMTP_DEBUG,ALLOWED_EMAILS: process.env.ALLOWED_EMAILS,EMAIL_LOGIN_ENABLED: process.env.EMAIL_LOGIN_ENABLED,REGISTRATION_ENABLED: process.env.REGISTRATION_ENABLED,diff --git a/packages/emails/src/send.test.ts b/packages/emails/src/send.test.tsnew file mode 100644index 000000000..cec0620e3--- /dev/null+++ b/packages/emails/src/send.test.ts@@ -0,0 +1,41 @@+import { expect, test } from "vitest";++import { recipientDomains, scrubAddresses } from "./send";++test("returns the domain of a bare address", () => {+ expect(recipientDomains("[email protected]")).toEqual(["example.com"]);+});++test("strips a display-name wrapper and lowercases", () => {+ expect(recipientDomains("Jessie Smith <[email protected]>")).toEqual([+ "example.com",+ ]);+});++test("returns one domain per recipient without leaking later addresses", () => {+ "one.test",+ "two.test",+ ]);+});++test("returns nothing when there is no address", () => {+ expect(recipientDomains("not-an-address")).toEqual([]);+});++test("scrubs addresses out of SMTP replies", () => {+ expect(+ scrubAddresses(+ "550 5.1.1 <[email protected]>: Recipient address rejected: [email protected]",+ ),+ ).toBe("550 5.1.1 <[redacted]>: Recipient address rejected: [redacted]");+ expect(+ scrubAddresses('Rejected "support team"@example.com and [email protected]'),+ ).toBe("Rejected [redacted] and [redacted]");+ expect(scrubAddresses('Rejected "a\\"b"@example.com')).toBe(+ "Rejected [redacted]",+ );+ expect(scrubAddresses("connect ECONNREFUSED 127.0.0.1:1")).toBe(+ "connect ECONNREFUSED 127.0.0.1:1",+ );+});diff --git a/packages/emails/src/send.tsx b/packages/emails/src/send.tsxindex 9bd1b36fa..19fc97e6e 100644--- a/packages/emails/src/send.tsx+++ b/packages/emails/src/send.tsx@@ -52,6 +52,76 @@ type DispatchOptions = {errorLabel: string;};+// Local part is either a quoted string with backslash escapes (RFC 5321+// §4.1.2) or a run of unquoted characters.+const ADDRESS_PATTERN = /(?:"(?:\\.|[^"\\])*"|[^\s<>,;:"]+)@[^\s<>,;:"]+/g;++/**+ * Domains of every address in a nodemailer `to` string (comma or semicolon+ * separated, display-name wrappers allowed), deduplicated and lowercased.+ * Enough to tell a misconfigured provider from a single bad mailbox without+ * putting an address in the log sink.+ */+export function recipientDomains(to: string) {+ const domains = new Set<string>();+ for (const address of to.match(ADDRESS_PATTERN) ?? []) {+ domains.add(address.slice(address.lastIndexOf("@") + 1).toLowerCase());+ }+ return [...domains];+}++/** Replaces anything address-shaped so SMTP replies can be logged verbatim. */+export function scrubAddresses(text: string) {+ return text.replace(ADDRESS_PATTERN, "[redacted]");+}++/**+ * Nodemailer rejections carry the recipient in `message`, `response`,+ * `rejected` and `rejectedErrors`, so the raw error must not be logged.+ * Keeps the fields an operator needs to diagnose a transport failure.+ */+/**+ * Nodemailer's send result. Only the SMTP transport fills `accepted`,+ * `rejected` and `response`; SES returns its own shape.+ */+function describeSendResult(info: unknown) {+ if (typeof info !== "object" || info === null) {+ return {};+ }+ const { messageId, accepted, rejected, response } = info as {+ messageId?: string;+ accepted?: unknown[];+ rejected?: unknown[];+ response?: string;+ };+ return {+ messageId,+ acceptedCount: Array.isArray(accepted) ? accepted.length : undefined,+ rejectedCount: Array.isArray(rejected) ? rejected.length : undefined,+ response: response ? scrubAddresses(response) : undefined,+ };+}++function describeTransportError(e: unknown) {+ if (!(e instanceof Error)) {+ return { errorMessage: scrubAddresses(String(e)) };+ }+ const { code, command, responseCode, response } = e as Error & {+ code?: string;+ command?: string;+ responseCode?: number;+ response?: string;+ };+ return {+ errorName: e.name,+ errorMessage: scrubAddresses(e.message),+ errorCode: code,+ errorCommand: command,+ errorResponseCode: responseCode,+ errorResponse: response ? scrubAddresses(response) : undefined,+ };+}+function buildHeaders(listUnsubscribeUrl?: string,): Record<string, string> | undefined {@@ -77,7 +147,7 @@ async function dispatch(options: DispatchOptions) {}try {- await getTransport().sendMail({+ const info = await getTransport().sendMail({from: resolveFrom(options.from),to: options.to,replyTo: options.replyTo,@@ -88,12 +158,28 @@ async function dispatch(options: DispatchOptions) {icalEvent: options.icalEvent,headers: buildHeaders(options.listUnsubscribeUrl),});+ // Proves the hand-off to the server, so "sent but never arrived" can be+ // separated from "never sent" without an SMTP transcript.+ logger.info(+ {+ ...describeSendResult(info),+ recipientDomains: recipientDomains(options.to),+ subject: options.subject,+ },+ `Sent email: ${options.errorLabel}`,+ );} catch (e) {// Operational (SMTP/transport) failures are logged, not thrown — sending is// fire-and-forget. Render/template (code) errors are NOT caught here, so they// propagate to the caller's error reporting (Sentry via onRequestError).+ // Only the domain is logged: the log sink has its own retention and sits+ // outside the data map, so the address itself must never land there.logger.error(- { error: e, recipient: options.to, subject: options.subject },+ {+ ...describeTransportError(e),+ recipientDomains: recipientDomains(options.to),+ subject: options.subject,+ },`Failed to send email: ${options.errorLabel}`,);}diff --git a/packages/emails/src/transport.test.ts b/packages/emails/src/transport.test.tsindex 39694edc2..7348b56c8 100644--- a/packages/emails/src/transport.test.ts+++ b/packages/emails/src/transport.test.ts@@ -6,10 +6,6 @@ vi.mock("nodemailer", () => ({createTransport: (options: unknown) => createTransport(options),}));-vi.mock("@rallly/logger", () => ({- logger: { child: () => ({}) },-}));-const { createTransportForProvider } = await import("./transport");const SMTP_ENV_VARS = [@@ -18,7 +14,6 @@ const SMTP_ENV_VARS = ["SMTP_HOST","SMTP_PORT","SMTP_SECURE",- "SMTP_DEBUG",];const savedEnv: Record<string, string | undefined> = {};diff --git a/packages/emails/src/transport.ts b/packages/emails/src/transport.tsindex 44daaaf44..75b99cd65 100644--- a/packages/emails/src/transport.ts+++ b/packages/emails/src/transport.ts@@ -1,17 +1,10 @@import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2";import { defaultProvider } from "@aws-sdk/credential-provider-node";-import { logger } from "@rallly/logger";import type { Transporter } from "nodemailer";import { createTransport } from "nodemailer";type EmailProvider = "ses" | "smtp";-// Pino's (obj, msg) call signature matches nodemailer's bunyan-style logger,-// so the child can be handed over as is.-function createSmtpDebugLogger() {- return logger.child({ name: "smtp" }, { level: "trace" });-}-export type SupportedEmailProviders = EmailProvider;let cachedTransport: Transporter | undefined;@@ -48,14 +41,15 @@ export function createTransportForProvider(provider: EmailProvider) {);}+ if (process.env.SMTP_DEBUG === "true") {+ console.warn(+ "⚠️ SMTP_DEBUG is no longer supported and can be removed. Send failures are logged with the SMTP server's response.",+ );+ }+const rejectUnauthorized =process.env.SMTP_REJECT_UNAUTHORIZED !== "false";- // Logs the full SMTP conversation including the auth exchange, where- // credentials are only base64 encoded. Never enable outside of- // troubleshooting.- const debug = process.env.SMTP_DEBUG === "true";-// Warn about security change if no explicit settingif (process.env.SMTP_REJECT_UNAUTHORIZED === undefined &&@@ -80,8 +74,6 @@ export function createTransportForProvider(provider: EmailProvider) {rejectUnauthorized,servername: process.env.SMTP_TLS_SERVERNAME,},- debug,- logger: debug ? createSmtpDebugLogger() : undefined,});}}diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.tsindex 85aea409f..01e4c1c3e 100644--- a/packages/logger/src/index.ts+++ b/packages/logger/src/index.ts@@ -10,9 +10,14 @@ const stream = isDevelopment}): undefined;+// Defence in depth: call sites are not supposed to log addresses at all, but+// a stray field must not reach the sink with the sink's retention.+const redact = ["email", "recipient", "to"].flatMap((key) => [key, `*.${key}`]);+export const logger = pino({level,+ redact,},stream,);