fix: 修复 safeUrl 未拦截危险 URL scheme - #5
Open
Zi-Yi-Ming wants to merge 1 commit into
Open
Zi-Yi-Ming wants to merge 1 commit into
Zi-Yi-Ming wants to merge 1 commit into
Conversation
safeUrl() 原先只校验 URL 首字符(/^[\w./-]/),导致 javascript:、data:、 vbscript:、file:、blob:、about: 等危险 scheme 可进入生成的 HTML URL 属性 (<a href>、<img src>、<video src>、data-zoom)。 现在对命中该分支的候选值先做 decodeURIComponent() 提取 scheme,再与 denylist 比对,命中则返回空字符串;decode 包裹在 try/catch 中,畸形百分号编码不会抛异常。 正常 http(s)、//、站内绝对路径与相对路径行为保持不变,HTML escaping 未被削弱。 新增 tests/markdown.test.mjs 回归测试:覆盖正常 URL、危险 scheme、大小写变体、 百分号编码、畸形编码,以及渲染层 href / src / data-zoom 输出。
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.
改动内容
site/js/markdown.js中safeUrl()对 URL scheme 校验过于宽松的问题。javascript:、data:、vbscript:、file:、blob:、about:等危险 scheme,同时兼容大小写及 URL 编码形式。safeUrl()回归测试,并补充链接、图片、视频渲染层面的安全测试。Why
原有
safeUrl()在部分 URL 上仅通过首字符检查(/^[\w./-]/),导致包含危险 scheme 的值可能继续进入 HTML 的href、src和data-zoom属性。本次采用最小范围修复,仅收紧 URL scheme 校验,不引入新的依赖,也不改变正常的
http(s)、站内路径及相对路径行为。验证方式
node --test tests/markdown.test.mjs:12/12 通过git diff --check:通过(无空白字符错误)npm run audit:通过(需将CHROME_PATH指向本机 Chrome;相关工具默认硬编码 macOS 路径/Applications/Google Chrome.app/Contents/MacOS/Google Chrome,在 Windows 上无法启动浏览器)npm test:部分通过后失败,失败项与本次修改无关,详见下方说明npm test失败说明(与本次修改无关)设置
CHROME_PATH后,npm test依次执行:tools/build-site-index.mjs— 通过tools/check-boot.mjs— 通过tools/check-landing.mjs— 通过tools/check-features.mjs— 通过tools/check-mobile.mjs— 失败:360px articleWhere 对比度应至少达到 3:1该断言读取的是
.article__where的getComputedStyle().color(颜色来自未改动的site/css/article.css中的var(--ink-faded)),与本次 URL scheme 校验无关。对照实验确认:将页面所用脚本替换为修改前 HEAD 版本的markdown.js后,失败信息完全一致(同样为360px articleWhere 对比度应至少达到 3:1)。因此这是既有问题,本次未作修改。安全影响
修复 Markdown renderer 中的 URL scheme 校验缺陷,避免危险 scheme 进入生成的 HTML URL 属性。
当前仓库的 Markdown 内容来自站内静态文件(
content/site-index.json、content/site-content.json,同源 fetch),未发现"外部不可信 Markdown 输入 → renderer → 真实浏览器执行"的完整链路。因此本问题准确定位为 URL sanitization / unsafe URL scheme validation 安全缺陷,并存在潜在 XSS 风险(一旦内容来源变为用户可控,<a href="javascript:...">将成为可点击的 XSS 向量),而非已确认的远程 XSS。Product fact updates
不涉及。