workspace-hook-trust-protocol.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { describe, expect, it, vi } from "vitest";
  2. import { InMemoryWorkspaceHookPolicyProvider } from "@zcode/core";
  3. import { grantWorkspaceHookTrustForProtocol } from "../src/zcode-protocol/workspace-hook-trust.js";
  4. import { notifyWorkspaceHookTrustGrantSessions } from "../src/zcode-protocol/server.js";
  5. const BUNDLE = "b".repeat(64);
  6. const DIGEST = "a".repeat(64);
  7. function userDecidesPolicyProvider(): InMemoryWorkspaceHookPolicyProvider {
  8. return new InMemoryWorkspaceHookPolicyProvider({
  9. mode: "user_decides",
  10. policyRevision: "test:user-decides:v1",
  11. });
  12. }
  13. describe("workspace Hook Trust protocol operation", () => {
  14. it("maps the trusted host workspace request to exact canonical pretrust input", async () => {
  15. const grant = vi.fn(async () => ({
  16. workspacePath: "/repo",
  17. workspaceIdentity: "workspace:repo",
  18. bundleDigest: BUNDLE,
  19. reasonCode: "workspace_hooks_trusted_persistent" as const,
  20. items: [
  21. {
  22. reviewItemId: "item-0",
  23. event: "SessionStart",
  24. matcher: null,
  25. displayCommand: "echo project",
  26. sourcePath: ".zcode/config.json",
  27. configuredEnabled: false,
  28. hookDeclarationDigest: DIGEST,
  29. trustState: "trusted_persistent" as const,
  30. },
  31. ],
  32. }));
  33. await expect(
  34. grantWorkspaceHookTrustForProtocol(
  35. {
  36. workspace: {
  37. workspacePath: "/repo",
  38. workspaceIdentity: "workspace:repo",
  39. workspaceKey: "workspace:repo",
  40. },
  41. bundleDigest: BUNDLE,
  42. hookDeclarationDigest: DIGEST,
  43. },
  44. { appVersion: "test", grant, policyProvider: userDecidesPolicyProvider() },
  45. ),
  46. ).resolves.toEqual({ accepted: true });
  47. expect(grant).toHaveBeenCalledWith({
  48. workspacePath: "/repo",
  49. workspaceIdentity: "workspace:repo",
  50. bundleDigest: BUNDLE,
  51. hookDeclarationDigests: [DIGEST],
  52. appVersion: "test",
  53. });
  54. });
  55. it.each([
  56. {
  57. policy: {
  58. mode: "deny" as const,
  59. reason: "managed",
  60. policyRevision: "test:deny:v1",
  61. },
  62. reasonCode: "workspace_hooks_blocked_by_policy",
  63. },
  64. {
  65. policy: {
  66. mode: "allow_trusted_only" as const,
  67. policyRevision: "test:pretrust:v1",
  68. },
  69. reasonCode: "workspace_hooks_policy_requires_pretrust",
  70. },
  71. ])(
  72. "rejects Settings pretrust before mutation for $policy.mode",
  73. async ({ policy, reasonCode }) => {
  74. const grant = vi.fn();
  75. await expect(
  76. grantWorkspaceHookTrustForProtocol(
  77. {
  78. workspace: {
  79. workspacePath: "/repo",
  80. workspaceIdentity: "workspace:repo",
  81. workspaceKey: "workspace:repo",
  82. },
  83. bundleDigest: BUNDLE,
  84. hookDeclarationDigest: DIGEST,
  85. },
  86. {
  87. grant,
  88. policyProvider: new InMemoryWorkspaceHookPolicyProvider(policy),
  89. },
  90. ),
  91. ).resolves.toEqual({ accepted: false, reasonCode });
  92. expect(grant).not.toHaveBeenCalled();
  93. },
  94. );
  95. it("fails closed before mutation when the managed policy provider is unavailable", async () => {
  96. const grant = vi.fn();
  97. await expect(
  98. grantWorkspaceHookTrustForProtocol(
  99. {
  100. workspace: { workspacePath: "/repo", workspaceKey: "/repo" },
  101. bundleDigest: BUNDLE,
  102. hookDeclarationDigest: DIGEST,
  103. },
  104. {
  105. grant,
  106. policyProvider: {
  107. getPolicy: () => {
  108. throw new Error("managed policy unavailable");
  109. },
  110. subscribe: () => () => undefined,
  111. },
  112. },
  113. ),
  114. ).resolves.toEqual({
  115. accepted: false,
  116. reasonCode: "workspace_hooks_blocked_by_policy",
  117. });
  118. expect(grant).not.toHaveBeenCalled();
  119. });
  120. it("returns a stable rejection for a known public reason code", async () => {
  121. await expect(
  122. grantWorkspaceHookTrustForProtocol(
  123. {
  124. workspace: { workspacePath: "/repo", workspaceKey: "/repo" },
  125. bundleDigest: BUNDLE,
  126. hookDeclarationDigest: DIGEST,
  127. },
  128. {
  129. policyProvider: userDecidesPolicyProvider(),
  130. grant: vi.fn(async () => {
  131. throw new Error("workspace_hooks_bundle_changed");
  132. }),
  133. },
  134. ),
  135. ).resolves.toEqual({
  136. accepted: false,
  137. reasonCode: "workspace_hooks_bundle_changed",
  138. });
  139. });
  140. it("redacts absolute paths and arbitrary implementation messages from protocol reasonCode", async () => {
  141. const internalMessage =
  142. "Unable to read Workspace Hook config: /Users/alice/private/repo/.zcode/config.json";
  143. const result = await grantWorkspaceHookTrustForProtocol(
  144. {
  145. workspace: { workspacePath: "/repo", workspaceKey: "/repo" },
  146. bundleDigest: BUNDLE,
  147. hookDeclarationDigest: DIGEST,
  148. },
  149. {
  150. policyProvider: userDecidesPolicyProvider(),
  151. grant: vi.fn(async () => {
  152. throw new Error(internalMessage);
  153. }),
  154. },
  155. );
  156. expect(result).toEqual({
  157. accepted: false,
  158. reasonCode: "workspace_hooks_config_unreadable",
  159. });
  160. });
  161. it("does not trust arbitrary workspace_hooks_ prefixed implementation messages", async () => {
  162. await expect(
  163. grantWorkspaceHookTrustForProtocol(
  164. {
  165. workspace: { workspacePath: "/repo", workspaceKey: "/repo" },
  166. bundleDigest: BUNDLE,
  167. hookDeclarationDigest: DIGEST,
  168. },
  169. {
  170. policyProvider: userDecidesPolicyProvider(),
  171. grant: vi.fn(async () => {
  172. throw new Error("workspace_hooks_internal_path_/Users/alice");
  173. }),
  174. },
  175. ),
  176. ).resolves.toEqual({
  177. accepted: false,
  178. reasonCode: "workspace_hooks_config_unreadable",
  179. });
  180. });
  181. });
  182. // P2 回归:Settings pretrust 授权成功后,server 必须按 workspaceKey 通知同 workspace
  183. // 的全部活跃 session 重载 Trust store(否则运行中 session 的 coordinator 内存镜像
  184. // 停留旧值:已信任 Hook 继续被拒、banner pendingCount 不刷新)。
  185. describe("notifyWorkspaceHookTrustGrantSessions fan-out", () => {
  186. function fakeSessionRecord(
  187. workspaceKey: string,
  188. workspacePath: string,
  189. workspaceIdentity?: string,
  190. ) {
  191. const reload = vi.fn(async () => undefined);
  192. return {
  193. reload,
  194. record: {
  195. app: { reloadWorkspaceHookTrust: reload },
  196. workspace: {
  197. workspaceKey,
  198. workspacePath,
  199. ...(workspaceIdentity ? { workspaceIdentity } : {}),
  200. },
  201. } as never,
  202. };
  203. }
  204. it("notifies every live session of the granted workspace and skips others", async () => {
  205. const grantedA1 = fakeSessionRecord("workspace:repo-a", "/repo-a", "workspace:repo-a");
  206. const grantedA2 = fakeSessionRecord("workspace:repo-a", "/repo-a", "workspace:repo-a");
  207. const otherB = fakeSessionRecord("workspace:repo-b", "/repo-b", "workspace:repo-b");
  208. const sessions = new Map([
  209. ["s-a1", grantedA1.record],
  210. ["s-a2", grantedA2.record],
  211. ["s-b1", otherB.record],
  212. ]);
  213. await notifyWorkspaceHookTrustGrantSessions({
  214. grantedWorkspaceKey: "workspace:repo-a",
  215. sessions,
  216. });
  217. expect(grantedA1.reload).toHaveBeenCalledTimes(1);
  218. expect(grantedA2.reload).toHaveBeenCalledTimes(1);
  219. expect(otherB.reload).not.toHaveBeenCalled();
  220. });
  221. it("is a safe no-op without a granted workspace key", async () => {
  222. const session = fakeSessionRecord("workspace:repo-a", "/repo-a");
  223. await notifyWorkspaceHookTrustGrantSessions({
  224. grantedWorkspaceKey: undefined,
  225. sessions: new Map([["s1", session.record]]),
  226. });
  227. expect(session.reload).not.toHaveBeenCalled();
  228. });
  229. });