workflow-draft-path.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import path from "node:path";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. isPreapprovedWorkflowDraftWrite,
  5. isWorkflowDraftPath,
  6. } from "../src/permission/workflow-draft-path.js";
  7. const WORKING_DIRECTORY = "/ws/project";
  8. describe("isWorkflowDraftPath", () => {
  9. it("accepts a relative path resolved against the working directory", () => {
  10. expect(
  11. isWorkflowDraftPath({
  12. filePath: ".zcode/workflow-drafts/report.dwf.ts",
  13. pathModule: path.posix,
  14. workingDirectory: WORKING_DIRECTORY,
  15. }),
  16. ).toBe(true);
  17. });
  18. it("accepts an absolute path inside the drafts directory", () => {
  19. expect(
  20. isWorkflowDraftPath({
  21. filePath: "/ws/project/.zcode/workflow-drafts/report.dwf.ts",
  22. pathModule: path.posix,
  23. workingDirectory: WORKING_DIRECTORY,
  24. }),
  25. ).toBe(true);
  26. });
  27. it("accepts a file nested below the drafts directory", () => {
  28. expect(
  29. isWorkflowDraftPath({
  30. filePath: ".zcode/workflow-drafts/nested/report.dwf.ts",
  31. pathModule: path.posix,
  32. workingDirectory: WORKING_DIRECTORY,
  33. }),
  34. ).toBe(true);
  35. });
  36. it("rejects the drafts directory itself", () => {
  37. expect(
  38. isWorkflowDraftPath({
  39. filePath: ".zcode/workflow-drafts",
  40. pathModule: path.posix,
  41. workingDirectory: WORKING_DIRECTORY,
  42. }),
  43. ).toBe(false);
  44. });
  45. it("rejects a traversal that climbs back out of the drafts directory", () => {
  46. expect(
  47. isWorkflowDraftPath({
  48. filePath: ".zcode/workflow-drafts/../../secrets.env",
  49. pathModule: path.posix,
  50. workingDirectory: WORKING_DIRECTORY,
  51. }),
  52. ).toBe(false);
  53. });
  54. // 前缀不是包含:目录名只是另一个目录的前缀时必须落空,否则 `-other` 这类同级目录会被
  55. // 顺带免掉确认。
  56. it("rejects a sibling directory that merely shares the name prefix", () => {
  57. expect(
  58. isWorkflowDraftPath({
  59. filePath: ".zcode/workflow-drafts-other/report.dwf.ts",
  60. pathModule: path.posix,
  61. workingDirectory: WORKING_DIRECTORY,
  62. }),
  63. ).toBe(false);
  64. });
  65. it("rejects the sibling definitions directory", () => {
  66. expect(
  67. isWorkflowDraftPath({
  68. filePath: ".zcode/workflows/report.dwf.ts",
  69. pathModule: path.posix,
  70. workingDirectory: WORKING_DIRECTORY,
  71. }),
  72. ).toBe(false);
  73. });
  74. it("rejects another project's drafts directory", () => {
  75. expect(
  76. isWorkflowDraftPath({
  77. filePath: "/ws/other/.zcode/workflow-drafts/report.dwf.ts",
  78. pathModule: path.posix,
  79. workingDirectory: WORKING_DIRECTORY,
  80. }),
  81. ).toBe(false);
  82. });
  83. it("rejects an empty working directory and an empty file path", () => {
  84. expect(
  85. isWorkflowDraftPath({
  86. filePath: ".zcode/workflow-drafts/report.dwf.ts",
  87. pathModule: path.posix,
  88. workingDirectory: "",
  89. }),
  90. ).toBe(false);
  91. expect(
  92. isWorkflowDraftPath({
  93. filePath: "",
  94. pathModule: path.posix,
  95. workingDirectory: WORKING_DIRECTORY,
  96. }),
  97. ).toBe(false);
  98. });
  99. it("uses the current platform's path module by default", () => {
  100. expect(
  101. isWorkflowDraftPath({
  102. filePath: path.join(".zcode", "workflow-drafts", "report.dwf.ts"),
  103. workingDirectory: path.resolve("ws", "project"),
  104. }),
  105. ).toBe(true);
  106. });
  107. // Windows 语义单独跑一遍:反斜杠分隔、盘符绝对路径,以及跨盘时 `relative` 返回的是
  108. // 绝对路径而不是 `..`——少了 isAbsolute 那一条,D 盘的文件会被判成 C 盘草稿。
  109. describe("on Windows paths", () => {
  110. const windowsWorkingDirectory = "C:\\ws\\project";
  111. it("accepts a backslash-separated path inside the drafts directory", () => {
  112. expect(
  113. isWorkflowDraftPath({
  114. filePath: ".zcode\\workflow-drafts\\report.dwf.ts",
  115. pathModule: path.win32,
  116. workingDirectory: windowsWorkingDirectory,
  117. }),
  118. ).toBe(true);
  119. });
  120. it("accepts a drive-qualified absolute path inside the drafts directory", () => {
  121. expect(
  122. isWorkflowDraftPath({
  123. filePath: "C:\\ws\\project\\.zcode\\workflow-drafts\\report.dwf.ts",
  124. pathModule: path.win32,
  125. workingDirectory: windowsWorkingDirectory,
  126. }),
  127. ).toBe(true);
  128. });
  129. it("rejects a path on another drive", () => {
  130. expect(
  131. isWorkflowDraftPath({
  132. filePath: "D:\\ws\\project\\.zcode\\workflow-drafts\\report.dwf.ts",
  133. pathModule: path.win32,
  134. workingDirectory: windowsWorkingDirectory,
  135. }),
  136. ).toBe(false);
  137. });
  138. it("rejects a backslash traversal out of the drafts directory", () => {
  139. expect(
  140. isWorkflowDraftPath({
  141. filePath: ".zcode\\workflow-drafts\\..\\..\\secrets.env",
  142. pathModule: path.win32,
  143. workingDirectory: windowsWorkingDirectory,
  144. }),
  145. ).toBe(false);
  146. });
  147. it("rejects a sibling directory that merely shares the name prefix", () => {
  148. expect(
  149. isWorkflowDraftPath({
  150. filePath: ".zcode\\workflow-drafts-other\\report.dwf.ts",
  151. pathModule: path.win32,
  152. workingDirectory: windowsWorkingDirectory,
  153. }),
  154. ).toBe(false);
  155. });
  156. });
  157. });
  158. describe("isPreapprovedWorkflowDraftWrite", () => {
  159. const draftWrite = {
  160. input: { file_path: ".zcode/workflow-drafts/report.dwf.ts" },
  161. pathModule: path.posix,
  162. workingDirectory: WORKING_DIRECTORY,
  163. };
  164. it("covers Edit and Write", () => {
  165. expect(isPreapprovedWorkflowDraftWrite({ ...draftWrite, toolName: "Edit" })).toBe(true);
  166. expect(isPreapprovedWorkflowDraftWrite({ ...draftWrite, toolName: "Write" })).toBe(true);
  167. });
  168. it("covers no other tool, including the patch tool that shares the edit permission", () => {
  169. for (const toolName of ["ApplyPatch", "Bash", "Read", "NotebookEdit"]) {
  170. expect(isPreapprovedWorkflowDraftWrite({ ...draftWrite, toolName }), toolName).toBe(false);
  171. }
  172. });
  173. it("needs a working directory in the context", () => {
  174. expect(
  175. isPreapprovedWorkflowDraftWrite({
  176. ...draftWrite,
  177. toolName: "Edit",
  178. workingDirectory: undefined,
  179. }),
  180. ).toBe(false);
  181. });
  182. it("needs a string file_path in the input", () => {
  183. for (const input of [undefined, null, "a string input", {}, { file_path: 42 }]) {
  184. expect(
  185. isPreapprovedWorkflowDraftWrite({ ...draftWrite, input, toolName: "Edit" }),
  186. JSON.stringify(input) ?? "undefined",
  187. ).toBe(false);
  188. }
  189. });
  190. });