Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

10.6 KB
Raw Blame
// Native WebView coverage for origin ACLs and the disclosed download payload.
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import http from "node:http";
import os from "node:os";
import path from "node:path";
import { spawn } from "node:child_process";
import { once } from "node:events";

if (process.platform !== "darwin") {
  throw new Error("This native IPC fixture currently requires macOS.");
}
const repo = process.cwd();
const root = await fs.mkdtemp(path.join(os.tmpdir(), "pake-ipc-security-"));
const fixture = path.join(root, "fixture");
const downloads = path.join(root, "downloads");
const identifier = `com.pake.ipc-probe-${process.pid}`;
const reports = new Map();
const downloadRequests = [];
let app;
let attackerOrigin;
let entryOrigin;

function page(stage, origin) {
  return `<!doctype html><meta charset="utf-8"><title>Pake IPC security probe</title>
<body>Testing ${stage} origin permissions<main><section id="player"><video></video><button>Pause</button></section></main><script>
(async () => {
  const invoke = window.__PAKE_NATIVE__.core.invoke;
  const check = async (command, args) => {
    try { await invoke(command, args); return "allowed"; }
    catch (error) { return "denied: " + String(error); }
  };
  const results = {};
  results.download = await check("download_file", {params: {
    url: ${JSON.stringify(origin + "/payload")}, filename: ${JSON.stringify(stage + ".txt")},
    user_agent: navigator.userAgent, page_url: location.href
  }});
  results.userAgent = navigator.userAgent;
  results.zoom = await check("set_zoom", {percent: 100});
  results.closeNotification = await check("close_notification", {id: "pake-probe-not-delivered"});
  results.window = await check("plugin:window|is_fullscreen", {label: "pake"});
  results.printShim = String(window.print).includes("plugin:webview|print");
  // A missing label fails after the ACL check without opening a print panel.
  results.print = await check("plugin:webview|print", {label: "pake-probe-missing-webview"});
  ${
    stage === "entry"
      ? `results.traversal = await check("download_file", {params: {
    url: ${JSON.stringify(origin + "/payload")}, filename: "../outside.txt"
  }});`
      : ""
  }
  ${
    stage === "attacker"
      ? `
  await new Promise(resolve => document.readyState === "loading"
    ? document.addEventListener("DOMContentLoaded", resolve, {once:true}) : resolve());
  await new Promise(resolve => setTimeout(resolve, 250));
  for (const [url, filename] of [["/native.txt", "native.txt"], [${JSON.stringify(entryOrigin + "/attachment")}, "native-cross.txt"]]) {
    const anchor = document.createElement("a");
    anchor.href = url;
    anchor.download = filename;
    document.body.appendChild(anchor);
    anchor.click();
    anchor.remove();
    await new Promise(resolve => setTimeout(resolve, 500));
  }
  // The polyfill's route to a full-screen window, where the engine has no
  // fullscreen of its own. macOS uses WebKit's, which is no IPC call and asks
  // for a user gesture as a browser does; tests/browser/fullscreen.mjs holds
  // the polyfill to leaving the player alone when this is denied.
  results.fullscreen = await check("plugin:window|set_fullscreen", {label: "pake", value: true});
  `
      : ""
  }
  await fetch("/report", {method:"POST", body:JSON.stringify(results)});
  ${stage === "entry" ? `location.replace(${JSON.stringify(attackerOrigin)});` : ""}
})().catch(error => fetch("/report", {method:"POST", body:JSON.stringify({error:String(error)})}));
</script>`;
}

async function server(stage) {
  let origin;
  const server = http.createServer(async (request, response) => {
    if (request.url === "/native.txt" || request.url === "/attachment") {
      response.writeHead(200, {
        "content-type": "application/octet-stream",
        "content-disposition": `attachment; filename="${request.url === "/native.txt" ? "native.txt" : "native-cross.txt"}"`,
      });
      response.end("pake-native-browser-download");
    } else if (request.url === "/payload") {
      downloadRequests.push({ stage, headers: request.headers });
      response.writeHead(200, { "content-type": "text/plain" });
      response.end("pake-native-ipc-proof");
    } else if (request.url === "/report") {
      const chunks = [];
      for await (const chunk of request) chunks.push(chunk);
      reports.set(stage, JSON.parse(Buffer.concat(chunks).toString()));
      response.end("ok");
    } else {
      response.writeHead(200, {
        "content-type": "text/html",
        "set-cookie": [
          "pakeFixtureSession=entry; Path=/; HttpOnly; SameSite=Lax",
          "pakeWrongPath=fixture; Path=/never; HttpOnly",
        ],
      });
      response.end(page(stage, origin));
    }
  });
  server.listen(0, "127.0.0.1");
  await once(server, "listening");
  origin = `http://127.0.0.1:${server.address().port}`;
  return { server, origin };
}

const attacker = await server("attacker");
attackerOrigin = attacker.origin;
const entry = await server("entry");
entryOrigin = entry.origin;
try {
  await fs.mkdir(fixture);
  await fs.cp(path.join(repo, "src-tauri"), path.join(fixture, "src-tauri"), {
    recursive: true,
    filter: (file) => {
      const relative = path.relative(path.join(repo, "src-tauri"), file);
      return (
        !["target", ".pake", "gen", ".cargo"].includes(
          relative.split(path.sep)[0],
        ) && !relative.startsWith(path.join("permissions", "autogenerated"))
      );
    },
  });
  await fs.copyFile(
    path.join(repo, "rust-toolchain.toml"),
    path.join(fixture, "rust-toolchain.toml"),
  );
  await fs.mkdir(path.join(fixture, "dist"));
  await fs.writeFile(path.join(fixture, "dist", "index.html"), "IPC fixture");
  const pake = JSON.parse(
    await fs.readFile(path.join(fixture, "src-tauri", "pake.json")),
  );
  pake.windows[0].url = entry.origin;
  pake.windows[0].url_type = "web";
  pake.download_dir = downloads;
  pake.system_tray.macos = false;
  const tauri = JSON.parse(
    await fs.readFile(path.join(fixture, "src-tauri", "tauri.conf.json")),
  );
  tauri.identifier = identifier;
  tauri.productName = "PakeSecurityProbe";
  const configDir = path.join(fixture, "src-tauri", ".pake");
  await fs.mkdir(configDir);
  await fs.writeFile(path.join(configDir, "pake.json"), JSON.stringify(pake));
  await fs.writeFile(
    path.join(configDir, "tauri.conf.json"),
    JSON.stringify(tauri),
  );

  const log = await fs.open(path.join(root, "build.log"), "w");
  console.log("Building native IPC fixture...");
  const compiler = spawn(
    "cargo",
    ["build", "--locked", "--features", "cli-build"],
    {
      cwd: path.join(fixture, "src-tauri"),
      env: {
        ...process.env,
        TAURI_CONFIG: JSON.stringify(tauri),
        CARGO_TARGET_DIR: path.join(repo, "src-tauri", "target"),
      },
      stdio: ["ignore", log.fd, log.fd],
    },
  );
  const [exitCode] = await once(compiler, "exit");
  await log.close();
  if (exitCode !== 0) {
    console.error(await fs.readFile(path.join(root, "build.log"), "utf8"));
    throw new Error(`Native fixture build failed with exit code ${exitCode}`);
  }
  console.log("Running real macOS WebView origin probes...");
  app = spawn(path.join(repo, "src-tauri", "target", "debug", "pake"), [], {
    stdio: "ignore",
  });
  const deadline = Date.now() + 45_000;
  while (!reports.has("attacker") && Date.now() < deadline) {
    if (app.exitCode !== null)
      throw new Error(`Native app exited with ${app.exitCode}`);
    await new Promise((resolve) => setTimeout(resolve, 100));
  }
  assert.ok(reports.has("entry"), "configured entry page did not report");
  assert.ok(reports.has("attacker"), "cross-origin page did not report");
  const trusted = reports.get("entry");
  const untrusted = reports.get("attacker");
  const request = downloadRequests.find((item) => item.stage === "entry");
  assert.equal(request.headers["user-agent"], trusted.userAgent);
  assert.equal(request.headers.referer, entryOrigin + "/");
  assert.match(
    request.headers.cookie,
    /(?:^|; )pakeFixtureSession=entry(?:;|$)/,
  );
  assert.ok(!request.headers.cookie.includes("pakeWrongPath"));
  for (const command of [
    "download",
    "zoom",
    "window",
    "traversal",
    "closeNotification",
  ]) {
    assert.equal(trusted[command], "allowed", `configured origin: ${command}`);
  }
  for (const command of [
    "download",
    "zoom",
    "window",
    "closeNotification",
    "print",
  ]) {
    assert.match(
      untrusted[command],
      /^denied: .*not allowed/i,
      `untrusted origin: ${command}`,
    );
  }
  for (const report of [trusted, untrusted]) {
    assert.equal(report.printShim, true, "window.print is not the IPC shim");
  }
  assert.doesNotMatch(
    trusted.print,
    /not allowed/i,
    "configured origin: print",
  );
  assert.match(trusted.print, /^denied: .*webview not found/i);
  const downloadDeadline = Date.now() + 5000;
  while (Date.now() < downloadDeadline) {
    try {
      await fs.stat(path.join(downloads, "native-cross.txt"));
      break;
    } catch {
      await new Promise((resolve) => setTimeout(resolve, 100));
    }
  }
  for (const file of ["native.txt", "native-cross.txt"]) {
    assert.equal(
      await fs.readFile(path.join(downloads, file), "utf8"),
      "pake-native-browser-download",
    );
  }
  assert.match(untrusted.fullscreen, /^denied: .*not allowed/i);
  assert.equal(
    await fs.readFile(path.join(downloads, "entry.txt"), "utf8"),
    "pake-native-ipc-proof",
  );
  assert.equal(
    await fs.readFile(path.join(downloads, "outside.txt"), "utf8"),
    "pake-native-ipc-proof",
  );
  for (const file of [
    path.join(root, "outside.txt"),
    path.join(downloads, "attacker.txt"),
  ]) {
    await assert.rejects(fs.stat(file), { code: "ENOENT" });
  }
  console.log(
    "Native IPC PASS: download UA, Referer and HttpOnly cookies match the webview; entry commands and window.print work, cross-origin commands are denied, browser downloads survive denied IPC, an untrusted origin cannot make the window full screen, traversal remains inside Downloads.",
  );
} finally {
  if (app && app.exitCode === null) {
    const stopped = once(app, "exit");
    app.kill("SIGTERM");
    await stopped;
  }
  await Promise.all(
    [entry, attacker].map(
      ({ server }) => new Promise((resolve) => server.close(resolve)),
    ),
  );
  await fs.rm(root, { recursive: true, force: true });
  await fs.rm(
    path.join(os.homedir(), "Library", "Application Support", identifier),
    { recursive: true, force: true },
  );
  await fs.rm(path.join(os.homedir(), "Library", "Caches", identifier), {
    recursive: true,
    force: true,
  });
  await fs.rm(path.join(os.homedir(), "Library", "WebKit", identifier), {
    recursive: true,
    force: true,
  });
}