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.1 KB
Raw Blame
import { liveSessionAuthorized } from "@/lib/auth/live-authorization";
import { MANAGED_APP_SESSION_COOKIE, verifyManagedAppSession } from "./session";
import { CAMOUFOX_VIEWER_COOKIE, verifyCamoufoxViewerCookie } from "@/lib/camoufox/viewer-auth";
import { getApprovedDevice } from "@/lib/auth/device-store";
import { deviceSessionValid } from "@/lib/auth/live-session";
import { roleAtLeast } from "@/lib/auth/roles";
import { isCamoufoxViewerHost } from "@/lib/camoufox/origin";
import { camoufoxViewerUpstreamPath } from "@/lib/camoufox/viewer-gate";
import { managedAppIdForHost, managedAppOrigin } from "./origin";
import { getManagedAppDefinition } from "./catalog";
import { managedAppUpstream } from "./upstream-target";
import { upstreamSocketHeaders } from "./proxy-headers";
import { IS_DEMO } from "@/lib/demo";
import { shellAppSocketPolicy } from "@/lib/surfaces/socket-policy";
import type { OrcaReadinessResult } from "./orca-readiness-parse";

export interface SocketPolicyDeps {
  getOrcaReadiness?: () => Promise<OrcaReadinessResult>;
}

const denyUnavailable = (reason: string): OrcaReadinessResult => ({ available: false, reason });

async function defaultOrcaReadiness(): Promise<OrcaReadinessResult> {
  try {
    const { getOrcaReadiness } = await import("./orca-readiness");
    return await getOrcaReadiness();
  } catch {
    return denyUnavailable("bridge_unavailable");
  }
}

function cookieValues(request: Request, name: string): string[] {
  const cookieHeader = request.headers.get("cookie");
  if (cookieHeader) {
    const values: string[] = [];
    for (const part of cookieHeader.split(";")) {
      const [k, ...rest] = part.trim().split("=");
      if (k === name) values.push(rest.join("="));
    }
    if (values.length > 0) return values;
  }
  const reqWithCookies = request as { cookies?: { getAll?: (n: string) => Array<{ value: string }> } };
  if (typeof reqWithCookies.cookies?.getAll === "function") {
    return reqWithCookies.cookies.getAll(name).map((c) => c.value);
  }
  return [];
}

export async function socketPolicy(request: Request, deps: SocketPolicyDeps = {}) {
  if (IS_DEMO || request.method !== "GET") return null;
  const host = request.headers.get("host") ?? "", origin = request.headers.get("origin");
  if (origin) { try { if (new URL(origin).host !== host) return null; } catch { return null; } }
  if (!/(?:^|,)\s*websocket\s*(?:,|$)/i.test(request.headers.get("upgrade") ?? "")) return null;
  const viewer = isCamoufoxViewerHost(host), app = managedAppIdForHost(host);
  if (!viewer && !app) return shellAppSocketPolicy(request);
  if (app === "orca") {
    if (!origin || origin === "null") return null;
    const expected = managedAppOrigin("orca");
    if (!expected) return null;
    try { if (new URL(origin).origin.toLowerCase() !== expected.toLowerCase()) return null; } catch { return null; }
  }
  const secret = process.env.OS_SESSION_SECRET ?? "";
  let identity: string | undefined;
  for (const value of cookieValues(request, viewer ? CAMOUFOX_VIEWER_COOKIE : MANAGED_APP_SESSION_COOKIE)) {
    const session = viewer ? verifyCamoufoxViewerCookie(value, secret) : verifyManagedAppSession(app!, value, secret);
    if (!session?.device_id) continue;
    if (viewer) {
      const device = await getApprovedDevice(session.device_id);
      if (!deviceSessionValid(session, device) || !roleAtLeast(device.role, "operator")) continue;
    } else if (!await liveSessionAuthorized(session, "operator")) continue;
    identity = session.device_id; break;
  }
  if (!identity) return null;
  let socketIdentity = identity;
  let base: URL;
  try {
    if (viewer) {
      base = managedAppUpstream(process.env.CAMOUFOX_NOVNC_URL ?? "http://127.0.0.1:6080");
    } else if (app === "orca") {
      // Re-verify readiness on every authorization (initial and each revalidation):
      // the session proves WHO, not that the owned runtime still owns the endpoint.
      // The identity also binds device to the exact runtime/PID/start ticks/inode so a
      // same-device runtime swap changes identity and closes the live socket.
      const readiness = await (deps.getOrcaReadiness ?? defaultOrcaReadiness)();
      if (!readiness.available || !readiness.runtimeId || !readiness.startTicks) return null;
      socketIdentity = JSON.stringify({
        device: identity,
        runtimeId: readiness.runtimeId,
        serverPid: readiness.serverPid,
        startTicks: readiness.startTicks,
        listenerInode: readiness.listenerInode ?? null,
      });
      base = new URL(readiness.boundEndpoint);
      if (base.protocol !== "ws:" || base.username || base.password) return null;
      base.protocol = "http:";
    } else {
      base = managedAppUpstream(getManagedAppDefinition(app!).dashboardUrl);
    }
  } catch {
    return null;
  }
  const url = new URL(request.url);
  const pathname = viewer ? camoufoxViewerUpstreamPath(url.pathname) : url.pathname;
  if (!pathname || pathname.startsWith("/_next")) return null;
  const target = new URL(base); target.pathname = pathname; target.search = url.search;
  return { identity: socketIdentity, target: target.toString(), headers: Object.fromEntries(upstreamSocketHeaders(request.headers, app ?? undefined, base)) };
}