Skip to content

fix: 修复 Markdown / 文本对比 / 正则工具中的 XSS - #12

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-correctness-bugs-b353
Draft

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-correctness-bugs-b353

Conversation

@cursor

@cursor cursor Bot commented May 17, 2026

Copy link
Copy Markdown

问题与影响

在纯前端工具页中,多处将用户输入直接写入 innerHTML,导致存储型 DOM XSS:攻击者诱导用户粘贴恶意内容即可在同一页面上下文执行脚本,可窃取剪贴板、篡改界面或劫持会话(对在本页粘贴敏感 Token 的用户风险尤其高)。

根因

  1. Markdown 预览renderMD() 将未匹配的原始 HTML 片段与简易替换结果一并赋给 innerHTML
  2. 文本对比textDiff() 将两侧行内容拼进 HTML 字符串而未转义。
  3. 正则测试testRegex() 将匹配子串及异常信息拼进 HTML 而未转义。

修复

  • 新增 escapeHtml(),在 textDifftestRegex 中对用户侧文本做 HTML 实体转义后再插入模板。
  • 从 jsDelivr 引入 DOMPurify,对 Markdown 生成的 HTML 在写入前做 sanitize;若脚本被拦截则回退为 textContent 纯文本预览,避免静默恢复为不安全路径。

验证

  • 人工核对:innerHTML 赋值路径中用户内容均已转义或由 DOMPurify 过滤;无自动化测试(仓库为单文件静态页,无现有测试框架)。
Open in Web View Automation 

Escape user-controlled strings before innerHTML in diff/regex tools.
Sanitize markdown-generated HTML with DOMPurify; plain-text fallback if CDN blocked.

Co-authored-by: Muki182 <Muki182@users.noreply.github.com>
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.

1 participant