Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(security): keep connected app bootstrap code static

Abdurrahman Fakhrul0f33666b50e9
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index f3a649ca..1b9665be 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -8,6 +8,7 @@ this is the *what*, and it is what Settings → Account → About shows as “Wh
**Fixed**
+- `security` keep connected app bootstrap code static
- `security` protect public admission and OAuth registration trust
**Tests**
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md
index 91772368..0a3ba345 100644
--- a/docs/PROGRESS.md
+++ b/docs/PROGRESS.md
@@ -1,5 +1,7 @@
## 2026-10-09: Reuse MSO login for reviewed private applications
+The bootstrap JavaScript is static: the login destination lives in percent-encoded HTML data, never code construction. A hostile app identifier cannot change the script or escape its data attribute; regression coverage executes the no-ticket redirect with hostile metadata.
+
The Vault window previously used a separate Basic login even after the Owner signed into MSO. Reviewed shell-only loopback applications can now opt into an isolated MSO-derived session through private registry state. The existing browser session supplies a short-lived, exact-origin ticket; its app exchanges that fragment-only ticket for a host-only cookie without sharing the cockpit cookie or password. Normal external connections keep their own authentication and portable manifests cannot grant this mode.
Every app HTTP request requires the current Owner role and durable device/session generation. WebSockets use the existing continuously authorized server relay, so demotion, logout, revocation and expiry stop both directions rather than authorizing only the initial upgrade. App cookies cannot authenticate cockpit APIs, upstream requests strip all MSO credentials, and duplicate hosts/off-box targets fail closed. The derived cookie retains the parent expiry independently of the one-minute ticket. Regression coverage checks purpose/origin separation, expiry, role changes, cookie stripping, proxy routing and production HTTP/socket handoff with synthetic stores. Instance ingress, domain and vault data remain outside source.
diff --git a/lib/surfaces/session-gate.ts b/lib/surfaces/session-gate.ts
index fe613d6b..03e6bae1 100644
--- a/lib/surfaces/session-gate.ts
+++ b/lib/surfaces/session-gate.ts
@@ -14,15 +14,12 @@ function response(body: string | null, status: number, contentType = "text/plain
return new NextResponse(body, { status, headers: { ...shellAppResponseHeaders(), "content-type": contentType,
"content-security-policy": `default-src 'none'; script-src 'self'; connect-src 'self'; frame-ancestors ${new URL(process.env.OS_PUBLIC_ORIGIN!).origin}` } });
}
-function script(app: SurfaceApp) {
- const cockpit = new URL(process.env.OS_PUBLIC_ORIGIN!).origin;
- const login = new URL("/login", cockpit);
- login.searchParams.set("returnTo", `/api/v1/shell-apps/${encodeURIComponent(app.id)}/session?redirect=1`);
- return `(async()=>{const p=new URLSearchParams(location.hash.slice(1));const t=p.get('app_ticket');p.delete('app_ticket');history.replaceState(null,'',location.pathname+location.search+(p.toString()?'#'+p.toString():''));if(!t){location.replace(${JSON.stringify(login)});return;}const r=await fetch('/__mso_app_auth',{method:'POST',headers:{authorization:'Bearer '+t},credentials:'include',cache:'no-store'});if(!r.ok){document.body.textContent='MSO session expired. Reopen this application from MSO.';return;}location.reload();})().catch(()=>{document.body.textContent='Application authorization failed.';});`;
+function script() {
+ return "(async()=>{const p=new URLSearchParams(location.hash.slice(1));const t=p.get('app_ticket');p.delete('app_ticket');history.replaceState(null,'',location.pathname+location.search+(p.toString()?'#'+p.toString():''));if(!t){location.replace(decodeURIComponent(document.body.dataset.msoLogin));return;}const r=await fetch('/__mso_app_auth',{method:'POST',headers:{authorization:'Bearer '+t},credentials:'include',cache:'no-store'});if(!r.ok){document.body.textContent='MSO session expired. Reopen this application from MSO.';return;}location.reload();})().catch(()=>{document.body.textContent='Application authorization failed.';});";
}
export async function gateShellApp(request: NextRequest, app: SurfaceApp) {
const pathname = request.nextUrl.pathname;
- if (pathname === "/__mso_app_bootstrap.js") return request.method === "GET" ? response(script(app), 200, "application/javascript; charset=utf-8") : response(null, 404);
+ if (pathname === "/__mso_app_bootstrap.js") return request.method === "GET" ? response(script(), 200, "application/javascript; charset=utf-8") : response(null, 404);
if (pathname === "/__mso_app_auth") {
if (request.method !== "POST" || request.headers.get("origin") !== app.origin) return response(null, 403);
const token = /^Bearer ([A-Za-z0-9._-]{20,4096})$/.exec(request.headers.get("authorization") ?? "")?.[1];
@@ -37,5 +34,7 @@ export async function gateShellApp(request: NextRequest, app: SurfaceApp) {
}
if (await shellAppSession(request, app)) return null;
if (request.method !== "GET" || !["document", "iframe"].includes(request.headers.get("sec-fetch-dest") ?? "")) return response(null, 401);
- return response('<!doctype html><meta charset="utf-8"><title>MSO application authorization</title><body><p>Authorizing application...</p><script src="/__mso_app_bootstrap.js" defer></script></body>', 200, "text/html; charset=utf-8");
+ const login = new URL("/login", new URL(process.env.OS_PUBLIC_ORIGIN!).origin);
+ login.searchParams.set("returnTo", `/api/v1/shell-apps/${encodeURIComponent(app.id)}/session?redirect=1`);
+ return response(`<!doctype html><meta charset="utf-8"><title>MSO application authorization</title><body data-mso-login="${encodeURIComponent(login.href)}"><p>Authorizing application...</p><script src="/__mso_app_bootstrap.js" defer></script></body>`, 200, "text/html; charset=utf-8");
}
diff --git a/lib/surfaces/session.test.ts b/lib/surfaces/session.test.ts
index ca8ebdad..c3e087e0 100644
--- a/lib/surfaces/session.test.ts
+++ b/lib/surfaces/session.test.ts
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { NextRequest } from "next/server";
+import { runInNewContext } from "node:vm";
import { createShellAppCredential, verifyShellAppCredential, sessionApps, shellAppSession, SHELL_APP_COOKIE, shellAppUpstreamHeaders } from "./session";
import { gateShellApp } from "./session-gate";
import { shellAppSocketPolicy } from "./socket-policy";
@@ -80,7 +81,21 @@ describe("isolated connected-app sessions", () => {
expect((await gateShellApp(request("/", "", { "sec-fetch-dest": "iframe" }), app))?.status).toBe(200);
expect((await gateShellApp(request("/api/websockets", ""), app))?.status).toBe(401);
const script = await gateShellApp(request("/__mso_app_bootstrap.js", ""), app);
- expect(await script!.text()).toContain("returnTo");
+ expect(await script!.text()).toContain("document.body.dataset.msoLogin");
+ });
+ it("keeps app identifiers in encoded HTML data rather than generated JavaScript", async () => {
+ const hostile = { ...app, id: 'x"><script>globalThis.injected=true</script>' };
+ const html = await (await gateShellApp(request("/", "", { "sec-fetch-dest": "iframe" }), hostile))!.text();
+ const value = /data-mso-login="([^"]+)"/.exec(html)![1];
+ expect(html).not.toContain(hostile.id);
+ const javascript = await (await gateShellApp(request("/__mso_app_bootstrap.js", ""), hostile))!.text();
+ expect(javascript).toBe(await (await gateShellApp(request("/__mso_app_bootstrap.js", ""), app))!.text());
+ const replace = vi.fn(), context = { URLSearchParams, decodeURIComponent, history: { replaceState: vi.fn() }, location: { hash: "", pathname: "/", search: "", replace }, document: { body: { dataset: { msoLogin: value }, textContent: "" } }, injected: false };
+ await runInNewContext(javascript, context);
+ expect(context.injected).toBe(false);
+ const target = new URL(replace.mock.calls[0][0]);
+ expect(target.origin).toBe("https://cockpit.example.test");
+ expect(target.searchParams.get("returnTo")).toBe(`/api/v1/shell-apps/${encodeURIComponent(hostile.id)}/session?redirect=1`);
});
it("authorizes sockets with app credentials and stops authorizing after revocation or demotion", async () => {
const req = request("/api/websockets", token(), { origin: app.origin, upgrade: "websocket" });