Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { PREVIEW_WEBVIEW_PREFERENCES } from "./WebviewPreferences.ts";
4
5function parseWebPreferences(input: string): Record<string, unknown> {
6 const out: Record<string, unknown> = {};
7 for (const pair of input.split(",")) {
8 if (pair !== pair.trim()) {
9 // Electron's parser doesn't trim; surface the bug as undefined-key.
10 out[pair] = pair.split("=")[1];
11 continue;
12 }
13 const [key, value] = pair.split("=");
14 if (!key) continue;
15 out[key] = value;
16 }
17 return out;
18}
19
20describe("PREVIEW_WEBVIEW_PREFERENCES", () => {
21 const parsed = parseWebPreferences(PREVIEW_WEBVIEW_PREFERENCES);
22
23 it("contains exactly the three security-critical keys", () => {
24 expect(Object.keys(parsed).toSorted()).toEqual(
25 ["contextIsolation", "nodeIntegration", "sandbox"].toSorted(),
26 );
27 });
28
29 it("uses canonical JS-boolean string literals (not yes/no, on/off, 1/0)", () => {
30 // `value="no"` is a TRUTHY string when assigned to webPreferences.X — so
31 // `contextIsolation="no"` would silently leave isolation ENABLED. Lock
32 // the values to `"true"` / `"false"` so the parser does the right thing.
33 for (const value of Object.values(parsed)) {
34 expect(value).toMatch(/^(true|false)$/);
35 }
36 });
37
38 it("disables context isolation (so react-grab can see the page's React DevTools hook)", () => {
39 expect(parsed["contextIsolation"]).toBe("false");
40 });
41
42 it("keeps the renderer sandbox enabled (so the page cannot reach Node APIs)", () => {
43 expect(parsed["sandbox"]).toBe("true");
44 });
45
46 it("disables nodeIntegration (defense in depth — page never gets Node)", () => {
47 expect(parsed["nodeIntegration"]).toBe("false");
48 });
49
50 it("contains no whitespace (Electron's parser does not trim)", () => {
51 expect(PREVIEW_WEBVIEW_PREFERENCES).not.toMatch(/\s/);
52 });
53});