bughunt-regressions.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
  2. import { existsSync } from "node:fs";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { describe, expect, it } from "vitest";
  6. import { createEngine } from "@nengine/core";
  7. import { createEngineNamespaces, McpGateway } from "../src/index.js";
  8. /**
  9. * Regression tests for the 2026-08-14 full-repo bug-fix round:
  10. * - content ids must never escape the content dir (path traversal)
  11. * - a failed update write rolls the registry entry back
  12. * - patch.id renames are rejected loudly, not silently ignored
  13. * - callTool with undefined args returns a clean error result
  14. */
  15. const TIME_YAML = [
  16. "id: default",
  17. "dayLengthTicks: 480",
  18. "seasons:",
  19. " - { name: spring, dayCount: 28 }",
  20. "start: { day: 1, season: spring }",
  21. "",
  22. ].join("\n");
  23. const FARM_YAML = [
  24. "id: farm",
  25. "width: 2",
  26. "height: 2",
  27. 'tilesetRef: "tiles"',
  28. "spawn: { x: 0, y: 0 }",
  29. "layers:",
  30. " - name: ground",
  31. " data:",
  32. " - [a, b]",
  33. " - [b, a]",
  34. "",
  35. ].join("\n");
  36. interface Setup {
  37. engine: ReturnType<typeof createEngine>;
  38. gateway: McpGateway;
  39. tmp: string;
  40. close: () => Promise<void>;
  41. }
  42. async function setup(): Promise<Setup> {
  43. const tmp = await mkdtemp(join(tmpdir(), "farm-bughunt-"));
  44. await mkdir(join(tmp, "maps"), { recursive: true });
  45. await writeFile(join(tmp, "time.yaml"), TIME_YAML);
  46. await writeFile(join(tmp, "maps", "farm.yaml"), FARM_YAML);
  47. const engine = createEngine({ contentDir: tmp });
  48. const gateway = new McpGateway();
  49. for (const namespace of createEngineNamespaces(engine, { gateway })) {
  50. gateway.register(namespace);
  51. }
  52. await engine.start();
  53. return {
  54. engine,
  55. gateway,
  56. tmp,
  57. close: async () => {
  58. await engine.stop();
  59. await rm(tmp, { recursive: true, force: true });
  60. },
  61. };
  62. }
  63. async function callText(gateway: McpGateway, name: string, args: Record<string, unknown> = {}): Promise<{
  64. ok: boolean;
  65. text: string;
  66. }> {
  67. const result = await gateway.callTool(name, args);
  68. return { ok: result.isError !== true, text: result.content[0]?.text ?? "" };
  69. }
  70. describe("authoring path traversal (bug-fix round)", () => {
  71. it("item.create rejects an id with path separators — nothing is written outside content/", async () => {
  72. const { gateway, tmp, close } = await setup();
  73. try {
  74. const result = await callText(gateway, "item.create", {
  75. def: { id: "../../escape", name: "Escape", kind: "tool", stackable: false },
  76. });
  77. expect(result.ok).toBe(false);
  78. expect(result.text).toMatch(/unsafe content id/);
  79. expect(existsSync(join(tmp, "..", "escape.yaml"))).toBe(false);
  80. expect(existsSync(join(tmp, "..", "escape"))).toBe(false);
  81. } finally {
  82. await close();
  83. }
  84. });
  85. it("item.update rejects an unsafe target id", async () => {
  86. const { gateway, close } = await setup();
  87. try {
  88. const result = await callText(gateway, "item.update", {
  89. id: "../../escape",
  90. patch: { name: "X" },
  91. });
  92. expect(result.ok).toBe(false);
  93. // The traversal id can never be registered (create is guarded), so the
  94. // update is rejected either as not-found or as unsafe — never written.
  95. expect(result.text).toMatch(/unsafe content id|not found/);
  96. } finally {
  97. await close();
  98. }
  99. });
  100. it("patch.id renames are rejected loudly, never silently ignored", async () => {
  101. const { gateway, engine, close } = await setup();
  102. try {
  103. await callText(gateway, "item.create", {
  104. def: { id: "hoe", name: "Hoe", kind: "tool", stackable: false },
  105. });
  106. const result = await callText(gateway, "item.update", {
  107. id: "hoe",
  108. patch: { id: "renamed-hoe", name: "Renamed" },
  109. });
  110. expect(result.ok).toBe(false);
  111. expect(result.text).toMatch(/renaming via patch.id is not supported/);
  112. // the original is untouched
  113. expect(engine.registry.get("item", "hoe")?.name).toBe("Hoe");
  114. } finally {
  115. await close();
  116. }
  117. });
  118. });
  119. describe("update write-failure rollback (bug-fix round)", () => {
  120. it("a failed write leaves the live registry entry intact (old file + old def)", async () => {
  121. const { gateway, engine, tmp, close } = await setup();
  122. try {
  123. await callText(gateway, "item.create", {
  124. def: { id: "hoe", name: "Hoe", kind: "tool", stackable: false },
  125. });
  126. // Replace the item file with a DIRECTORY so the rewrite fails (EISDIR).
  127. await rm(join(tmp, "items", "hoe.yaml"));
  128. await mkdir(join(tmp, "items", "hoe.yaml"));
  129. const result = await callText(gateway, "item.update", {
  130. id: "hoe",
  131. patch: { name: "Renamed" },
  132. });
  133. expect(result.ok).toBe(false);
  134. expect(result.text).toMatch(/EISDIR|illegal operation|directory/i);
  135. // The registry entry survived the failed write.
  136. expect(engine.registry.get("item", "hoe")?.name).toBe("Hoe");
  137. } finally {
  138. await close();
  139. }
  140. });
  141. });
  142. describe("gateway.callTool argument handling (bug-fix round)", () => {
  143. it("undefined args return a clean error result, not a raw TypeError", async () => {
  144. const { gateway, close } = await setup();
  145. try {
  146. const result = await gateway.callTool("item.get", undefined as never);
  147. expect(result.isError).toBe(true);
  148. expect(result.content[0]?.text).toMatch(/missing required arguments/);
  149. } finally {
  150. await close();
  151. }
  152. });
  153. });