Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(licenses): collectClosure 强制显式目标平台,取代可绕过的源码守卫 (#503)
zhanghengxd415574ffeb8e
diff --git a/docs/legal/notices/README.md b/docs/legal/notices/README.mdindex f78ed5abcc..ab0384701c 100644--- a/docs/legal/notices/README.md+++ b/docs/legal/notices/README.md@@ -23,8 +23,8 @@每个产物闭包都按显式目标平台收集,产物内容不受生成机器上装了哪些平台可选包影响。生成器判断可选依赖是否存在靠 `node_modules` 里有没有对应目录,而 pnpm 并不保证只安装 `pnpm.supportedArchitectures` 声明的架构,因此省掉目标平台参数会让声明内容随-生成机器漂移。覆盖全部支持架构的闭包由 `SUPPORTED_TARGETS` 按该字段叉乘得出;新增-闭包时不要省掉目标平台参数。+生成机器漂移。覆盖全部支持架构的闭包由 `SUPPORTED_TARGETS` 按该字段叉乘得出。目标平台+是必填的:省掉它会让生成直接失败,而不是静默产出与本机相关的声明。移动端使用 Expo managed workflow,完整 Pod/Gradle 图只在原生构建阶段产生。因此移动端静态声明会明确标注范围,不能替代发布构建所产生的 `Podfile.lock` 或 Gradle dependencydiff --git a/scripts/__tests__/third-party-notices.test.mjs b/scripts/__tests__/third-party-notices.test.mjsindex 541715a84c..7fe6b7ad84 100644--- a/scripts/__tests__/third-party-notices.test.mjs+++ b/scripts/__tests__/third-party-notices.test.mjs@@ -98,16 +98,37 @@ test("mobile notices exclude build-time platform binaries but keep their JS pack}});-// 闭包必须按显式目标平台收集:collectClosure() 判断可选依赖是否存在只看-// node_modules 里有没有目录,省掉 target 会让产物随生成机器的安装集合漂移。-test("every dependency closure is collected against an explicit target", () => {+// 闭包必须按显式目标平台收集:collectClosure() 判断可选依赖是否存在只看 node_modules+// 里有没有目录,省掉 target 就会让产物随生成机器的安装集合漂移。+//+// 这里守的是那一处强制校验本身,而不是去枚举调用点。枚举的写法(无论正则还是括号扫描)+// 都能被换一种调用形式绕过,且在本文件上会实打实误报——生成器的错误消息与注释里都出现+// 了 `collectClosure(`,而源码含带引号的正则字面量,使得零依赖的字符串跳过无法可靠实现。+// 强制校验则覆盖全部调用形式:`licenses:generate` 是产物的唯一生成途径,缺 target 的调用+// 必然在生成时抛错,产物不可能带着机器相关的内容进仓库。+test("collectClosure() refuses to run without an explicit target", () => {const source = read("scripts/generate-third-party-notices.mjs");- const untargeted = [...source.matchAll(/collectClosure\(\s*\[[^\]]*\]\s*\)/g)];- assert.deepEqual(- untargeted.map((match) => match[0]),- [],- "collectClosure() 调用缺少目标平台参数,产物会随本机安装的平台可选包漂移",+ const signature = source.match(/function collectClosure\(([^)]*)\)/)?.[1];+ assert.ok(signature, "找不到 collectClosure 定义,守卫已失效");+ assert.doesNotMatch(+ signature,+ /=/,+ "target 不得有默认值:默认值会让缺 target 的调用静默恢复成机器相关行为",);+ assert.match(source, /requires an explicit target/);+ // linux 目标必须连 libc 一起强制:只有 linux 分 glibc / musl,缺了这一轴+ // matchesPackageConstraint() 会把两种变体同时放行,产物又随本机装了哪个变体漂移。+ assert.match(+ source,+ /os === "linux"\s*\?\s*\["os", "cpu", "libc"\]/,+ "linux 目标未强制 libc:glibc 与 musl 变体会同时进闭包",+ );+ // matchesTarget() 不得再有「target 为空则一律放行」的分支,那是漂移的根源。+ const matchesTarget = source.match(+ /function matchesTarget\([^)]*\)\s*\{[\s\S]*?\n\}/,+ )?.[0];+ assert.ok(matchesTarget, "找不到 matchesTarget 定义,守卫已失效");+ assert.doesNotMatch(matchesTarget, /return true/);});test("commercial distributions do not resolve forbidden Sustainable Use dependencies", () => {diff --git a/scripts/generate-third-party-notices.mjs b/scripts/generate-third-party-notices.mjsindex 08201ebc05..55530724b8 100644--- a/scripts/generate-third-party-notices.mjs+++ b/scripts/generate-third-party-notices.mjs@@ -242,16 +242,39 @@ function matchesPackageConstraint(values, actual) {}function matchesTarget(pkgJson, target) {- if (!target) return true;return (matchesPackageConstraint(pkgJson.os, target.os) &&matchesPackageConstraint(pkgJson.cpu, target.cpu) &&+ // libc 只对 linux 有意义,非 linux 目标可以省略这一轴(desktop-win / desktop-macos+ // 与移动端的 target 就没带),省略时由 matchesPackageConstraint() 按「未声明约束一律+ // 放行」处理;SUPPORTED_TARGETS 叉乘出来的 target 则各轴齐全,os 不是 linux 时也带+ // libc,那种情况下这一轴只会挡掉声明了 libc 约束的包,而声明该约束的包都把 os 限定+ // 在 linux,早已被上面的 os 轴挡掉。matchesPackageConstraint(pkgJson.libc, target.libc));}-/** BFS 遍历给定入口的生产依赖闭包。workspace 内部包穿透但不收录。 */-function collectClosure(entryDirs, target = null) {+/**+ * BFS 遍历给定入口的生产依赖闭包。workspace 内部包穿透但不收录。+ *+ * `target` 必填且强制校验:本函数判断一个平台可选依赖是否存在,只看 `node_modules` 里+ * 有没有对应目录,所以缺了 target 就等于把「本机恰好装了哪些架构」写进产物。这里刻意+ * 不提供「不过滤」的默认值——宁可让调用点直接报错,也不给出一条能静默恢复机器相关+ * 行为的路径。+ */+function collectClosure(entryDirs, target) {+ // 只有 linux 分 glibc / musl,所以 libc 只在 linux 目标上必填:缺了它+ // matchesPackageConstraint() 会因为「未声明约束一律放行」把 glibc 与 musl 变体同时收进+ // 闭包,产物重新随本机装了哪个变体漂移——正是本函数要挡掉的那种机器相关行为。+ const requiredAxes =+ target?.os === "linux" ? ["os", "cpu", "libc"] : ["os", "cpu"];+ for (const axis of requiredAxes) {+ if (typeof target?.[axis] !== "string" || target[axis].length === 0) {+ throw new Error(+ `collectClosure() requires an explicit target.${axis}: license notices would otherwise depend on the generating machine`,+ );+ }+ }const collected = new Map(); // key: name@versionconst visitedDirs = new Set();const missing = new Set();@@ -389,11 +412,11 @@ function mergeClosures(...closures) {/*** 由根 package.json 的 `pnpm.supportedArchitectures` 叉乘出的目标平台集合。*- * 覆盖「本仓声明支持的全部架构」而非单一发行产物的闭包必须逐个 target 收集再合并,- * 不能省掉 target 让 matchesTarget() 一律放行:collectClosure() 判断一个平台可选依赖- * 是否存在只看 `node_modules` 里有没有对应目录,而实际装出来的集合可能超出- * supportedArchitectures 的声明(例如 libc 只声明 glibc 时仍装进 musl 变体),产物- * 内容就会随生成机器漂移。这里不假设包管理器只安装声明过的架构。+ * 覆盖「本仓声明支持的全部架构」而非单一发行产物的闭包必须逐个 target 收集再合并:+ * collectClosure() 判断一个平台可选依赖是否存在只看 `node_modules` 里有没有对应目录,+ * 而实际装出来的集合可能超出 supportedArchitectures 的声明(例如 libc 只声明 glibc 时+ * 仍装进 musl 变体),产物内容就会随生成机器漂移。这里不假设包管理器只安装声明过的+ * 架构。*/function readSupportedTargets() {const declared = readJson(path.join(REPO_ROOT, "package.json")).pnpm