signWindowsHook.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { afterEach, beforeEach, expect, it, vi } from "vitest";
  2. import { mkdtemp, rm, writeFile } from "node:fs/promises";
  3. import { createRequire } from "node:module";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. const nodeRequire = createRequire(import.meta.url);
  7. // 内置模块的 CJS 导出对象是全局单例,钩子在调用点 require,
  8. // 测试通过替换该对象上的 spawnSync 拦截真实进程。
  9. const childProcess = nodeRequire("node:child_process") as {
  10. spawnSync: ReturnType<typeof vi.fn>;
  11. };
  12. import signWindowsHook from "../scripts/sign-windows-hook.cjs";
  13. const { isWindowsPortableExecutable } = signWindowsHook as unknown as {
  14. isWindowsPortableExecutable: (filePath: string) => Promise<boolean>;
  15. };
  16. const spawnSyncMock = vi.fn();
  17. beforeEach(() => {
  18. spawnSyncMock.mockReset();
  19. vi.spyOn(childProcess, "spawnSync").mockImplementation((...args) =>
  20. spawnSyncMock(...(args as Parameters<typeof spawnSyncMock>)),
  21. );
  22. });
  23. afterEach(() => {
  24. vi.restoreAllMocks();
  25. });
  26. async function createTempFile(name: string, magic: Buffer): Promise<string> {
  27. const dir = await mkdtemp(join(tmpdir(), "zcode-sign-hook-"));
  28. const filePath = join(dir, name);
  29. await writeFile(filePath, magic);
  30. return filePath;
  31. }
  32. async function cleanupTempFile(filePath: string): Promise<void> {
  33. await rm(join(filePath, ".."), { recursive: true, force: true });
  34. }
  35. it("isNest 调用直接跳过,不触发 vsigntool", async () => {
  36. await signWindowsHook({ path: "C:\\app\\ZCode.exe", isNest: true });
  37. expect(spawnSyncMock).not.toHaveBeenCalled();
  38. });
  39. it("非 PE 二进制(Mach-O/ELF)跳过签名,不触发 vsigntool", async () => {
  40. const machO = await createTempFile(
  41. "sharp-darwin-arm64.node",
  42. Buffer.from([0xcf, 0xfa, 0xed, 0xfe, 0x00, 0x00]),
  43. );
  44. const elf = await createTempFile("sharp-linux-x64.node", Buffer.from([0x7f, 0x45, 0x4c, 0x46]));
  45. try {
  46. await signWindowsHook({ path: machO });
  47. await signWindowsHook({ path: elf });
  48. expect(spawnSyncMock).not.toHaveBeenCalled();
  49. } finally {
  50. await cleanupTempFile(machO);
  51. await cleanupTempFile(elf);
  52. }
  53. });
  54. it("PE(MZ)文件正常交给 vsigntool 签名", async () => {
  55. const exe = await createTempFile("ZCode.exe", Buffer.from([0x4d, 0x5a, 0x90, 0x00]));
  56. spawnSyncMock.mockReturnValue({ status: 0 });
  57. try {
  58. await signWindowsHook({ path: exe });
  59. expect(spawnSyncMock).toHaveBeenCalledTimes(1);
  60. const [command, args] = spawnSyncMock.mock.calls[0] as unknown as [string, string[], unknown];
  61. expect(command).toBe("powershell.exe");
  62. expect(args).toContain("-FilePath");
  63. expect(args).toContain(exe);
  64. } finally {
  65. await cleanupTempFile(exe);
  66. }
  67. });
  68. it("vsigntool 失败时抛出包含文件路径的错误", async () => {
  69. const dll = await createTempFile("koffi.node", Buffer.from([0x4d, 0x5a, 0x00, 0x00]));
  70. spawnSyncMock.mockReturnValue({ status: 1 });
  71. try {
  72. await expect(signWindowsHook({ path: dll })).rejects.toThrow(dll);
  73. } finally {
  74. await cleanupTempFile(dll);
  75. }
  76. });
  77. it("isWindowsPortableExecutable 按魔数判定;确认非 PE 与读取失败严格区分", async () => {
  78. const pe = await createTempFile("pe.node", Buffer.from([0x4d, 0x5a]));
  79. const machO = await createTempFile("macho.node", Buffer.from([0xcf, 0xfa, 0xed, 0xfe]));
  80. const truncated = await createTempFile("short.node", Buffer.from([0x4d]));
  81. try {
  82. await expect(isWindowsPortableExecutable(pe)).resolves.toBe(true);
  83. await expect(isWindowsPortableExecutable(machO)).resolves.toBe(false);
  84. // 成功读取且不足 2 字节:确认不可能是 MZ,按非 PE 处理。
  85. await expect(isWindowsPortableExecutable(truncated)).resolves.toBe(false);
  86. // 读取失败(文件不存在、路径是目录、权限/IO 错误)必须抛错,
  87. // 不能与“确认非 PE”混同——否则 PE 会带着签名成功的假象漏签(评审 CR-01)。
  88. await expect(isWindowsPortableExecutable(join(pe, "..", "missing.node"))).rejects.toThrow();
  89. await expect(isWindowsPortableExecutable(join(pe, ".."))).rejects.toThrow();
  90. // sign() 对读取失败同样向上抛,不调用 vsigntool 也不返回成功。
  91. await expect(signWindowsHook({ path: join(pe, "..", "missing.node") })).rejects.toThrow();
  92. expect(spawnSyncMock).not.toHaveBeenCalled();
  93. } finally {
  94. await cleanupTempFile(pe);
  95. await cleanupTempFile(machO);
  96. await cleanupTempFile(truncated);
  97. }
  98. });