跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

windows-browser-extensions.test.mjs

63 行 · 5.1 KB历史下载
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
4import path from "node:path";
5import { tmpdir } from "node:os";
6import { Readable } from "node:stream";
7import { pathToFileURL } from "node:url";
8import { extractWindowsExtensionArchive, nativeExtensionID } from "../windows-browser-extensions.mjs";
9import { fixtureZip } from "./windows-browser-fixtures.mjs";
10const moduleURL = process.env.CAGE_BROWSER_TEST_MODULE_DIRECTORY ? pathToFileURL(path.join(process.env.CAGE_BROWSER_TEST_MODULE_DIRECTORY, "browser-extensions.mjs")) : new URL("../../../internal/cage/browser-extensions.mjs", import.meta.url);
11const { BrowserExtensions } = await import(moduleURL);
12
13const manifest = JSON.stringify({ manifest_version: 3, name: "Fixture extension", version: "1.0", permissions: ["storage"], content_scripts: [{ matches: ["https://example.test/*"], js: ["content.js"] }] });
14async function fixture(t) { const root = await mkdtemp(path.join(tmpdir(), "cage-windows-extension-")); t.after(() => rm(root, { recursive: true, force: true })); return root; }
15
16test("extension IDs use native Windows UTF16LE and normalize only drive case", () => {
17 const windows = "C:\\Users\\Guest\\Cage\\extensions\\example";
18 assert.equal(nativeExtensionID(windows, "win32"), "knnkgdbljifnnoaknlcaeepfhjhcnebo");
19 assert.equal(nativeExtensionID("c:" + windows.slice(2), "win32"), "knnkgdbljifnnoaknlcaeepfhjhcnebo");
20 assert.equal(nativeExtensionID(windows, "linux"), "fnbldeeooamfcppikgofhpodlhnnkleh");
21 assert.notEqual(nativeExtensionID(windows.toLowerCase(), "win32"), nativeExtensionID(windows, "win32"));
22});
23
24test("Windows ZIP preview extracts only declared safe files and includes script permissions", async t => {
25 const root = await fixture(t), archive = path.join(root, "fixture.zip"), output = path.join(root, "unpacked");
26 await writeFile(archive, fixtureZip([{ name: "package/manifest.json", data: manifest }, { name: "package/content.js", data: "void 0" }]));
27 const preview = await extractWindowsExtensionArchive(archive, output);
28 assert.equal(preview.relative, "package"); assert.deepEqual(preview.hostPermissions, ["https://example.test/*"]);
29 assert.equal(await readFile(path.join(output, "package", "content.js"), "utf8"), "void 0");
30});
31
32test("ZIP traversal, links, ADS, reserved names, duplicate Windows paths and declared bombs fail before extraction", async t => {
33 const root = await fixture(t);
34 const bad = [
35 { name: "../outside" }, { name: "/outside" }, { name: "C:/outside" }, { name: "a\\b" },
36 { name: "normal:stream" }, { name: "CON.txt" }, { name: "file." }, { name: "file " },
37 { name: "link", attributes: 0o120777 << 16 }, { name: "encrypted", flags: 1 }, { name: "bomb", size: (32 << 20) + 1 },
38 ];
39 let index = 0;
40 for (const entry of bad) {
41 const archive = path.join(root, String(index) + ".zip"), output = path.join(root, String(index++));
42 await writeFile(archive, fixtureZip([{ name: "manifest.json", data: manifest }, entry]));
43 await assert.rejects(extractWindowsExtensionArchive(archive, output));
44 await assert.rejects(readFile(path.join(output, "manifest.json"))); // nothing was written before validation
45 }
46 const duplicate = path.join(root, "duplicate.zip"); await writeFile(duplicate, fixtureZip([{ name: "manifest.json", data: manifest }, { name: "A.js" }, { name: "a.js" }]));
47 await assert.rejects(extractWindowsExtensionArchive(duplicate, path.join(root, "duplicates")), /重复/);
48 const oversized = path.join(root, "oversized.zip"); await writeFile(oversized, fixtureZip(Array.from({ length: 5 }, (_, n) => ({ name: "file" + n, size: 32 << 20 }))));
49 await assert.rejects(extractWindowsExtensionArchive(oversized, path.join(root, "oversized")), /128 MB/);
50 const damaged = path.join(root, "damaged.zip"); await writeFile(damaged, fixtureZip([{ name: "manifest.json", data: manifest, crc: 0 }]));
51 await assert.rejects(extractWindowsExtensionArchive(damaged, path.join(root, "damaged")), /校验/);
52});
53
54test("disabled extension import computes the Windows ID without loading or executing it", async t => {
55 const root = await fixture(t), calls = [];
56 const extensions = new BrowserExtensions({ directory: root, protectedID: "a".repeat(32), control: async () => "private-control", idForPath: p => nativeExtensionID(p, "win32"), extractArchive: extractWindowsExtensionArchive,
57 cdp: async (method, params) => { calls.push(method); if (method === "Runtime.evaluate") return { result: { value: { items: [] } } }; throw new Error("an extension must not load during disabled import"); } });
58 const preview = await extensions.upload(Readable.from([fixtureZip([{ name: "manifest.json", data: manifest }, { name: "content.js", data: "globalThis.executed=true" }])]));
59 const result = await extensions.request({ mode: "install", session: preview.session, enabled: false });
60 assert.equal(result.items.length, 1); assert.equal(result.items[0].enabled, false);
61 assert.equal(result.items[0].id, nativeExtensionID(path.join(root, "extensions", "packages", preview.session), "win32"));
62 assert(!calls.includes("Extensions.loadUnpacked"));
63});