feat(plugins): P1 插件外置(<资料目录>/plugins 覆盖内置、sha256 校验、blob 注入、来源标签);CSP 放行 blob:
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 1m11s
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 1m11s
This commit is contained in:
1 parent
2626c663c3
commit
5da4f255d8
5 files changed
+106
-10
No files matched your search
@@ -8470,6 +8470,53 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) =>
|
||||
return false
|
||||
}
|
||||
}
|
||||
// 外部插件(P1):代码在 <资料目录>/plugins/ 下,不经 HTTP。
|
||||
// 读文本 → 用 WebCrypto 校验清单里的 sha256 → 通过后才用 blob URL 注入(CSP 已放行 blob:)。
|
||||
function wPluginSource() {
|
||||
const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null
|
||||
return (F && F.plugins && typeof F.plugins.read === 'function') ? F.plugins : null
|
||||
}
|
||||
async function wVerifyPluginCode(code, integrity) {
|
||||
if (!integrity) return false
|
||||
const expect = String(integrity).replace(/^sha256-/, '')
|
||||
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(code))
|
||||
let bin = ''
|
||||
const u8 = new Uint8Array(buf)
|
||||
for (let i = 0; i < u8.length; i++) bin += String.fromCharCode(u8[i])
|
||||
return btoa(bin) === expect
|
||||
}
|
||||
async function wLoadExternalPluginEntry(record) {
|
||||
record.status = 'loading'
|
||||
const src = wPluginSource()
|
||||
if (!src) { record.status = 'error'; record.error = '外部插件不可用'; return false }
|
||||
try {
|
||||
const code = await src.read(record.file)
|
||||
if (record.integrity) {
|
||||
const okHash = await wVerifyPluginCode(code, record.integrity)
|
||||
if (!okHash) throw new Error('完整性校验失败(外部插件)')
|
||||
}
|
||||
const url = URL.createObjectURL(new Blob([code], { type: 'text/javascript' }))
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
const script = document.createElement('script')
|
||||
script.src = url
|
||||
script.async = false
|
||||
script.onload = () => resolve()
|
||||
script.onerror = () => reject(new Error('外部插件脚本执行失败'))
|
||||
document.head.appendChild(script)
|
||||
})
|
||||
} finally { setTimeout(() => { try { URL.revokeObjectURL(url) } catch (e) { /* ignore */ } }, 30000) }
|
||||
const registered = PluginHost && typeof PluginHost.list === 'function' && PluginHost.list().some((p) => p.id === record.id)
|
||||
record.status = registered ? 'loaded' : 'not-registered'
|
||||
if (registered && typeof PluginHost.isEnabled === 'function' && PluginHost.isEnabled(record.id)) PluginHost.setEnabled(record.id, true)
|
||||
return record.status === 'loaded'
|
||||
} catch (err) {
|
||||
record.status = 'error'
|
||||
record.error = String((err && err.message) || err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 按需加载(插件管理里勾选 idle 行):下载脚本 → 注册 → 显式启用;失败时返回错误供 UI 回滚勾选并提示。
|
||||
async function wEnsurePluginLoaded(id) {
|
||||
const record = wRuntimePlugins.find((r) => r.id === id)
|
||||
@@ -8483,19 +8530,32 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) =>
|
||||
}
|
||||
async function wLoadRuntimePlugins() {
|
||||
if (typeof fetch !== 'function' || typeof document === 'undefined') return
|
||||
let manifest = null
|
||||
// 内置清单(随包)先读;外部清单(<资料目录>/plugins/)只覆盖同 id 的条目 —— 合并而非替换
|
||||
let builtin = []
|
||||
try {
|
||||
const resp = await fetch('/plugins/index.json', { cache: 'no-store' })
|
||||
if (!resp.ok) return
|
||||
manifest = await resp.json()
|
||||
} catch (e) { return }
|
||||
if (resp.ok) {
|
||||
const j = await resp.json()
|
||||
builtin = Array.isArray(j) ? j : (Array.isArray(j && j.plugins) ? j.plugins : [])
|
||||
}
|
||||
} catch (e) { /* 内置读不到就只看外部 */ }
|
||||
let ext = []
|
||||
try {
|
||||
const src = wPluginSource()
|
||||
if (src) ext = await src.manifest()
|
||||
} catch (e) { /* 外部清单读到一半失败:忽略 */ }
|
||||
const byId = new Map(builtin.map((e) => [String((e && e.id) || ''), Object.assign({}, e, { source: 'builtin' })]))
|
||||
for (const e of ext) { const id = String((e && e.id) || ''); if (id) byId.set(id, Object.assign({}, e, { source: 'external' })) }
|
||||
const manifest = Array.from(byId.values())
|
||||
if (!manifest.length) return
|
||||
const list = Array.isArray(manifest) ? manifest : (manifest && Array.isArray(manifest.plugins) ? manifest.plugins : [])
|
||||
const ordered = list.slice().sort((a, b) => String((a && a.id) || '').localeCompare(String((b && b.id) || '')))
|
||||
const pending = [] // 已启用插件的加载 Promise:循环内全部同步 append 后再并行等待(执行顺序由 script.async=false 保证)
|
||||
for (const entry of ordered) {
|
||||
const id = String((entry && entry.id) || '')
|
||||
const file = String((entry && entry.file) || '')
|
||||
const record = { id, file, version: (entry && entry.version) || '', status: 'loading', error: null }
|
||||
const isExt = (entry && entry.source) === 'external'
|
||||
const record = { id, file, version: (entry && entry.version) || '', status: 'loading', error: null, source: isExt ? 'external' : 'builtin' }
|
||||
wRuntimePlugins.push(record)
|
||||
if (!id || !/^[A-Za-z0-9._-]+$/.test(file)) { record.status = 'bad-entry'; continue }
|
||||
const integrity = String((entry && entry.integrity) || '')
|
||||
@@ -8505,7 +8565,7 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) =>
|
||||
record.integrity = integrity
|
||||
// 只下载已启用的;未启用记 idle(不注入脚本),插件管理里勾选时经 wEnsurePluginLoaded 按需下载
|
||||
// 收进 pending 而非逐个 await:wLoadPluginEntry 在首个 await 前已 append ⇒ 并发抓取;async=false 保证执行顺序不变(冷启动 3 RTT → 1)
|
||||
if (PluginHost && typeof PluginHost.isEnabled === 'function' && PluginHost.isEnabled(id)) pending.push(wLoadPluginEntry(record))
|
||||
if (PluginHost && typeof PluginHost.isEnabled === 'function' && PluginHost.isEnabled(id)) pending.push(isExt ? wLoadExternalPluginEntry(record) : wLoadPluginEntry(record))
|
||||
else record.status = 'idle'
|
||||
}
|
||||
await Promise.all(pending)
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": "default-src 'self'; script-src 'self'; worker-src 'self' blob:; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; font-src 'self' data:; connect-src 'self' ipc: http://ipc.localhost https: blob: data: http://127.0.0.1:30092 http://localhost:30092 http://127.0.0.1:3425 http://localhost:3425; object-src 'none'; base-uri 'none'; frame-ancestors 'none'"
|
||||
"csp": "default-src 'self'; script-src 'self' blob:; worker-src 'self' blob:; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; font-src 'self' data:; connect-src 'self' ipc: http://ipc.localhost https: blob: data: http://127.0.0.1:30092 http://localhost:30092 http://127.0.0.1:3425 http://localhost:3425; object-src 'none'; base-uri 'none'; frame-ancestors 'none'"
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
@@ -42,4 +42,4 @@
|
||||
"icons/icon.ico"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -395,6 +395,31 @@
|
||||
} catch (e) { return 0 }
|
||||
}
|
||||
|
||||
// ---- 插件外置(P1):<资料目录>/plugins/ 覆盖随包的内置插件 ----
|
||||
const PLUG_DIR = 'plugins'
|
||||
async function pluginManifest() {
|
||||
try {
|
||||
const txt = await fs().readTextFile(await join(dir, PLUG_DIR, 'index.json'))
|
||||
const j = JSON.parse(txt)
|
||||
return Array.isArray(j) ? j : (Array.isArray(j && j.plugins) ? j.plugins : [])
|
||||
} catch (e) { return [] }
|
||||
}
|
||||
async function pluginRead(file) {
|
||||
const safe = String(file || '')
|
||||
if (!/^[A-Za-z0-9._-]+$/.test(safe)) throw new Error('非法插件文件名')
|
||||
return fs().readTextFile(await join(dir, PLUG_DIR, safe))
|
||||
}
|
||||
async function pluginRemove(file) {
|
||||
const safe = String(file || '')
|
||||
if (!/^[A-Za-z0-9._-]+$/.test(safe)) throw new Error('非法插件文件名')
|
||||
try { await fs().remove(await join(dir, PLUG_DIR, safe)); return true } catch (e) { return false }
|
||||
}
|
||||
async function pluginDir() {
|
||||
const p = await join(dir, PLUG_DIR)
|
||||
try { await fs().mkdir(p, { recursive: true }) } catch (e) { /* 已存在 */ }
|
||||
return p
|
||||
}
|
||||
|
||||
async function loadInto() {
|
||||
// data.json 不存在 ⇒ 把当前 WebView 里的数据迁过去(只做一次)
|
||||
const target = await join(dir, DATA_FILE)
|
||||
@@ -653,6 +678,7 @@
|
||||
|
||||
global.IWBStorageFile = {
|
||||
enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages,
|
||||
plugins: { manifest: pluginManifest, read: pluginRead, remove: pluginRemove, dir: pluginDir },
|
||||
storage: () => storage,
|
||||
kv: () => kv,
|
||||
dir: () => dir,
|
||||
|
||||
@@ -25,12 +25,22 @@
|
||||
if (!pluginItems.length && !remoteIdle.length && !remoteFailed.length) return el('div', { className: 'iwb-hint' }, '暂无已注册插件。插件随应用一起发布,启用后其面板 / 命令 / 节点类型才会出现。')
|
||||
return el('div', { className: 'iwb-set-page' },
|
||||
el('div', { className: 'iwb-set-page-title' }, '插件管理'),
|
||||
isDesk() && (() => {
|
||||
const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null
|
||||
const extCount = (wRuntimePlugins || []).filter((r) => r && r.source === 'external').length
|
||||
const dirHint = (F && F.dir && F.dir()) ? (F.dir() + '/plugins/') : '资料目录/plugins/'
|
||||
return el('div', { className: 'iwb-hint' },
|
||||
'外部插件:' + dirHint + '(放 index.json + 插件 js 即可覆盖内置;' + (extCount ? '当前 ' + extCount + ' 个来自外部' : '当前全部来自内置') + ')。',
|
||||
)
|
||||
})(),
|
||||
el('div', { className: 'iwb-hint' }, (isDesk()
|
||||
? '插件随应用一起发布(离线可用),升级插件需要更新应用版本。停用只隐藏其入口:既有的插件节点仍照常渲染、运行与保存。'
|
||||
? '插件随应用发布(离线可用);外部插件放在资料目录 plugins/ 下可独立维护,不动本体。停用只隐藏其入口:既有的插件节点仍照常渲染、运行与保存。'
|
||||
: '内置插件随应用发布;带「远程」标签的插件来自同源 /plugins/index.json(KV 提供),更新只需上传到 KV、刷新即生效。停用只隐藏其入口:既有的插件节点仍照常渲染、运行与保存。')),
|
||||
pluginItems.map((p) => {
|
||||
return el('div', { key: p.id, className: 'iwb-plugin-row' },
|
||||
el('label', { className: 'iwb-label iwb-grow' },
|
||||
el('span', { className: 'iwb-hint', style: { marginRight: 6 } },
|
||||
((wRuntimePlugins || []).find((r) => r && r.id === p.id && r.source === 'external') ? '外部' : '内置')),
|
||||
el('input', {
|
||||
type: 'checkbox', checked: p.enabled, disabled: !p.compatible,
|
||||
onChange: (e) => { const r = PluginHost.setEnabled(p.id, e.target.checked); setPluginsRev((v) => v + 1); if (!r.ok) setMsg({ type: 'err', text: p.name + ':' + r.error }) },
|
||||
|
||||
@@ -150,7 +150,7 @@ const MAX_SRC_COMPONENT_LINES = 531
|
||||
// ② 同时守「app.js 占总源码 ≤ 40%」(2026-11-12 实测 38.4%)—— app.js 可以涨,
|
||||
// 但必须涨得比 src 慢;功能继续加,占比自然往下走;
|
||||
// ③ Workbench 单函数上限**不动**(3,091)—— 那才是真坏味道的护栏,总量松它不松。
|
||||
const MAX_APP_JS_LINES = 8720 // 2026-11-17 桌面版:下载语义/关闭自动下载/关图库定时清理(+14 行,同步上调)
|
||||
const MAX_APP_JS_LINES = 8780 // 2026-11-17 桌面版:插件外置(外部清单优先 + 完整性校验 + blob 注入)约 +52 行;下一轮抽到 src/runtime-plugin-external.js 后回调
|
||||
const MAX_APP_JS_SHARE = 0.40
|
||||
|
||||
// ── 历史记账(供回溯,逐条保留)──────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user