Skip to content

🐛 跨浏览器过滤 userScripts 不支持的 @match scheme - #1772

Open
cyfung1031 wants to merge 10 commits into
mainfrom
fix/invalid-match-scheme-1771
Open

cyfung1031 wants to merge 10 commits into
mainfrom
fix/invalid-match-scheme-1771

Conversation

@cyfung1031

@cyfung1031 cyfung1031 commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Checklist / 检查清单

背景

#1771 的 RED 用例确认:ScriptCat 会把 notsupported://*/* 与有效的 https/http 一起放进浏览器原生 userScripts 的 match-pattern 数组,单个不受支持的 scheme 因而使整条 userscript 注册失败。

根因不是 MV3 本身,而是 ScriptCat 在进入浏览器原生 match-pattern API 前没有按浏览器 API 契约过滤 scheme。

本次改动

修复位于 ScriptCat 到浏览器原生 userScripts API 的边界,不收窄 ScriptCat 自己的 URL matcher:

  • Chromium 原生 matches / excludeMatches 只保留当前支持的 *、http、https、file;Firefox 额外保留其支持的 ws、wss、ftp。
  • 两个浏览器都会过滤各自 API 不支持的未知 scheme,不会把 Chromium 白名单错误地套到 Firefox。
  • 新脚本注册、全局 blacklist 的原生排除项,以及已有 CompiledResource 的启动恢复路径共用同一过滤函数。
  • 过滤后没有可注册 match 时,不创建无效的原生注册项;ScriptCat 内部 matcher 仍保留原始规则。

实现考虑

过滤集合跟随各浏览器的 userScripts Match Patterns 契约:Chromium 只接受 *、http、https、file;Firefox 的 MV3 userScripts schema 另外接受 ws、wss、ftp。因此这次修复同时覆盖两个浏览器,又不会误删 Firefox 支持的 scheme。

同时没有通过失败后逐项试注册来“探测”坏 pattern:先在无副作用的 API 投影层按浏览器契约过滤,注册流程保持单次、确定性。

建议审查重点

  • 混合 https/http/notsupported 时最终 RegisteredUserScript.matches 只剩当前浏览器支持的有效项。
  • Firefox 的 ws、wss、ftp 不会被错误过滤。
  • excludeMatches、全局 blacklist 和旧 CompiledResource 不会绕过同一过滤;内部 URL 匹配不受影响。

关联

Fixes #1771

验证

  • Focused Vitest:pnpm exec vitest run src/app/service/service_worker/utils.match-scheme.test.ts src/app/service/service_worker/utils.test.ts src/app/service/service_worker/runtime.test.ts,3 files、85 tests passed。
  • pnpm exec tsc --noEmit --incremental false、Prettier 与 changed-file ESLint 均通过。
  • 完整 pnpm exec vitest run:373 files 中 372 passed、1 failed;唯一失败是既有的 src/app/service/service_worker/trash_event_partition.test.ts,happy-dom 不支持加载 chrome-extension:// iframe,与本次改动无关。
  • 之前 head 的 GitHub Actions 结果不能代表当前 head;本次 head a0be9f1c3d5372757dd1204733f1ea846cbdabf2 的 CI 结果以 GitHub Actions 为准。

@cyfung1031
cyfung1031 marked this pull request as ready for review September 22, 2026 19:38
Comment thread src/app/service/service_worker/utils.ts Outdated

export function filterUserScriptApiPatternsByScheme(patterns: readonly string[]): string[] {
// Firefox 的 match-pattern scheme 集合与 Chromium 不同;这里只在已验证的 Chromium API 边界收紧。
if (isFirefox()) return [...patterns];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确定firefox不能也受益么?

@cyfung1031 cyfung1031 Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像是。应该不用分 firefox 和 chromium. Agent 的处理比较保守

避免未经验证地缩窄 Firefox 现有行为。

E2E 沒 Firefox, Agent 不敢改

@cyfung1031 cyfung1031 changed the title 🐛 过滤 Chromium 不支持的 @match scheme 🐛 跨浏览器过滤 userScripts 不支持的 @match scheme Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Unsupported @match scheme invalidates the whole userscript registration

2 participants