conversation-session-formal-admission-paths.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { realpath } from "node:fs/promises";
  2. import { isAbsolute, relative, resolve } from "node:path";
  3. import {
  4. diagnostic,
  5. isRecord,
  6. toRepoPath,
  7. } from "./conversation-session-formal-admission-utils.mjs";
  8. const RUNTIME_FILE_FIXTURE_SOURCES = new Set([
  9. "created-by-helper",
  10. "created-by-provider-tool",
  11. "created-by-spec",
  12. ]);
  13. export async function resolveProviderFixturePaths({
  14. desktopRoot,
  15. providerFixtureRoot,
  16. providerFixtures,
  17. reasons,
  18. root,
  19. specPath,
  20. }) {
  21. const resolved = [];
  22. for (const manifestPath of providerFixtures) {
  23. const absolutePath = resolve(desktopRoot, manifestPath);
  24. if (
  25. isAbsolute(manifestPath) ||
  26. hasTraversalSegment(manifestPath) ||
  27. !isPathContainedBy(providerFixtureRoot, absolutePath)
  28. ) {
  29. reasons.push(
  30. diagnostic({
  31. code: "UNSAFE_PROVIDER_FIXTURE_PATH",
  32. message: `provider fixture must stay inside the Upstream fixture root: ${manifestPath}`,
  33. specPath,
  34. subjectPath: toRepoPath(root, absolutePath),
  35. }),
  36. );
  37. continue;
  38. }
  39. const realContainment = await inspectRealContainment(
  40. providerFixtureRoot,
  41. absolutePath,
  42. );
  43. if (realContainment.exists && !realContainment.contained) {
  44. reasons.push(
  45. diagnostic({
  46. code: "UNSAFE_PROVIDER_FIXTURE_PATH",
  47. message: `provider fixture symlink escapes the Upstream fixture root: ${manifestPath}`,
  48. specPath,
  49. subjectPath: toRepoPath(root, absolutePath),
  50. }),
  51. );
  52. continue;
  53. }
  54. resolved.push({ absolutePath, manifestPath });
  55. }
  56. return resolved;
  57. }
  58. export async function validateStaticFilePaths({
  59. desktopRoot,
  60. fixtureRoot,
  61. reasons,
  62. root,
  63. specPath,
  64. staticPaths,
  65. }) {
  66. for (const fixturePath of staticPaths) {
  67. const absolutePath = resolve(desktopRoot, fixturePath);
  68. if (
  69. isAbsolute(fixturePath) ||
  70. hasTraversalSegment(fixturePath) ||
  71. !isPathContainedBy(fixtureRoot, absolutePath)
  72. ) {
  73. reasons.push(
  74. diagnostic({
  75. code: "UNSAFE_FILE_FIXTURE_PATH",
  76. message: `static file fixture must stay inside the desktop fixture root: ${fixturePath}`,
  77. specPath,
  78. subjectPath: toRepoPath(root, absolutePath),
  79. }),
  80. );
  81. continue;
  82. }
  83. const realContainment = await inspectRealContainment(
  84. fixtureRoot,
  85. absolutePath,
  86. );
  87. if (!realContainment.exists) {
  88. reasons.push(
  89. diagnostic({
  90. code: "MISSING_FILE_FIXTURE",
  91. message: `declared file fixture is missing: ${fixturePath}`,
  92. specPath,
  93. subjectPath: toRepoPath(root, absolutePath),
  94. }),
  95. );
  96. continue;
  97. }
  98. if (!realContainment.contained) {
  99. reasons.push(
  100. diagnostic({
  101. code: "UNSAFE_FILE_FIXTURE_PATH",
  102. message: `static file fixture symlink escapes the desktop fixture root: ${fixturePath}`,
  103. specPath,
  104. subjectPath: toRepoPath(root, absolutePath),
  105. }),
  106. );
  107. }
  108. }
  109. }
  110. export function validateFileFixtures(value, subjectPath, specPath, reasons) {
  111. if (!Array.isArray(value)) {
  112. reasons.push(
  113. diagnostic({
  114. code: "INVALID_MANIFEST_SHAPE",
  115. message: "case manifest fileFixtures must be an array",
  116. specPath,
  117. subjectPath,
  118. }),
  119. );
  120. return null;
  121. }
  122. const staticPaths = [];
  123. let valid = true;
  124. for (const fixture of value) {
  125. if (typeof fixture === "string") {
  126. staticPaths.push(fixture);
  127. continue;
  128. }
  129. // 运行时 descriptor 只有在生产者来源可审计且描述完整时才豁免存在检查,
  130. // 避免仅凭一个 /tmp path 绕过静态 fixture containment。
  131. if (
  132. isRecord(fixture) &&
  133. typeof fixture.path === "string" &&
  134. fixture.path.trim().length > 0 &&
  135. typeof fixture.description === "string" &&
  136. fixture.description.trim().length > 0 &&
  137. RUNTIME_FILE_FIXTURE_SOURCES.has(fixture.source)
  138. ) {
  139. continue;
  140. }
  141. reasons.push(
  142. diagnostic({
  143. code: "INVALID_RUNTIME_FILE_FIXTURE_DESCRIPTOR",
  144. message:
  145. "runtime file fixture descriptors require path, description, and an allowed source",
  146. specPath,
  147. subjectPath,
  148. }),
  149. );
  150. valid = false;
  151. }
  152. return valid ? { staticPaths } : null;
  153. }
  154. function hasTraversalSegment(path) {
  155. return path.replaceAll("\\", "/").split("/").includes("..");
  156. }
  157. function isPathContainedBy(root, path) {
  158. const relativePath = relative(root, path).replaceAll("\\", "/");
  159. return (
  160. relativePath === "" ||
  161. (relativePath !== ".." &&
  162. !relativePath.startsWith("../") &&
  163. !isAbsolute(relativePath))
  164. );
  165. }
  166. async function inspectRealContainment(root, candidate) {
  167. try {
  168. const [realRoot, realCandidate] = await Promise.all([
  169. realpath(root),
  170. realpath(candidate),
  171. ]);
  172. return {
  173. contained: isPathContainedBy(realRoot, realCandidate),
  174. exists: true,
  175. };
  176. } catch (error) {
  177. if (error?.code === "ENOENT") {
  178. return { contained: true, exists: false };
  179. }
  180. throw error;
  181. }
  182. }