'use strict' // 插件清单哈希守卫:manifest.files / plugins/index.json 的 sha256 必须等于真实字节。 // 背景(v3.26.109):promptlib 1.14.0 改了 main.js 但 manifest.files 还写着上一版的哈希, // 市场装下来直接「完整性校验失败(main.js 的 sha256 不符)」,用户装不上插件。 // 这里在门禁里盯住源码侧,gen-market.js 里还有一道生成期断言。 const fs = require('fs') const path = require('path') const crypto = require('crypto') const ROOT = path.join(__dirname, '..') const PLUGINS = path.join(ROOT, 'plugins') const sha = (buf) => crypto.createHash('sha256').update(buf).digest('hex') let pass = 0 let fail = 0 function ok (name, cond, extra) { if (cond) { pass++; console.log(' ✅ ' + name) } else { fail++; console.log(' ❌ ' + name + (extra ? ' —— ' + extra : '')) } } console.log('===== 插件清单哈希:manifest.files 必须等于真实字节 =====') const index = JSON.parse(fs.readFileSync(path.join(PLUGINS, 'index.json'), 'utf8')) const byId = new Map(index.map((e) => [e.id, e])) for (const id of fs.readdirSync(PLUGINS)) { const dir = path.join(PLUGINS, id) if (!fs.statSync(dir).isDirectory()) continue const manifestPath = path.join(dir, 'manifest.json') if (!fs.existsSync(manifestPath)) continue const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')) const bad = [] for (const [name, expect] of Object.entries(manifest.files || {})) { const p = path.join(dir, name) if (!fs.existsSync(p)) { bad.push(name + ' 缺失'); continue } const actual = sha(fs.readFileSync(p)) if (actual !== String(expect).toLowerCase()) bad.push(name + ' 期望 ' + String(expect).slice(0, 8) + ' 实际 ' + actual.slice(0, 8)) } ok('manifest.files 与字节一致:' + id, bad.length === 0, bad.join(';')) const entry = byId.get(id) if (!entry) continue const badIdx = [] for (const [name, expect] of Object.entries(entry.files || {})) { const p = path.join(dir, name) if (!fs.existsSync(p)) { badIdx.push(name + ' 缺失'); continue } const actual = sha(fs.readFileSync(p)) if (actual !== String(expect).toLowerCase()) badIdx.push(name + ' 期望 ' + String(expect).slice(0, 8) + ' 实际 ' + actual.slice(0, 8)) } ok('plugins/index.json 与字节一致:' + id, badIdx.length === 0, badIdx.join(';')) ok('版本两处一致:' + id, String(entry.version) === String(manifest.version), 'index=' + entry.version + ' manifest=' + manifest.version) } console.log('\nplugin manifest hash checks: ' + pass + ' 通过 / ' + fail + ' 失败') process.exit(fail ? 1 : 0)