Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(frontend): pre-screen email addresses before sign-up and auth sends (#15959)

Bently67db23f22fb8
diff --git a/autogpt_platform/frontend/src/lib/auth/__tests__/access-blocks-flow.test.ts b/autogpt_platform/frontend/src/lib/auth/__tests__/access-blocks-flow.test.ts
index 83b901a9a8..7cde17abc5 100644
--- a/autogpt_platform/frontend/src/lib/auth/__tests__/access-blocks-flow.test.ts
+++ b/autogpt_platform/frontend/src/lib/auth/__tests__/access-blocks-flow.test.ts
@@ -1,6 +1,10 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { SIGN_IN_BLOCKED_CODE, SIGN_UP_BLOCKED_CODE } from "../access-blocks";
-import { DISPOSABLE_EMAIL_CODE } from "../disposable-email";
+import { Resolver } from "node:dns/promises";
+import {
+ DISPOSABLE_EMAIL_CODE,
+ UNDELIVERABLE_EMAIL_CODE,
+} from "../disposable-email";
// Runs the real Better Auth handler with this app's auth options, as
// email-verification-flow.test.ts does, to prove the hooks refuse and log
@@ -180,6 +184,42 @@ describe("IP and email domain blocks", () => {
expect(testDB.current.UserAuthIdentity).toHaveLength(0);
});
+ it("refuses a sign-up on a domain that takes no mail", async () => {
+ vi.stubEnv("NEXT_PUBLIC_BEHAVE_AS", "CLOUD");
+ const lookup = vi
+ .spyOn(Resolver.prototype, "resolveMx")
+ .mockRejectedValue(
+ Object.assign(new Error("ENOTFOUND"), { code: "ENOTFOUND" }),
+ );
+
+ const response = await signUp(handler, "[email protected]", "192.0.2.1");
+ const mxLookups = lookup.mock.calls.length;
+ lookup.mockRestore();
+
+ expect(mxLookups).toBe(1);
+
+ expect(response.status).toBe(400);
+ expect(await response.json()).toMatchObject({
+ code: UNDELIVERABLE_EMAIL_CODE,
+ message: "Please use a real email address you can receive mail at.",
+ });
+ expect(testDB.current.UserAuthIdentity).toHaveLength(0);
+ });
+
+ it("lets a sign-up through when the mail-server lookup errors", async () => {
+ vi.stubEnv("NEXT_PUBLIC_BEHAVE_AS", "CLOUD");
+ const lookup = vi
+ .spyOn(Resolver.prototype, "resolveMx")
+ .mockRejectedValue(
+ Object.assign(new Error("ESERVFAIL"), { code: "ESERVFAIL" }),
+ );
+
+ const response = await signUp(handler, "[email protected]", "192.0.2.1");
+ lookup.mockRestore();
+
+ expect(response.status).toBe(200);
+ });
+
it("answers an admin's domain block before the disposable list", async () => {
blocks.domains = ["mailinator.com"];
diff --git a/autogpt_platform/frontend/src/lib/auth/__tests__/disposable-email.test.ts b/autogpt_platform/frontend/src/lib/auth/__tests__/disposable-email.test.ts
index bfee64a37a..fe939f40f5 100644
--- a/autogpt_platform/frontend/src/lib/auth/__tests__/disposable-email.test.ts
+++ b/autogpt_platform/frontend/src/lib/auth/__tests__/disposable-email.test.ts
@@ -12,6 +12,14 @@ function mx(records: Records) {
return vi.fn(async (_domain: string) => records);
}
+function dnsWith(resolveMx: (domain: string) => Promise<Records>) {
+ return {
+ resolveMx,
+ resolve4: async () => ["192.0.2.1"],
+ resolve6: async () => [],
+ };
+}
+
async function codeOf(promise: Promise<unknown>) {
try {
await promise;
@@ -30,7 +38,7 @@ describe("assertNotDisposableEmail", () => {
"refuses %s, on the list",
async (email) => {
await expect(
- codeOf(assertNotDisposableEmail(email, { resolveMX: mx([]) })),
+ codeOf(assertNotDisposableEmail(email, { dns: dnsWith(mx([])) })),
).resolves.toBe(DISPOSABLE_EMAIL_CODE);
},
);
@@ -40,7 +48,9 @@ describe("assertNotDisposableEmail", () => {
await expect(
codeOf(
- assertNotDisposableEmail("[email protected]", { resolveMX }),
+ assertNotDisposableEmail("[email protected]", {
+ dns: dnsWith(resolveMX),
+ }),
),
).resolves.toBe(DISPOSABLE_EMAIL_CODE);
expect(resolveMX).toHaveBeenCalledWith("rotating-inbox.example");
@@ -58,7 +68,7 @@ describe("assertNotDisposableEmail", () => {
await expect(
codeOf(
assertNotDisposableEmail(email, {
- resolveMX: mx([{ exchange: "mx.example.net" }]),
+ dns: dnsWith(mx([{ exchange: "mx.example.net" }])),
}),
),
).resolves.toBeNull();
@@ -72,7 +82,9 @@ describe("assertNotDisposableEmail", () => {
});
await expect(
- codeOf(assertNotDisposableEmail("[email protected]", { resolveMX })),
+ codeOf(
+ assertNotDisposableEmail("[email protected]", { dns: dnsWith(resolveMX) }),
+ ),
).resolves.toBeNull();
});
@@ -87,7 +99,7 @@ describe("assertNotDisposableEmail", () => {
await expect(
codeOf(
assertNotDisposableEmail("[email protected]", {
- resolveMX,
+ dns: dnsWith(resolveMX),
timeoutMs: 20,
}),
),
@@ -99,7 +111,7 @@ describe("assertNotDisposableEmail", () => {
await expect(
codeOf(
- assertNotDisposableEmail("[email protected]", { resolveMX: mx([]) }),
+ assertNotDisposableEmail("[email protected]", { dns: dnsWith(mx([])) }),
),
).resolves.toBeNull();
});
diff --git a/autogpt_platform/frontend/src/lib/auth/__tests__/email-deliverability.test.ts b/autogpt_platform/frontend/src/lib/auth/__tests__/email-deliverability.test.ts
new file mode 100644
index 0000000000..0d45b8297a
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/auth/__tests__/email-deliverability.test.ts
@@ -0,0 +1,176 @@
+// @vitest-environment node
+import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
+import {
+ acceptsMail,
+ clearMailServerCache,
+ type DnsLookups,
+ hasMailServer,
+ isDisposableEmail,
+ isDisposableEmailDomain,
+} from "../email-deliverability";
+
+function dnsError(code: string) {
+ return Object.assign(new Error(code), { code });
+}
+
+function failsWith(code: string) {
+ return vi.fn(async (_domain: string): Promise<never> => {
+ throw dnsError(code);
+ });
+}
+
+function lookups(overrides: Partial<DnsLookups> = {}) {
+ return {
+ resolveMx: vi.fn(async (_domain: string) => [
+ { exchange: "mx.mail.example" },
+ ]),
+ resolve4: failsWith("ENODATA"),
+ resolve6: failsWith("ENODATA"),
+ ...overrides,
+ };
+}
+
+beforeEach(() => {
+ clearMailServerCache();
+ vi.stubEnv("NEXT_PUBLIC_BEHAVE_AS", "CLOUD");
+});
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+});
+
+describe("isDisposableEmailDomain", () => {
+ "matches %s",
+ (email) => {
+ expect(isDisposableEmailDomain(email)).toBe(true);
+ },
+ );
+
+ test.each([
+ ])("does not match %s", (email) => {
+ expect(isDisposableEmailDomain(email)).toBe(false);
+ });
+});
+
+describe("hasMailServer", () => {
+ test("is true with an MX record", async () => {
+ await expect(hasMailServer("mx.example", { dns: lookups() })).resolves.toBe(
+ true,
+ );
+ });
+
+ test("is false for a null MX", async () => {
+ const dns = lookups({ resolveMx: vi.fn(async () => [{ exchange: "" }]) });
+ await expect(hasMailServer("null.example", { dns })).resolves.toBe(false);
+ });
+
+ test("falls back to an A record when there is no MX", async () => {
+ const dns = lookups({
+ resolveMx: failsWith("ENODATA"),
+ resolve4: vi.fn(async () => ["192.0.2.1"]),
+ });
+ await expect(hasMailServer("a-only.example", { dns })).resolves.toBe(true);
+ });
+
+ test("is false with no MX, A or AAAA record", async () => {
+ const dns = lookups({ resolveMx: failsWith("ENODATA") });
+ await expect(hasMailServer("empty.example", { dns })).resolves.toBe(false);
+ });
+
+ test("is false when the domain does not exist", async () => {
+ const dns = lookups({ resolveMx: failsWith("ENOTFOUND") });
+ await expect(hasMailServer("nx.example", { dns })).resolves.toBe(false);
+ });
+
+ test.each(["ESERVFAIL", "ETIMEOUT", "ECONNREFUSED"])(
+ "fails open on %s",
+ async (code) => {
+ const dns = lookups({ resolveMx: failsWith(code) });
+ await expect(hasMailServer("err.example", { dns })).resolves.toBe(true);
+ },
+ );
+
+ test.each(["resolve4", "resolve6"] as const)(
+ "fails open when %s errors after no MX",
+ async (lookup) => {
+ const dns = lookups({
+ resolveMx: failsWith("ENODATA"),
+ [lookup]: failsWith("ESERVFAIL"),
+ });
+ await expect(hasMailServer("half.example", { dns })).resolves.toBe(true);
+ },
+ );
+
+ test("fails open when DNS is too slow", async () => {
+ const dns = lookups({
+ resolveMx: vi.fn(() => new Promise<never>(() => {})),
+ });
+ await expect(
+ hasMailServer("slow.example", { dns, timeoutMs: 10 }),
+ ).resolves.toBe(true);
+ });
+
+ test("caches a definitive answer, case-insensitively", async () => {
+ const dns = lookups({ resolveMx: failsWith("ENOTFOUND") });
+ await hasMailServer("cached.example", { dns });
+ await expect(hasMailServer("CACHED.example", { dns })).resolves.toBe(false);
+ expect(dns.resolveMx).toHaveBeenCalledTimes(1);
+ });
+
+ test("does not cache an inconclusive answer", async () => {
+ const dns = lookups({ resolveMx: failsWith("ESERVFAIL") });
+ await hasMailServer("flaky.example", { dns });
+ await hasMailServer("flaky.example", { dns });
+ expect(dns.resolveMx).toHaveBeenCalledTimes(2);
+ });
+});
+
+describe("isDisposableEmail", () => {
+ test("flags a listed domain without a lookup", async () => {
+ const dns = lookups();
+ await expect(isDisposableEmail("[email protected]", { dns })).resolves.toBe(
+ true,
+ );
+ expect(dns.resolveMx).not.toHaveBeenCalled();
+ });
+
+ test("is off with AUTH_ALLOW_DISPOSABLE_EMAILS", async () => {
+ vi.stubEnv("AUTH_ALLOW_DISPOSABLE_EMAILS", "true");
+ await expect(
+ isDisposableEmail("[email protected]", { dns: lookups() }),
+ ).resolves.toBe(false);
+ });
+
+ test("flags a domain whose mail lands on a disposable inbox host", async () => {
+ const dns = lookups({
+ resolveMx: vi.fn(async () => [{ exchange: "in.mail.tm." }]),
+ });
+ await expect(
+ isDisposableEmail("[email protected]", { dns }),
+ ).resolves.toBe(true);
+ });
+});
+
+describe("acceptsMail", () => {
+ test("refuses a domain with no mail server", async () => {
+ const dns = lookups({ resolveMx: failsWith("ENOTFOUND") });
+ await expect(acceptsMail("[email protected]", { dns })).resolves.toBe(false);
+ });
+
+ test("passes a domain with an MX", async () => {
+ await expect(
+ acceptsMail("[email protected]", { dns: lookups() }),
+ ).resolves.toBe(true);
+ });
+
+ test("skips the check outside cloud", async () => {
+ vi.stubEnv("NEXT_PUBLIC_BEHAVE_AS", "LOCAL");
+ const dns = lookups({ resolveMx: vi.fn(async () => [{ exchange: "" }]) });
+ await expect(acceptsMail("[email protected]", { dns })).resolves.toBe(true);
+ expect(dns.resolveMx).not.toHaveBeenCalled();
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/auth/__tests__/email.test.ts b/autogpt_platform/frontend/src/lib/auth/__tests__/email.test.ts
index 95bb1138d7..96d78039e2 100644
--- a/autogpt_platform/frontend/src/lib/auth/__tests__/email.test.ts
+++ b/autogpt_platform/frontend/src/lib/auth/__tests__/email.test.ts
@@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/services/environment", () => ({
environment: {
getAGPTServerBaseUrl: () => "https://backend.example.com",
+ isCloud: () => true,
},
}));
@@ -11,7 +12,9 @@ vi.mock("../service-token", () => ({
mintServiceToken: (...args: unknown[]) => mintServiceTokenMock(...args),
}));
+import { Resolver } from "node:dns/promises";
import { sendAuthEmail } from "../email";
+import { clearMailServerCache } from "../email-deliverability";
const resetUrl = "https://platform.example.com/reset-password?token=abc123";
const args = {
@@ -27,6 +30,7 @@ beforeEach(() => {
mintServiceTokenMock.mockReset();
mintServiceTokenMock.mockResolvedValue("svc-token");
vi.stubGlobal("fetch", fetchMock);
+ clearMailServerCache();
});
afterEach(() => {
@@ -100,4 +104,42 @@ describe("sendAuthEmail", () => {
const logged = infoSpy.mock.calls.map((c) => String(c[0]));
expect(logged.some((l) => l.includes(resetUrl))).toBe(false);
});
+
+ it.each([
+ ["a domain that does not exist", "ENOTFOUND"],
+ ["a domain with no MX, A or AAAA record", "ENODATA"],
+ ])("skips the send to %s and reports success", async (_, code) => {
+ vi.stubEnv("NODE_ENV", "production");
+ const error = Object.assign(new Error(code), { code });
+ vi.spyOn(Resolver.prototype, "resolveMx").mockRejectedValue(error);
+ vi.spyOn(Resolver.prototype, "resolve4").mockRejectedValue(error);
+ vi.spyOn(Resolver.prototype, "resolve6").mockRejectedValue(error);
+ const warnSpy = vi
+ .spyOn(console, "warn")
+ .mockImplementation(() => undefined);
+
+ await expect(sendAuthEmail(args)).resolves.toBeUndefined();
+
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(JSON.stringify(warnSpy.mock.calls)).not.toContain(args.to);
+ });
+
+ it("still sends to a disposable domain", async () => {
+ fetchMock.mockResolvedValue(new Response(null, { status: 204 }));
+
+ await sendAuthEmail({ ...args, to: "[email protected]" });
+
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ });
+
+ it("still sends when the DNS lookup errors", async () => {
+ fetchMock.mockResolvedValue(new Response(null, { status: 204 }));
+ vi.spyOn(Resolver.prototype, "resolveMx").mockRejectedValue(
+ Object.assign(new Error("ESERVFAIL"), { code: "ESERVFAIL" }),
+ );
+
+ await sendAuthEmail(args);
+
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ });
});
diff --git a/autogpt_platform/frontend/src/lib/auth/auth.ts b/autogpt_platform/frontend/src/lib/auth/auth.ts
index 84094152c5..bfac8fcf41 100644
--- a/autogpt_platform/frontend/src/lib/auth/auth.ts
+++ b/autogpt_platform/frontend/src/lib/auth/auth.ts
@@ -12,10 +12,14 @@ import { Pool } from "pg";
import { assertSignInAllowed, assertSignUpAllowed } from "./access-blocks";
import { getTrustedClientIP } from "./client-ip";
import { DISABLED_AUTH_PATHS } from "./disabled-paths";
-import { assertNotDisposableEmail } from "./disposable-email";
+import {
+ assertEmailHasMailServer,
+ assertNotDisposableEmail,
+} from "./disposable-email";
import { runAfterResponse } from "./background-tasks";
import { mirrorVerifiedEmailToPlatformUser } from "./email-mirror";
import { sendAuthEmail } from "./email";
+import { sharedLookups } from "./email-deliverability";
import { isAwaitingEmailVerification } from "./email-verification";
import { emailRepeatSignUp } from "./existing-user-sign-up";
import { capAuthEmailsPerIP } from "./ip-email-cap";
@@ -469,5 +473,7 @@ async function assertEmailSignUpAllowed(
ipAddress: string | null,
) {
await assertSignUpAllowed(authDbPool, { email, ipAddress });
- await assertNotDisposableEmail(email);
+ const dns = sharedLookups();
+ await assertNotDisposableEmail(email, { dns });
+ await assertEmailHasMailServer(email, { dns });
}
diff --git a/autogpt_platform/frontend/src/lib/auth/disposable-email.ts b/autogpt_platform/frontend/src/lib/auth/disposable-email.ts
index 461bda942a..18fa20138a 100644
--- a/autogpt_platform/frontend/src/lib/auth/disposable-email.ts
+++ b/autogpt_platform/frontend/src/lib/auth/disposable-email.ts
@@ -1,68 +1,45 @@
import { APIError } from "better-auth/api";
-import { Resolver } from "node:dns/promises";
-import { getDomainAndParents } from "./access-blocks";
-import list from "./disposable-email-domains.json";
-
-// Vendored from github.com/disposable-email-domains/disposable-email-domains
-// (CC0); refresh with scripts/refresh-disposable-email-domains.mjs.
-const DISPOSABLE_DOMAINS = new Set(list.domains);
-
-// Temporary-inbox services that rotate domains faster than the list: any
-// domain whose mail lands on one of these hosts is refused too.
-export const DISPOSABLE_MX_HOSTS = new Set(["in.mail.tm"]);
+import {
+ acceptsMail,
+ type DnsLookups,
+ isDisposableEmail,
+} from "./email-deliverability";
export const DISPOSABLE_EMAIL_CODE = "DISPOSABLE_EMAIL";
const DISPOSABLE_EMAIL_MESSAGE =
"This email address can't be used to sign up. Use a permanent email address.";
-const MX_TIMEOUT_MS = 1500;
-
-type ResolveMX = (domain: string) => Promise<Array<{ exchange: string }>>;
+export const UNDELIVERABLE_EMAIL_CODE = "UNDELIVERABLE_EMAIL";
+const UNDELIVERABLE_EMAIL_MESSAGE =
+ "Please use a real email address you can receive mail at.";
interface Options {
- resolveMX?: ResolveMX;
+ dns?: DnsLookups;
timeoutMs?: number;
}
// Sign-up only. Set AUTH_ALLOW_DISPOSABLE_EMAILS=true to turn it off.
+// Fails open: a DNS error or a slow answer lets the sign-up through.
export async function assertNotDisposableEmail(
email: string,
- { resolveMX = resolveMXWithTimeout, timeoutMs = MX_TIMEOUT_MS }: Options = {},
+ options: Options = {},
) {
- if (process.env.AUTH_ALLOW_DISPOSABLE_EMAILS === "true") return;
- const domains = getDomainAndParents(email);
- const listed = domains.some((domain) => DISPOSABLE_DOMAINS.has(domain));
- if (listed || (await hasDisposableMX(domains[0], resolveMX, timeoutMs))) {
- throw new APIError("FORBIDDEN", {
- code: DISPOSABLE_EMAIL_CODE,
- message: DISPOSABLE_EMAIL_MESSAGE,
- });
- }
+ if (!(await isDisposableEmail(email, options))) return;
+ throw new APIError("FORBIDDEN", {
+ code: DISPOSABLE_EMAIL_CODE,
+ message: DISPOSABLE_EMAIL_MESSAGE,
+ });
}
-// Fails open: a DNS error or a slow answer lets the sign-up through.
-async function hasDisposableMX(
- domain: string | undefined,
- resolveMX: ResolveMX,
- timeoutMs: number,
+// Sign-up only, cloud only: refuses a domain DNS says takes no mail (see
+// email-deliverability.ts). A DNS error or timeout lets the sign-up through.
+export async function assertEmailHasMailServer(
+ email: string,
+ options: Options = {},
) {
- if (!domain) return false;
- let timer: ReturnType<typeof setTimeout> | undefined;
- const timedOut = new Promise<null>((resolve) => {
- timer = setTimeout(() => resolve(null), timeoutMs);
+ if (await acceptsMail(email, options)) return;
+ throw new APIError("BAD_REQUEST", {
+ code: UNDELIVERABLE_EMAIL_CODE,
+ message: UNDELIVERABLE_EMAIL_MESSAGE,
});
- try {
- const records = await Promise.race([resolveMX(domain), timedOut]);
- return (records ?? []).some((record) =>
- DISPOSABLE_MX_HOSTS.has(record.exchange.toLowerCase().replace(/\.$/, "")),
- );
- } catch {
- return false;
- } finally {
- clearTimeout(timer);
- }
-}
-
-function resolveMXWithTimeout(domain: string) {
- return new Resolver({ timeout: MX_TIMEOUT_MS, tries: 1 }).resolveMx(domain);
}
diff --git a/autogpt_platform/frontend/src/lib/auth/email-deliverability.ts b/autogpt_platform/frontend/src/lib/auth/email-deliverability.ts
new file mode 100644
index 0000000000..1b5c184739
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/auth/email-deliverability.ts
@@ -0,0 +1,202 @@
+import { Resolver } from "node:dns/promises";
+import { environment } from "@/services/environment";
+import { getDomainAndParents } from "./access-blocks";
+import list from "./disposable-email-domains.json";
+
+// Vendored from github.com/disposable-email-domains/disposable-email-domains
+// (CC0); refresh with scripts/refresh-disposable-email-domains.mjs.
+const DISPOSABLE_DOMAINS = new Set(list.domains);
+
+// Temporary-inbox services that rotate domains faster than the list: any
+// domain whose mail lands on one of these hosts is disposable too.
+export const DISPOSABLE_MX_HOSTS = new Set(["in.mail.tm"]);
+
+const DNS_TIMEOUT_MS = 2500;
+const CACHE_TTL_MS = 10 * 60 * 1000;
+const CACHE_MAX_ENTRIES = 1000;
+
+// Codes Node's resolver gives when DNS answers that a name or record type
+// does not exist. Anything else (timeout, SERVFAIL, refused) is inconclusive.
+const NO_DOMAIN_CODES = new Set(["ENOTFOUND"]);
+const NO_RECORDS_CODES = new Set(["ENODATA"]);
+
+export interface DnsLookups {
+ resolveMx: (domain: string) => Promise<Array<{ exchange: string }>>;
+ resolve4: (domain: string) => Promise<string[]>;
+ resolve6: (domain: string) => Promise<string[]>;
+}
+
+interface Options {
+ dns?: DnsLookups;
+ timeoutMs?: number;
+}
+
+const cache = new Map<string, { value: boolean; expiresAt: number }>();
+
+export function clearMailServerCache() {
+ cache.clear();
+}
+
+export function getEmailDomain(email: string) {
+ return getDomainAndParents(email)[0];
+}
+
+export function isDisposableEmailDomain(email: string) {
+ return getDomainAndParents(email).some((domain) =>
+ DISPOSABLE_DOMAINS.has(domain),
+ );
+}
+
+// False only when DNS definitively says the domain takes no mail: it does not
+// exist, publishes a null MX (RFC 7505), or has neither MX nor an A/AAAA
+// record to fall back to (RFC 5321 implicit MX). Fails open on DNS errors.
+export async function hasMailServer(
+ domain: string,
+ { dns = defaultLookups(), timeoutMs = DNS_TIMEOUT_MS }: Options = {},
+) {
+ const key = domain.toLowerCase().replace(/\.$/, "");
+ if (!key) return false;
+ const cached = cache.get(key);
+ if (cached && cached.expiresAt > Date.now()) return cached.value;
+
+ const value = await lookUpMailServer(key, dns, timeoutMs);
+ if (value !== null) {
+ if (cache.size >= CACHE_MAX_ENTRIES) cache.clear();
+ cache.set(key, { value, expiresAt: Date.now() + CACHE_TTL_MS });
+ }
+ return value ?? true;
+}
+
+// One MX lookup per domain for a whole check: sign-up asks both whether the
+// domain is disposable and whether it takes mail, under one deadline.
+export function sharedLookups(timeoutMs = DNS_TIMEOUT_MS): DnsLookups {
+ const dns = defaultLookups();
+ const mx = new Map<string, Promise<Array<{ exchange: string }>>>();
+ return {
+ ...dns,
+ resolveMx: (domain) => {
+ const cached = mx.get(domain);
+ if (cached) return cached;
+ const answer = withDeadline(dns.resolveMx(domain), timeoutMs);
+ mx.set(domain, answer);
+ return answer;
+ },
+ };
+}
+
+export async function isDisposableEmail(email: string, options: Options = {}) {
+ if (process.env.AUTH_ALLOW_DISPOSABLE_EMAILS === "true") return false;
+ if (isDisposableEmailDomain(email)) return true;
+ const domain = getEmailDomain(email);
+ if (!domain) return false;
+ const { dns = defaultLookups(), timeoutMs = DNS_TIMEOUT_MS } = options;
+ const mx = await lookUp(() => dns.resolveMx(domain), timeoutMs);
+ return (
+ mx.kind === "records" &&
+ mx.records.some((record) =>
+ DISPOSABLE_MX_HOSTS.has(record.exchange.toLowerCase().replace(/\.$/, "")),
+ )
+ );
+}
+
+// Cloud only: local and self-hosted setups sign up with test or internal
+// domains (example.com publishes a null MX).
+export async function acceptsMail(email: string, options: Options = {}) {
+ if (!environment.isCloud()) return true;
+ const domain = getEmailDomain(email);
+ return Boolean(domain) && (await hasMailServer(domain, options));
+}
+
+// null means DNS gave no definitive answer.
+async function lookUpMailServer(
+ domain: string,
+ dns: DnsLookups,
+ timeoutMs: number,
+): Promise<boolean | null> {
+ const mx = await lookUp(() => dns.resolveMx(domain), timeoutMs);
+ if (mx.kind === "records") {
+ if (mx.records.length === 0) return lookUpAddress(domain, dns, timeoutMs);
+ const isNullMX = mx.records.every(
+ (record) => record.exchange === "" || record.exchange === ".",
+ );
+ return !isNullMX;
+ }
+ if (mx.kind === "no_domain") return false;
+ if (mx.kind === "no_records") return lookUpAddress(domain, dns, timeoutMs);
+ return null;
+}
+
+async function lookUpAddress(
+ domain: string,
+ dns: DnsLookups,
+ timeoutMs: number,
+): Promise<boolean | null> {
+ const [v4, v6] = await Promise.all([
+ lookUp(() => dns.resolve4(domain), timeoutMs),
+ lookUp(() => dns.resolve6(domain), timeoutMs),
+ ]);
+ if (hasRecords(v4) || hasRecords(v6)) return true;
+ const isMissing = (result: LookupResult<unknown>) =>
+ result.kind === "no_domain" ||
+ result.kind === "no_records" ||
+ (result.kind === "records" && result.records.length === 0);
+ return isMissing(v4) && isMissing(v6) ? false : null;
+}
+
+type LookupResult<T> =
+ | { kind: "records"; records: T[] }
+ | { kind: "no_domain" }
+ | { kind: "no_records" }
+ | { kind: "unknown" };
+
+function hasRecords<T>(result: LookupResult<T>) {
+ return result.kind === "records" && result.records.length > 0;
+}
+
+async function lookUp<T>(
+ query: () => Promise<T[]>,
+ timeoutMs: number,
+): Promise<LookupResult<T>> {
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const timedOut = new Promise<LookupResult<T>>((resolve) => {
+ timer = setTimeout(() => resolve({ kind: "unknown" }), timeoutMs);
+ });
+ const answer = query().then(
+ (records): LookupResult<T> => ({
+ kind: "records",
+ records: Array.isArray(records) ? records : [],
+ }),
+ (error: unknown): LookupResult<T> => {
+ const code = (error as { code?: unknown } | null)?.code;
+ if (typeof code !== "string") return { kind: "unknown" };
+ if (NO_DOMAIN_CODES.has(code)) return { kind: "no_domain" };
+ if (NO_RECORDS_CODES.has(code)) return { kind: "no_records" };
+ return { kind: "unknown" };
+ },
+ );
+ try {
+ return await Promise.race([answer, timedOut]);
+ } finally {
+ clearTimeout(timer);
+ }
+}
+
+function withDeadline<T>(query: Promise<T>, timeoutMs: number) {
+ return new Promise<T>((resolve, reject) => {
+ const timer = setTimeout(
+ () =>
+ reject(Object.assign(new Error("DNS timeout"), { code: "ETIMEOUT" })),
+ timeoutMs,
+ );
+ query.then(resolve, reject).finally(() => clearTimeout(timer));
+ });
+}
+
+function defaultLookups(): DnsLookups {
+ const resolver = new Resolver({ timeout: DNS_TIMEOUT_MS, tries: 1 });
+ return {
+ resolveMx: (domain) => resolver.resolveMx(domain),
+ resolve4: (domain) => resolver.resolve4(domain),
+ resolve6: (domain) => resolver.resolve6(domain),
+ };
+}
diff --git a/autogpt_platform/frontend/src/lib/auth/email.ts b/autogpt_platform/frontend/src/lib/auth/email.ts
index 5662c90230..db0635abb6 100644
--- a/autogpt_platform/frontend/src/lib/auth/email.ts
+++ b/autogpt_platform/frontend/src/lib/auth/email.ts
@@ -1,4 +1,5 @@
import { environment } from "@/services/environment";
+import { acceptsMail } from "./email-deliverability";
import { mintServiceToken } from "./service-token";
export type AuthEmailType =
@@ -22,10 +23,20 @@ interface SendAuthEmailArgs {
* configure on either side.
*
* In production a failed mint/send throws so an "email sent" UI can't lie
- * about an undeliverable message. Elsewhere the action link is logged to the
- * server console instead (local backends usually have no Postmark credential).
+ * about it; outside production the action link is logged to the server
+ * console instead (local backends usually have no Postmark credential).
+ * An address whose domain DNS says takes no mail is skipped silently, as if
+ * sent, so it can't bounce. Disposable domains still get their email: older
+ * accounts on one need it to reset a password or move to a real address.
*/
export async function sendAuthEmail({ to, type, url }: SendAuthEmailArgs) {
+ // DNS errors still send.
+ if (!(await acceptsMail(to))) {
+ console.warn("[auth-email] Skipped an address that takes no mail", {
+ type,
+ });
+ return;
+ }
try {
const token = await mintServiceToken("auth-email:send");
const endpoint = `${environment.getAGPTServerBaseUrl()}/api/auth/email/send`;
diff --git a/autogpt_platform/frontend/src/tests/integrations/vitest.setup.tsx b/autogpt_platform/frontend/src/tests/integrations/vitest.setup.tsx
index a031e7213a..b55c834ecd 100644
--- a/autogpt_platform/frontend/src/tests/integrations/vitest.setup.tsx
+++ b/autogpt_platform/frontend/src/tests/integrations/vitest.setup.tsx
@@ -9,14 +9,21 @@ import { resetCheckoutRedirect } from "@/hooks/useCheckoutRedirect";
// (lib/auth/client-ip.ts).
process.env.TRUST_X_FORWARDED_FOR ??= "true";
-// Sign-ups look up their domain's MX (lib/auth/disposable-email.ts); no test
-// does that on the network.
+// Sign-ups and auth emails look up their domain's mail server
+// (lib/auth/email-deliverability.ts); no test does that on the network. Every
+// domain answers with no MX but an A record, so it takes mail.
vi.mock("node:dns/promises", async (importActual) => {
const actual = await importActual<typeof import("node:dns/promises")>();
class Resolver {
async resolveMx() {
return [];
}
+ async resolve4() {
+ return ["192.0.2.1"];
+ }
+ async resolve6() {
+ return [];
+ }
}
return { ...actual, default: { ...actual, Resolver }, Resolver };
});