chore(plugin-style): 插件样式注入加自检 + 复盘写进 DESKTOP_PITFALLS
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 2m24s

这次事故(CSP nonce 让插件动态 <style> 被整段丢弃)已修,补两道防线,避免再静默复发:

- src/runtime-plugin-loader.js:注入后检查 node.sheet;head 场景为 null 时自动改挂 body;
  仍为 null 就写 _boot.log 并 console.warn(不再静默丢样式)
- src/storage-file.js:对外暴露 logLine(),供其它模块写 _boot.log 做诊断
- docs/DESKTOP_PITFALLS.md:新增「2026-11-27 插件样式全丢复盘」,写清 nonce → unsafe-inline 失效
  的链条、同根因的历史(v3.26.7 / v3.26.9),以及四条铁律(index.html 禁内联、样式自检、
  改 CSP 或 index.html 必须桌面端实机验面板)
This commit is contained in:
qiqi committed 2026-11-27 22:10:17 +08:00
1 parent 9210bc3d0b
commit 487eeadf1f
4 files changed
+38 -2

No files matched your search

+17
View File
@@ -142,3 +142,20 @@ ImageWorkbench/
6. **地址不能只靠 `on_page_load`**:花瓣等 SPA 用 `history.pushState` 换路由,不触发原生加载事件;必须轮询 `canvas_webview_state`(或注入 hook),否则地址栏、收藏、刷新全部停在首屏域名。
7. **发布时不要并行 build**:`publish-update.sh` 编译 Rust 时另一个 `npm run build` 会删 `dist/`,导致“资源不存在”构建失败;发布构建必须串行。
8. **外部路径的 fs 授权不是持久的**:相册 `{__file}` 只存路径,Tauri fs scope 重启即失效。`grant_paths` 必须写 `file-grants.json`,启动时恢复;旧画布启动后还要扫描 `__file` 再 grant 一次。否则“旧图缩略图还在、组合节点全感叹号、新图正常”,很容易误判成缓存。
## 六、2026-11-27 插件样式全丢复盘(CSP nonce 第三次踩坑)
- **症状**:风格库 / 浏览器 / 提示词库的停靠面板与画布上的样式节点**全部丢样式**(DOM 正常、宿主外链 style.css 正常,只有插件运行时注入的 style 不生效)
- **根因链**:
1. 为了让启动不再白屏,我在 index.html 里加了内联 style + 内联 script(静态启动屏)
2. Tauri 构建会给 index.html 里的**内联块**注入 CSP nonce
3. CSP3 规定:**nonce 一旦出现,'unsafe-inline' 就被忽略**
4. 插件样式是运行时 document.createElement('style') 注入的,**没有 nonce** ⇒ 被整段丢弃
5. 于是所有插件样式失效,宿主外链样式不受影响 ⇒ 看起来「只有插件坏了」
- **同一根因的历史**:5f8f4d2(v3.26.9,靠给动态 style 补 nonce 修)、v3.26.7(WKWebView head 里 style 的 sheet === null,改成挂 body)
- **修法**:index.html 只保留启动屏的**标记**,样式走外链 style.css,页面里没有任何内联 style/script
- **铁律**:
1. **index.html 里禁止写内联 style / script**(只允许外链与纯标记)。scripts/check-dist.js 已加硬断言,违反直接红
2. 插件/动态样式注入后要检查 sheet:为 null(WKWebView 的 head 场景)就自动改挂 body —— src/runtime-plugin-loader.js 的 applyStyle 已这么做
3. 注入后**当场自检 node.sheet**:为空就写 _boot.log(IWStorageFile.__iwbBootLog)并 console.warn,不允许静默丢样式 —— src/runtime-plugin-loader.js 的 applyStyle 已这么做
4. 改 index.html 生成逻辑(scripts/build.js)或 CSP(src-tauri/tauri.conf.json)后,必须**在桌面端实机看一次插件面板**:web 预览(无 Tauri nonce)永远看不出这个问题
+1 -1
View File
@@ -22,7 +22,7 @@
- 图片**新写入主通道 = IndexedDB Blob `{__blob}` / key `imgblob_<hash>_<len>`** (A) `app.js:2143`;`{__cache}`(Cache Storage)为兜底;\`ref_\`(`app.js` 内定义的 `IDB_REF_PREFIX = 'ref_'`)为 legacy 形态 (A)。
- 大图判定:`isBigDataUrl` = `data:` 且 `length > 3000` (A) `app.js:2018`;**画布保存保留阈值** `< 16384` 才原样保留 (A) `app.js:2182`。**两处阈值并存,含义不同**。
- 单测是**源码契约测试**:`tests/` 目录共 **493** 处源码文本钉点(`source.includes` / `source.indexOf` / `/re/.test(source)`),分布在 **20** 个文件(其中 `iwb-v32-web-test.js` 一个就占 **407**)→ **结构性重构会先打红测试**。**该数字已由 `tests/source-text-ratchet-test.js` 的「只减不增」棘轮守门**(2026-10-20 起守卫范围从 1 个文件扩到全目录:新增任何一处源码文本钉点都会打红)。**2026-11-12 复算**(`npm run audit:pins`,v3.24.76):二分 STRUCT **417** / NAME **76**;形态 **POS 403 / NEG 87 / COUNT 3**;难度 **易 62 / 中 316 / 难 115**。钉点减法第一批(v3.24.72)把 **537 → 505**:8 条探针钉点改为「门禁内 spec 是否真的读该计数器」、5 条改为运行时断言(生产态 `window` 上无探针)、10 条改为 DOM 断言(质量档位禁用 / 传输页无三模式按钮 / 模型项 title)、4 条改为运行时对象断言(健康检查两域、策略对象冻结)、其余 9 条有同文件 DOM 等价断言或仅剩 NEG。第二批(v3.24.76)把 **505 → 493**:94-5~7 三条「多标签释放语义」(10 处正则)换成 `tests/iwb-tabs-release.spec.js` 的 3 条真 e2e,剩 1 条「消费方契约」(断言该 spec 在门禁内且 spec 源码真的读两个探针);另 2 条 `wPurgeIntent.add` 钉点改为读 `src/workspace-purge.js`。**这一批一换就查出 3 个真缺陷**(objectURL 从未释放 / 彻底删除留孤儿标签 · session / 刷新后误回收打开标签的图)—— 见 CHANGELOG v3.24.76。
- `app.js` = 单 IIFE **8,323 行**(**双层预算**:绝对上限 8,329 / 占总源码 ≤ 40%,现 **26.2%**,2026-11-12 起;此前为单一上限 8,509、零余量);`Workbench()` = **2,174 行**(上限 **2,179**,余量 5;2026-11-16 把生成取消族 `cancelRunsFor`/`cancelRun`/`cancelTaskJobs` 与工作区列表写回族 `persistWs`/`duplicateWs` 搬进 `src/runtime-workbench-logic.js`(`createCancelActions` / `createWorkspaceListActions`),并删掉全仓零调用的 `runAll`,3090 → 2,998);大量逻辑已抽到 `src/*.js`(现 **117** 个模块 / 23,464 行,`sourceModules` 117 项;v3.24.68 新增 `clipboard-graph.js`,v3.24.74 新增 `image-admit.js`,v3.24.75 新增 `image-dedupe.js`,v3.24.76 新增 `workspace-purge.js`,v3.24.79 新增 `generation-provider-ping.js`,v3.24.80 删除无调用的 `createWBFlushViewport` 17 行)。
- `app.js` = 单 IIFE **8,323 行**(**双层预算**:绝对上限 8,329 / 占总源码 ≤ 40%,现 **26.2%**,2026-11-12 起;此前为单一上限 8,509、零余量);`Workbench()` = **2,174 行**(上限 **2,179**,余量 5;2026-11-16 把生成取消族 `cancelRunsFor`/`cancelRun`/`cancelTaskJobs` 与工作区列表写回族 `persistWs`/`duplicateWs` 搬进 `src/runtime-workbench-logic.js`(`createCancelActions` / `createWorkspaceListActions`),并删掉全仓零调用的 `runAll`,3090 → 2,998);大量逻辑已抽到 `src/*.js`(现 **117** 个模块 / 23,483 行,`sourceModules` 117 项;v3.24.68 新增 `clipboard-graph.js`,v3.24.74 新增 `image-admit.js`,v3.24.75 新增 `image-dedupe.js`,v3.24.76 新增 `workspace-purge.js`,v3.24.79 新增 `generation-provider-ping.js`,v3.24.80 删除无调用的 `createWBFlushViewport` 17 行)。
- 门禁:`npm run verify` = `verify:ci`(`build` → `node --check app.js` → `node --check dist/app.js` → `check-dist.js` → `lint`(ESLint 正确性规则,`src/` + `app.js`,P4-16a)→ `test:unit` → `test` → `test:facts`)+ `test:e2e` (A) `package.json`。
## 2. 门禁与环境现状
+16
View File
@@ -54,6 +54,19 @@
// 与单文件插件共用同一套「自注册」契约,区别只是多读一个 manifest 和可选 styles.css。
const styleNodes = {}
const pendingStyles = {}
// v3.26.109 教训(两次踩坑):
// 1) WKWebView 下插到 <head> 的 <style> 可能 sheet === null,不参与样式计算(v3.26.7);
// 2) 页面里只要有带 CSP nonce 的内联块,'unsafe-inline' 就失效,没有 nonce 的动态
// <style> 会被整段丢弃(v3.26.9、以及 v3.26.109 又在 index.html 写内联块复现)。
// 所以:挂到 body(更稳),挂完当场自检 sheet,不生效就写进 _boot.log —— 让问题可见,而不是静默丢样式。
function checkStyleSheet (id, node) {
try {
if (!node || node.sheet) return
const msg = 'plugin-style-no-sheet id=' + String(id)
try { const st = global.IWBStorageFile; if (st && st.logLine) st.logLine(msg) } catch (e) { /* ignore */ }
try { if (global.console && global.console.warn) global.console.warn('[iwb] ' + msg + '(样式未参与计算:检查 CSP nonce / 挂载位置)') } catch (e) { /* ignore */ }
} catch (e) { /* ignore */ }
}
function applyStyle (id, css) {
if (!css) return
try {
@@ -64,7 +77,10 @@
node.setAttribute('data-plugin', String(id))
node.textContent = String(css)
doc.head.appendChild(node)
// 自检:WKWebView 下 head 里的 style 可能 sheet = null(不参与样式计算)→ 自动改挂 body
if (!node.sheet) { try { (doc.body || doc.head).appendChild(node) } catch (e2) { /* ignore */ } }
styleNodes[id] = node
checkStyleSheet(id, node)
} catch (e) { /* 样式失败不影响脚本 */ }
}
function applyStyleFor (id) {
+4 -1
View File
@@ -1405,7 +1405,10 @@
}
global.IWBStorageFile = {
enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, deleteImageBytes, deleteGalleryBytesFor, galleryAudit, gcImages, rotateTrash, galleryThumb, readDisplayThumb, readDisplayThumbUrl, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs, cleanupLegacy, maintain,
enabled, ready: init, flush,
// 供其它模块把诊断写进 _boot.log(v3.26.109)
logLine: (m) => blog(m),
runtime, pickDir, switchTo, migrateTo, deleteImageBytes, deleteGalleryBytesFor, galleryAudit, gcImages, rotateTrash, galleryThumb, readDisplayThumb, readDisplayThumbUrl, writeDisplayThumb, writeDisplayThumbBlob, pruneThumbs, cleanupLegacy, maintain,
plugins: { manifest: pluginManifest, read: pluginRead, write: pluginWrite, remove: pluginRemove, dir: pluginDir, listLocal: pluginListLocal, listDirs: pluginListDirs, readFile: pluginReadFile, writeFile: pluginWriteFile, writeBinary: pluginWriteBinary, mkdir: pluginMkdir, removeDir: pluginRemoveDir, renameDir: pluginRenameDir, existsDir: pluginExistsDir },
pluginData: { read: pluginDataRead, write: pluginDataWrite, remove: pluginDataRemove, list: pluginDataList },
pluginFiles: { put: pluginFilePut, get: pluginFileGet, list: pluginFileList, remove: pluginFileRemove, stat: pluginFileStat, dir: pluginFilesDir, readBytes: pluginFileReadBytes, listAll: pluginFilesListAll },