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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. hooks: [{ type: "command", command: "./start.sh" }],
  35. },
  36. ],
  37. },
  38. },
  39. }),
  40. );
  41. return { storagePath, workspacePath, userConfigPath };
  42. }
  43. describe("Workspace Hook Trust CLI domain", () => {
  44. it("status exposes exact bundle/declaration digests and exact grant/revoke persists", async () => {
  45. const target = await fixture();
  46. const pending = await inspectWorkspaceHookTrust(target);
  47. expect(pending.reasonCode).toBe("workspace_hooks_pending_trust");
  48. expect(pending.bundleDigest).toMatch(/^[a-f0-9]{64}$/u);
  49. expect(pending.items).toHaveLength(1);
  50. const digest = pending.items[0]!.hookDeclarationDigest;
  51. const granted = await grantWorkspaceHookTrust({
  52. ...target,
  53. hookDeclarationDigests: [digest],
  54. appVersion: "test",
  55. });
  56. expect(granted.reasonCode).toBe("workspace_hooks_trusted_persistent");
  57. expect(granted.items[0]?.trustState).toBe("trusted_persistent");
  58. const revoked = await revokeWorkspaceHookTrustCli({
  59. ...target,
  60. hookDeclarationDigests: [digest],
  61. });
  62. expect(revoked.reasonCode).toBe("workspace_hooks_pending_trust");
  63. });
  64. it("all-current rejects a stale bundle without writing Trust", async () => {
  65. const target = await fixture();
  66. await expect(
  67. grantWorkspaceHookTrust({
  68. ...target,
  69. allCurrent: true,
  70. bundleDigest: "f".repeat(64),
  71. }),
  72. ).rejects.toThrow("workspace_hooks_bundle_changed");
  73. expect((await inspectWorkspaceHookTrust(target)).items[0]?.trustState).toBe("pending_trust");
  74. });
  75. it("exact grant rejects a stale Settings bundle before writing Trust", async () => {
  76. const target = await fixture();
  77. const pending = await inspectWorkspaceHookTrust(target);
  78. await expect(
  79. grantWorkspaceHookTrust({
  80. ...target,
  81. bundleDigest: "f".repeat(64),
  82. hookDeclarationDigests: [pending.items[0]!.hookDeclarationDigest],
  83. }),
  84. ).rejects.toThrow("workspace_hooks_bundle_changed");
  85. expect((await inspectWorkspaceHookTrust(target)).items[0]?.trustState).toBe("pending_trust");
  86. });
  87. it("corrupt Trust store rejects the first grant instead of silently replacing it", async () => {
  88. const target = await fixture();
  89. const pending = await inspectWorkspaceHookTrust(target);
  90. const securityPath = join(target.storagePath, "security");
  91. await mkdir(securityPath, { recursive: true });
  92. await writeFile(join(securityPath, "workspace-hook-trust-v1.json"), "{ invalid json");
  93. await expect(
  94. grantWorkspaceHookTrust({
  95. ...target,
  96. bundleDigest: pending.bundleDigest!,
  97. hookDeclarationDigests: [pending.items[0]!.hookDeclarationDigest],
  98. }),
  99. ).rejects.toThrow("workspace_hooks_trust_store_corrupt");
  100. });
  101. // SG-02 回归:status 对损坏 store 必须显式报 corrupt,不能伪装成 pending_trust——
  102. // 否则用户按提示执行 grant 只会得到 trust_store_corrupt,恢复指引自相矛盾。
  103. it("status surfaces corrupt Trust store as its own reasonCode instead of pending_trust", async () => {
  104. const target = await fixture();
  105. const securityPath = join(target.storagePath, "security");
  106. await mkdir(securityPath, { recursive: true });
  107. // JSON 合法但结构非法(缺 schemaVersion/必需字段),schema 校验失败同样 corrupt。
  108. await writeFile(
  109. join(securityPath, "workspace-hook-trust-v1.json"),
  110. JSON.stringify({ schemaVersion: 1, records: [{ workspaceIdentity: "/anywhere" }] }),
  111. );
  112. const status = await inspectWorkspaceHookTrust(target);
  113. expect(status.reasonCode).toBe("workspace_hooks_trust_store_corrupt");
  114. // corrupt 下任何 digest 都不可信:全部条目不得展示 trusted_persistent。
  115. for (const item of status.items) {
  116. expect(item.trustState).toBe("pending_trust");
  117. }
  118. });
  119. });