Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Stage framed session cookie issuance for safe rollback (#1583)

Regan Bell124b695cbdb7
diff --git a/plugins/portal/README.md b/plugins/portal/README.md
index f08ffcba1..02539de52 100644
--- a/plugins/portal/README.md
+++ b/plugins/portal/README.md
@@ -283,3 +283,9 @@ from the signed email login transaction.
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.
diff --git a/plugins/portal/src/index.ts b/plugins/portal/src/index.ts
index 84cf5a7b9..1ecc3baf2 100644
--- a/plugins/portal/src/index.ts
+++ b/plugins/portal/src/index.ts
@@ -16,6 +16,7 @@ import {
openImpersonation,
openTmp,
setCookie,
+ sessionCookieHeaders,
clearCookie,
readCookie,
randomToken,
@@ -852,17 +853,8 @@ function sessionCookieSet(value: string, sub: string): string[] {
secure: SECURE_COOKIES,
...(COOKIE_DOMAIN ? { domain: COOKIE_DOMAIN } : {}),
};
- const set = setCookie("portal_session", value, attrs);
- const framed = setCookie(FRAME_SESSION_COOKIE, value, { ...attrs, sameSite: "None" });
return [
- ...(COOKIE_DOMAIN
- ? [
- set,
- framed,
- clearCookie("portal_session", "/", SECURE_COOKIES),
- clearCookie(FRAME_SESSION_COOKIE, "/", SECURE_COOKIES),
- ]
- : [set, framed]),
+ ...sessionCookieHeaders(value, attrs, process.env.PORTAL_FRAME_SESSION_ENABLED === "1"),
...loginProviderCookie(sub),
];
}
diff --git a/plugins/portal/src/session.ts b/plugins/portal/src/session.ts
index 7ce263880..f85d7c958 100644
--- a/plugins/portal/src/session.ts
+++ b/plugins/portal/src/session.ts
@@ -134,6 +134,21 @@ export function clearCookie(name: string, path: string, secure: boolean, domain?
return parts.join("; ");
}
+export function sessionCookieHeaders(value: string, attrs: CookieOpts, framed = false): 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),
+ ...(attrs.domain
+ ? [
+ clearCookie("portal_session", attrs.path ?? "/", attrs.secure),
+ clearCookie("portal_session_x", attrs.path ?? "/", attrs.secure),
+ ]
+ : []),
+ ];
+}
+
export function readCookie(header: string | undefined, name: string): string | null {
if (!header) return null;
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
diff --git a/plugins/portal/test/session.test.ts b/plugins/portal/test/session.test.ts
index b04b131ae..a29a928b9 100644
--- a/plugins/portal/test/session.test.ts
+++ b/plugins/portal/test/session.test.ts
@@ -8,6 +8,7 @@ import {
openImpersonation,
openTmp,
setCookie,
+ sessionCookieHeaders,
clearCookie,
readCookie,
safeEqual,
@@ -260,3 +261,24 @@ test("login preserves opaque callback query values while rejecting normalized re
assert.equal(sanitizeReturnTo("/path/..//evil.example", "https://qm.example"), "/");
assert.equal(sanitizeReturnTo("/%2f%2fevil.example?session_uri=ok", "https://qm.example"), "/");
});
+
+for (const domain of [undefined, "example.test"]) {
+ test(`framed session issuance is staged and clears existing twins (${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"),
+ ),
+ );
+ });
+}
diff --git a/test/integration/admin-login.test.ts b/test/integration/admin-login.test.ts
index 3e97122db..c6fb85c73 100644
--- a/test/integration/admin-login.test.ts
+++ b/test/integration/admin-login.test.ts
@@ -47,8 +47,14 @@ test(
if (publicUrl.startsWith("https:")) {
assert.match(cookie, /Secure/);
const twin = res.headers.getSetCookie().find((value) => value.startsWith("portal_session_x="));
- assert.ok(twin, "the SameSite=None twin is issued beside the Lax cookie");
- assert.match(twin, /SameSite=None/);
+ 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, /Secure/);
}
return cookie.split(";")[0]!;