Skip to content

feat(markdown): 内嵌 SVG 按白名单保留绘图元素,脚本、外部引用和 style 仍被剥离 - #32

Merged
xiaoyumuxi merged 1 commit into
mainfrom
feat/markdown-inline-svg
Nov 14, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
feat/markdown-inline-svg

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联

桌面端 Markdown 预览改用 web view(Lithe-IDEA 的 Draft PR,随后补链接)。预览要显示 README 里的内嵌 <svg>,过滤器需要放行它。

改动

  • markdown.render 的 Ammonia 过滤器放行 SVG 的绘图元素:svg、g、defs、symbol、use、title、desc、各类图形、text/tspan/textPath、image、线性/径向渐变、stop、pattern、clipPath、mask、marker、filter,以及不加载外部资源的滤镜原语(feGaussianBlur、feOffset、feColorMatrix 等)。这些元素都带上几何属性和展示属性(viewBox、d、fill、stroke-*、transform、text-anchor 等)。
  • 仍然剥离:script、foreignObject、style 元素、动画元素(animate、set 等)、feImage、style 属性、事件属性、javascript: 等危险 URL。use 和 textPath 的 href 只能是本文档的 #id,引用外部文件时直接去掉。image 的 href 照常走 Ammonia 的 URL scheme 检查。
  • 元素只能经由 <svg> 切换到 SVG 命名空间,这条由 Ammonia 的命名空间检查保证,所以 <svg><iframe>… 一类的变异 XSS 仍被拒绝。
  • style 仍不开放,沿用 #1623 的决定;SVG 的颜色和线条改由展示属性表达。
  • 共享 fixture rust/fixtures/editor/markdown-render-v1.json 新增用例“inline SVG draws without scripts or external references”。新增 Rust 测试 satisfies_shared_render_fixture 跑完整份 fixture,以前这份 fixture 只有桌面端在跑。另有 inline_svg_keeps_drawing_and_drops_what_runs_or_embeds 覆盖 textPath、image、feImage、set 和 style 元素。
  • 契约 rust/contracts/rust-core-api.md 的“Markdown rendering”一节写明 SVG 放行与剥离的范围。

验证

  • cargo test -p lithe-project --lib markdown:8 个测试全部通过
  • cargo fmt -p lithe-project、cargo clippy -p lithe-project --all-targets -- -D warnings:通过

不涉及插件契约、能力版本或插件函数协议,所以不需要更新协议记录。

markdown.render 的 Ammonia 过滤器放行 SVG 的图形、文字、use、image、渐变、图案、裁剪、蒙版、标记和不加载外部资源的滤镜,以及它们的几何与展示属性;script、foreignObject、style 元素、动画元素、feImage 和 style 属性仍被剥离,use/textPath 只能引用本文档的 #id。共享 fixture markdown-render-v1.json 增加内嵌 SVG 用例,Core 新增测试跑完整份 fixture。
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