Email for professionals and teams
fix(security): resolve-check proxied image hosts, proxy on Node, drop remote fonts, rate limit (#220)
Hieu Nguyen (Jack)2280427f0200
diff --git a/src/app/api/remote-images/route.ts b/src/app/api/remote-images/route.tsindex 26b6dc5..63e0878 100644--- a/src/app/api/remote-images/route.ts+++ b/src/app/api/remote-images/route.ts@@ -1,7 +1,7 @@import { requireSessionUser } from "@/lib/api/auth";import { getEnv } from "@/lib/cloudflare";import { fetchRemoteImage, RemoteImageError } from "@/lib/remote-images/fetch";-import { isNodeRuntime } from "@/lib/runtime";+import { allowRemoteImageRequest } from "@/lib/remote-images/rate-limit";const IMAGE_HEADERS = {"Cache-Control": "private, no-store",@@ -14,12 +14,8 @@ export async function GET(request: Request) {const env = getEnv();const auth = await requireSessionUser(env, request);if (auth.error) return auth.error;-- if (isNodeRuntime(env)) {- return Response.json(- { error: "Remote images are unavailable in the self-hosted runtime" },- { status: 503, headers: { "Cache-Control": "no-store" } },- );+ if (!allowRemoteImageRequest(auth.user.id)) {+ return Response.json({ error: "Too many remote image requests" }, { status: 429, headers: { "Cache-Control": "no-store", "Retry-After": "60" } });}const requestUrl = new URL(request.url);diff --git a/src/app/layout.tsx b/src/app/layout.tsxindex 56c06f9..0559653 100644--- a/src/app/layout.tsx+++ b/src/app/layout.tsx@@ -1,7 +1,6 @@import type { Metadata } from "next";import { Geist, Geist_Mono } from "next/font/google";import { Providers } from "@/components/providers";-import { isNodeRuntime } from "@/lib/runtime";import { LanguageProvider } from "@/components/language-provider";import { DEFAULT_LOCALE, getDirection } from "@/lib/i18n/utils";import { getRequestLocale, loadServerMessages } from "@/lib/i18n/server";@@ -53,7 +52,7 @@ export default async function RootLayout({ children }: Readonly<{ children: Reacconst messages = locale === DEFAULT_LOCALE ? undefined : await loadServerMessages(locale);const branding = await getHomeBranding();return (- <html lang={locale} dir={getDirection(locale)} data-remote-image-proxy={isNodeRuntime() ? "disabled" : "enabled"} suppressHydrationWarning>+ <html lang={locale} dir={getDirection(locale)} data-remote-image-proxy="enabled" suppressHydrationWarning><head><script dangerouslySetInnerHTML={{ __html: sidebarBootstrapScript }} /><script dangerouslySetInnerHTML={{ __html: themeBootstrapScript }} />diff --git a/src/lib/remote-images/fetch.ts b/src/lib/remote-images/fetch.tsindex 793cdc1..7028a72 100644--- a/src/lib/remote-images/fetch.ts+++ b/src/lib/remote-images/fetch.ts@@ -1,3 +1,4 @@+import { queryDns } from "@/lib/dns-query";import { isPublicIpv4 } from "@/lib/spam/domain-utils";export const REMOTE_IMAGE_MAX_URL_LENGTH = 4096;@@ -34,6 +35,8 @@ export type RemoteImageResult = {export type RemoteImageFetchOptions = {fetch?: RemoteImageFetch;+ /** Resolves a hostname to its A and AAAA addresses; every one must be public. Defaults to DNS-over-HTTPS. */+ resolveHost?: (hostname: string) => Promise<string[]>;blockedOrigins?: Iterable<string>;timeoutMs?: number;maxBytes?: number;@@ -169,17 +172,45 @@ async function readLimitedBody(response: Response, maxBytes: number): Promise<Uireturn body;}+export function isPublicAddress(address: string): boolean {+ const value = address.trim().toLowerCase();+ if (!value.includes(":")) return isPublicIpv4(value);+ const mapped = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/.exec(value);+ if (mapped) return isPublicIpv4(mapped[1]);+ if (value === "::" || value === "::1") return false;+ // Unique local (fc00::/7), link-local (fe80::/10) and multicast (ff00::/8).+ return !/^(f[cd]|fe[89ab]|ff)/.test(value);+}++async function defaultResolveHost(hostname: string): Promise<string[]> {+ const [v4, v6] = await Promise.all([queryDns(hostname, "A"), queryDns(hostname, "AAAA")]);+ return [...v4, ...v6];+}++/** A public-looking name can still resolve to a private address, so check what it resolves to. */+async function assertPublicHost(resolve: (hostname: string) => Promise<string[]>, hostname: string): Promise<void> {+ let addresses: string[];+ try {+ addresses = await resolve(hostname);+ } catch {+ throw new RemoteImageError("Could not verify remote image host", 502, "upstream_error");+ }+ if (!addresses.length || addresses.some((address) => !isPublicAddress(address))) invalidUrl("Remote image host is not public");+}+export async function fetchRemoteImage(value: string, options: RemoteImageFetchOptions = {}): Promise<RemoteImageResult> {const fetchImpl = options.fetch ?? fetch;const blockedOrigins = [...(options.blockedOrigins ?? [])];const maxBytes = options.maxBytes ?? REMOTE_IMAGE_MAX_BYTES;const maxRedirects = options.maxRedirects ?? REMOTE_IMAGE_MAX_REDIRECTS;+ const resolveHost = options.resolveHost ?? defaultResolveHost;let current = parseRemoteImageUrl(value, blockedOrigins);const controller = new AbortController();const timeout = setTimeout(() => controller.abort(), options.timeoutMs ?? REMOTE_IMAGE_TIMEOUT_MS);try {for (let redirects = 0; ; redirects += 1) {+ await assertPublicHost(resolveHost, current.hostname);let response: Response;try {response = await fetchImpl(current.href, {diff --git a/src/lib/remote-images/rate-limit.ts b/src/lib/remote-images/rate-limit.tsnew file mode 100644index 0000000..d81d09c--- /dev/null+++ b/src/lib/remote-images/rate-limit.ts@@ -0,0 +1,24 @@+const WINDOW_MS = 60_000;+const MAX_REQUESTS = 300;+const MAX_TRACKED = 5_000;++const windows = new Map<string, { start: number; count: number }>();++/**+ * Fixed-window limit per user on proxied images, so a message with thousands of unique image URLs+ * cannot turn the proxy into a request amplifier. State is per isolate/process, which is enough to+ * bound a single noisy client.+ */+export function allowRemoteImageRequest(userId: string, now = Date.now()): boolean {+ const current = windows.get(userId);+ if (!current || now - current.start >= WINDOW_MS) {+ if (windows.size >= MAX_TRACKED) {+ for (const [key, entry] of windows) if (now - entry.start >= WINDOW_MS) windows.delete(key);+ if (windows.size >= MAX_TRACKED) windows.clear();+ }+ windows.set(userId, { start: now, count: 1 });+ return true;+ }+ current.count += 1;+ return current.count <= MAX_REQUESTS;+}diff --git a/src/lib/remote-images/reader.ts b/src/lib/remote-images/reader.tsindex c097648..b2d712d 100644--- a/src/lib/remote-images/reader.ts+++ b/src/lib/remote-images/reader.ts@@ -48,7 +48,11 @@ function proxyStrings(node: css.CssNode) {function proxyCss(ast: css.CssNode, proxyNames: Map<string, string>): string {css.walk(ast, {enter(node, item, list) {- if (node.type === "Declaration" && css.ident.decode(node.property).toLowerCase() === "src") return css.walk.skip;+ // Remote fonts cannot go through the image proxy; drop the source so they never load.+ if (node.type === "Declaration" && css.ident.decode(node.property).toLowerCase() === "src") {+ if (item && list) list.remove(item);+ return css.walk.skip;+ }if (node.type !== "Declaration") return;const property = css.ident.decode(node.property);if (property.startsWith("--")) {@@ -76,9 +80,14 @@ function proxyCss(ast: css.CssNode, proxyNames: Map<string, string>): string {return css.walk.skip;},});- css.walk(ast, node => {- if (node.type === "Declaration" && css.ident.decode(node.property).toLowerCase() === "src") return css.walk.skip;- if (node.type === "Url") node.value = remoteImageUrl(node.value);+ css.walk(ast, {+ enter(node, item, list) {+ if (node.type === "Declaration" && css.ident.decode(node.property).toLowerCase() === "src") {+ if (item && list) list.remove(item);+ return css.walk.skip;+ }+ if (node.type === "Url") node.value = remoteImageUrl(node.value);+ },});return css.generate(ast);}diff --git a/tests/remote-images-fetch.test.mjs b/tests/remote-images-fetch.test.mjsindex 4e3f940..583baad 100644--- a/tests/remote-images-fetch.test.mjs+++ b/tests/remote-images-fetch.test.mjs@@ -68,6 +68,7 @@ test("the route requires a session before any outbound fetch", async (t) => {test("a valid image ignores restrictive upstream response headers", async () => {const calls = [];const result = await fetchRemoteImage("https://images.example/photo.png", {+ resolveHost: async () => ["93.184.216.34"],fetch: async (url, init) => {calls.push({ url: url.href, init });return new Response(png, { headers: { "Content-Type": "image/png", "Cross-Origin-Resource-Policy": "same-origin" } });@@ -82,13 +83,20 @@ test("a valid image ignores restrictive upstream response headers", async () =>});test("the authenticated route replaces upstream CORP with locally usable safe headers", async (t) => {- t.mock.method(globalThis, "fetch", async () => new Response(png, {- headers: {- "Content-Type": "image/png",- "Cross-Origin-Resource-Policy": "same-origin",- "Set-Cookie": "upstream=secret",- },- }));+ t.mock.method(globalThis, "fetch", async (input) => {+ // The host check resolves the name over DoH first; answer it with a public address.+ if (String(input).includes("cloudflare-dns.com")) {+ const type = String(input).includes("type=AAAA") ? 28 : 1;+ return Response.json({ Status: 0, Answer: type === 1 ? [{ name: "images.example", type, TTL: 60, data: "93.184.216.34" }] : [] });+ }+ return new Response(png, {+ headers: {+ "Content-Type": "image/png",+ "Cross-Origin-Resource-Policy": "same-origin",+ "Set-Cookie": "upstream=secret",+ },+ });+ });const response = await GET(new Request("https://mail.example/api/remote-images?url=https%3A%2F%2Fimages.example%2Fphoto.png"));assert.equal(response.status, 200);assert.deepEqual(new Uint8Array(await response.arrayBuffer()), png);@@ -120,6 +128,7 @@ test("every redirect is validated and private redirect loops stop before anotherlet calls = 0;await assert.rejects(fetchRemoteImage("https://images.example/start", {+ resolveHost: async () => ["93.184.216.34"],fetch: async () => {calls += 1;return new Response(null, { status: 302, headers: { Location: "http://127.0.0.1/private.png" } });@@ -134,6 +143,7 @@ test("redirect count is capped", async () => {let calls = 0;await assert.rejects(fetchRemoteImage("https://images.example/loop", {+ resolveHost: async () => ["93.184.216.34"],maxRedirects: 3,fetch: async (url) => {calls += 1;@@ -156,6 +166,7 @@ test("streaming size cap cancels a body that exceeds the limit", async () => {});await assert.rejects(fetchRemoteImage("https://images.example/large.png", {+ resolveHost: async () => ["93.184.216.34"],maxBytes: png.byteLength,fetch: async () => new Response(body, { headers: { "Content-Type": "image/png" } }),}),@@ -167,12 +178,14 @@ test("streaming size cap cancels a body that exceeds the limit", async () => {test("non-image MIME and spoofed image signatures are rejected", async () => {await assert.rejects(fetchRemoteImage("https://images.example/page", {+ resolveHost: async () => ["93.184.216.34"],fetch: async () => new Response("<html></html>", { headers: { "Content-Type": "text/html" } }),}),(error) => error.code === "unsupported_type" && error.status === 415,);await assert.rejects(fetchRemoteImage("https://images.example/fake.png", {+ resolveHost: async () => ["93.184.216.34"],fetch: async () => new Response("<html></html>", { headers: { "Content-Type": "image/png" } }),}),(error) => error.code === "invalid_signature" && error.status === 415,@@ -182,6 +195,7 @@ test("non-image MIME and spoofed image signatures are rejected", async () => {test("the total request timeout aborts an in-flight fetch", async () => {await assert.rejects(fetchRemoteImage("https://images.example/slow.png", {+ resolveHost: async () => ["93.184.216.34"],timeoutMs: 10,fetch: async (_url, init) => new Promise((_resolve, reject) => {init.signal.addEventListener("abort", () => reject(init.signal.reason), { once: true });@@ -194,6 +208,7 @@ test("the total request timeout aborts an in-flight fetch", async () => {test("the total request timeout also covers a body that stalls after headers", async () => {await assert.rejects(fetchRemoteImage("https://images.example/slow-body.png", {+ resolveHost: async () => ["93.184.216.34"],timeoutMs: 10,fetch: async (_url, init) => new Response(new ReadableStream({start(controller) {@@ -227,3 +242,15 @@ test("invalid query targets are rejected before fetch", () => {}assert.throws(() => parseRemoteImageUrl(`https://example.com/${"a".repeat(4096)}`), /Invalid remote image URL/);});++test("a public name that resolves to a private address is refused, including after a redirect", async () => {+ for (const addresses of [["127.0.0.1"], ["10.0.0.5"], ["93.184.216.34", "169.254.169.254"], ["::1"], ["fd00::1"], ["::ffff:192.168.1.1"], []]) {+ await assert.rejects(fetchRemoteImage("https://images.example/a.png", { resolveHost: async () => addresses, fetch: async () => new Response(png, { headers: { "content-type": "image/png" } }) }), (error) => error instanceof RemoteImageError);+ }+ const seen = [];+ await assert.rejects(fetchRemoteImage("https://images.example/start", {+ resolveHost: async (host) => { seen.push(host); return host === "images.example" ? ["93.184.216.34"] : ["10.0.0.1"]; },+ fetch: async () => new Response(null, { status: 302, headers: { location: "https://rebind.example/a.png" } }),+ }), (error) => error instanceof RemoteImageError);+ assert.deepEqual(seen, ["images.example", "rebind.example"]);+});diff --git a/tests/remote-images-route.test.mjs b/tests/remote-images-route.test.mjsindex 9e1790b..9dbde4e 100644--- a/tests/remote-images-route.test.mjs+++ b/tests/remote-images-route.test.mjs@@ -20,14 +20,23 @@ await build({ entryPoints: [join(project, "src/app/api/remote-images/route.ts")]const { GET } = await import(pathToFileURL(join(temporary, "route.mjs")).href);const request = () => new Request("https://mail.example/api/remote-images?url=https%3A%2F%2Fcdn.example%2Flogo.png");-test("unauthenticated and self-hosted requests never make outbound proxy requests", async () => {+test("unauthenticated requests never make outbound proxy requests", async () => {globalThis.proxyEnv = {};globalThis.proxyFetch = () => { throw new Error("Unexpected outbound request"); };globalThis.proxyAuth = { error: Response.json({ error: "Unauthorized" }, { status: 401 }) };assert.equal((await GET(request())).status, 401);- globalThis.proxyAuth = { user: { id: "user" }, error: null };+});++test("the self-hosted runtime proxies images too, and one user is rate limited", async () => {+ globalThis.proxyAuth = { user: { id: "node-user" }, error: null };globalThis.proxyEnv = { MAILFLARE_RUNTIME: "node" };- assert.equal((await GET(request())).status, 503);+ globalThis.proxyFetch = async () => ({ body: new Uint8Array([1]), contentType: "image/png" });+ assert.equal((await GET(request())).status, 200);+ let limited = 0;+ for (let index = 0; index < 400; index += 1) if ((await GET(request())).status === 429) limited += 1;+ assert.ok(limited > 0);+ globalThis.proxyAuth = { user: { id: "someone-else" }, error: null };+ assert.equal((await GET(request())).status, 200);});test("authenticated Worker responses use private image headers and block the app origin", async () => {