Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

fix(email): report refused capture driver as unavailable in production (#212)

Kevin Rosead6cb80b0ab6
diff --git a/src/components/settings/organization-settings.tsx b/src/components/settings/organization-settings.tsx
index 743f2ef..4f50ef3 100644
--- a/src/components/settings/organization-settings.tsx
+++ b/src/components/settings/organization-settings.tsx
@@ -709,7 +709,8 @@ function EmailSetupPanel() {
const canSendTest =
status !== null &&
status.state !== "missing_key" &&
- status.state !== "capture";
+ status.state !== "capture" &&
+ status.state !== "capture_refused";
return (
<div className="space-y-5 border-t border-(--border) pt-8" id="email-setup">
@@ -845,6 +846,50 @@ function describeEmailStatus(status: EmailStatus): {
ready: true,
title: "Emails are being captured, not sent",
};
+ case "capture_refused":
+ return status.onVercel
+ ? {
+ lines: [
+ "EMAIL_DRIVER=capture drops every email and cannot be used in production, so this site refuses to send anything: no sign-up confirmations and no signing invitations.",
+ ],
+ ready: false,
+ steps: [
+ {
+ text: "In this project on Vercel, open Settings, then Environment Variables, and remove EMAIL_DRIVER (or set it to resend).",
+ },
+ {
+ link: {
+ href: RESEND_MARKETPLACE_URL,
+ label: "Resend on the Vercel Marketplace",
+ },
+ text: "Connect Resend and check that RESEND_API_KEY is listed for Production.",
+ },
+ {
+ text: "Open Deployments, open the ••• menu on the latest Production deployment and choose Redeploy, then reload this page.",
+ },
+ ],
+ title: "Email cannot be sent with EMAIL_DRIVER=capture",
+ }
+ : {
+ lines: [
+ "EMAIL_DRIVER=capture drops every email and cannot be used in production, so this site refuses to send anything: no sign-up confirmations and no signing invitations.",
+ ],
+ ready: false,
+ steps: [
+ {
+ text: "Remove EMAIL_DRIVER=capture from the environment variables where this site runs (or set EMAIL_DRIVER=resend).",
+ },
+ {
+ link: {
+ href: "https://resend.com/api-keys",
+ label: "Resend API keys",
+ },
+ text: "Configure a delivery provider by setting RESEND_API_KEY if it is not set already.",
+ },
+ { text: "Restart the app, then reload this page." },
+ ],
+ title: "Email cannot be sent with EMAIL_DRIVER=capture",
+ };
case "missing_key":
return status.onVercel
? {
diff --git a/src/lib/email-status.test.ts b/src/lib/email-status.test.ts
index b1b42ac..2d036a1 100644
--- a/src/lib/email-status.test.ts
+++ b/src/lib/email-status.test.ts
@@ -143,10 +143,113 @@ function fakeResend(
};
}
-test("the capture driver is reported as such without asking anyone", async () => {
+test("the capture driver is reported as capture only where it is allowed (issue #212)", async () => {
+ // Development and loopback production builds are allowed to capture mail.
assert.deepEqual(await getEmailStatus({ env: { EMAIL_DRIVER: "capture" } }), {
state: "capture",
});
+ assert.deepEqual(
+ await getEmailStatus({
+ env: { EMAIL_DRIVER: "capture", NODE_ENV: "development" },
+ }),
+ { state: "capture" },
+ );
+ assert.deepEqual(
+ await getEmailStatus({
+ env: {
+ APP_URL: "http://localhost:3207",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ },
+ }),
+ { state: "capture" },
+ );
+ assert.deepEqual(
+ await getEmailStatus({
+ env: {
+ APP_URL: "http://127.0.0.1:3207",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ },
+ }),
+ { state: "capture" },
+ );
+
+ // Deployed production installs refuse capture, even if RESEND_API_KEY is set.
+ const deployedProductionEnv = {
+ APP_URL: "https://sign.example.com",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ };
+
+ assert.deepEqual(await getEmailStatus({ env: deployedProductionEnv }), {
+ onVercel: false,
+ state: "capture_refused",
+ });
+ assert.deepEqual(
+ await getEmailStatus({
+ env: { ...deployedProductionEnv, RESEND_API_KEY: "re_test_key" },
+ }),
+ {
+ onVercel: false,
+ state: "capture_refused",
+ },
+ );
+ assert.deepEqual(
+ await getEmailStatus({
+ env: { EMAIL_DRIVER: "capture", NODE_ENV: "production" },
+ }),
+ {
+ onVercel: false,
+ state: "capture_refused",
+ },
+ );
+ assert.deepEqual(await addEmailDomain({ env: deployedProductionEnv }), {
+ onVercel: false,
+ state: "capture_refused",
+ });
+ assert.deepEqual(await recheckEmailDomain({ env: deployedProductionEnv }), {
+ onVercel: false,
+ state: "capture_refused",
+ });
+
+ // Vercel always refuses capture, regardless of NODE_ENV or APP_URL.
+ assert.deepEqual(
+ await getEmailStatus({ env: { EMAIL_DRIVER: "capture", VERCEL: "1" } }),
+ {
+ onVercel: true,
+ state: "capture_refused",
+ },
+ );
+ assert.deepEqual(
+ await getEmailStatus({
+ env: {
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ VERCEL: "1",
+ VERCEL_ENV: "production",
+ VERCEL_PROJECT_PRODUCTION_URL: "sign.example.com",
+ },
+ }),
+ {
+ onVercel: true,
+ state: "capture_refused",
+ },
+ );
+ assert.deepEqual(
+ await getEmailStatus({
+ env: {
+ APP_URL: "http://localhost:3207",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ VERCEL: "1",
+ },
+ }),
+ {
+ onVercel: true,
+ state: "capture_refused",
+ },
+ );
});
test("no Resend key means email is not connected", async () => {
@@ -545,19 +648,78 @@ test("a request turned away for going too fast is tried once more", async () =>
});
test("the administrator is told about email until it has been seen working", () => {
+ const readySettings = resolveSettings(
+ { ...stored, emailReadyAt: 1_800_000_100 },
+ env,
+ );
+
assert.equal(emailNeedsAttention(settings, env), true);
+ assert.equal(emailNeedsAttention(readySettings, env), false);
+
+ // Nothing to set up when messages are legitimately being captured locally.
assert.equal(
- emailNeedsAttention(
- resolveSettings({ ...stored, emailReadyAt: 1_800_000_100 }, env),
- env,
- ),
+ emailNeedsAttention(settings, { EMAIL_DRIVER: "capture" }),
false,
);
- // Nothing to set up when messages are only being captured.
assert.equal(
- emailNeedsAttention(settings, { EMAIL_DRIVER: "capture" }),
+ emailNeedsAttention(settings, {
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "development",
+ }),
false,
);
+ assert.equal(
+ emailNeedsAttention(settings, {
+ APP_URL: "http://localhost:3207",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ }),
+ false,
+ );
+ assert.equal(
+ emailNeedsAttention(settings, {
+ APP_URL: "http://127.0.0.1:3207",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ }),
+ false,
+ );
+
+ // When capture is refused in production or on Vercel, the administrator must
+ // be warned even if Resend had previously been marked ready.
+ const deployedProductionCaptureEnv = {
+ APP_URL: "https://sign.example.com",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ };
+
+ assert.equal(
+ emailNeedsAttention(settings, deployedProductionCaptureEnv),
+ true,
+ );
+ assert.equal(
+ emailNeedsAttention(readySettings, deployedProductionCaptureEnv),
+ true,
+ );
+ assert.equal(
+ emailNeedsAttention(settings, {
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ }),
+ true,
+ );
+ assert.equal(
+ emailNeedsAttention(settings, { EMAIL_DRIVER: "capture", VERCEL: "1" }),
+ true,
+ );
+ assert.equal(
+ emailNeedsAttention(readySettings, {
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ VERCEL: "1",
+ }),
+ true,
+ );
});
test("RESEND_REGION picks a known Resend region and ignores anything else", () => {
diff --git a/src/lib/email-status.ts b/src/lib/email-status.ts
index 2c8958f..655ac8a 100644
--- a/src/lib/email-status.ts
+++ b/src/lib/email-status.ts
@@ -6,7 +6,10 @@ import {
getAddressDomain,
isResendTestSender,
} from "./services/email-addresses.ts";
-import { resolveEmailDriver } from "./services/email.ts";
+import {
+ isCaptureEmailDriverAllowed,
+ resolveEmailDriver,
+} from "./services/email.ts";
import {
getSettings,
markEmailReady,
@@ -36,8 +39,14 @@ export type EmailDnsRecord = {
};
export type EmailStatus =
- /** Development: messages are written to disk, not sent. */
+ /** Development (or a local production build on localhost): messages are written to disk, not sent. */
| { state: "capture" }
+ /**
+ * EMAIL_DRIVER=capture is configured in a deployed environment (production
+ * with a non-loopback URL, or Vercel), where the capture driver is refused
+ * because it drops every email.
+ */
+ | { state: "capture_refused"; onVercel: boolean }
/**
* No Resend API key, so nothing can be sent. On Vercel the steps to fix it
* are Vercel's, which the page spells out.
@@ -385,7 +394,12 @@ async function inspectEmail({
client: null,
domainId: null,
markReady: noop,
- status: { state: "capture" },
+ status: isCaptureEmailDriverAllowed(env)
+ ? { state: "capture" }
+ : {
+ onVercel: readEnv(env, "VERCEL") === "1",
+ state: "capture_refused",
+ },
};
}
@@ -558,7 +572,7 @@ export function emailNeedsAttention(
): boolean {
try {
if (resolveEmailDriver(env) === "capture") {
- return false;
+ return !isCaptureEmailDriverAllowed(env);
}
} catch {
return true;
diff --git a/src/lib/services/email.test.ts b/src/lib/services/email.test.ts
index c6ef8a7..bf065ae 100644
--- a/src/lib/services/email.test.ts
+++ b/src/lib/services/email.test.ts
@@ -1,12 +1,17 @@
import assert from "node:assert/strict";
+import { mkdtemp, readdir, rm } from "node:fs/promises";
+import os from "node:os";
+import path from "node:path";
import { test } from "node:test";
import type { Resend } from "resend";
import {
+ CAPTURE_IN_PRODUCTION_ERROR_MESSAGE,
canSendEmail,
createEmailService,
createLazyEmailService,
+ isCaptureEmailDriverAllowed,
type EmailService,
} from "./email.ts";
@@ -173,13 +178,160 @@ test("signup knows when there is no email provider to send with (issue #95)", ()
canSendEmail({ EMAIL_DRIVER: "capture", NODE_ENV: "development" }),
true,
);
- // The capture driver drops mail, so a real site cannot count on it.
+ // A local production build on localhost may still use the capture driver.
+ assert.equal(
+ canSendEmail({
+ APP_URL: "http://localhost:3207",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ }),
+ true,
+ );
+ // The capture driver drops mail, so a real site cannot count on it, even
+ // when a Resend key is also present in the environment.
+ assert.equal(
+ canSendEmail({
+ APP_URL: "https://sign.example.com",
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ }),
+ false,
+ );
assert.equal(
canSendEmail({
APP_URL: "https://sign.example.com",
EMAIL_DRIVER: "capture",
NODE_ENV: "production",
+ RESEND_API_KEY: "re_test",
+ }),
+ false,
+ );
+ assert.equal(canSendEmail({ EMAIL_DRIVER: "capture", VERCEL: "1" }), false);
+ assert.equal(
+ canSendEmail({
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ VERCEL: "1",
+ }),
+ false,
+ );
+});
+
+test("the capture driver is allowed in development and local production builds, but refused in deployed production and on Vercel (issue #212)", async () => {
+ assert.equal(isCaptureEmailDriverAllowed({}), true);
+ assert.equal(isCaptureEmailDriverAllowed({ NODE_ENV: "development" }), true);
+ assert.equal(
+ isCaptureEmailDriverAllowed({
+ APP_URL: "http://localhost:3207",
+ NODE_ENV: "production",
+ }),
+ true,
+ );
+ assert.equal(
+ isCaptureEmailDriverAllowed({
+ APP_URL: "http://127.0.0.1:3207",
+ NODE_ENV: "production",
+ }),
+ true,
+ );
+ assert.equal(
+ isCaptureEmailDriverAllowed({
+ APP_URL: "http://[::1]:3207",
+ NODE_ENV: "production",
+ }),
+ true,
+ );
+
+ assert.equal(
+ isCaptureEmailDriverAllowed({
+ APP_URL: "https://sign.example.com",
+ NODE_ENV: "production",
+ }),
+ false,
+ );
+ assert.equal(isCaptureEmailDriverAllowed({ NODE_ENV: "production" }), false);
+ assert.equal(
+ isCaptureEmailDriverAllowed({
+ APP_URL: "http://sign.example.com",
+ NODE_ENV: "production",
+ }),
+ false,
+ );
+ assert.equal(isCaptureEmailDriverAllowed({ VERCEL: "1" }), false);
+ assert.equal(
+ isCaptureEmailDriverAllowed({ NODE_ENV: "production", VERCEL: "1" }),
+ false,
+ );
+ assert.equal(
+ isCaptureEmailDriverAllowed({
+ APP_URL: "http://localhost:3207",
+ NODE_ENV: "production",
+ VERCEL: "1",
}),
false,
);
+
+ const outboxDir = await mkdtemp(path.join(os.tmpdir(), "signudoc-capture-"));
+ const resolveAddresses = async () => ({
+ senderAddress: "Example <[email protected]>",
+ supportAddress: null,
+ });
+
+ try {
+ // Allowed in development and in a loopback production build: writes captured files.
+ const devService = createEmailService({
+ env: {
+ EMAIL_CAPTURE_DIR: outboxDir,
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "development",
+ },
+ resolveAddresses,
+ });
+
+ await devService.send(MESSAGE);
+ assert.equal((await readdir(outboxDir)).length, 1);
+
+ const localProdService = createEmailService({
+ env: {
+ APP_URL: "http://localhost:3207",
+ EMAIL_CAPTURE_DIR: outboxDir,
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ },
+ resolveAddresses,
+ });
+
+ await localProdService.send(MESSAGE);
+ assert.equal((await readdir(outboxDir)).length, 2);
+
+ // Refused in deployed production and on Vercel: throws and writes nothing.
+ for (const refusedEnv of [
+ {
+ APP_URL: "https://sign.example.com",
+ EMAIL_CAPTURE_DIR: outboxDir,
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ },
+ {
+ EMAIL_CAPTURE_DIR: outboxDir,
+ EMAIL_DRIVER: "capture",
+ VERCEL: "1",
+ },
+ {
+ EMAIL_CAPTURE_DIR: outboxDir,
+ EMAIL_DRIVER: "capture",
+ NODE_ENV: "production",
+ VERCEL: "1",
+ },
+ ]) {
+ assert.throws(
+ () => createEmailService({ env: refusedEnv, resolveAddresses }),
+ { message: CAPTURE_IN_PRODUCTION_ERROR_MESSAGE },
+ );
+ }
+
+ assert.equal((await readdir(outboxDir)).length, 2);
+ } finally {
+ await rm(outboxDir, { force: true, recursive: true });
+ }
});
diff --git a/src/lib/services/email.ts b/src/lib/services/email.ts
index 0957dd6..e6d2c2b 100644
--- a/src/lib/services/email.ts
+++ b/src/lib/services/email.ts
@@ -6,7 +6,12 @@ import { Resend } from "resend";
import { getSettings } from "../settings.ts";
import { type EmailTemplateKind } from "../types/domain.ts";
-import { getEnv, isLocalDeployment, type EnvSource } from "../utils/env.ts";
+import {
+ getEnv,
+ isLocalDeployment,
+ readEnv,
+ type EnvSource,
+} from "../utils/env.ts";
import { buildEmailAddresses, type EmailAddresses } from "./email-addresses.ts";
import { CAPTURED_MESSAGE_ID_PREFIX } from "./captured-email.ts";
import {
@@ -202,6 +207,33 @@ export function resolveEmailDriver(env: EnvSource = process.env): EmailDriver {
);
}
+export const CAPTURE_IN_PRODUCTION_ERROR_MESSAGE =
+ "EMAIL_DRIVER=capture drops every email and cannot be used in production.";
+
+/**
+ * Whether EMAIL_DRIVER=capture may be used in this deployment.
+ * Allowed in development, and for a production build that is being run on
+ * localhost (which is what the end-to-end test does). Never on Vercel or on
+ * a deployed production site, because it drops every email.
+ */
+export function isCaptureEmailDriverAllowed(
+ env: EnvSource = process.env,
+): boolean {
+ if (readEnv(env, "VERCEL") === "1") {
+ return false;
+ }
+
+ if (env.NODE_ENV !== "production") {
+ return true;
+ }
+
+ try {
+ return isLocalDeployment(env);
+ } catch {
+ return false;
+ }
+}
+
/**
* Development and test driver: nothing is sent. Each message is written as a
* JSON file so a person (or the end-to-end test) can read the links in it.
@@ -210,12 +242,8 @@ function createCaptureEmailService(
env: EnvSource,
resolveAddresses: () => Promise<EmailAddresses>,
): EmailService {
- // Allowed in development, and for a production build that is being run on
- // localhost (which is what the end-to-end test does). Never on a real site.
- if (env.NODE_ENV === "production" && !isLocalDeployment(env)) {
- throw new Error(
- "EMAIL_DRIVER=capture drops every email and cannot be used in production.",
- );
+ if (!isCaptureEmailDriverAllowed(env)) {
+ throw new Error(CAPTURE_IN_PRODUCTION_ERROR_MESSAGE);
}
// The comment stops Turbopack from tracing the whole project into the
@@ -295,7 +323,8 @@ export async function resolveEmailAddresses(
): Promise<EmailAddresses> {
const settings = await getSettings(env);
- return resolveEmailDriver(env) === "capture"
+ return resolveEmailDriver(env) === "capture" &&
+ isCaptureEmailDriverAllowed(env)
? buildEmailAddresses(settings, DEFAULT_CAPTURE_SENDER_ADDRESS)
: buildEmailAddresses(settings);
}