Skip to content

修正 fetchScriptBody 的 Accept 设定,避免触发 Error 406#1306

Open
cyfung1031 wants to merge 2 commits intoscriptscat:mainfrom
cyfung1031:fix-err-406
Open

修正 fetchScriptBody 的 Accept 设定,避免触发 Error 406#1306
cyfung1031 wants to merge 2 commits intoscriptscat:mainfrom
cyfung1031:fix-err-406

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Mar 22, 2026

Checklist / 检查清单

  • Fixes mentioned issues / 修复已提及的问题
  • Code reviewed by human / 代码通过人工检查
  • Changes tested / 已完成测试

Description / 描述

修正 fetchScriptBody 的 Accept 设定,避免触发 Error 406
(see #1271)
测试: https://openuserjs.org/scripts/alexchen/eHunter

Screenshots / 截图

Screenshot 2026-03-22 at 11 39 26

@cyfung1031 cyfung1031 added bug Something isn't working P0 🚑 需要紧急处理的内容 hotfix 需要尽快更新到扩展商店 labels Mar 22, 2026
Copy link
Member

@CodFrm CodFrm left a comment

Choose a reason for hiding this comment

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

有点多做多错的感觉了

"Cache-Control": "no-cache",
Accept: "text/javascript,application/javascript,text/plain,application/octet-stream,application/force-download",
/* 不指定 application/octet-stream 和 application/force-download 避免触发伺服器端 Error 406 */
Accept: "text/javascript, application/javascript, */*", // prefer JavaScript, but anything is acceptable
Copy link
Member

Choose a reason for hiding this comment

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

我觉得可以直接不指定了

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这个还好吧。之前没 */*
加了 */* 后就不会让 Server报错
只是 javascript 优先

const contentType = response.headers.get("content-type");

if (contentType) {
// 不接受非 JavaScript文本 的回应
Copy link
Member

Choose a reason for hiding this comment

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

我觉得也不用管,后面会去解析的,如果有问题依旧会报错

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

避免恶意连结。如果类型不对,就不下载了

Copy link
Collaborator Author

@cyfung1031 cyfung1031 Mar 22, 2026

Choose a reason for hiding this comment

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

这个包括网址不正确的跳页。会直接不下载而不是下载后再解析

@cyfung1031
Copy link
Collaborator Author

@CodFrm 你再看看吧。我认为这些测试都是必须的。因为不知道用户会点了什么连结
你还是觉得不需要的话,你可以直接删去

@CodFrm
Copy link
Member

CodFrm commented Mar 22, 2026

@CodFrm 你再看看吧。我认为这些测试都是必须的。因为不知道用户会点了什么连结 你还是觉得不需要的话,你可以直接删去

去了吧,我们也不清楚服务端会不会按照所规定的来执行,也会导致错误;反正最后会实际的解析脚本代码,来确定脚本是否正确

@cyfung1031
Copy link
Collaborator Author

@CodFrm 你再看看吧。我认为这些测试都是必须的。因为不知道用户会点了什么连结 你还是觉得不需要的话,你可以直接删去

去了吧,我们也不清楚服务端会不会按照所规定的来执行,也会导致错误;反正最后会实际的解析脚本代码,来确定脚本是否正确

之前 Accept 没加 */* 所以导致错误

@CodFrm
Copy link
Member

CodFrm commented Mar 27, 2026

@CodFrm 你再看看吧。我认为这些测试都是必须的。因为不知道用户会点了什么连结 你还是觉得不需要的话,你可以直接删去

去了吧,我们也不清楚服务端会不会按照所规定的来执行,也会导致错误;反正最后会实际的解析脚本代码,来确定脚本是否正确

之前 Accept 没加 */* 所以导致错误

如果不加这些,是不是都不会有这个问题

@CodFrm
Copy link
Member

CodFrm commented Mar 27, 2026

Code review

Found 1 issue:

  1. text/plain 回退问题:旧代码的 Accept 头中明确包含 text/plain,且 content-type 验证只拒绝 text/html,因此 Content-Type: text/plain 的响应会被正常接受并安装。新代码的验证逻辑只允许 text/javascriptapplication/javascriptapplication/octet-streamapplication/force-downloadtext/plain 不再被接受。许多脚本托管服务(如 GitHub raw、Gist 等 pastebin 类服务)会以 text/plain 提供 .user.js 文件,这会导致这些来源的脚本安装失败。

if (m) {
const contentTypeOK =
(m[2] === "javascript" && (m[1] === "text" || m[1] === "application")) ||
(m[1] === "application" && (m[2] === "octet-stream" || m[2] === "force-download"));
if (!contentTypeOK) {
throw new Error(`Response is ${contentType}, not a valid UserScript`);
// e.g. Response is text/html, not a valid UserScript

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hotfix 需要尽快更新到扩展商店 P0 🚑 需要紧急处理的内容

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants