Skip to content

fix(security): 防止 Markdown / 文本对比 / 正则结果中的 XSS - #13

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

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

Conversation

@cursor

@cursor cursor Bot commented May 18, 2026

Copy link
Copy Markdown

问题与影响

index.html 中多处将用户输入直接写入 innerHTML(Markdown 预览、文本对比、正则匹配结果)。攻击者可诱导用户粘贴恶意 HTML/脚本(例如 <img src=x onerror=...>),在同源页面上下文执行脚本,属于存储型/反射型混合的 DOM XSS,影响机密性与完整性。

根因

未对用户可控文本做 HTML 转义即拼接进 HTML 字符串。

修复

  • 新增 escapeHtml(),对 &"'<> 进行实体转义。
  • Markdown:在应用简易 Markdown 替换前先对全文转义。
  • 文本对比:对每行输出转义后再写入 innerHTML
  • 正则匹配:对匹配子串及异常 message 转义后再写入 innerHTML

验证

  • 本地用 Node 对 escapeHtml 逻辑做了断言(<script>&lt;script&gt;a&ba&amp;b)。
  • 仓库为纯静态单文件,未引入额外测试框架;可在浏览器中打开页面,在 Markdown 中粘贴 <img src=x onerror=alert(1)> 验证不再执行脚本。
Open in Web View Automation 

Markdown preview, text diff, and regex match output inserted raw user
input into innerHTML, allowing script injection from pasted content.
Add escapeHtml() and apply it to these code paths.

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