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(browser): isolate Camoufox viewer cache

Rahman0d6b774decac
diff --git a/app/api/v1/camoufox/session/route.ts b/app/api/v1/camoufox/session/route.ts
index e2b67ed7..af2b6df9 100644
--- a/app/api/v1/camoufox/session/route.ts
+++ b/app/api/v1/camoufox/session/route.ts
@@ -7,6 +7,7 @@ import { getSessionContext } from "@/lib/auth/require-session";
import { roleAtLeast } from "@/lib/auth/roles";
import { camoufoxViewerOrigin } from "@/lib/camoufox/origin";
import { createCamoufoxViewerTicket } from "@/lib/camoufox/viewer-auth";
+import { CAMOUFOX_VIEWER_PUBLIC_PREFIX } from "@/lib/camoufox/viewer-gate";
import { IS_DEMO } from "@/lib/demo";
export const runtime = "nodejs";
@@ -44,6 +45,6 @@ export async function GET() {
const password = (await fs.readFile(/* turbopackIgnore: true */ PASSWD_FILE, "utf8").catch(() => "")).trim();
// Absent file = the operator has not set one. Say so plainly rather than 500 — the
// browser window falls back to letting noVNC prompt.
- if (!password) return NextResponse.json({ password: null, viewerOrigin, viewerTicket, reason: "no_password_file" });
- return NextResponse.json({ password, viewerOrigin, viewerTicket });
+ if (!password) return NextResponse.json({ password: null, viewerOrigin, viewerTicket, viewerPathPrefix: CAMOUFOX_VIEWER_PUBLIC_PREFIX, reason: "no_password_file" });
+ return NextResponse.json({ password, viewerOrigin, viewerTicket, viewerPathPrefix: CAMOUFOX_VIEWER_PUBLIC_PREFIX });
}
diff --git a/frontend/slices/camoufox-browser/app.tsx b/frontend/slices/camoufox-browser/app.tsx
index 57aeb19e..11485ee5 100644
--- a/frontend/slices/camoufox-browser/app.tsx
+++ b/frontend/slices/camoufox-browser/app.tsx
@@ -20,9 +20,10 @@ async function viewerSrc(): Promise<string | null> {
password?: string | null;
viewerOrigin?: string | null;
viewerTicket?: string | null;
+ viewerPathPrefix?: string | null;
};
- if (!payload.viewerOrigin || !payload.viewerTicket) return null;
- const viewer = payload.viewerOrigin + "/vnc.html?path=websockify&autoconnect=1&resize=remote";
+ if (!payload.viewerOrigin || !payload.viewerTicket || !payload.viewerPathPrefix) return null;
+ const viewer = payload.viewerOrigin + payload.viewerPathPrefix + "/vnc.html?path=websockify&autoconnect=1&resize=remote";
const fragment = new URLSearchParams({ viewer_ticket: payload.viewerTicket });
// Password and viewer ticket live in the URL fragment, never the request/query.
// The sibling viewer bootstrap exchanges the short-lived ticket for a host-only
diff --git a/lib/camoufox/viewer-gate.test.ts b/lib/camoufox/viewer-gate.test.ts
index fc187150..73be1604 100644
--- a/lib/camoufox/viewer-gate.test.ts
+++ b/lib/camoufox/viewer-gate.test.ts
@@ -7,6 +7,7 @@ import {
createCamoufoxViewerCookie,
createCamoufoxViewerTicket,
} from "@/lib/camoufox/viewer-auth";
+import { CAMOUFOX_VIEWER_PUBLIC_PREFIX } from "@/lib/camoufox/viewer-gate";
const approved = vi.hoisted(() => ({
value: true,
@@ -69,25 +70,39 @@ describe("the Camoufox split-origin VNC bridge", () => {
return loadProxy(TEMPLATE);
}
- const vnc = (cookie?: string, path = "/vnc_lite.html", extra: Record<string, string> = {}) =>
- req(VIEWER_HOST, path, cookie ? { cookie: CAMOUFOX_VIEWER_COOKIE + "=" + cookie, ...extra } : extra);
+ const vnc = (
+ cookie?: string,
+ path = CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc_lite.html",
+ extra: Record<string, string> = {},
+ ) => req(
+ VIEWER_HOST,
+ path,
+ cookie ? { cookie: CAMOUFOX_VIEWER_COOKIE + "=" + cookie, ...extra } : extra,
+ );
it("serves only the inert authorization bootstrap before a viewer cookie exists", async () => {
const proxy = await load();
- const page = await proxy(vnc(undefined, "/vnc.html"));
+ const page = await proxy(vnc(undefined, CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc.html"));
expect(page.status).toBe(200);
expect(rewriteOf(page)).toBeNull();
expect(await page.text()).toContain("/__viewer_bootstrap.js");
expect(page.headers.get("content-security-policy")).toContain("frame-ancestors https://mso.example.com");
expect(page.headers.get("set-cookie")).toBeNull();
+ expect(page.headers.get("cache-control")).toBe("no-store");
+ expect(page.headers.get("cdn-cache-control")).toBe("no-store");
- const asset = await proxy(vnc(undefined, "/app/ui.js"));
+ const asset = await proxy(vnc(undefined, CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/app/ui.js"));
expect(asset.status).toBe(404);
+ expect(asset.headers.get("cache-control")).toBe("no-store");
});
it("does not accept the cockpit session cookie on the sibling viewer host", async () => {
const proxy = await load();
- const page = await proxy(req(VIEWER_HOST, "/vnc.html", { cookie: "session=" + cockpitSession() }));
+ const page = await proxy(req(
+ VIEWER_HOST,
+ CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc.html",
+ { cookie: "session=" + cockpitSession() },
+ ));
expect(page.status).toBe(200);
expect(rewriteOf(page)).toBeNull();
expect(await page.text()).toContain("Authorizing secure browser viewer");
@@ -136,7 +151,7 @@ describe("the Camoufox split-origin VNC bridge", () => {
it("maps an approved sibling viewer to loopback noVNC and strips credentials upstream", async () => {
const proxy = await load();
- const res = await proxy(vnc(viewerCookie(), "/websockify", {
+ const res = await proxy(vnc(viewerCookie(), CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/websockify", {
upgrade: "websocket",
connection: "Upgrade",
authorization: "Bearer must-not-leak",
@@ -148,11 +163,27 @@ describe("the Camoufox split-origin VNC bridge", () => {
expect(res.headers.get("x-middleware-request-authorization")).toBeNull();
});
- it("maps root to the full noVNC UI and preserves only viewer query state", async () => {
+ it("redirects legacy root paths into the cache-isolated viewer namespace", async () => {
const proxy = await load();
- expect(new URL(rewriteOf(await proxy(vnc(viewerCookie(), "/")))!).pathname).toBe("/vnc.html");
- const res = await proxy(vnc(viewerCookie(), "/vnc.html?path=websockify&resize=remote"));
+ const root = await proxy(vnc(undefined, "/"));
+ expect(root.status).toBe(307);
+ expect(root.headers.get("location")).toContain(CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc.html");
+ const legacy = await proxy(vnc(undefined, "/vnc.html"));
+ expect(legacy.status).toBe(307);
+ expect(legacy.headers.get("location")).toContain(CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc.html");
+ });
+
+ it("maps namespaced viewer paths to noVNC and preserves only viewer query state", async () => {
+ const proxy = await load();
+ const root = await proxy(vnc(viewerCookie(), CAMOUFOX_VIEWER_PUBLIC_PREFIX));
+ expect(new URL(rewriteOf(root)!).pathname).toBe("/vnc.html");
+ expect(root.headers.get("cache-control")).toBe("no-store");
+ const res = await proxy(vnc(
+ viewerCookie(),
+ CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc.html?path=websockify&resize=remote",
+ ));
expect(new URL(rewriteOf(res)!).search).toBe("?path=websockify&resize=remote");
+ expect(res.headers.get("cloudflare-cdn-cache-control")).toBe("no-store");
});
it("never exposes the retired same-origin bridge", async () => {
@@ -164,11 +195,14 @@ describe("the Camoufox split-origin VNC bridge", () => {
it("refuses non-read viewer methods and off-box noVNC destinations", async () => {
const proxy = await load();
- const post = new NextRequest("https://" + VIEWER_HOST + "/vnc.html", {
+ const post = new NextRequest(
+ "https://" + VIEWER_HOST + CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc.html",
+ {
method: "POST",
headers: { host: VIEWER_HOST, cookie: CAMOUFOX_VIEWER_COOKIE + "=" + viewerCookie() },
- body: "x",
- });
+ body: "x",
+ },
+ );
expect((await proxy(post)).status).toBe(404);
const offBox = await load("http://evil.example");
diff --git a/lib/camoufox/viewer-gate.ts b/lib/camoufox/viewer-gate.ts
index b4968166..c1c7e62f 100644
--- a/lib/camoufox/viewer-gate.ts
+++ b/lib/camoufox/viewer-gate.ts
@@ -14,11 +14,21 @@ import { IS_DEMO } from "@/lib/demo";
const AUTHORIZED = Symbol("camoufox-viewer-authorized");
export type CamoufoxViewerGateResult = typeof AUTHORIZED | NextResponse;
+export const CAMOUFOX_VIEWER_PUBLIC_PREFIX = "/_mso-camoufox";
+
+function noStore(headers: Headers): void {
+ headers.set("cache-control", "no-store");
+ headers.set("cdn-cache-control", "no-store");
+ headers.set("cloudflare-cdn-cache-control", "no-store");
+}
+
function notFound() {
- return new NextResponse("Not Found", {
+ const response = new NextResponse("Not Found", {
status: 404,
headers: { "content-type": "text/plain; charset=utf-8" },
});
+ noStore(response.headers);
+ return response;
}
async function approvedDevice(token: string, kind: "ticket" | "cookie"): Promise<string | null> {
@@ -73,20 +83,22 @@ function bootstrap(method: string) {
// The global cockpit header is DENY. This split-origin viewer is the one exception:
// its CSP frame-ancestors names the exact cockpit and remains the authoritative gate.
response.headers.delete("x-frame-options");
+ noStore(response.headers);
return response;
}
function bootstrapScript() {
- return new NextResponse(BOOTSTRAP_JS, {
+ const response = new NextResponse(BOOTSTRAP_JS, {
status: 200,
headers: {
"content-type": "application/javascript; charset=utf-8",
- "cache-control": "no-store",
"referrer-policy": "no-referrer",
"x-content-type-options": "nosniff",
"cross-origin-resource-policy": "same-origin",
},
});
+ noStore(response.headers);
+ return response;
}
async function exchangeTicket(request: NextRequest) {
@@ -108,7 +120,7 @@ async function exchangeTicket(request: NextRequest) {
maxAge: Math.floor(CAMOUFOX_VIEWER_COOKIE_TTL_MS / 1000),
},
);
- response.headers.set("cache-control", "no-store");
+ noStore(response.headers);
return response;
}
@@ -125,14 +137,38 @@ export async function gateCamoufoxViewer(
return request.method === "GET" ? bootstrapScript() : notFound();
}
if (pathname === "/__viewer_auth") return exchangeTicket(request);
- if (await hasViewerSession(request)) return AUTHORIZED;
+
if (
(request.method === "GET" || request.method === "HEAD") &&
(pathname === "/" || pathname === "/vnc.html")
+ ) {
+ const redirect = new URL(request.url);
+ redirect.pathname = CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc.html";
+ return NextResponse.redirect(redirect, 307);
+ }
+
+ const inViewerNamespace =
+ pathname === CAMOUFOX_VIEWER_PUBLIC_PREFIX ||
+ pathname.startsWith(CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/");
+ if (!inViewerNamespace) return notFound();
+
+ if (await hasViewerSession(request)) return AUTHORIZED;
+ if (
+ (request.method === "GET" || request.method === "HEAD") &&
+ pathname === CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/vnc.html"
) return bootstrap(request.method);
return notFound();
}
+export function camoufoxViewerUpstreamPath(pathname: string): string | null {
+ if (
+ pathname !== CAMOUFOX_VIEWER_PUBLIC_PREFIX &&
+ !pathname.startsWith(CAMOUFOX_VIEWER_PUBLIC_PREFIX + "/")
+ ) return null;
+ const stripped = pathname.slice(CAMOUFOX_VIEWER_PUBLIC_PREFIX.length);
+ return stripped === "" || stripped === "/" ? "/vnc.html" : stripped;
+}
+
export function camoufoxViewerAuthorized(
result: CamoufoxViewerGateResult,
): result is typeof AUTHORIZED {
diff --git a/proxy.ts b/proxy.ts
index fddc08b0..844a9d40 100644
--- a/proxy.ts
+++ b/proxy.ts
@@ -36,6 +36,7 @@ import { IS_DEMO } from "@/lib/demo";
import { camoufoxViewerCsp, isCamoufoxViewerHost } from "@/lib/camoufox/origin";
import {
camoufoxViewerAuthorized,
+ camoufoxViewerUpstreamPath,
gateCamoufoxViewer,
} from "@/lib/camoufox/viewer-gate";
import { applyPrivatePageCachePolicy } from "@/lib/auth/page-cache";
@@ -200,8 +201,10 @@ export async function proxy(request: NextRequest) {
) {
return notFound();
}
+ const upstreamPath = camoufoxViewerUpstreamPath(pathname);
+ if (!upstreamPath) return notFound();
const target = new URL(base);
- target.pathname = pathname === "/" ? "/vnc.html" : pathname;
+ target.pathname = upstreamPath;
target.search = request.nextUrl.search;
const response = NextResponse.rewrite(target, {
request: { headers: upstreamSocketHeaders(request.headers) },
@@ -214,6 +217,9 @@ export async function proxy(request: NextRequest) {
response.headers.set("Referrer-Policy", "no-referrer");
response.headers.set("X-Content-Type-Options", "nosniff");
response.headers.set("Cross-Origin-Resource-Policy", "same-origin");
+ response.headers.set("Cache-Control", "no-store");
+ response.headers.set("CDN-Cache-Control", "no-store");
+ response.headers.set("Cloudflare-CDN-Cache-Control", "no-store");
response.headers.delete("X-Frame-Options");
return response;
}
diff --git a/scripts/e2e/camoufox-connection.mjs b/scripts/e2e/camoufox-connection.mjs
index 69996030..8793e419 100644
--- a/scripts/e2e/camoufox-connection.mjs
+++ b/scripts/e2e/camoufox-connection.mjs
@@ -26,6 +26,7 @@ export async function camoufoxConnectionJourney(page, fixture) {
password: null,
viewerOrigin,
viewerTicket: "fixture-viewer-ticket-0123456789abcdef",
+ viewerPathPrefix: "/_mso-camoufox",
},
});
const viewer = route => route.fulfill({ status: 200, contentType: "text/html", body: "<!doctype html><title>Private fixture viewer</title><p>Client-only viewer fixture</p>" });
@@ -52,7 +53,7 @@ export async function camoufoxConnectionJourney(page, fixture) {
await retry.click();
const frame = page.locator('iframe[title="Camoufox browser"]');
await expect(frame).toBeVisible();
- expect((await frame.getAttribute("src")) ?? "").toContain(viewerOrigin + "/vnc.html?");
+ expect((await frame.getAttribute("src")) ?? "").toContain(viewerOrigin + "/_mso-camoufox/vnc.html?");
await expect(page.frameLocator('iframe[title="Camoufox browser"]').getByText("Client-only viewer fixture")).toBeVisible();
expect(credentials).toBe(beforeCredentials + 1); expect(posts).toBe(0);
mode = "stopping"; reads = 0;