Email for professionals and teams
fix(security): resolve webhook hosts and refuse private addresses (#224)
Hieu Nguyen (Jack)0f40e2e3d089
diff --git a/src/lib/email/webhooks.ts b/src/lib/email/webhooks.tsindex 5af496b..e598598 100644--- a/src/lib/email/webhooks.ts+++ b/src/lib/email/webhooks.ts@@ -3,7 +3,7 @@ import { getDb } from "@/db";import type { AppDatabase } from "@/db";import { webhookDeliveries, webhooks } from "@/db/schema";import { newId } from "@/lib/ids";-import { isPublicHttpsUrl, PUBLIC_HTTPS_URL_MESSAGE } from "@/lib/security/public-url";+import { assertResolvesPublic, isPublicHttpsUrl, PUBLIC_HTTPS_URL_MESSAGE } from "@/lib/security/public-url";export type WebhookEventType = "message.inbound" | "message.outbound" | "message.failed";@@ -101,6 +101,7 @@ async function attemptDelivery(try {// Hooks saved before URL validation existed must not become an SSRF path.if (!isPublicHttpsUrl(hook.url)) throw new Error(PUBLIC_HTTPS_URL_MESSAGE);+ await assertResolvesPublic(new URL(hook.url).hostname);const signature = await signPayload(hook.secret, delivery.payload);const res = await fetch(hook.url, {method: "POST",diff --git a/src/lib/security/public-url.ts b/src/lib/security/public-url.tsindex a9de3bf..75a9182 100644--- a/src/lib/security/public-url.ts+++ b/src/lib/security/public-url.ts@@ -1,4 +1,5 @@-import { isForbiddenHostname, isIpLiteral } from "@/lib/remote-images/fetch";+import { queryDns } from "@/lib/dns-query";+import { isForbiddenHostname, isIpLiteral, isPublicAddress } from "@/lib/remote-images/fetch";/*** Text-level check that a URL we are asked to call points at a public HTTPS host. It cannot see@@ -14,3 +15,14 @@ export function isPublicHttpsUrl(value: string): boolean {}export const PUBLIC_HTTPS_URL_MESSAGE = "Webhook URLs must use https:// and point at a public host";++/**+ * A public name can still point at a private address. Resolve it (DNS-over-HTTPS) and require every+ * address to be public. There is a short gap between this check and the request itself, which is why+ * redirects stay disabled at the call site.+ */+export async function assertResolvesPublic(hostname: string): Promise<void> {+ const [v4, v6] = await Promise.all([queryDns(hostname, "A"), queryDns(hostname, "AAAA")]);+ const addresses = [...v4, ...v6];+ if (!addresses.length || addresses.some((address) => !isPublicAddress(address))) throw new Error(PUBLIC_HTTPS_URL_MESSAGE);+}