guarded-mode.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. CronAutomationSchema,
  4. inheritPermissionMode,
  5. restrictPermissionMode,
  6. type CollaborationMode,
  7. } from "@zcode/contracts";
  8. import { PermissionService } from "../src/permission/service.js";
  9. import { bashToolEntry } from "../src/tool/handlers/bash.js";
  10. describe("Guarded is an opt-in mode, not a global gate", () => {
  11. it("independent Plan keeps its restriction over guarded dangerous commands", () => {
  12. const service = new PermissionService();
  13. const context = {
  14. toolName: "Bash",
  15. input: { command: "rm -rf build" },
  16. mode: "guarded" as const,
  17. riskLevel: "high" as const,
  18. };
  19. const capability = {
  20. ...bashToolEntry.metadata,
  21. userApprovalRule: "safety.bash.remove-critical-path",
  22. };
  23. expect(service.checkPermission({ ...context, planEnabled: true }, capability)).toMatchObject({
  24. decision: "deny",
  25. ruleId: "mode.plan.nonReadOnly",
  26. });
  27. expect(service.checkPermission({ ...context, planEnabled: false }, capability)).toMatchObject({
  28. decision: "ask",
  29. approvalMode: "user-once",
  30. });
  31. });
  32. it("guarded retains workflow alwaysAsk and ordinary session grants", () => {
  33. const service = new PermissionService();
  34. const context = {
  35. toolName: "CreateWorkflow",
  36. input: {},
  37. mode: "guarded" as const,
  38. riskLevel: "high" as const,
  39. };
  40. const capability = { alwaysAsk: true };
  41. expect(service.checkPermission(context, capability).decision).toBe("ask");
  42. service.grantSessionPermission([
  43. { type: "addRules", behavior: "allow", rules: [{ toolName: "CreateWorkflow" }] },
  44. ]);
  45. expect(service.checkPermission(context, capability).decision).toBe("allow");
  46. });
  47. it("automation records preserve explicit guarded/yolo and do not migrate absence", () => {
  48. const record = {
  49. automationId: "guarded-fixture",
  50. title: "fixture",
  51. cronExpr: "0 * * * *",
  52. prompt: "fixture",
  53. enabled: true,
  54. lifecycleStatus: "active",
  55. runCount: 0,
  56. recurring: true,
  57. };
  58. for (const mode of ["guarded", "yolo", undefined]) {
  59. const parsed = CronAutomationSchema.parse({ ...record, ...(mode ? { mode } : {}) });
  60. expect(parsed.mode).toBe(mode);
  61. }
  62. });
  63. it.each(["guarded", "yolo", "build", "edit", "plan", "auto"] as CollaborationMode[])(
  64. "inheritance from %s only transforms YOLO children",
  65. (parent) => {
  66. for (const child of [
  67. "yolo",
  68. "build",
  69. "edit",
  70. "plan",
  71. "auto",
  72. "guarded",
  73. ] as CollaborationMode[]) {
  74. expect(inheritPermissionMode(parent, child)).toBe(
  75. parent === "guarded" && child === "yolo" ? "guarded" : child,
  76. );
  77. }
  78. },
  79. );
  80. // 插件 profile 的 permissionMode 只能收紧:从严到宽 plan、build、edit、guarded、yolo;
  81. // auto 未实现,任一方是 auto 时都继承父会话模式。
  82. it.each([
  83. ["build", "yolo", "build"],
  84. ["build", "guarded", "build"],
  85. ["build", "edit", "build"],
  86. ["build", "build", "build"],
  87. ["build", "plan", "plan"],
  88. ["build", "auto", "build"],
  89. ["plan", "edit", "plan"],
  90. ["plan", "yolo", "plan"],
  91. ["edit", "yolo", "edit"],
  92. ["edit", "build", "build"],
  93. ["edit", "plan", "plan"],
  94. ["guarded", "yolo", "guarded"],
  95. ["guarded", "edit", "edit"],
  96. ["yolo", "yolo", "yolo"],
  97. ["yolo", "guarded", "guarded"],
  98. ["yolo", "plan", "plan"],
  99. ["yolo", "auto", "yolo"],
  100. ["auto", "plan", "auto"],
  101. ["auto", "yolo", "auto"],
  102. ] as Array<[CollaborationMode, CollaborationMode, CollaborationMode]>)(
  103. "restricting parent %s with child %s yields %s",
  104. (parent, child, expected) => {
  105. expect(restrictPermissionMode(parent, child)).toBe(expected);
  106. },
  107. );
  108. it("requires once approval only in guarded and preserves YOLO baseline for nonmatches", () => {
  109. const service = new PermissionService();
  110. for (const mode of ["guarded", "yolo"] as const) {
  111. for (const command of ["rm -rf build", "echo hello", "bash -c 'rm -rf build'"]) {
  112. const input = { command };
  113. const capability = bashToolEntry.resolvePermissionCapability?.(input, {
  114. mode,
  115. bashShellSelection: {
  116. dialect: "posix",
  117. source: "auto-detected",
  118. display: { name: "bash" },
  119. },
  120. });
  121. const decision = service.checkPermission(
  122. { toolName: "Bash", input, mode, riskLevel: "high" },
  123. { ...bashToolEntry.metadata, ...capability },
  124. );
  125. expect(decision.decision).toBe(
  126. mode === "guarded" && command === "rm -rf build" ? "ask" : "allow",
  127. );
  128. expect(decision.approvalMode).toBe(
  129. mode === "guarded" && command === "rm -rf build" ? "user-once" : undefined,
  130. );
  131. }
  132. }
  133. });
  134. // 2026-09-17 review:selection 缺失是基础设施状态,不能让 guarded 静默退回 YOLO。
  135. it("matches with POSIX grammar when the session shell selection is not yet available", () => {
  136. const input = { command: "rm -rf build" };
  137. const capability = bashToolEntry.resolvePermissionCapability?.(input, {
  138. mode: "guarded",
  139. });
  140. expect(capability).toEqual({
  141. userApprovalRule: "safety.bash.remove-critical-path",
  142. });
  143. const legacy = bashToolEntry.resolvePermissionCapability?.(input, {
  144. mode: "guarded",
  145. bashShellSelection: {
  146. dialect: "legacy-shell",
  147. source: "legacy-fallback",
  148. display: { name: "system shell" },
  149. },
  150. });
  151. expect(legacy).toBeUndefined();
  152. });
  153. });