|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { |
| 6 | + DEFAULT_PERMISSION_EXEC_TIMEOUT_MS, |
| 7 | + executePermissionCommand, |
| 8 | +} from "../src/permission-exec.js"; |
| 9 | +import { inspectPathPermissions } from "../src/permissions.js"; |
| 10 | +import { readSecureFile } from "../src/secure-file.js"; |
| 11 | + |
| 12 | +const tempDirs: string[] = []; |
| 13 | + |
| 14 | +afterEach(async () => { |
| 15 | + await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true }))); |
| 16 | +}); |
| 17 | + |
| 18 | +describe("Windows permission command execution", () => { |
| 19 | + it("terminates a wedged child process on a deterministic deadline", async () => { |
| 20 | + const timeoutMs = 100; |
| 21 | + const startedAt = performance.now(); |
| 22 | + |
| 23 | + await expect( |
| 24 | + executePermissionCommand(process.execPath, ["-e", "setInterval(() => {}, 1_000)"], timeoutMs), |
| 25 | + ).rejects.toThrow(`Windows permission inspection timed out after ${timeoutMs}ms`); |
| 26 | + |
| 27 | + expect(performance.now() - startedAt).toBeLessThan(3_000); |
| 28 | + expect(DEFAULT_PERMISSION_EXEC_TIMEOUT_MS).toBe(10_000); |
| 29 | + }); |
| 30 | + |
| 31 | + it("fails closed without starting an ACL query when owner inspection fails", async () => { |
| 32 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "fs-safe-permission-timeout-")); |
| 33 | + tempDirs.push(dir); |
| 34 | + const target = path.join(dir, "secret.json"); |
| 35 | + await fs.writeFile(target, "{}", { mode: 0o600 }); |
| 36 | + const exec = vi.fn().mockRejectedValue(new Error("owner query timed out")); |
| 37 | + |
| 38 | + const result = await inspectPathPermissions(target, { |
| 39 | + platform: "win32", |
| 40 | + env: { SystemRoot: "C:\\Windows" }, |
| 41 | + exec, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(result).toMatchObject({ |
| 45 | + ok: true, |
| 46 | + source: "unknown", |
| 47 | + ownerError: "Error: owner query timed out", |
| 48 | + error: expect.stringContaining("Windows owner inspection failed"), |
| 49 | + }); |
| 50 | + expect(exec).toHaveBeenCalledTimes(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it("inspects permissions once for one secure read", async () => { |
| 54 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "fs-safe-permission-count-")); |
| 55 | + tempDirs.push(dir); |
| 56 | + const target = path.join(dir, "secret.json"); |
| 57 | + await fs.writeFile(target, "{}", { mode: 0o600 }); |
| 58 | + const exec = vi.fn(async (command: string, args: string[]) => { |
| 59 | + if (command.toLowerCase().endsWith("powershell.exe")) { |
| 60 | + return { |
| 61 | + stdout: JSON.stringify({ |
| 62 | + ownerSid: "S-1-5-21-42", |
| 63 | + currentUserSid: "S-1-5-21-42", |
| 64 | + principalSids: [{ name: "S-1-5-21-42", sid: "S-1-5-21-42" }], |
| 65 | + principalTranslationFailed: false, |
| 66 | + remote: false, |
| 67 | + }), |
| 68 | + stderr: "", |
| 69 | + }; |
| 70 | + } |
| 71 | + return { stdout: `${args[0]} *S-1-5-21-42:(F)\n`, stderr: "" }; |
| 72 | + }); |
| 73 | + |
| 74 | + const result = await readSecureFile({ |
| 75 | + filePath: target, |
| 76 | + inject: { |
| 77 | + platform: "win32", |
| 78 | + env: { SystemRoot: "C:\\Windows" }, |
| 79 | + exec, |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result.permissions).toMatchObject({ source: "windows-acl", ownerTrusted: true }); |
| 84 | + expect(exec).toHaveBeenCalledTimes(2); |
| 85 | + expect(exec.mock.calls.map(([command]) => path.win32.basename(command))).toEqual([ |
| 86 | + "powershell.exe", |
| 87 | + "icacls.exe", |
| 88 | + ]); |
| 89 | + }); |
| 90 | +}); |
0 commit comments