Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Always issue the framed portal session cookie (#1617)

Tom Firthbbc3e60b497c
diff --git a/plugins/portal/README.md b/plugins/portal/README.md
index 02539de52..b6a65c1e0 100644
--- a/plugins/portal/README.md
+++ b/plugins/portal/README.md
@@ -284,8 +284,10 @@ The link is available without a remembered browser preference.
The proxy also signs the original authenticated subject as `authenticatedAs`. Core verifies that it still belongs to the canonical person, rejecting stale claims after unlinking. Trusted-entry failures offer retry of that provider without an alternate email sign-in link; invitation authentication remains unchanged.
-Framed app sessions require `PORTAL_FRAME_SESSION_ENABLED=1`, which defaults off.
-Enable it only after every serving core and rollback candidate strips
-`portal_session_x` before forwarding requests to deployed apps. Older cores can
-forward that cookie's session bearer to app code. Until then, normal portal
-sessions remain available and login or session refresh clears framed cookies.
+The portal always issues `portal_session_x`, a `SameSite=None` twin of the
+session cookie that lets opted-in apps be framed by named origins. The cores
+serving the portal must include `f75cfd1c` (#1503), which strips
+`portal_session_x` before forwarding requests to deployed apps. Once this portal
+is deployed, do not roll core back below `f75cfd1c` on its own: an older core
+forwards that cookie's session bearer to app code, and cookies already issued
+stay in browsers until they expire (`PORTAL_SESSION_TTL_S`, 7 days by default).
diff --git a/plugins/portal/src/index.ts b/plugins/portal/src/index.ts
index a1f41c1d9..4255ed0c1 100644
--- a/plugins/portal/src/index.ts
+++ b/plugins/portal/src/index.ts
@@ -860,10 +860,7 @@ function sessionCookieSet(value: string, sub: string): string[] {
secure: SECURE_COOKIES,
...(COOKIE_DOMAIN ? { domain: COOKIE_DOMAIN } : {}),
};
- return [
- ...sessionCookieHeaders(value, attrs, process.env.PORTAL_FRAME_SESSION_ENABLED === "1"),
- ...loginProviderCookie(sub),
- ];
+ return [...sessionCookieHeaders(value, attrs), ...loginProviderCookie(sub)];
}
function setSession(res: ServerResponse, headers: string[]): void {
diff --git a/plugins/portal/src/session.ts b/plugins/portal/src/session.ts
index 19570ef1b..654e36565 100644
--- a/plugins/portal/src/session.ts
+++ b/plugins/portal/src/session.ts
@@ -136,12 +136,10 @@ export function clearCookie(name: string, path: string, secure: boolean, domain?
return parts.join("; ");
}
-export function sessionCookieHeaders(value: string, attrs: CookieOpts, framed = false): string[] {
+export function sessionCookieHeaders(value: string, attrs: CookieOpts): string[] {
return [
setCookie("portal_session", value, attrs),
- framed
- ? setCookie("portal_session_x", value, { ...attrs, sameSite: "None" })
- : clearCookie("portal_session_x", attrs.path ?? "/", attrs.secure, attrs.domain),
+ setCookie("portal_session_x", value, { ...attrs, sameSite: "None" }),
...(attrs.domain
? [
clearCookie("portal_session", attrs.path ?? "/", attrs.secure),
diff --git a/plugins/portal/test/local-auth-bypass.test.ts b/plugins/portal/test/local-auth-bypass.test.ts
index e84b54177..5aaf748ae 100644
--- a/plugins/portal/test/local-auth-bypass.test.ts
+++ b/plugins/portal/test/local-auth-bypass.test.ts
@@ -39,6 +39,12 @@ test("local auth bypass signs in loopback portal requests without OIDC", async (
assert.equal(login.status, 302);
assert.equal(login.headers.get("location"), "/admin/");
assert.match(login.headers.get("set-cookie") ?? "", /portal_session=[^,]*Max-Age=604800\b/);
+ const session = login.headers.getSetCookie().find((cookie) => cookie.startsWith("portal_session="));
+ const twin = login.headers.getSetCookie().find((cookie) => cookie.startsWith("portal_session_x="));
+ assert.ok(session && twin);
+ assert.equal(twin.split(";")[0], session.split(";")[0]!.replace("portal_session=", "portal_session_x="));
+ assert.match(twin, /SameSite=Lax/);
+ assert.doesNotMatch(twin, /Secure/);
const admin = await fetch(`${base}/admin/api/me`);
assert.equal(admin.status, 200);
diff --git a/plugins/portal/test/router.test.ts b/plugins/portal/test/router.test.ts
index 546d5356a..7053e222d 100644
--- a/plugins/portal/test/router.test.ts
+++ b/plugins/portal/test/router.test.ts
@@ -621,6 +621,10 @@ test("sliding renewal: a fresh session is NOT re-stamped, an aged one is re-issu
assert.match(setCookie, new RegExp(`Max-Age=${SESSION_TTL_S}\\b`), "the renewed cookie carries a full TTL");
const claims = open(decodeURIComponent(m![1] ?? ""), sessionKey) as { sub: string; exp: number } | null;
assert.equal(claims?.sub, "U1", "the renewed cookie is valid and preserves the sub");
+ const twin = aged.headers.getSetCookie().find((cookie) => cookie.startsWith("portal_session_x="));
+ assert.ok(twin?.startsWith(`portal_session_x=${m![1]};`), "renewal re-issues the framed twin with the same session");
+ assert.match(twin!, /SameSite=Lax/, "the twin falls back to Lax on a non-https origin");
+ assert.match(twin!, new RegExp(`Max-Age=${SESSION_TTL_S}\\b`));
assert.ok(
(claims?.exp ?? 0) > Math.floor(Date.now() / 1000) + SESSION_TTL_S - 800,
"exp is pushed out to ~now + full TTL",
diff --git a/plugins/portal/test/session.test.ts b/plugins/portal/test/session.test.ts
index 059e0bb43..4eb3e248a 100644
--- a/plugins/portal/test/session.test.ts
+++ b/plugins/portal/test/session.test.ts
@@ -263,26 +263,31 @@ test("login preserves opaque callback query values while rejecting normalized re
});
for (const domain of [undefined, "example.test"]) {
- test(`framed session issuance is staged and clears existing twins (${domain ?? "host-only"})`, () => {
+ test(`login and renewal always issue the framed session twin (${domain ?? "host-only"})`, () => {
const attrs = { path: "/", maxAge: 100, secure: true, domain };
const headers = sessionCookieHeaders("signed-session", attrs);
assert.ok(headers.some((header) => header.startsWith("portal_session=signed-session;")));
- const twins = headers.filter((header) => header.startsWith("portal_session_x="));
- assert.equal(twins.length, domain ? 2 : 1);
- assert.ok(twins.every((header) => header.startsWith("portal_session_x=;") && header.includes("Max-Age=0")));
- if (domain) assert.ok(twins.some((header) => header.includes(`Domain=${domain}`)));
- const enabled = sessionCookieHeaders("signed-session", attrs, true);
- assert.ok(
- enabled.some(
- (header) =>
- header.startsWith("portal_session_x=signed-session;") &&
- header.includes("SameSite=None") &&
- header.includes("Secure"),
- ),
- );
+ const twins = headers.filter((header) => header.startsWith("portal_session_x=signed-session;"));
+ assert.equal(twins.length, 1);
+ assert.match(twins[0]!, /SameSite=None/);
+ assert.match(twins[0]!, /Secure/);
+ if (domain) {
+ assert.match(twins[0]!, new RegExp(`Domain=${domain}`));
+ assert.ok(headers.some((header) => header.startsWith("portal_session_x=;") && !header.includes("Domain=")));
+ } else {
+ assert.ok(!headers.some((header) => header.startsWith("portal_session_x=;")));
+ }
});
}
+test("the framed session twin falls back to Lax on a non-https origin", () => {
+ const twin = sessionCookieHeaders("signed-session", { path: "/", maxAge: 100, secure: false }).find((header) =>
+ header.startsWith("portal_session_x="),
+ );
+ assert.match(twin ?? "", /SameSite=Lax/);
+ assert.doesNotMatch(twin ?? "", /Secure/);
+});
+
test("openSession preserves signed app-only authority and rejects malformed markers", () => {
const now = Math.floor(Date.now() / 1000);
const claims = { k: "session", sub: "[email protected]", org: "acme", iat: now, exp: now + 3600 };
diff --git a/test/integration/admin-login.test.ts b/test/integration/admin-login.test.ts
index c6fb85c73..6f03dd948 100644
--- a/test/integration/admin-login.test.ts
+++ b/test/integration/admin-login.test.ts
@@ -44,18 +44,15 @@ test(
assert.ok(cookie);
assert.match(cookie, /HttpOnly/);
assert.match(cookie, /SameSite=Lax/);
+ const twin = res.headers.getSetCookie().find((value) => value.startsWith("portal_session_x="));
+ assert.ok(twin);
+ assert.equal(twin.split(";")[0], cookie.split(";")[0]!.replace("portal_session=", "portal_session_x="));
if (publicUrl.startsWith("https:")) {
assert.match(cookie, /Secure/);
- const twin = res.headers.getSetCookie().find((value) => value.startsWith("portal_session_x="));
- assert.ok(twin);
- if (env.PORTAL_FRAME_SESSION_ENABLED === "1") {
- assert.match(twin, /SameSite=None/);
- assert.ok(!twin.startsWith("portal_session_x=;"));
- } else {
- assert.match(twin, /^portal_session_x=;/);
- assert.match(twin, /Max-Age=0/);
- }
+ assert.match(twin, /SameSite=None/);
assert.match(twin, /Secure/);
+ } else {
+ assert.match(twin, /SameSite=Lax/);
}
return cookie.split(";")[0]!;
};