workspace-hook-trust.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { resolve } from "node:path";
  2. import { workspaceHookPolicySchema } from "@zcode/contracts";
  3. import type { WorkspaceHookPolicyProvider } from "@zcode/core";
  4. import {
  5. zcodeWorkspaceHookTrustGrantParamsSchema,
  6. zcodeWorkspaceHookTrustGrantReasonCodeSchema,
  7. zcodeWorkspaceHookTrustGrantResultSchema,
  8. type ZCodeWorkspaceHookTrustGrantReasonCode,
  9. type ZCodeWorkspaceHookTrustGrantResult,
  10. } from "@zcode/shared";
  11. import {
  12. grantWorkspaceHookTrust,
  13. type WorkspaceHookTrustCliStatus,
  14. } from "../workspace-hook-trust-cli.js";
  15. type GrantWorkspaceHookTrust = typeof grantWorkspaceHookTrust;
  16. /**
  17. * 没有 task/session 时的 workspace 级 Trust authority。
  18. *
  19. * 可信 Host 只能提交 Settings 看到的精确 bundle/declaration;真正授权前仍由 Agent
  20. * 重新发现 canonical snapshot。这里不创建隐藏 task,也不接受 UI 直接提供的记录内容。
  21. */
  22. export async function grantWorkspaceHookTrustForProtocol(
  23. rawParams: unknown,
  24. dependencies: {
  25. appVersion?: string;
  26. grant?: GrantWorkspaceHookTrust;
  27. policyProvider: WorkspaceHookPolicyProvider;
  28. },
  29. ): Promise<ZCodeWorkspaceHookTrustGrantResult> {
  30. const params = zcodeWorkspaceHookTrustGrantParamsSchema.parse(rawParams);
  31. const grant = dependencies.grant ?? grantWorkspaceHookTrust;
  32. const workspaceIdentity =
  33. params.workspace.workspaceIdentity?.trim() || resolve(params.workspace.workspacePath);
  34. const policyRejection = getPolicyRejectionReason(
  35. dependencies.policyProvider,
  36. workspaceIdentity,
  37. );
  38. if (policyRejection) {
  39. return zcodeWorkspaceHookTrustGrantResultSchema.parse({
  40. accepted: false,
  41. reasonCode: policyRejection,
  42. });
  43. }
  44. try {
  45. const status = await grant({
  46. workspacePath: params.workspace.workspacePath,
  47. ...(params.workspace.workspaceIdentity
  48. ? { workspaceIdentity: params.workspace.workspaceIdentity }
  49. : {}),
  50. bundleDigest: params.bundleDigest,
  51. hookDeclarationDigests: [params.hookDeclarationDigest],
  52. ...(dependencies.appVersion ? { appVersion: dependencies.appVersion } : {}),
  53. });
  54. return zcodeWorkspaceHookTrustGrantResultSchema.parse(
  55. didGrantExactDeclaration(status, params.hookDeclarationDigest)
  56. ? { accepted: true }
  57. : { accepted: false, reasonCode: toPublicReasonCode(status.reasonCode) },
  58. );
  59. } catch (error) {
  60. return zcodeWorkspaceHookTrustGrantResultSchema.parse({
  61. accepted: false,
  62. reasonCode: toPublicReasonCode(error),
  63. });
  64. }
  65. }
  66. function getPolicyRejectionReason(
  67. policyProvider: WorkspaceHookPolicyProvider,
  68. workspaceIdentity: string,
  69. ): ZCodeWorkspaceHookTrustGrantReasonCode | undefined {
  70. try {
  71. const policy = workspaceHookPolicySchema.parse(
  72. policyProvider.getPolicy(workspaceIdentity),
  73. );
  74. if (policy.mode === "user_decides") return undefined;
  75. return policy.mode === "allow_trusted_only"
  76. ? "workspace_hooks_policy_requires_pretrust"
  77. : "workspace_hooks_blocked_by_policy";
  78. } catch {
  79. // Settings 无 session 路径曾在重新发现后直接写 Trust store,绕过受信
  80. // embedder policy。policy provider 异常时也必须 fail closed,不能退回默认可授权。
  81. return "workspace_hooks_blocked_by_policy";
  82. }
  83. }
  84. function toPublicReasonCode(error: unknown): ZCodeWorkspaceHookTrustGrantReasonCode {
  85. const candidate = error instanceof Error ? error.message : error;
  86. const parsed = zcodeWorkspaceHookTrustGrantReasonCodeSchema.safeParse(candidate);
  87. if (parsed.success) return parsed.data;
  88. // 这里曾把底层 Error.message 直接放进 reasonCode,导致配置绝对路径和
  89. // 用户名越过 Agent/Host/UI 协议边界。未知异常只能收敛到公开稳定码,不能透传文本。
  90. return "workspace_hooks_config_unreadable";
  91. }
  92. function didGrantExactDeclaration(
  93. status: WorkspaceHookTrustCliStatus,
  94. hookDeclarationDigest: string,
  95. ): boolean {
  96. return status.items.some(
  97. (item) =>
  98. item.hookDeclarationDigest === hookDeclarationDigest &&
  99. item.trustState === "trusted_persistent",
  100. );
  101. }