fix(security): add DOMPurify sanitization for Markdown rendering XSS - #1212
Open
ErlichLiu wants to merge 1 commit into
Open
fix(security): add DOMPurify sanitization for Markdown rendering XSS#1212ErlichLiu wants to merge 1 commit into
ErlichLiu wants to merge 1 commit into
Conversation
- Add shared markdown-sanitize.ts with strict DOMPurify config: FORBID_TAGS (iframe/object/embed/form), ALLOW_DATA_ATTR: false, explicit ADD_ATTR whitelist for Proma custom data attributes - Sanitize HTML before innerHTML in enhanceMarkdownHtml and htmlToMarkdown - Replace local sanitizeHtml in markdown-preview-extensions with shared module - Add 7 XSS protection tests (script/onerror/javascript:/iframe/object stripping + math block/details block preservation) References: HITCON 2023 Obsidian iframe local file disclosure, VS Code webview CSP defense-in-depth, Open WebUI DOMPurify pattern
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.
问题
markdown-rich-text.ts使用markdown-it(html: true)渲染 Markdown,输出通过innerHTML赋值给 DOM,缺少 DOMPurify 净化。这是 6 月 23 日深度 Bug 审计中标记的最后一个 P0 安全问题。修复方案
基于业界调研(Obsidian DOMPurify 方案 + VS Code CSP 纵深防御 + Open WebUI 模式),在 HTML 进入 DOM 前用 DOMPurify 严格配置净化。
改动
markdown-sanitize.ts— 共享 DOMPurify 净化模块FORBID_TAGS: iframe/object/embed/form 等高危标签(Obsidian 曾因允许 iframe 导致本地文件泄露,HITCON 2023)ALLOW_DATA_ATTR: false: 禁止任意 data-* 属性,防止 DOM clobberingADD_ATTR白名单: 只允许 Proma 渲染管线依赖的 data-type/data-html/data-markdown/data-latex 等markdown-rich-text.ts—enhanceMarkdownHtml和htmlToMarkdown的innerHTML前加sanitizeMarkdownHtml()markdown-preview-extensions.tsx— 删除本地sanitizeHtml函数,改用共享模块(配置统一,避免漂移)防御层级
调研依据
Proma 采用 Obsidian 同路线(DOMPurify),配置比 Obsidian 更严格(禁止 iframe + ALLOW_DATA_ATTR: false)。