'use strict' // P4-15:A2 插件完整性校验(SRI)——发布脚本纯函数 + app.js loader 接线 const assert = require('assert') const fs = require('fs') const path = require('path') const root = path.resolve(__dirname, '..') ;(async () => { const mod = await import(path.join(root, 'scripts', 'plugins-publish.mjs')) // 已知向量:sha256('abc') 的 base64 assert.strictEqual(mod.integrityOf(Buffer.from('abc')), 'sha256-ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=') // 文件名 → id(须 plugin-.js 且 id 满足契约) assert.strictEqual(mod.idFromFile('/x/plugin-xianyi-cutout.js'), 'xianyi-cutout') assert.strictEqual(mod.idFromFile('plugin-AB.js'), '') assert.strictEqual(mod.idFromFile('other.js'), '') // 清单构建:按 id 排序、version 可缺省、缺字段/重复即抛 const idx = mod.buildIndex([ { id: 'b-plugin', file: 'plugin-b-plugin.js', version: '2.0.0', integrity: 'sha256-x' }, { id: 'a-plugin', file: 'plugin-a-plugin.js', integrity: 'sha256-y' }, ]) assert.strictEqual(idx.map((e) => e.id).join(','), 'a-plugin,b-plugin') assert.strictEqual(idx[0].version, '') assert.throws(() => mod.buildIndex([{ id: 'a-plugin', file: 'f.js' }]), '缺 integrity 应抛') assert.throws(() => mod.buildIndex([{ id: 'a-plugin', file: 'f.js', integrity: 'i' }, { id: 'a-plugin', file: 'g.js', integrity: 'i' }]), 'id 重复应抛') // storedNameOf:内容寻址存储名(同字节同名、不同字节不同名、非法 id 抛) const sn1 = mod.storedNameOf('xianyi-cutout', Buffer.from('abc')) const sn2 = mod.storedNameOf('xianyi-cutout', Buffer.from('abc')) const sn3 = mod.storedNameOf('xianyi-cutout', Buffer.from('abd')) assert.strictEqual(sn1, sn2, '同 id 同字节应得到相同存储名(幂等)') assert.notStrictEqual(sn1, sn3, '不同字节应得到不同存储名') assert.throws(() => mod.storedNameOf('AB', Buffer.from('abc')), '非法 id 应抛') assert.throws(() => mod.storedNameOf('x', Buffer.from('abc')), '过短 id 应抛') assert.ok(/^plugin-xianyi-cutout\.[0-9a-f]{12}\.js$/.test(sn1), '存储名应匹配 plugin-..js') // 清单合并(2026-11-03 事故回归):只发一个插件时**不得**把其余插件从清单里抹掉 const prevIdx = [ { id: 'a-plugin', file: 'plugin-a-plugin.111111111111.js', version: '1.0.0', integrity: 'sha256-a' }, { id: 'b-plugin', file: 'plugin-b-plugin.222222222222.js', version: '1.0.0', integrity: 'sha256-b' }, ] const merged = mod.mergeIndexes(prevIdx, [{ id: 'b-plugin', file: 'plugin-b-plugin.333333333333.js', version: '2.0.0', integrity: 'sha256-b2' }]) assert.strictEqual(merged.length, 2, '合并后应保留未传入的 a-plugin') assert.strictEqual(merged.find((e) => e.id === 'a-plugin').file, 'plugin-a-plugin.111111111111.js', '未传入项应原样保留') assert.strictEqual(merged.find((e) => e.id === 'b-plugin').version, '2.0.0', '同 id 应由新条目覆盖') assert.strictEqual(mod.mergeIndexes([], prevIdx).length, 2, '空既有清单时等同于全新发布') assert.strictEqual(mod.mergeIndexes(prevIdx, []).length, 2, '不传新条目时不应丢条目') // 陈旧键判定(--gc 用):只认「plugins/ 下 + 线上清单不引用」,清单自身永不列为陈旧 const gcKeys = [ 'plugins/index.json', 'plugins/plugin-a-plugin.111111111111.js', 'plugins/plugin-a-plugin.999999999999.js', 'plugins/plugin-b-plugin.222222222222.js', 'other/keep-me.js', ] const gcIndex = [ { id: 'a-plugin', file: 'plugin-a-plugin.111111111111.js', version: '1.0.0', integrity: 'sha256-a' }, { id: 'b-plugin', file: 'plugin-b-plugin.222222222222.js', version: '1.0.0', integrity: 'sha256-b' }, ] const stale = mod.staleKeys(gcKeys, gcIndex) assert.deepStrictEqual(stale, ['plugins/plugin-a-plugin.999999999999.js'], '旧哈希键应判为陈旧,清单引用与 index.json 不应被判陈旧') assert.deepStrictEqual(mod.staleKeys(gcKeys, []), [ 'plugins/plugin-a-plugin.111111111111.js', 'plugins/plugin-a-plugin.999999999999.js', 'plugins/plugin-b-plugin.222222222222.js', ], '空清单时所有插件键都陈旧(但 index.json 仍保留)') assert.deepStrictEqual(mod.staleKeys([], gcIndex), [], '空 KV 不应报陈旧') assert.deepStrictEqual(mod.staleKeys(null, null), [], '非法入参应安全返回空') // --keep:归档保留的插件(如 ai-chat 移出清单但字节留 KV)不得被判陈旧 assert.deepStrictEqual(mod.staleKeys(gcKeys, gcIndex, ['a-plugin']), [], 'keep 传插件 id 时其全部哈希版本都保留') assert.deepStrictEqual(mod.staleKeys(gcKeys, gcIndex, ['plugins/plugin-a-plugin.999999999999.js']), [], 'keep 传完整键也应生效') // 上传实现不得再依赖 wrangler(v3/v4 子命令名不同,spawnSync 必踩版本坑) const pubSrc = fs.readFileSync(path.join(root, 'scripts', 'plugins-publish.mjs'), 'utf8') assert.ok(!/spawnSync\('npx'/.test(pubSrc), '插件发布脚本不应再 spawn npx wrangler') assert.ok(/storage\/kv\/namespaces\//.test(pubSrc), '上传应走 Cloudflare REST API(storage/kv/namespaces)') // 仓库清单本身必须是「多插件」形态的护栏:被单插件覆盖后此处直接红 const repoIdx = JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'index.json'), 'utf8')) assert.ok(Array.isArray(repoIdx) && repoIdx.length >= 6, 'plugins/index.json 应含全部已发布插件(当前 ≥6),被覆写成 1 个即视为事故') // app.js loader:integrity 存在则交给浏览器强制校验;格式不合法即拒;**缺失即拒(fail-closed)** const appJs = fs.readFileSync(path.join(root, 'app.js'), 'utf8') assert.ok(/script\.integrity = integrity; script\.crossOrigin = 'anonymous'/.test(appJs), 'loader 应把 integrity 交给浏览器强制校验') assert.ok(/record\.status = 'bad-entry'; record\.error = 'integrity 格式不合法'/.test(appJs), 'integrity 格式不合法应拒绝加载') assert.ok(/if \(!integrity\) \{ record\.status = 'bad-entry'; record\.error = 'integrity 缺失(拒绝加载)'; continue \}/.test(appJs), '缺失 integrity 应拒绝加载(fail-closed)') assert.ok(!/record\.integrity = integrity \|\| 'missing'/.test(appJs), '不应再有「缺失记为 missing 并继续加载」的过渡分支') console.log('plugin integrity checks passed') })().catch((e) => { console.error(e); process.exit(1) })