licenses.mjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env node
  2. /* eslint-disable max-lines -- 三方 license 盘点/声明/门禁一体脚本,数据表与校验逻辑集中维护。 */
  3. // 三方 license 盘点/声明/门禁 一体脚本
  4. //
  5. // 用法:
  6. // node scripts/licenses.mjs notices 生成 THIRD-PARTY-NOTICES.md(精确版本、原始版权和许可文本)
  7. // node scripts/licenses.mjs check 手动门禁:出现 allowlist 之外或许可未知的包即退出码 1
  8. //
  9. // 数据口径:
  10. // - 实装清单 = 全 workspace node_modules 的递归集合,含嵌套版本与符号链接。
  11. // - 声明生成:third-party-npm.mjs 按生产依赖的精确版本收集真实许可文件。
  12. // - prod 判定按锁文件生产图中的精确版本。
  13. // - 商用/半开放检查作用于全量实装包(prod+dev)
  14. import { generateThirdPartyNotices } from "./generate-third-party-notices.mjs";
  15. import { readVerifiedNotices } from "./third-party-notices.mjs";
  16. import {
  17. isAllowedLicenseIdentifier,
  18. isReviewedLegacyLicense,
  19. legacyLicenseIdentifier,
  20. } from "./license-identifiers.mjs";
  21. import { readFile } from "node:fs/promises";
  22. import {
  23. readWorkspaceProductionGraph,
  24. scanInstalledPackages,
  25. missingProductionPackages,
  26. } from "./third-party-npm.mjs";
  27. import path from "node:path";
  28. import { fileURLToPath } from "node:url";
  29. const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
  30. const command = process.argv[2] || "notices";
  31. if (command === "notices") {
  32. await generateThirdPartyNotices(ROOT);
  33. process.exit(0);
  34. }
  35. // 标识门禁与声明生成共用 workspace 图和递归扫描,不按包名猜测生产范围。
  36. const { required, projects } = await readWorkspaceProductionGraph(ROOT);
  37. const ownNames = new Set(projects.map((project) => project.name));
  38. const installed = new Map();
  39. const MANUAL_LICENSE = {
  40. "exif-parser": "MIT", // 包内 LICENSE.md
  41. khroma: "MIT", // 包内 license 文件
  42. semaphore: "MIT", // 包内 README License 段
  43. "css-value": "MIT", // 包内 Readme License 段
  44. "@fig/autocomplete-helpers": "MIT", // 包内 LICENSE
  45. // react-scan 开发依赖链;license 字段缺失,包内 LICENSE 为 Aiden Bai 的标准 MIT。
  46. "@react-grab/cli": "MIT",
  47. };
  48. function normLicense(value) {
  49. if (typeof value === "string") return value.trim();
  50. if (Array.isArray(value)) return value.map(normLicense).join(" OR ");
  51. return value?.type?.trim() || "(missing)";
  52. }
  53. const scanned = await scanInstalledPackages(ROOT, projects);
  54. missingProductionPackages(required, scanned);
  55. for (const [key, { pkg }] of scanned) {
  56. if (ownNames.has(pkg.name)) continue;
  57. installed.set(key, {
  58. name: pkg.name,
  59. version: pkg.version,
  60. license: normLicense(
  61. pkg.license ?? pkg.licenses ?? legacyLicenseIdentifier(pkg) ?? MANUAL_LICENSE[pkg.name],
  62. ),
  63. isProd: required.has(key),
  64. });
  65. }
  66. // ---------- 分类 ----------
  67. function classify(raw) {
  68. const s = raw.replace(/[()]/g, " ").trim();
  69. if (s === "(missing)" || s === "missing" || s === "") return "missing";
  70. if (/^UNLICENSED|SEE LICEN[CS]E IN|UNKNOWN|N\/A$/i.test(s)) return "unresolved";
  71. const or = s
  72. .split(/\s+OR\s+/i)
  73. .map((x) => x.trim())
  74. .filter(Boolean);
  75. if (or.length > 1 && or.some((x) => isAllowedLicenseIdentifier(x))) return "green";
  76. const and = s
  77. .split(/\s+AND\s+/i)
  78. .map((x) => x.trim())
  79. .filter(Boolean);
  80. if (and.length > 1 && and.every((x) => isAllowedLicenseIdentifier(x))) return "green";
  81. const t = or[0] || and[0] || s;
  82. if (/\bAGPL/i.test(t)) return "red-agpl";
  83. if (/\bLGPL/i.test(t)) return "yellow-lgpl";
  84. if (/\bGPL/i.test(t)) return "red-gpl";
  85. if (/^(MPL|EPL|CDDL)/i.test(t)) return "yellow-weak";
  86. if (/^(BUSL|BSL|Elastic|SSPL|PolyForm|FSL|CAL-1)/i.test(t)) return "red-semiopen";
  87. if (/^CC-BY-NC/i.test(t)) return "red-nc";
  88. if (/^CC-BY-(ND|SA)/i.test(t)) return "yellow-cc";
  89. return isAllowedLicenseIdentifier(t) ? "green" : "review";
  90. }
  91. for (const r of installed.values()) r.bucket = classify(r.license);
  92. // ---------- 构建工具许可标识复核(不是二进制发行义务豁免) ----------
  93. const WEAK_ALLOW = [
  94. [/^lightningcss/, "当前仅构建依赖;进入生产图时需重新核对 MPL 源码提供义务"],
  95. // react-scan 开发依赖链。license 字段为 SEE LICENSE IN LICENSE,包内实际为 Modified MIT:
  96. // 在 MIT 基础上附加“禁止用作 ML 训练数据”“禁止转卖为付费托管服务”两条用途限制。
  97. // 当前仅在本地/CI 作为开发态诊断工具运行,不随产品分发、不落入受限用途;进入生产图时需重新评审。
  98. [
  99. /react-doctor$/,
  100. "仅开发态 react-scan 诊断工具;Modified MIT 的用途限制不影响内部使用,不随产品分发",
  101. ],
  102. ];
  103. // ---------- 生产依赖的逐项复核结论:按包名登记并绑定许可类别,许可变化后重新阻断 ----------
  104. const PROD_REVIEWED = [
  105. [
  106. /^@img\/sharp-(libvips-|win32-)/,
  107. "yellow-lgpl",
  108. "libvips 以独立、可替换的共享库随 sharp 分发,仅真实 Computer Use 截图使用;声明附 LGPL-3.0 原文",
  109. ],
  110. [
  111. /^elkjs$/,
  112. "yellow-weak",
  113. "仅内部开发文档站 apps/dev-docs 使用,不随产品分发、不部署;按 EPL-2.0 使用",
  114. ],
  115. ];
  116. function weakAllowReason(r) {
  117. if (r.isProd) {
  118. for (const [re, bucket, why] of PROD_REVIEWED)
  119. if (re.test(r.name) && r.bucket === bucket) return why;
  120. return null;
  121. }
  122. for (const [re, why] of WEAK_ALLOW) if (re.test(r.name)) return why;
  123. return null;
  124. }
  125. if (command === "check") {
  126. const bad = [];
  127. for (const r of installed.values()) {
  128. if (r.bucket === "green" || isReviewedLegacyLicense(r)) continue;
  129. // 已登记 WEAK_ALLOW 复核结论的构建依赖允许 missing/unresolved 桶:这类包的 license 字段
  130. // 常缺失或写成 SEE LICENSE IN <file>,无法自动归入 yellow;人工复核记录本身就是放行结论。
  131. if (!r.isProd && weakAllowReason(r)) continue;
  132. if (r.bucket.startsWith("yellow") && weakAllowReason(r)) continue;
  133. bad.push(r);
  134. }
  135. if (bad.length) {
  136. console.error(`✗ license 检查失败:${bad.length} 个包超出 allowlist:`);
  137. for (const r of bad.sort((a, b) => a.name.localeCompare(b.name)))
  138. console.error(` [${r.bucket}] ${r.name}@${r.version} → ${r.license}`);
  139. console.error(
  140. "\n处置:替换依赖,或在 scripts/licenses.mjs 的 WEAK_ALLOW(构建依赖)/ PROD_REVIEWED(生产依赖)登记人工复核结论。",
  141. );
  142. process.exit(1);
  143. }
  144. await readVerifiedNotices(ROOT, { requireComplete: process.argv.includes("--strict") });
  145. const reviewRequired =
  146. JSON.parse(await readFile(path.join(ROOT, "third-party/inventory.json"), "utf8"))
  147. .reviewRequired ?? [];
  148. if (reviewRequired.length)
  149. console.warn(
  150. `待补齐/核验材料 ${reviewRequired.length} 项;发布前运行 node scripts/licenses.mjs check --strict,不得将基础检查通过视为合规完成。`,
  151. );
  152. const n = installed.size;
  153. console.log(
  154. `✓ 许可标识与声明新鲜度检查通过:${n} 个实装包(构建依赖复核 ${[...installed.values()].filter((r) => r.bucket.startsWith("yellow")).length} 项);材料限制见 third-party/README.md`,
  155. );
  156. } else {
  157. console.error("用法: node scripts/licenses.mjs [notices|check]");
  158. process.exit(2);
  159. }