Repository navigation
🐛 修复 Firefox 上 GM_xmlhttpRequest 请求 http 地址直接 onerror - #1855
Merged
Merged
Conversation
Firefox MV3 未声明 extension_pages CSP 时默认使用 "script-src 'self'; upgrade-insecure-requests;",后台页发出的 http:// 请求 被强制升级为 https:// 而失败。显式声明 "script-src 'self';",脚本策略与默认一致, 只去掉 upgrade-insecure-requests,与 Chrome MV3 行为对齐。
同主机其余接口均为 https,仅 precreate 写成了 http://。Firefox 去掉 upgrade-insecure-requests 后该请求不再被自动升级,需改为 https。
Vitest 4 对 vmThreads/vmForks 强制 isolate=false,并在 isolate=false && maxWorkers===1 时把整个项目的文件合并成一个任务、跑在同一个 VM 上下文里(setup 只执行一次)。 #1767 把本地 maxWorkers 改为 1 后,fast/ui 项目的模块缓存、vi.mock 与 chrome mock 在文件之间共享,本地全量每次挂几十到上百个文件;CI 多 worker 下每个文件独立上下文,不暴露。 本地 vm 池项目改为 2 个 worker 以保留按文件隔离,CI 配额不变;新增配置契约测试防止回退。
vm 池在 maxWorkers 为 1 时必然把整个项目合进同一个 VM 上下文,因此本地不再调高 worker 数, 而是按 process.env.CI 区分:本地 fast/ui 用 threads + isolate:true(1 个 worker),CI 仍用 vmThreads 与原配额。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist / 检查清单
Description / 描述
close #1846
背景
Firefox 上 1.5.1(AMO 首个 MV3 版本)里,
GM_xmlhttpRequest请求http://地址会立刻onerror(status 0、readyState 4),不弹确认框;Chrome 正常。issue 推测是站点访问权限检查的问题,实测不是:权限已具备,请求也发出了,但被 Firefox 改成了 https。原因:manifest 未声明
content_security_policy.extension_pages时,Firefox 给 MV3 扩展页面的默认 CSP 是script-src 'self'; upgrade-insecure-requests;(prefextensions.webextensions.default-content-security-policy.v3,在 Firefox 154 里直接读出)。Firefox 上 GM_xhr 由后台页发出,http://被强制升级为https://,对端没有 TLS 就失败。MV2 的默认 CSP 不带这一项,所以 0.16(MV2)、Tampermonkey 5.5.0 / Violentmonkey 2.50.0(AMO 上均为 MV2)都没有这个问题。回环地址不会被升级,所以原来请求本地 mock 的 Firefox GM 测试没有覆盖到。不止 IP,域名形式的http://同样受影响。本次改动
createFirefoxManifest显式声明extension_pages: "script-src 'self';"。脚本加载规则和原来的默认值完全一致,只去掉upgrade-insecure-requests,与 Chrome MV3 行为对齐。Chrome manifest 不变。百度网盘 precreate 接口由
http://改为https://。同主机其余接口都是 https,只有这一处是 http,URL 里带着access_token。之前 Firefox 靠自动升级碰巧走的是 https,第 1 项上线后会变成明文,所以一起修掉,单独一个提交。(按维护者要求并入本 PR,超出 [Compatibility] Firefox 上 GM_xmlhttpRequest 请求 IP 地址直接 onerror,Chrome 正常 #1846 的范围) 修复本地 Vitest 全量测试必然失败的问题:本地
fast/ui两个项目改用threads池并开启isolate,仍然只用 1 个 worker;按process.env.CI区分,CI 仍用vmThreads和原来的配额,不受影响。vmThreads/vmForks强制isolate = false;调度器在isolate === false && maxWorkers === 1时,会把整个项目的文件合成一个任务,放进同一个 VM 上下文执行,setup 只跑一次。🔧 限制本地测试为单 worker,CI 保留并行分片 #1767 把本地 worker 数改成 1 之后,测试文件之间就共享了模块缓存、vi.mock和 chrome mock。例如dom.test.ts把chrome.permissions换掉不恢复,下一个文件会报permissions.reset is not a function;sandbox/runtime.test.ts的局部vi.mock会让skill_script_executor.test.ts报No "executeSkillScript" export。CI 是多 worker,每个文件一个独立上下文,所以不暴露。vm 池在 1 个 worker 下没有配置项能避开这种合并,所以本地只能换池。vi.mock,这些文件本来就默认按文件隔离。真正的问题是本地拓扑让隔离失效,恢复隔离才是根因修复。threads和 CIvmThreads的运行环境有差异(见vitest.config.ts里 ISOLATED 名单的注释),可能出现只在一边失败的情况。另一个方案是 vm 池 + 本地 2 个 worker,那样更快、和 CI 同池,但维护者选择保持本地单 worker。已知限制 / 影响面
http://fetch / XHR 不再被自动升级(这正是本次修复的目的)。代价是:如果脚本里写的是http://地址(@require、@resource、更新地址等)但对方只开了 https,1.5.1 在 Firefox 上碰巧能用,修复后会失败。这与 Chrome、TM、VM、0.16 的行为一致。建议审查重点
scripts/build-config.js中 CSP 的取值是否就是"默认值去掉upgrade-insecure-requests"。验证(HEAD
782ad2ba,base0037a1ca)vitest run packages/filesystem/ scripts/ src/manifest.test.ts共 18 个文件、275 个用例通过;提交钩子的 prettier、typecheck、eslint 通过。dist/ext(Selenium/geckodriver 解包安装真实扩展),三个版本结果一致。159、160 的默认 CSP pref 同样是script-src 'self'; upgrade-insecure-requests;:https://www.chaoxing.com+http://154.44.26.234/...):修复前 CSP 下onerror status=0 readyState=4,HEAD 下onload 200,返回真实 JSON。script-src 'self';。addons-linter:0 个错误;CSP 相关警告只针对原有的sandboxCSP,没有针对extension_pages的。https://pan.baidu.com/rest/2.0/xpan/file?method=precreate(无效 token)返回{"errno":-6},说明 https 接口可用。维护者用782ad2ba构建的dist/ext实测「备份到百度网盘」上传成功,这条备份流程会经过 precreate 接口。HEAD00dfce6b相对782ad2ba只改了测试配置,运行时代码相同。a4f273dc):改前,本地默认全量pnpm exec vitest run每次失败几十到上百个文件,干净的 base 上同样如此(例如 37 个文件、229 个用例)。改后,本地全量 382 个文件、5222 个用例全部通过,耗时 187 秒(系统负载 4~6);CI=true pnpm exec vitest run --no-coverage同样全部通过,耗时 35 秒。