Multiplayer agent harness for work.
Keep portal sign-out local and stop automatic re-login (#1808)
Sina Matian742ed668c68f
diff --git a/plugins/admin/public/index.html b/plugins/admin/public/index.htmlindex d1144ca88..da6a1bb5b 100644--- a/plugins/admin/public/index.html+++ b/plugins/admin/public/index.html@@ -7247,9 +7247,13 @@if (API_BASE) {try {- await fetch("/auth/logout", { method: "POST" });- } catch {}- location.href = "/";+ const response = await fetch("/auth/logout", { method: "POST", headers: { accept: "application/json" } });+ if (!response.ok) return alert(`Couldn't sign out (${response.status}). You are still signed in.`);+ const result = await response.json();+ location.href = result.redirectTo || "/";+ } catch (e) {+ alert("Couldn't sign out: " + (e && e.message ? e.message : e));+ }return;}refresh();diff --git a/plugins/portal/README.md b/plugins/portal/README.mdindex b6a65c1e0..f117fe120 100644--- a/plugins/portal/README.md+++ b/plugins/portal/README.md@@ -146,9 +146,17 @@ disposable test deployment: the test redeems real links for its configured admin- **Surface isolation.** The private surface hop carries a signed portal identity; core verifiesit independently, so a synthesized cookie alone confers no user authority. User deploymentsstay on a dedicated apps hostname and are never proxied through the portal or admin origin.-- **Stateless logout.** `POST /auth/logout` clears the cookie but can't revoke an already-issued- session before `exp`; the core's `canAdminister` (re-read per request) remains the live admin- revocation path. Slack has no RP-initiated end-session, so SSO re-login is silent.+- **Local logout.** `POST /auth/logout` clears the portal cookies and returns+ `redirectTo`; browsers follow it to `/auth/signed-out`, a terminal page that+ never redirects to the provider. Sign-out is local to this portal: it does not+ contact the OIDC provider or end the provider session, so choosing **Sign in**+ may complete silently through an existing provider session. Sign out of the+ provider directly to end it everywhere. Built-in broker and anonymous logout+ retain their existing flows. Local-development logout also stops on the+ signed-out page until the user chooses **Sign in**.+- **Stateless session limits.** Clearing browser cookies does not revoke a copied+ portal session before `exp`; the core's `canAdminister` (re-read per request)+ remains the live admin revocation path.## Playground modediff --git a/plugins/portal/src/index.ts b/plugins/portal/src/index.tsindex ffc958c81..13c5d8f09 100644--- a/plugins/portal/src/index.ts+++ b/plugins/portal/src/index.ts@@ -1030,6 +1030,27 @@ async function handle(req: IncomingMessage, res: ServerResponse): Promise<void>if (pathname === "/auth/desktop" || pathname === "/auth/desktop/redeem") return desktopLogin(req, res, url);if (pathname === "/auth/invite") return inviteLogin(req, res);if (pathname === "/auth/admin-login") return adminLogin(req, res);+ if (pathname === "/auth/signed-out" && method === "GET") {+ if (currentSession(req))+ return sendHtml(+ res,+ 409,+ connectPage({+ title: "Still signed in",+ body: "Use Sign out from the account menu to end this session.",+ action: `<a class="btn" href="/">Back to the portal</a>`,+ }),+ );+ return sendHtml(+ res,+ 200,+ connectPage({+ title: "Signed out",+ body: "You have signed out of this portal.",+ action: `<a class="btn" href="/auth/login">Sign in</a>`,+ }),+ );+ }if (pathname === "/auth/logout" && method === "POST") {if (!sameOriginRequest(req)) return json(res, 403, { error: "forbidden" });if (AUTH_BROKER_UPSTREAM && url.searchParams.get("everywhere") === "1") {@@ -1057,8 +1078,11 @@ async function handle(req: IncomingMessage, res: ServerResponse): Promise<void>}}const signedOutSession = currentSession(req);+ const redirectTo = !AUTH_BROKER_UPSTREAM && signedOutSession && !signedOutSession.anon ? "/auth/signed-out" : "/";setSession(res, [...(signedOutSession ? loginProviderCookie(signedOutSession.sub) : []),+ clearCookie("portal_impersonate", "/", SECURE_COOKIES),+ clearCookie("portal_trusted_tmp", "/auth/trusted", SECURE_COOKIES),clearCookie("portal_session", "/", SECURE_COOKIES, COOKIE_DOMAIN),clearCookie(FRAME_SESSION_COOKIE, "/", SECURE_COOKIES, COOKIE_DOMAIN),...(COOKIE_DOMAIN@@ -1071,10 +1095,10 @@ async function handle(req: IncomingMessage, res: ServerResponse): Promise<void>: []),]);if (wantsHtml(req)) {- res.writeHead(303, { location: "/", "cache-control": "no-store" });+ res.writeHead(303, { location: redirectTo, "cache-control": "no-store" });return void res.end();}- return json(res, 200, { ok: true });+ return json(res, 200, { ok: true, redirectTo });}if (brokerPath) {diff --git a/plugins/portal/test/local-auth-bypass.test.ts b/plugins/portal/test/local-auth-bypass.test.tsindex 5aaf748ae..57c1bc6e7 100644--- a/plugins/portal/test/local-auth-bypass.test.ts+++ b/plugins/portal/test/local-auth-bypass.test.ts@@ -71,6 +71,14 @@ test("local auth bypass respects logout until explicit login", async () => {});assert.equal(logout.status, 200);assert.match(logout.headers.get("set-cookie") ?? "", /portal_local_logout=1/);+ const destination = ((await logout.json()) as { redirectTo: string }).redirectTo;+ assert.equal(destination, "/auth/signed-out");+ const landing = await fetch(`${base}${destination}`, {+ headers: { cookie: "portal_local_logout=1" },+ redirect: "manual",+ });+ assert.equal(landing.status, 200);+ assert.match(await landing.text(), /You have signed out of this portal/);const loggedOut = await fetch(`${base}/admin/api/me`, {headers: { cookie: "portal_local_logout=1" },diff --git a/plugins/portal/test/logout-routes.test.ts b/plugins/portal/test/logout-routes.test.tsnew file mode 100644index 000000000..6224b716e--- /dev/null+++ b/plugins/portal/test/logout-routes.test.ts@@ -0,0 +1,83 @@+import test from "node:test";+import assert from "node:assert/strict";+import { createServer } from "node:http";+import type { AddressInfo } from "node:net";+import { deriveKey, seal } from "../src/session.ts";++let idpRequests = 0;+const idp = createServer((_req, res) => {+ idpRequests++;+ res.setHeader("content-type", "application/json");+ res.end(JSON.stringify({ issuer, end_session_endpoint: `${issuer}/logout` }));+});+await new Promise<void>((resolve) => idp.listen(0, "127.0.0.1", resolve));+const issuer = `http://127.0.0.1:${(idp.address() as AddressInfo).port}`;+const core = createServer((_req, res) => res.end("{}"));+await new Promise<void>((resolve) => core.listen(0, "127.0.0.1", resolve));+const origin = "http://127.0.0.1:19996";+const secret = "logout-route-fixture-secret-at-least-32-characters";+Object.assign(process.env, {+ NODE_ENV: "test",+ PORTAL_PUBLIC_URL: origin,+ PORTAL_SESSION_SECRET: secret,+ CORE_ORG_ID: "logout",+ CORE_API_URL: `http://127.0.0.1:${(core.address() as AddressInfo).port}`,+ OIDC_ISSUER: issuer,+ OIDC_AUTH_ENDPOINT: `${issuer}/authorize`,+ OIDC_CLIENT_ID: "primary-client",+ PORTAL_LOCAL_AUTH_BYPASS: "0",+ AUTH_BROKER_UPSTREAM: "",+});+const { server } = await import("../src/index.ts");+await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));+const base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;+test.after(() => {+ server.close();+ idp.close();+ core.close();+});+function cookie(extra: Record<string, unknown> = {}) {+ const now = Math.floor(Date.now() / 1000);+ return `portal_session=${seal({ k: "session", sub: "fixture", org: "logout", iat: now, exp: now + 3600, ...extra }, deriveKey(secret, "portal.session.v1"))}`;+}+async function logout(extra: Record<string, unknown> = {}, html = false) {+ return fetch(`${base}/auth/logout?returnTo=https://evil.example`, {+ method: "POST",+ redirect: "manual",+ headers: { origin, cookie: cookie(extra), accept: html ? "text/html" : "application/json" },+ });+}+test("external OIDC sign-out is local: it never contacts the provider and stays signed out until Sign in", async () => {+ const before = idpRequests;+ const response = await logout();+ assert.equal(response.status, 200);+ assert.equal(((await response.json()) as { redirectTo: string }).redirectTo, "/auth/signed-out");+ for (const name of [+ "portal_session",+ "portal_session_x",+ "portal_oidc_tmp",+ "portal_trusted_tmp",+ "portal_impersonate",+ ])+ assert.ok(+ response.headers.getSetCookie().some((value) => value.startsWith(`${name}=;`) && value.includes("Max-Age=0")),+ );+ const html = await logout({}, true);+ assert.equal(html.status, 303);+ assert.equal(html.headers.get("location"), "/auth/signed-out");+ const landing = await fetch(`${base}/auth/signed-out`, { redirect: "manual", headers: { accept: "text/html" } });+ assert.equal(landing.status, 200);+ const body = await landing.text();+ assert.match(body, /You have signed out of this portal/);+ assert.match(body, /href="\/auth\/login"/);+ assert.equal(idpRequests, before);+});+test("anonymous sessions keep the existing root redirect", async () => {+ const result = (await (await logout({ anon: true })).json()) as { redirectTo: string };+ assert.equal(result.redirectTo, "/");+});+test("signed-out landing does not falsely claim an authenticated session ended", async () => {+ const signedIn = await fetch(`${base}/auth/signed-out`, { headers: { cookie: cookie() } });+ assert.equal(signedIn.status, 409);+ assert.match(await signedIn.text(), /Still signed in/);+});diff --git a/plugins/web-ui/src/shell.ts b/plugins/web-ui/src/shell.tsindex 90ddaaff8..92892a0ba 100644--- a/plugins/web-ui/src/shell.ts+++ b/plugins/web-ui/src/shell.ts@@ -261,9 +261,11 @@ export async function signOut(): Promise<void> {return;}let endedSession: boolean;+ let redirectTo = "/";try {const r = await fetch("/auth/logout", { method: "POST", headers: { accept: "application/json" } });endedSession = r.ok;+ if (r.ok) redirectTo = ((await r.json()) as { redirectTo?: string }).redirectTo ?? "/";} catch {endedSession = false;}@@ -272,7 +274,7 @@ export async function signOut(): Promise<void> {return;}clearPortalAttempt();- location.href = "/";+ location.href = redirectTo;}export async function exitImpersonation(): Promise<void> {