fs-workflow-draft-symlink.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // ZP-20261110-04:草稿目录的符号链接不得把免确认的草稿写入导向项目外。
  2. // 运行时证据:临时目录中真实建立符号链接,断言越界写入被拒绝且项目外文件不变。
  3. import { mkdir, mkdtemp, readdir, readFile, rm, symlink, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  7. import { createNodeFileSystemAdapter } from "@zcode/adapters/fs";
  8. const DIRECTORY_LINK_TYPE = process.platform === "win32" ? "junction" : "dir";
  9. const USER_CONFIG_TEXT = '{"hooks":{}}\n';
  10. const EVIL_CONFIG_TEXT =
  11. '{"hooks":{"SessionStart":[{"hooks":[{"type":"command","command":"evil"}]}]}}\n';
  12. let root: string;
  13. let project: string;
  14. let outside: string;
  15. beforeEach(async () => {
  16. root = await mkdtemp(join(tmpdir(), "zcode-draft-symlink-"));
  17. project = join(root, "project");
  18. outside = join(root, "outside");
  19. await mkdir(join(project, ".zcode"), { recursive: true });
  20. await mkdir(outside, { recursive: true });
  21. });
  22. afterEach(async () => {
  23. await rm(root, { recursive: true, force: true });
  24. });
  25. describe("writeTextFile symlinkFreeBase (workflow draft write guard)", () => {
  26. it("refuses a draft write when the drafts directory is a symlink out of the project", async () => {
  27. const userConfig = join(outside, "config.json");
  28. await writeFile(userConfig, USER_CONFIG_TEXT);
  29. await symlink(outside, join(project, ".zcode", "workflow-drafts"), DIRECTORY_LINK_TYPE);
  30. const fs = createNodeFileSystemAdapter();
  31. await expect(
  32. fs.writeTextFile({
  33. path: join(project, ".zcode", "workflow-drafts", "config.json"),
  34. content: EVIL_CONFIG_TEXT,
  35. createParents: true,
  36. atomic: true,
  37. symlinkFreeBase: project,
  38. }),
  39. ).rejects.toThrow(/symlink/i);
  40. expect(await readFile(userConfig, "utf8")).toBe(USER_CONFIG_TEXT);
  41. });
  42. it("refuses a draft write through a symlinked subdirectory of the drafts directory", async () => {
  43. const draftsDir = join(project, ".zcode", "workflow-drafts");
  44. await mkdir(draftsDir, { recursive: true });
  45. const userConfig = join(outside, "config.json");
  46. await writeFile(userConfig, USER_CONFIG_TEXT);
  47. await symlink(outside, join(draftsDir, "sub"), DIRECTORY_LINK_TYPE);
  48. const fs = createNodeFileSystemAdapter();
  49. await expect(
  50. fs.writeTextFile({
  51. path: join(draftsDir, "sub", "config.json"),
  52. content: EVIL_CONFIG_TEXT,
  53. createParents: true,
  54. atomic: true,
  55. symlinkFreeBase: project,
  56. }),
  57. ).rejects.toThrow(/symlink/i);
  58. expect(await readFile(userConfig, "utf8")).toBe(USER_CONFIG_TEXT);
  59. });
  60. it("does not create missing directories outside the project through a symlinked drafts directory", async () => {
  61. await symlink(outside, join(project, ".zcode", "workflow-drafts"), DIRECTORY_LINK_TYPE);
  62. const fs = createNodeFileSystemAdapter();
  63. await expect(
  64. fs.writeTextFile({
  65. path: join(project, ".zcode", "workflow-drafts", "newdir", "x.dwf.ts"),
  66. content: "export {};\n",
  67. createParents: true,
  68. atomic: true,
  69. symlinkFreeBase: project,
  70. }),
  71. ).rejects.toThrow(/symlink/i);
  72. expect(await readdir(outside)).toEqual([]);
  73. });
  74. it("refuses a draft write when .zcode itself is a symlink out of the project", async () => {
  75. await rm(join(project, ".zcode"), { recursive: true });
  76. await symlink(outside, join(project, ".zcode"), DIRECTORY_LINK_TYPE);
  77. const fs = createNodeFileSystemAdapter();
  78. await expect(
  79. fs.writeTextFile({
  80. path: join(project, ".zcode", "workflow-drafts", "config.json"),
  81. content: EVIL_CONFIG_TEXT,
  82. createParents: true,
  83. atomic: true,
  84. symlinkFreeBase: project,
  85. }),
  86. ).rejects.toThrow(/symlink/i);
  87. expect(await readdir(outside)).toEqual([]);
  88. });
  89. it("refuses a write whose target is not under symlinkFreeBase", async () => {
  90. const fs = createNodeFileSystemAdapter();
  91. await expect(
  92. fs.writeTextFile({
  93. path: join(outside, "config.json"),
  94. content: EVIL_CONFIG_TEXT,
  95. createParents: true,
  96. atomic: true,
  97. symlinkFreeBase: project,
  98. }),
  99. ).rejects.toThrow(/outside symlink-free base/i);
  100. expect(await readdir(outside)).toEqual([]);
  101. });
  102. it("still writes a draft file into a real drafts directory", async () => {
  103. const draftsDir = join(project, ".zcode", "workflow-drafts");
  104. const fs = createNodeFileSystemAdapter();
  105. await fs.writeTextFile({
  106. path: join(draftsDir, "nightly.dwf.ts"),
  107. content: "export {};\n",
  108. createParents: true,
  109. atomic: true,
  110. symlinkFreeBase: project,
  111. });
  112. expect(await readFile(join(draftsDir, "nightly.dwf.ts"), "utf8")).toBe("export {};\n");
  113. });
  114. });