Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🥅 Answer email link scanner HEAD requests without rendering
Luke Vella9b9fe0c919c3
diff --git a/apps/web/src/proxy.test.ts b/apps/web/src/proxy.test.tsnew file mode 100644index 000000000..ce91784be--- /dev/null+++ b/apps/web/src/proxy.test.ts@@ -0,0 +1,33 @@+import { NextRequest } from "next/server";+import { describe, expect, it } from "vitest";+import { proxy } from "./proxy";++const request = (method: string, path: string) =>+ new NextRequest(new URL(path, "https://app.rallly.co"), { method });++describe("proxy", () => {+ it.each([+ "/invite/abc123",+ "/invite/abc123/vote",+ "/poll/abc123",+ "/unsubscribe/token",+ ])("answers HEAD %s without rendering", async (path) => {+ const res = await proxy(request("HEAD", path));+ expect(res.status).toBe(200);+ expect(res.headers.get("x-middleware-rewrite")).toBeNull();+ });++ it.each([+ "/poll/abc123/edit",+ "/",+ "/settings/profile",+ ])("rewrites HEAD %s", async (path) => {+ const res = await proxy(request("HEAD", path));+ expect(res.headers.get("x-middleware-rewrite")).not.toBeNull();+ });++ it("rewrites GET /invite/abc123", async () => {+ const res = await proxy(request("GET", "/invite/abc123"));+ expect(res.headers.get("x-middleware-rewrite")).toContain("/invite/abc123");+ });+});diff --git a/apps/web/src/proxy.ts b/apps/web/src/proxy.tsindex cf254874d..a03c614bb 100644--- a/apps/web/src/proxy.ts+++ b/apps/web/src/proxy.ts@@ -45,6 +45,11 @@ async function handleMaintenance(req: NextRequest) {return NextResponse.redirect(new URL(MAINTENANCE_PATH, nextUrl));}+// Email link scanners (Safe Links and similar) send HEAD to every link in an+// invite or notification email, in bursts. Rendering these pages for them+// opens a database connection per request for a response with no body.+const EMAIL_LINK_PATH = /^\/(?:invite|poll|unsubscribe)\/[^/]+(?:\/vote)?\/?$/;+export const proxy = async (req: NextRequest) => {const { nextUrl } = req;const newUrl = nextUrl.clone();@@ -59,6 +64,10 @@ export const proxy = async (req: NextRequest) => {return NextResponse.redirect(new URL("/", nextUrl));}+ if (req.method === "HEAD" && EMAIL_LINK_PATH.test(pathname)) {+ return new NextResponse(null, { status: 200 });+ }+const locale = getLocaleFromRequest(req);newUrl.pathname = `/${locale}${pathname}`;