source-inventory.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { readdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
  2. import path from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. import { gunzipSync } from "node:zlib";
  5. import { createHash } from "node:crypto";
  6. import { inventoryPath, readExtraction } from "./lib/release-metadata.mjs";
  7. import { retiredSourceRoots } from "./lib/retired-sources.mjs";
  8. import { readSuites, suiteInventoryViolations } from "./lib/vitest-suites.mjs";
  9. const root = fileURLToPath(new URL("../", import.meta.url));
  10. const skipped = new Set([
  11. ".git",
  12. "node_modules",
  13. "dist",
  14. ".cache",
  15. ".pnpm-store",
  16. ".turbo",
  17. ".DS_Store",
  18. ]);
  19. function filesIn(directory, prefix = "") {
  20. return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
  21. if (skipped.has(entry.name) || entry.name.endsWith(".tsbuildinfo"))
  22. return [];
  23. const relative = prefix ? `${prefix}/${entry.name}` : entry.name;
  24. if (entry.isSymbolicLink())
  25. throw new Error(`Source symlink requires explicit review: ${relative}`);
  26. return entry.isDirectory()
  27. ? filesIn(path.join(directory, entry.name), relative)
  28. : [relative];
  29. });
  30. }
  31. const files = filesIn(root).sort();
  32. const violations = suiteInventoryViolations(files, readSuites(root));
  33. // Standard MIT text with the reviewed first-party attribution.
  34. // Source: https://opensource.org/license/mit
  35. const rootLicenseSha256 =
  36. "28bb5c2948742f9f8d27ed84882844d09dff07cb5df64f20729875e8582a18ea";
  37. if (
  38. createHash("sha256")
  39. .update(readFileSync(path.join(root, "LICENSE")))
  40. .digest("hex") !== rootLicenseSha256
  41. )
  42. violations.push(
  43. "LICENSE: expected MIT text with the reviewed MiniMax Code attribution",
  44. );
  45. const internalText =
  46. /(?:[\w.-]+\.xaminim\.com|weaver\/idl|@mavis\/thrift-gen|\/Users\/minimax(?:\/|\b)|\/archon\/internal\/api\/)/u;
  47. const credential =
  48. /-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----\r?\n[A-Za-z0-9+/=\r\n]{100,}|\b(?:ghp_|github_pat_)[A-Za-z0-9_]{30,}|\bsk-[A-Za-z0-9_-]{32,}/u;
  49. for (const file of files) {
  50. if (retiredSourceRoots.some((prefix) => file.startsWith(prefix)))
  51. violations.push(`${file}: retired source`);
  52. if (/^(?:.*\/)?\.env(?:\.|$)/u.test(file) && !file.endsWith(".env.example"))
  53. violations.push(`${file}: environment file`);
  54. if (file === "scripts/source-inventory.mjs") continue;
  55. let content = readFileSync(path.join(root, file));
  56. if (file.endsWith(".gz")) content = gunzipSync(content);
  57. const text = content.toString("utf8");
  58. if (internalText.test(text))
  59. violations.push(`${file}: internal source reference`);
  60. if (credential.test(text))
  61. violations.push(`${file}: possible embedded credential`);
  62. }
  63. const { packageRoots } = readExtraction(root);
  64. const packages = packageRoots.map((directory) => ({
  65. directory,
  66. manifest: JSON.parse(
  67. readFileSync(path.join(root, directory, "package.json"), "utf8"),
  68. ),
  69. }));
  70. const names = new Set(packages.map(({ manifest }) => manifest.name));
  71. for (const { directory, manifest } of packages) {
  72. if (!manifest.license || manifest.license === "UNLICENSED")
  73. violations.push(`${directory}: missing open-source license`);
  74. for (const section of [
  75. "dependencies",
  76. "devDependencies",
  77. "optionalDependencies",
  78. ]) {
  79. for (const [name, version] of Object.entries(manifest[section] ?? {})) {
  80. if (version.startsWith("workspace:") && !names.has(name))
  81. violations.push(`${directory}: unknown workspace ${name}`);
  82. }
  83. }
  84. for (const [name, entry] of Object.entries(manifest.exports ?? {})) {
  85. const target =
  86. typeof entry === "string"
  87. ? entry
  88. : (entry.types ?? entry.import ?? entry.default);
  89. if (!target) continue;
  90. const source = target
  91. .replace(/^\.\/dist\//u, "./src/")
  92. .replace(/\.d\.ts$/u, ".ts")
  93. .replace(/\.js$/u, ".ts");
  94. if (!existsSync(path.resolve(root, directory, source)))
  95. violations.push(`${directory}: missing export ${name}`);
  96. }
  97. }
  98. const native = JSON.parse(
  99. readFileSync(
  100. path.join(root, "third_party/sandbox-runtime/native-integrity.json"),
  101. "utf8",
  102. ),
  103. );
  104. for (const entry of native.files) {
  105. const bytes = readFileSync(
  106. path.join(root, "third_party/sandbox-runtime", entry.path),
  107. );
  108. if (createHash("sha256").update(bytes).digest("hex") !== entry.sha256)
  109. violations.push(`${entry.path}: native helper integrity mismatch`);
  110. }
  111. if (violations.length)
  112. throw new Error(`Public source check failed:\n${violations.join("\n")}`);
  113. if (process.argv.includes("--write")) {
  114. const projected = [...new Set([...files, inventoryPath])].sort();
  115. writeFileSync(
  116. path.join(root, inventoryPath),
  117. JSON.stringify({ schemaVersion: 1, files: projected }, null, 2) + "\n",
  118. );
  119. console.log(`Recorded ${projected.length} reviewed source paths.`);
  120. } else {
  121. const expected = JSON.parse(
  122. readFileSync(path.join(root, inventoryPath), "utf8"),
  123. ).files;
  124. const actualSet = new Set(files),
  125. expectedSet = new Set(expected);
  126. const missing = expected.filter((file) => !actualSet.has(file));
  127. const added = files.filter((file) => !expectedSet.has(file));
  128. if (missing.length || added.length)
  129. throw new Error(
  130. `Source inventory changed; review before updating.\nMissing: ${missing.join(", ")}\nAdded: ${added.join(", ")}`,
  131. );
  132. console.log(
  133. `Public source check passed: ${files.length} files, workspace exports and native helper integrity verified.`,
  134. );
  135. }