Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Reuse an existing trusted-provider session on trusted launch (#2025)

Joshua France54fa67924c56
diff --git a/plugins/portal/src/index.ts b/plugins/portal/src/index.ts
index 0ac3c4a9b..fdae1cee7 100644
--- a/plugins/portal/src/index.ts
+++ b/plugins/portal/src/index.ts
@@ -1604,6 +1604,15 @@ async function trustedAuth(req: IncomingMessage, res: ServerResponse, url: URL):
url.searchParams.get("returnTo") ??
openTmp(readCookie(req.headers.cookie, "portal_oidc_tmp"), tmpKey, Date.now())?.returnTo ??
null;
+ const existing = currentSession(req);
+ const trustedPrefix = `oidc:${createHash("sha256").update(trustedOidc!.issuer).digest("hex")}:`;
+ if (existing && !existing.anon && !existing.appOnly && existing.sub.startsWith(trustedPrefix)) {
+ res.writeHead(302, {
+ location: sanitizeReturnTo(returnTo, PUBLIC_URL, APPS_DOMAIN),
+ "cache-control": "no-store",
+ });
+ return void res.end();
+ }
const login = trustedEntry.start(sanitizeReturnTo(returnTo, PUBLIC_URL, APPS_DOMAIN));
setSession(res, [setCookie(cookieName, login.cookie, { path, maxAge: login.ttl, secure: SECURE_COOKIES })]);
res.writeHead(302, { location: login.location, "cache-control": "no-store" });
diff --git a/plugins/portal/test/trusted-entry-routes.test.ts b/plugins/portal/test/trusted-entry-routes.test.ts
index a905dadb6..14c51568d 100644
--- a/plugins/portal/test/trusted-entry-routes.test.ts
+++ b/plugins/portal/test/trusted-entry-routes.test.ts
@@ -258,3 +258,19 @@ test(
assert.equal(new URL(primary.headers.get("location")!).origin, "https://primary.example.test");
},
);
+
+test("trusted launch with a valid session returns to the app without another sign-in", async () => {
+ const login = await start();
+ const signedIn = await fetch(login.callback, { redirect: "manual", headers: { cookie: login.cookie } });
+ const session = signedIn.headers
+ .getSetCookie()
+ .find((cookie) => cookie.startsWith("portal_session="))!
+ .split(";")[0]!;
+ const relaunch = await fetch(`${base}/auth/trusted/login?returnTo=%2Fs%2Fabc`, {
+ redirect: "manual",
+ headers: { cookie: session },
+ });
+ assert.equal(relaunch.status, 302);
+ assert.equal(relaunch.headers.get("location"), "/s/abc");
+ assert.ok(!relaunch.headers.getSetCookie().some((cookie) => cookie.startsWith("portal_trusted_tmp=")));
+});