Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🐛 Reject localhost, dotless hostnames and IPs as the cookie domain (#2941)

Luke Vella4185281978c4
diff --git a/apps/web/package.json b/apps/web/package.json
index a9045275e..91353c940 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -98,6 +98,7 @@
"smoothscroll-polyfill": "^0.4.4",
"superjson": "^2.0.0",
"tailwindcss": "^4.1.18",
+ "tldts": "^6.1.86",
"undici": "^7.29.0",
"zod": "^4.3.6",
"zustand": "^5.0.14",
diff --git a/apps/web/src/env.ts b/apps/web/src/env.ts
index 2b4322d36..a37f5c1c4 100644
--- a/apps/web/src/env.ts
+++ b/apps/web/src/env.ts
@@ -5,6 +5,7 @@
import "server-only";
import { createEnv } from "@t3-oss/env-nextjs";
import * as z from "zod";
+import { cookieDomainSchema } from "@/lib/cookie-domain";
// Base URL fallback on preview deployments, where `NEXT_PUBLIC_BASE_URL` is
// unset. Prefer the stable branch alias (the URL Vercel links to) over the
@@ -206,15 +207,11 @@ export const env = createEnv({
* Domain to attach to server-set cookies (auth session, locale).
* Set to a parent domain prefixed with a leading dot (e.g. `.rallly.co`)
* to make these cookies readable across subdomains. When unset, cookies
- * stay scoped to the apex host of the request.
+ * stay scoped to the exact request host. Must be a registrable DNS
+ * domain: browsers ignore the Domain attribute for localhost, dotless
+ * hostnames, IP addresses, and public suffixes, which breaks sign-in.
*/
- NEXT_PUBLIC_COOKIE_DOMAIN: z
- .string()
- .regex(/^\.?[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*$/, {
- message:
- "must be a hostname or optional leading-dot domain (no protocol/port/path)",
- })
- .optional(),
+ NEXT_PUBLIC_COOKIE_DOMAIN: cookieDomainSchema.optional(),
},
/*
* Due to how Next.js bundles environment variables on Edge and Client,
@@ -290,7 +287,11 @@ export const env = createEnv({
API_BASE_URL: process.env.API_BASE_URL,
NEXT_PUBLIC_TURNSTILE_SITE_KEY: process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY,
NEXT_PUBLIC_CDN_BASE_URL: process.env.NEXT_PUBLIC_CDN_BASE_URL,
- NEXT_PUBLIC_COOKIE_DOMAIN: process.env.NEXT_PUBLIC_COOKIE_DOMAIN,
+ // Empty string means unset: the process env can override an .env file
+ // value but never remove it, so this is the only way a dev server on
+ // plain localhost can neutralize a configured cookie domain.
+ NEXT_PUBLIC_COOKIE_DOMAIN:
+ process.env.NEXT_PUBLIC_COOKIE_DOMAIN || undefined,
},
skipValidation: !!process.env.SKIP_ENV_VALIDATION,
});
diff --git a/apps/web/src/lib/cookie-domain.test.ts b/apps/web/src/lib/cookie-domain.test.ts
new file mode 100644
index 000000000..2e9d08b3f
--- /dev/null
+++ b/apps/web/src/lib/cookie-domain.test.ts
@@ -0,0 +1,62 @@
+import { describe, expect, it } from "vitest";
+
+import { cookieDomainSchema } from "./cookie-domain";
+
+describe("cookieDomainSchema", () => {
+ it.each([
+ ".rallly.co",
+ "rallly.example.com",
+ ".rallly.example.com",
+ "example.com",
+ ])("accepts registrable domain %s", (value) => {
+ expect(cookieDomainSchema.safeParse(value).success).toBe(true);
+ });
+
+ it.each([
+ "https://example.com",
+ "example.com:3000",
+ "example.com/path",
+ ])("rejects non-hostname %s", (value) => {
+ expect(cookieDomainSchema.safeParse(value).success).toBe(false);
+ });
+
+ // Browsers refuse to create a domain cookie for these hosts and silently
+ // store the cookie host-only instead (RFC 6265bis public suffix handling).
+ // The session cookie then collides with hostOnlyCookieCleanup's host-only
+ // deletions and every sign-in destroys its own session.
+ it.each([
+ "localhost",
+ ".localhost",
+ "intranet-host",
+ ])("rejects dotless hostname %s", (value) => {
+ expect(cookieDomainSchema.safeParse(value).success).toBe(false);
+ });
+
+ it.each([
+ "127.0.0.1",
+ "192.168.1.10",
+ ".127.0.0.1",
+ ])("rejects IP address %s", (value) => {
+ expect(cookieDomainSchema.safeParse(value).success).toBe(false);
+ });
+
+ // Browsers also refuse to set a cookie whose Domain is a public suffix:
+ // ignored entirely when it differs from the request host, downgraded to
+ // host-only when it matches. The private section counts too, so a site on
+ // foo.vercel.app cannot share cookies via Domain=.vercel.app.
+ it.each([
+ ".com",
+ "co.uk",
+ ".co.uk",
+ "vercel.app",
+ ])("rejects public suffix %s", (value) => {
+ expect(cookieDomainSchema.safeParse(value).success).toBe(false);
+ });
+
+ // Dotted domains under an unlisted TLD (intranet setups) stay valid.
+ it("accepts a dotted intranet domain", () => {
+ expect(cookieDomainSchema.safeParse(".rallly.corp.internal").success).toBe(
+ true,
+ );
+ });
+});
diff --git a/apps/web/src/lib/cookie-domain.ts b/apps/web/src/lib/cookie-domain.ts
new file mode 100644
index 000000000..c37e80d54
--- /dev/null
+++ b/apps/web/src/lib/cookie-domain.ts
@@ -0,0 +1,26 @@
+import { getDomain } from "tldts";
+import * as z from "zod";
+
+export const cookieDomainSchema = z
+ .string()
+ .regex(/^\.?[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*$/, {
+ message:
+ "must be a hostname or optional leading-dot domain (no protocol/port/path)",
+ })
+ .refine(
+ (value) => {
+ // Browsers only honor a Domain attribute naming a registrable domain.
+ // For anything else (localhost, single-label hostnames, IP addresses,
+ // public suffixes like co.uk or vercel.app) the cookie is either
+ // dropped or silently stored host-only, where it shares a store key
+ // with hostOnlyCookieCleanup's host-only deletions, so every sign-in
+ // deletes the session it just set. getDomain returns null for exactly
+ // this class; unlisted TLDs (intranet domains) still resolve.
+ const host = value.replace(/^\./, "");
+ return getDomain(host, { allowPrivateDomains: true }) !== null;
+ },
+ {
+ message:
+ "must be a registrable domain. Browsers ignore a Domain attribute naming localhost, an IP address, a single-label hostname, or a public suffix (like .com or .vercel.app), which breaks sign-in. Unset it to scope cookies to the request host instead.",
+ },
+ );
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 59dfbe633..cdf645b09 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -97,7 +97,7 @@ importers:
version: 8.1.0([email protected])
'@vercel/analytics':
specifier: ^1.5.0
dayjs:
specifier: ^1.11.13
version: 1.11.19
@@ -136,7 +136,7 @@ importers:
version: 6.0.0(@types/[email protected])([email protected])
next-seo:
specifier: ^6.1.0
pg:
specifier: ^8.17.1
version: 8.18.0
@@ -426,6 +426,9 @@ importers:
tailwindcss:
specifier: ^4.1.18
version: 4.1.18
+ tldts:
+ specifier: ^6.1.86
+ version: 6.1.86
undici:
specifier: ^7.29.0
version: 7.29.0
@@ -18381,7 +18384,7 @@ snapshots:
dependencies:
uncrypto: 0.1.3
optionalDependencies:
react: 19.2.6
@@ -22135,7 +22138,7 @@ snapshots:
react: 19.2.6
react-dom: 19.2.6([email protected])
dependencies:
react: 19.2.6