🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(auth): add NEXTAUTH_COOKIE_NAME_SUFFIX to namespace auth cookies across instances (#15094)
Max Deichmann1378523bea3a
diff --git a/web/src/__tests__/server/unit/cookieName.servertest.ts b/web/src/__tests__/server/unit/cookieName.servertest.tsnew file mode 100644index 0000000000..50f23b2d21--- /dev/null+++ b/web/src/__tests__/server/unit/cookieName.servertest.ts@@ -0,0 +1,76 @@+import { describe, it, expect, beforeEach, vi } from "vitest";++// getCookieName / shouldSecureCookies read these off `env` at call time, so the+// tests mutate mockEnv.env between cases.+const mockEnv = vi.hoisted(() => ({+ env: {+ NEXTAUTH_URL: "https://example.com",+ NEXTAUTH_COOKIE_DOMAIN: undefined as string | undefined,+ NEXT_PUBLIC_BASE_PATH: "",+ NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: undefined as string | undefined,+ NEXTAUTH_COOKIE_NAME_SUFFIX: undefined as string | undefined,+ VERCEL: undefined as string | undefined,+ },+}));++vi.mock("@/src/env.mjs", () => mockEnv);++import { getCookieName } from "@/src/server/utils/cookies";++const BASE = "next-auth.session-token";+// Every region accepted by env.mjs's NEXT_PUBLIC_LANGFUSE_CLOUD_REGION enum.+const CLOUD_REGIONS = ["US", "EU", "STAGING", "DEV", "HIPAA", "JP"] as const;++describe("getCookieName", () => {+ beforeEach(() => {+ mockEnv.env.NEXTAUTH_URL = "https://example.com";+ mockEnv.env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;+ mockEnv.env.NEXTAUTH_COOKIE_NAME_SUFFIX = undefined;+ });++ it("no region and no suffix -> bare secure name (unchanged self-hosted default)", () => {+ expect(getCookieName(BASE)).toBe(`__Secure-${BASE}`);+ });++ it.each(CLOUD_REGIONS)(+ "region %s -> region suffix (unchanged cloud behavior)",+ (region) => {+ mockEnv.env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = region;+ expect(getCookieName(BASE)).toBe(`__Secure-${BASE}.${region}`);+ },+ );++ it("suffix with no region -> suffix applied (new self-hosted capability)", () => {+ mockEnv.env.NEXTAUTH_COOKIE_NAME_SUFFIX = "pr-1234";+ expect(getCookieName(BASE)).toBe(`__Secure-${BASE}.pr-1234`);+ });++ // The production safety guarantee: a configured region ALWAYS wins, so setting+ // NEXTAUTH_COOKIE_NAME_SUFFIX on a cloud deployment (e.g. by config mistake)+ // can never change its cookie names -> existing sessions are never invalidated.+ it.each(CLOUD_REGIONS)(+ "region %s takes precedence over an also-set suffix (never alters cloud cookies)",+ (region) => {+ mockEnv.env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = region;+ mockEnv.env.NEXTAUTH_COOKIE_NAME_SUFFIX = "pr-1234";+ expect(getCookieName(BASE)).toBe(`__Secure-${BASE}.${region}`);+ },+ );++ it("non-https NEXTAUTH_URL drops the __Secure- prefix but keeps the suffix", () => {+ mockEnv.env.NEXTAUTH_URL = "http://localhost:3000";+ mockEnv.env.NEXTAUTH_COOKIE_NAME_SUFFIX = "pr-1234";+ expect(getCookieName(BASE)).toBe(`${BASE}.pr-1234`);+ });++ it("applies to every auth cookie name it is given", () => {+ mockEnv.env.NEXTAUTH_COOKIE_NAME_SUFFIX = "pr-1234";+ for (const n of [+ "next-auth.session-token",+ "next-auth.csrf-token",+ "next-auth.callback-url",+ ]) {+ expect(getCookieName(n)).toBe(`__Secure-${n}.pr-1234`);+ }+ });+});diff --git a/web/src/env.mjs b/web/src/env.mjsindex c023feef47..6ddb882cc1 100644--- a/web/src/env.mjs+++ b/web/src/env.mjs@@ -73,6 +73,23 @@ export const env = createEnv({: [],),NEXTAUTH_COOKIE_DOMAIN: z.string().optional(),+ // Optional suffix appended to the auth cookie NAMES (e.g. ".pr-1234" ->+ // "__Secure-next-auth.session-token.pr-1234"). Lets multiple Langfuse+ // instances that share a parent cookie domain (preview/review environments)+ // avoid reading each other's session cookie, which — encrypted with a+ // different NEXTAUTH_SECRET — fails to decrypt and wedges login. It is the+ // self-hosted-friendly equivalent of the per-region suffix Langfuse Cloud+ // derives from NEXT_PUBLIC_LANGFUSE_CLOUD_REGION, without enabling cloud mode.+ // SAFETY: the cloud region ALWAYS takes precedence over this (see+ // getCookieName), so it can never alter cookie names on a region deployment+ // even if set by mistake; it only takes effect when no region is configured.+ NEXTAUTH_COOKIE_NAME_SUFFIX: z+ .string()+ .regex(+ /^[a-zA-Z0-9_-]+$/,+ "NEXTAUTH_COOKIE_NAME_SUFFIX may only contain letters, numbers, hyphens, and underscores",+ )+ .optional(),LANGFUSE_TEAM_SLACK_WEBHOOK: z.url().optional(),LANGFUSE_NEW_USER_SIGNUP_WEBHOOK: z.url().optional(),LANGFUSE_ADMIN_ACCESS_WEBHOOK: z.url().optional(),@@ -528,6 +545,7 @@ export const env = createEnv({NEXT_PUBLIC_BUILD_ID: process.env.NEXT_PUBLIC_BUILD_ID,NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,NEXTAUTH_COOKIE_DOMAIN: process.env.NEXTAUTH_COOKIE_DOMAIN,+ NEXTAUTH_COOKIE_NAME_SUFFIX: process.env.NEXTAUTH_COOKIE_NAME_SUFFIX,NEXTAUTH_URL: process.env.NEXTAUTH_URL,LANGFUSE_MCP_ALLOWED_HOSTS: process.env.LANGFUSE_MCP_ALLOWED_HOSTS,NEXT_PUBLIC_LANGFUSE_CLOUD_REGION:diff --git a/web/src/server/utils/cookies.ts b/web/src/server/utils/cookies.tsindex f486167c7b..116a2f3955 100644--- a/web/src/server/utils/cookies.ts+++ b/web/src/server/utils/cookies.ts@@ -24,9 +24,19 @@ export const getCookieName = (name: string) =>[shouldSecureCookies() ? "__Secure-" : "",name,+ // Namespaces the cookie NAME so instances that share a parent cookie domain+ // never read each other's session cookie (a foreign one, encrypted with a+ // different NEXTAUTH_SECRET, fails to decrypt -> JWT_SESSION_ERROR -> login+ // loop). The Langfuse Cloud region ALWAYS takes precedence, so any deployment+ // that sets NEXT_PUBLIC_LANGFUSE_CLOUD_REGION (US/EU/STAGING/HIPAA/JP) keeps+ // byte-identical cookie names — NEXTAUTH_COOKIE_NAME_SUFFIX can never change+ // them, even if it is also set by mistake. The suffix applies only to+ // self-hosted deployments with no region (e.g. per-PR preview environments).env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION? `.${env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION}`- : "",+ : env.NEXTAUTH_COOKIE_NAME_SUFFIX+ ? `.${env.NEXTAUTH_COOKIE_NAME_SUFFIX}`+ : "",].join("");/** ProjectCookie carries the server-stamped origin and project id of a user's most recent project. */