workspace-hook-trust-cli.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import {
  6. grantWorkspaceHookTrust,
  7. inspectWorkspaceHookTrust,
  8. revokeWorkspaceHookTrustCli,
  9. } from "../src/workspace-hook-trust-cli.js";
  10. const roots: string[] = [];
  11. afterEach(async () => {
  12. await Promise.all(roots.splice(0).map((path) => rm(path, { recursive: true, force: true })));
  13. });
  14. async function fixture() {
  15. const root = await mkdtemp(join(tmpdir(), "zcode-hook-trust-cli-"));
  16. roots.push(root);
  17. const workspacePath = join(root, "workspace");
  18. const storagePath = join(root, "user-data");
  19. const userConfigPath = join(root, "config.json");
  20. await mkdir(join(workspacePath, ".zcode"), { recursive: true });
  21. await writeFile(
  22. userConfigPath,
  23. JSON.stringify({ storage: { dir: storagePath }, hooks: { enabled: true, events: {} } }),
  24. );
  25. await writeFile(
  26. join(workspacePath, ".zcode", "config.json"),
  27. JSON.stringify({
  28. hooks: {
  29. enabled: true,
  30. events: {
  31. SessionStart: [
  32. {
  33. matcher: "startup",
  34. // process 写法在所有平台都绑定入口(Windows 上 command 不绑定),Windows CI 上断言同样成立。
  35. hooks: [{ type: "process", command: "sh", args: ["start.sh"] }],
  36. },
  37. ],
  38. },
  39. },
  40. }),
  41. );
  42. // 入口脚本必须存在才可授权(D24)。
  43. await writeFile(join(workspacePath, "start.sh"), "echo one\n", "utf8");
  44. return { storagePath, workspacePath, userConfigPath };
  45. }
  46. describe("Workspace Hook Trust CLI domain", () => {
  47. it("status exposes exact bundle/declaration digests and exact grant/revoke persists", async () => {
  48. const target = await fixture();
  49. const pending = await inspectWorkspaceHookTrust(target);
  50. expect(pending.reasonCode).toBe("workspace_hooks_pending_trust");
  51. expect(pending.bundleDigest).toMatch(/^[a-f0-9]{64}$/u);
  52. expect(pending.items).toHaveLength(1);
  53. const digest = pending.items[0]!.hookDeclarationDigest;
  54. const granted = await grantWorkspaceHookTrust({
  55. ...target,
  56. hookDeclarationDigests: [digest],
  57. appVersion: "test",
  58. });
  59. expect(granted.reasonCode).toBe("workspace_hooks_trusted_persistent");
  60. expect(granted.items[0]?.trustState).toBe("trusted_persistent");
  61. const revoked = await revokeWorkspaceHookTrustCli({
  62. ...target,
  63. hookDeclarationDigests: [digest],
  64. });
  65. expect(revoked.reasonCode).toBe("workspace_hooks_pending_trust");
  66. });
  67. it("all-current rejects a stale bundle without writing Trust", async () => {
  68. const target = await fixture();
  69. await expect(
  70. grantWorkspaceHookTrust({
  71. ...target,
  72. allCurrent: true,
  73. bundleDigest: "f".repeat(64),
  74. }),
  75. ).rejects.toThrow("workspace_hooks_bundle_changed");
  76. expect((await inspectWorkspaceHookTrust(target)).items[0]?.trustState).toBe("pending_trust");
  77. });
  78. it("exact grant rejects a stale Settings bundle before writing Trust", async () => {
  79. const target = await fixture();
  80. const pending = await inspectWorkspaceHookTrust(target);
  81. await expect(
  82. grantWorkspaceHookTrust({
  83. ...target,
  84. bundleDigest: "f".repeat(64),
  85. hookDeclarationDigests: [pending.items[0]!.hookDeclarationDigest],
  86. }),
  87. ).rejects.toThrow("workspace_hooks_bundle_changed");
  88. expect((await inspectWorkspaceHookTrust(target)).items[0]?.trustState).toBe("pending_trust");
  89. });
  90. it("corrupt Trust store rejects the first grant instead of silently replacing it", async () => {
  91. const target = await fixture();
  92. const pending = await inspectWorkspaceHookTrust(target);
  93. const securityPath = join(target.storagePath, "security");
  94. await mkdir(securityPath, { recursive: true });
  95. await writeFile(join(securityPath, "workspace-hook-trust-v1.json"), "{ invalid json");
  96. await expect(
  97. grantWorkspaceHookTrust({
  98. ...target,
  99. bundleDigest: pending.bundleDigest!,
  100. hookDeclarationDigests: [pending.items[0]!.hookDeclarationDigest],
  101. }),
  102. ).rejects.toThrow("workspace_hooks_trust_store_corrupt");
  103. });
  104. // SG-02 回归:status 对损坏 store 必须显式报 corrupt,不能伪装成 pending_trust——
  105. // 否则用户按提示执行 grant 只会得到 trust_store_corrupt,恢复指引自相矛盾。
  106. it("status surfaces corrupt Trust store as its own reasonCode instead of pending_trust", async () => {
  107. const target = await fixture();
  108. const securityPath = join(target.storagePath, "security");
  109. await mkdir(securityPath, { recursive: true });
  110. // JSON 合法但结构非法(缺 schemaVersion/必需字段),schema 校验失败同样 corrupt。
  111. await writeFile(
  112. join(securityPath, "workspace-hook-trust-v1.json"),
  113. JSON.stringify({ schemaVersion: 1, records: [{ workspaceIdentity: "/anywhere" }] }),
  114. );
  115. const status = await inspectWorkspaceHookTrust(target);
  116. expect(status.reasonCode).toBe("workspace_hooks_trust_store_corrupt");
  117. // corrupt 下任何 digest 都不可信:全部条目不得展示 trusted_persistent。
  118. for (const item of status.items) {
  119. expect(item.trustState).toBe("pending_trust");
  120. }
  121. });
  122. });
  123. /**
  124. * D24(ZP-20261110-06):入口脚本不存在或无法读取时没有可审核的内容,拒绝授权;
  125. * 已授权脚本内容变化后旧 Trust 不再命中。
  126. */
  127. describe("Workspace Hook Trust CLI 入口脚本绑定(D24)", () => {
  128. it("脚本内容变化后旧 Trust 不命中,重新授权后恢复", async () => {
  129. const target = await fixture();
  130. const pending = await inspectWorkspaceHookTrust(target);
  131. await grantWorkspaceHookTrust({
  132. ...target,
  133. hookDeclarationDigests: [pending.items[0]!.hookDeclarationDigest],
  134. });
  135. expect((await inspectWorkspaceHookTrust(target)).items[0]?.trustState).toBe(
  136. "trusted_persistent",
  137. );
  138. await writeFile(join(target.workspacePath, "start.sh"), "echo two\n", "utf8");
  139. const changed = await inspectWorkspaceHookTrust(target);
  140. expect(changed.items[0]?.trustState).toBe("pending_trust");
  141. expect(changed.items[0]?.hookDeclarationDigest).not.toBe(
  142. pending.items[0]!.hookDeclarationDigest,
  143. );
  144. await grantWorkspaceHookTrust({
  145. ...target,
  146. hookDeclarationDigests: [changed.items[0]!.hookDeclarationDigest],
  147. });
  148. expect((await inspectWorkspaceHookTrust(target)).items[0]?.trustState).toBe(
  149. "trusted_persistent",
  150. );
  151. });
  152. it("脚本不存在:status 为 blocked_untrusted,exact 与 all-current 授权都拒绝且不写 store", async () => {
  153. const target = await fixture();
  154. await rm(join(target.workspacePath, "start.sh"));
  155. const status = await inspectWorkspaceHookTrust(target);
  156. expect(status.items[0]?.trustState).toBe("blocked_untrusted");
  157. expect(status.reasonCode).toBe("workspace_hook_script_unavailable");
  158. await expect(
  159. grantWorkspaceHookTrust({
  160. ...target,
  161. hookDeclarationDigests: [status.items[0]!.hookDeclarationDigest],
  162. }),
  163. ).rejects.toThrow("workspace_hook_script_unavailable");
  164. await expect(
  165. grantWorkspaceHookTrust({
  166. ...target,
  167. allCurrent: true,
  168. bundleDigest: status.bundleDigest!,
  169. }),
  170. ).rejects.toMatchObject({
  171. message: "workspace_hook_script_unavailable",
  172. unavailableItems: [expect.stringContaining("sh start.sh")],
  173. });
  174. // 文件出现后按实际内容生成新 digest,由用户首次授权。
  175. await writeFile(join(target.workspacePath, "start.sh"), "echo one\n", "utf8");
  176. const created = await inspectWorkspaceHookTrust(target);
  177. expect(created.items[0]?.trustState).toBe("pending_trust");
  178. expect(created.items[0]?.hookDeclarationDigest).not.toBe(
  179. status.items[0]!.hookDeclarationDigest,
  180. );
  181. });
  182. it("已授权脚本被删除:旧 Trust 不再放行", async () => {
  183. const target = await fixture();
  184. const pending = await inspectWorkspaceHookTrust(target);
  185. await grantWorkspaceHookTrust({
  186. ...target,
  187. hookDeclarationDigests: [pending.items[0]!.hookDeclarationDigest],
  188. });
  189. await rm(join(target.workspacePath, "start.sh"));
  190. expect((await inspectWorkspaceHookTrust(target)).items[0]?.trustState).toBe(
  191. "blocked_untrusted",
  192. );
  193. });
  194. });