Files
qiqi 9210bc3d0b
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m8s
fix(csp): 去掉 index.html 里的内联 style/script —— 插件样式失效的真因(同 v3.26.9)
现象:风格库 / 浏览器 / 提示词库的面板与画布上的样式节点全部丢样式(DOM 正常、宿主外链
style.css 正常,只有插件动态注入的 <style> 不生效)。

根因:Tauri 会为 index.html 里的内联块加 CSP nonce;CSP3 规定 nonce 一出现 'unsafe-inline'
即被忽略。插件样式是运行时 document.createElement('style') 注入的、没有 nonce,于是被整段丢弃。
这正是 v3.26.9 修过、v3.26.10 回退掉的那条链(当时靠给动态 style 补 nonce 解决)。
我上两版把启动屏做成了内联 <style>+<script>,等于把这个坑重新踩了一遍。

- scripts/boot-splash.js:只导出标记(复用宿主已有的 .iwb-splash* 类),不再有内联样式/脚本
- scripts/build.js:index.html 只内联启动屏标记,样式走外链 style.css
- src/ui-splash.js:React 启动屏挂载时直接移除 #iwb-boot-static 节点
- scripts/check-dist.js:script 数回到 3,并新增断言「index.html 不得含内联 <style>/<script>」
  并写明原因,防止下次再踩
2026-11-27 22:01:37 +08:00

78 lines
3.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const root = path.resolve(__dirname, '..')
const dist = path.join(root, 'dist')
function fail(reason) {
console.log('FAIL ' + reason)
process.exit(1)
}
function pass(msg) {
console.log('PASS ' + msg)
}
const versionJsonPath = path.join(dist, 'version.json')
if (!fs.existsSync(versionJsonPath)) fail('dist/version.json 不存在')
let meta
try {
meta = JSON.parse(fs.readFileSync(versionJsonPath, 'utf8'))
} catch (e) {
fail('dist/version.json 解析失败: ' + e.message)
}
// 1. dist/app.js 以 banner `/*! IWB <version> */` 开头,且 <version> 与 version.json.version 一致
const appPath = path.join(dist, 'app.js')
if (!fs.existsSync(appPath)) fail('dist/app.js 不存在')
const app = fs.readFileSync(appPath, 'utf8')
const expectedBanner = '/*! IWB ' + meta.version + ' */'
if (!app.startsWith(expectedBanner)) {
fail('dist/app.js 未以 banner 开头(期望: ' + JSON.stringify(expectedBanner) + ',实际前 48 字符: ' + JSON.stringify(app.slice(0, 48)) + ')')
}
pass('dist/app.js banner 与 version.json.version 一致: ' + expectedBanner)
// 2. dist/index.html 含 `app.js?v=`,恰好 3 个 <script,且不含 src/
const htmlPath = path.join(dist, 'index.html')
if (!fs.existsSync(htmlPath)) fail('dist/index.html 不存在')
const html = fs.readFileSync(htmlPath, 'utf8')
if (!html.includes('app.js?v=')) fail('dist/index.html 缺少 app.js?v=')
const scriptCount = (html.match(/<script/g) || []).length
if (scriptCount !== 3) fail('dist/index.html <script 数量应为 3,实际 ' + scriptCount)
// v3.26.109:index.html 里不允许出现内联 <style>/<script> —— Tauri 会给它们加 CSP nonce,
// CSP3 下 nonce 一出现 'unsafe-inline' 就失效,插件动态注入的 <style> 会被整段丢弃(v3.26.9 事故)。
const inlineStyle = /<style[^>]*>[\s\S]*?<\/style>/i.test(html)
const inlineScript = /<script(?![^>]*\bsrc=)[^>]*>[\s\S]*?<\/script>/i.test(html)
if (inlineStyle || inlineScript) fail('dist/index.html 含内联 <style>/<script>:会触发 CSP nonce,插件样式将全部失效')
if (!html.includes('iwb-boot-static')) fail('dist/index.html 缺少静态启动屏(局域网资料目录启动时的白屏兜底)')
if (html.includes('src/')) fail('dist/index.html 仍含 src/ 引用')
pass('dist/index.html 含 app.js?v=、静态启动屏标记、无内联 style/script,无 src/ 引用')
// 3. dist/app.js 含无引号标记子串,证明内联压缩 worker 字面量在 minify 后仍存活
if (!app.includes('self.onmessage=async function(ev)')) {
fail('dist/app.js 缺少 self.onmessage=async function(ev)(内联压缩 worker 未存活)')
}
pass('dist/app.js 保留内联压缩 worker 标记 self.onmessage=async function(ev)')
// 4. dist/src 目录不应存在
if (fs.existsSync(path.join(dist, 'src'))) fail('dist/src 目录仍存在')
pass('dist/src 目录不存在')
// 5. dist/version.json 可解析且 version/packageVersion/commit 均非空
for (const key of ['version', 'packageVersion', 'commit']) {
if (!meta[key] || String(meta[key]).length === 0) fail('dist/version.json.' + key + ' 为空')
}
pass('dist/version.json 的 version/packageVersion/commit 均非空')
// 6. PWA 安装面已移除(2026-11-26):根图标/manifest 不再进 dist,也不能再被 index.html 引用。
// sw.js 必须保留:它负责更新检测和 /__iwb_cache__ 图片直出,不是纯 PWA 安装面。
for (const f of ['favicon.svg', 'manifest.webmanifest', 'icon-192.png', 'icon-512.png', 'apple-touch-icon.png']) {
if (fs.existsSync(path.join(dist, f))) fail('dist/' + f + ' 不应存在(PWA 安装面已移除)')
if (html.includes(f)) fail('dist/index.html 仍引用已删除的 ' + f)
}
if (!fs.existsSync(path.join(dist, 'sw.js'))) fail('dist/sw.js 必须保留(更新检测 + 图片缓存)')
pass('PWA 图标/manifest 未进 dist,index.html 无引用;sw.js 保留')