示例工程 05-preload-and-contextbridge

05-preload-and-contextbridge/insecure-no-context-isolation/main/index.ts

typescript · 0.8 KB

// ⚠️ INSECURE DEMO — 仅用于演示攻击面,禁止照抄
import { app, BrowserWindow } from "electron";

app.whenReady().then(async () => {
  const win = new BrowserWindow({
    show: false,
    webPreferences: {
      // 这三项是经典的危险组合:页面直接拿到 Node。
      contextIsolation: false,
      sandbox: false,
      nodeIntegration: true,
      webSecurity: true,
    },
  });

  await win.loadURL("data:text/html,<h1>insecure</h1>");
  const probe = (await win.webContents.executeJavaScript(
    `({
      hasRequire: typeof require !== "undefined",
      hasProcess: typeof process !== "undefined",
    })`,
  )) as { hasRequire: boolean; hasProcess: boolean };

  console.log(JSON.stringify({ evt: "insecure-probe", ...probe }));
  app.quit();
});