修复 QQ 跳转链接中 URL 编码导致的双斜杠问题 #658
Open
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.
问题描述
在使用 redirect 插件处理 QQ 跳转链接时,当目标 URL 包含编码的斜杠(
%2F)时,会导致跳转到错误的地址。复现步骤
访问以下 QQ 跳转链接:
插件解析 URL 参数后,
%2F被URLSearchParams自动解码为/实际跳转到的地址变成:
(注意:域名后有两个斜杠)
访问该地址时返回错误:
预期行为
应该跳转到正确的地址:
(域名后只有一个斜杠)
问题根源
在
src/utils/querystring.ts中,parse()函数使用URLSearchParams来解析 URL 参数。URLSearchParams会自动对参数值进行 URL 解码,将%2F解码为/。当原始 URL 为
https://anuneko.com/%2F时:%2F本应表示路径中的单个斜杠/https://anuneko.com//(协议后的//+ 解码后的/)解决方案
在
src/scripts/redirect/index.ts中添加了 URL 规范化处理:#normalize()私有方法,用于规范化 URL 路径/\/+/g将路径中的多个连续斜杠替换为单个斜杠#parse()方法中,在#ensure()之后调用#normalize()代码变更
测试验证
修复后,访问
https://c.pc.qq.com/ios.html?url=https://anuneko.com/%2F将正确跳转到https://anuneko.com/。影响范围
此修复不会影响其他正常的跳转链接,只会规范化路径中的多余斜杠,使 URL 更加标准化。
相关 Issue
此问题可能影响所有通过 URL 参数传递目标地址的跳转场景,特别是当目标 URL 包含编码字符时。