| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 4 | import path from "node:path"; |
| 5 | import { tmpdir } from "node:os"; |
| 6 | import { Readable } from "node:stream"; |
| 7 | import { pathToFileURL } from "node:url"; |
| 8 | import { extractWindowsExtensionArchive, nativeExtensionID } from "../windows-browser-extensions.mjs"; |
| 9 | import { fixtureZip } from "./windows-browser-fixtures.mjs"; |
| 10 | const 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); |
| 11 | const { BrowserExtensions } = await import(moduleURL); |
| 12 | |
| 13 | const manifest = JSON.stringify({ manifest_version: 3, name: "Fixture extension", version: "1.0", permissions: ["storage"], content_scripts: [{ matches: ["https://example.test/*"], js: ["content.js"] }] }); |
| 14 | async function fixture(t) { const root = await mkdtemp(path.join(tmpdir(), "cage-windows-extension-")); t.after(() => rm(root, { recursive: true, force: true })); return root; } |
| 15 | |
| 16 | test("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 | |
| 24 | test("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 | |
| 32 | test("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"))); |
| 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 | |
| 54 | test("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 | }); |