Multiplayer agent harness for work.
refactor: derive the apps cookie domain from the portal host, not a suffix list
Josh France2b2c4d424463
diff --git a/plugins/portal/README.md b/plugins/portal/README.mdindex 889db26b8..3c6752fbc 100644--- a/plugins/portal/README.md+++ b/plugins/portal/README.md@@ -105,9 +105,11 @@ would hand anonymous sessions to surfaces that never see the `anon` flag. Thedeployment proxy (`/d/<app>/`), on by default for signed-in portals, turnsitself off under the playground, and anonymous sessions are refused it atrequest time too. Outside the playground, `PORTAL_APPS_DOMAIN` defaults to-`DEPLOY_APPS_DOMAIN` and `PORTAL_COOKIE_DOMAIN` to the parent domain the portal-host shares with the apps domain, so one core-side variable configures both-processes. Anonymous sessions are also refused the `/connect/*` and+`DEPLOY_APPS_DOMAIN`, and `PORTAL_COOKIE_DOMAIN` to the portal host itself when+the apps domain sits directly under it (`apps.<portal host>`), so one core-side+variable configures both processes. Any other layout needs an explicit+`PORTAL_COOKIE_DOMAIN` — deriving a shared parent by guesswork risks landing on+a public suffix browsers refuse. Anonymous sessions are also refused the `/connect/*` and`/drop/*` flows, so a visitor can't attach real OAuth tokens or dropped secretsto a throwaway principal that a cleared cookie orphans.diff --git a/plugins/portal/src/index.ts b/plugins/portal/src/index.tsindex 9d02ae5d7..58a31fa44 100644--- a/plugins/portal/src/index.ts+++ b/plugins/portal/src/index.ts@@ -59,7 +59,7 @@ const SESSION_RENEW_AFTER_S = Math.floor(SESSION_TTL_S / 2);const APPS_DOMAIN = process.env.PORTAL_APPS_DOMAIN || process.env.DEPLOY_APPS_DOMAIN || undefined;const COOKIE_DOMAIN =process.env.PORTAL_COOKIE_DOMAIN ||- (APPS_DOMAIN ? commonParentDomain(hostOf(process.env.PORTAL_PUBLIC_URL ?? ""), APPS_DOMAIN) : undefined);+ (APPS_DOMAIN ? derivedCookieDomain(hostOf(process.env.PORTAL_PUBLIC_URL ?? ""), APPS_DOMAIN) : undefined);const IS_PROD = process.env.NODE_ENV === "production";const SECURE_COOKIES = PUBLIC_URL.startsWith("https://");const ORIGIN = (() => {@@ -303,37 +303,9 @@ function hostOf(raw: string): string {}}-function isMultiPartPublicSuffix(domain: string): boolean {- return [- "co.uk",- "org.uk",- "gov.uk",- "ac.uk",- "com.au",- "net.au",- "org.au",- "co.nz",- "co.jp",- "or.jp",- "ne.jp",- "com.br",- "com.mx",- "co.in",- "co.za",- "com.sg",- "com.hk",- "com.tw",- ].includes(domain);-}--export function commonParentDomain(a: string, b: string): string | undefined {- const as = a.toLowerCase().split(".").filter(Boolean);- const bs = b.toLowerCase().split(".").filter(Boolean);- let n = 0;- while (n < as.length && n < bs.length && as[as.length - 1 - n] === bs[bs.length - 1 - n]) n++;- if (n < 2) return undefined;- const parent = as.slice(as.length - n).join(".");- return isMultiPartPublicSuffix(parent) ? undefined : parent;+export function derivedCookieDomain(portalHost: string, appsDomain: string): string | undefined {+ const host = portalHost.toLowerCase();+ return host.includes(".") && appsDomain.toLowerCase().endsWith(`.${host}`) ? host : undefined;}export function hostIsWithinDomain(host: string, domain: string): boolean {@@ -1281,7 +1253,7 @@ export function bootChecks(): void {}if (APPS_DOMAIN && !COOKIE_DOMAIN) {problems.push(- `the apps domain (${APPS_DOMAIN}) shares no parent domain with the portal host, so no session cookie can cover both — set PORTAL_COOKIE_DOMAIN explicitly or serve the portal and apps under one parent domain (app returnTo without a domain-wide session cookie loops sign-in forever)`,+ `the apps domain (${APPS_DOMAIN}) is not a subdomain of the portal host, so the cookie domain cannot be derived — set PORTAL_COOKIE_DOMAIN to the parent domain covering both (app returnTo without a domain-wide session cookie loops sign-in forever)`,);}if (COOKIE_DOMAIN && !hostIsWithinDomain(hostOf(PUBLIC_URL), COOKIE_DOMAIN)) {diff --git a/plugins/portal/test/apps-domain-defaults.test.ts b/plugins/portal/test/apps-domain-defaults.test.tsindex ee7983608..e2fc0d53a 100644--- a/plugins/portal/test/apps-domain-defaults.test.ts+++ b/plugins/portal/test/apps-domain-defaults.test.ts@@ -21,24 +21,32 @@ delete process.env.PORTAL_APPS_DOMAIN;delete process.env.PORTAL_COOKIE_DOMAIN;process.env.DEPLOY_APPS_DOMAIN = "apps.qm.example.com";-const { commonParentDomain, bootChecks } = await import("../src/index.ts");+const { derivedCookieDomain, bootChecks } = await import("../src/index.ts");test.after(() => {upstream.close();});-test("commonParentDomain finds the deepest shared dot-suffix, never a bare TLD", () => {- assert.equal(commonParentDomain("qm.example.com", "apps.qm.example.com"), "qm.example.com");- assert.equal(commonParentDomain("portal.example.com", "apps.example.com"), "example.com");- assert.equal(commonParentDomain("example.com", "apps.example.com"), "example.com");- assert.equal(commonParentDomain("a.example.com", "b.other.net"), undefined);- assert.equal(commonParentDomain("a.example.com", "b.acme.com"), undefined, "a bare TLD is not a cookie domain");- assert.equal(commonParentDomain("", "apps.example.com"), undefined);+test("the cookie domain derives only when the apps domain sits under the portal host", () => {+ assert.equal(derivedCookieDomain("qm.example.com", "apps.qm.example.com"), "qm.example.com");+ assert.equal(derivedCookieDomain("Example.com", "APPS.example.COM"), "example.com");assert.equal(- commonParentDomain("portal.foo.co.uk", "apps.bar.co.uk"),+ derivedCookieDomain("portal.example.com", "apps.example.com"),undefined,- "a shared public suffix like co.uk is not a cookie domain browsers accept",+ "sibling layouts need an explicit PORTAL_COOKIE_DOMAIN — guessing a shared parent risks landing on a public suffix",);+ assert.equal(+ derivedCookieDomain("portal.foo.co.uk", "apps.bar.co.uk"),+ undefined,+ "no public-suffix list needed: only the portal host itself, a domain the operator demonstrably controls, is derived",+ );+ assert.equal(+ derivedCookieDomain("localhost", "apps.localhost"),+ undefined,+ "a single-label host is never a cookie domain",+ );+ assert.equal(derivedCookieDomain("", "apps.example.com"), undefined);+ assert.equal(derivedCookieDomain("example.com", "appsXexample.com"), undefined, "dot-boundary required");});test("DEPLOY_APPS_DOMAIN alone yields a working portal apps setup — cookie domain derived, boot clean", () => {@@ -53,5 +61,5 @@ test("an apps domain that shares no parent with the portal host refuses to bootencoding: "utf8",});assert.notEqual(r.status, 0);- assert.match(r.stderr, /shares no parent domain/);+ assert.match(r.stderr, /not a subdomain of the portal host/);});