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.

5.5 KB
Raw Blame
import { MIN_SECRET_LEN, signSession, verifySession, type SessionPayload } from "@/lib/auth/session";
import { liveSessionAuthorized } from "@/lib/auth/live-authorization";
import { managedAppOrigin, cockpitOrigin } from "./origin";
import type { ManagedAppId } from "./types";

export const MANAGED_APP_SESSION_COOKIE = "__Host-mso-managed-app";
const TICKET_TTL = 60_000;
const COOKIE_TTL = 2 * 60 * 60 * 1000;
type ManagedSession = SessionPayload & {parent_expires_at:number};
function key(id: ManagedAppId, kind: "ticket" | "cookie", secret: string): string {
  const origin = managedAppOrigin(id);
  if (!origin || secret.length < MIN_SECRET_LEN) throw new Error("managed application authorization is unavailable");
  return `managed-app:${origin}:${kind}:${secret}`;
}
export function createManagedAppTicket(id: ManagedAppId, session: SessionPayload, secret: string, now = Date.now()): string {
  const parentExpiry = (session as Partial<ManagedSession>).parent_expires_at ?? session.expires_at;
  const payload: ManagedSession = { ...session, parent_expires_at:parentExpiry, expires_at: Math.min(parentExpiry, now + TICKET_TTL) };
  return signSession(payload, key(id, "ticket", secret));
}
export function createManagedAppCookie(id: ManagedAppId, session: SessionPayload, secret: string, now=Date.now()): string {
  const parentExpiry = (session as Partial<ManagedSession>).parent_expires_at ?? session.expires_at;
  const payload: ManagedSession = { ...session, parent_expires_at:parentExpiry, expires_at:Math.min(parentExpiry,now + COOKIE_TTL) };
  return signSession(payload, key(id, "cookie", secret));
}
export function verifyManagedAppSession(id: ManagedAppId, token: string, secret: string, kind: "ticket" | "cookie" = "cookie"): ManagedSession | null {
  try {
    const session = verifySession(token,key(id,kind,secret)) as ManagedSession|null;
    return session && Number.isFinite(session.parent_expires_at) && session.parent_expires_at >= session.expires_at ? session : null;
  } catch { return null; }
}
export async function managedAppSession(request: Request, id: ManagedAppId): Promise<SessionPayload | null> {
  const secret = process.env.OS_SESSION_SECRET ?? "";
  const candidates = (request.headers.get("cookie") ?? "").split(";");
  for (const candidate of candidates) {
    const [name, ...parts] = candidate.trim().split("=");
    if (name !== MANAGED_APP_SESSION_COOKIE) continue;
    const session = verifyManagedAppSession(id, parts.join("="), secret);
    if (session && await liveSessionAuthorized(session, "operator")) return session;
  }
  return null;
}

const SCRIPT = "(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){document.body.textContent='Open this application from MSO to authorize it.';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='Application authorization failed. Reopen it from MSO.';return;}location.reload();})().catch(()=>{document.body.textContent='Application authorization failed.';});";
function response(id: ManagedAppId, body: string | null, status: number, contentType = "text/plain; charset=utf-8"): Response {
  return new Response(body, { status, headers: {
    "content-type": contentType, "cache-control": "no-store, no-transform",
    "cdn-cache-control": "no-store", "cloudflare-cdn-cache-control": "no-store",
    "referrer-policy": "no-referrer", "x-content-type-options": "nosniff",
    "x-mso-managed-app": id,
    "content-security-policy": `default-src 'none'; script-src 'self'; connect-src 'self'; frame-ancestors ${cockpitOrigin() ?? "'none'"}`,
  } });
}
// An app receives a purpose- and origin-bound credential, never the cockpit cookie.
export async function gateManagedApp(request: Request, id: ManagedAppId): Promise<Response | null> {
  const pathname = new URL(request.url).pathname;
  if (pathname === "/__mso_app_bootstrap.js") return request.method === "GET" ? response(id, SCRIPT, 200, "application/javascript; charset=utf-8") : response(id, null, 404);
  if (pathname === "/__mso_app_auth") {
    if (request.method !== "POST" || request.headers.get("origin") !== managedAppOrigin(id)) return response(id, null, 403);
    const token = /^Bearer ([A-Za-z0-9._-]{20,4096})$/.exec(request.headers.get("authorization") ?? "")?.[1];
    const secret = process.env.OS_SESSION_SECRET ?? "";
    const session = token ? verifyManagedAppSession(id, token, secret, "ticket") : null;
    if (!session || !await liveSessionAuthorized(session, "operator")) return response(id, null, 401);
    const result = response(id, null, 204);
    const maxAge = Math.max(0, Math.floor((Math.min(session.parent_expires_at, Date.now() + COOKIE_TTL) - Date.now()) / 1000));
    result.headers.set("set-cookie", `${MANAGED_APP_SESSION_COOKIE}=${createManagedAppCookie(id, session, secret)}; Path=/; Max-Age=${maxAge}; HttpOnly; Secure; SameSite=Strict`);
    return result;
  }
  if (await managedAppSession(request, id)) return null;
  if (request.method !== "GET" || request.headers.get("sec-fetch-dest") !== "iframe" && request.headers.get("sec-fetch-dest") !== "document") return response(id, null, 401);
  return response(id, '<!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");
}