From 67d4dc9207e9062cc5aa2d4fd0e459f51129644c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Tu=E1=BA=A5n=20Anh?= Date: Thu, 12 Nov 2026 04:52:36 +0700 Subject: [PATCH 1/2] feat(markdown): render svg fences as inline SVG --- components/MarkdownCode.tsx | 4 ++ components/SvgBlock.test.mjs | 92 +++++++++++++++++++++++++++++++ components/SvgBlock.tsx | 101 +++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 components/SvgBlock.test.mjs create mode 100644 components/SvgBlock.tsx diff --git a/components/MarkdownCode.tsx b/components/MarkdownCode.tsx index 51645cc18..9806c370d 100644 --- a/components/MarkdownCode.tsx +++ b/components/MarkdownCode.tsx @@ -1,5 +1,6 @@ import type { HTMLAttributes } from "react"; import { MermaidBlock, CodeBlock } from "./MermaidBlock"; +import { SvgBlock } from "./SvgBlock"; /** Build the `code` renderer shared by MarkdownBody and FileViewer's * ReactMarkdown configs (the two previously inlined near-identical copies). @@ -23,6 +24,9 @@ export function markdownCodeRenderer(options: { if (lang === "mermaid") { return ; } + if (lang === "svg") { + return ; + } return ; } return ( diff --git a/components/SvgBlock.test.mjs b/components/SvgBlock.test.mjs new file mode 100644 index 000000000..468a0bd77 --- /dev/null +++ b/components/SvgBlock.test.mjs @@ -0,0 +1,92 @@ +import "../tests/setup-dom.mjs"; +import assert from "node:assert/strict"; +import test from "node:test"; +import React from "react"; +import { renderToStaticMarkup } from "react-dom/server"; + +// SvgBlock parses at render time with the browser globals; jsdom provides +// them on window, so expose them before importing the component. +globalThis.DOMParser = window.DOMParser; +globalThis.XMLSerializer = window.XMLSerializer; + +const { SvgBlock, parseSvgMarkup } = await import("./SvgBlock.tsx"); +const { markdownCodeRenderer } = await import("./MarkdownCode.tsx"); + +const validSvg = ` + svg block renders inline + +`; + +test("parseSvgMarkup keeps a valid svg and normalizes its root", () => { + const markup = parseSvgMarkup(validSvg); + assert.match(markup, /]*viewBox="0 0 420 90"/); + assert.match(markup, /max-width:100%;height:auto/); + assert.match(markup, /fill="currentColor"/); + assert.match(markup, /xmlns="http:\/\/www\.w3\.org\/2000\/svg"/); + // Theme-variable fills survive sanitization. + assert.match(markup, /fill="var\(--muted\)"/); +}); + +test("parseSvgMarkup strips script, event handlers, foreignObject, style and javascript hrefs", () => { + const markup = parseSvgMarkup(` + + +
evil
+ x + y + +
`); + assert.doesNotMatch(markup, / and the browser +// parses and executes the nested SVG's scripts. vbscript: is dead in modern +// browsers but still honored by some embedded WebViews — block it too. +const UNSAFE_HREF = /^\s*(javascript|vbscript|data:)/i; const UNSAFE_ATTRIBUTE = /^on/i; +// A style attribute can smuggle code through url(javascript:…) or the legacy +// IE expression(…); match inside url(…) rather than at the value start, +// since the URL sits after a property name (background: url(…)). +const UNSAFE_STYLE = /expression\s*\(|url\s*\(\s*['"]?\s*(javascript|vbscript|data):/i; function sanitizeSvg(root: Element): void { for (const tag of UNSAFE_TAGS) { @@ -48,6 +56,9 @@ function sanitizeSvg(root: Element): void { if (/^(href|xlink:href)$/i.test(attr.name) && UNSAFE_HREF.test(attr.value)) { el.removeAttribute(attr.name); } + if (/^style$/i.test(attr.name) && UNSAFE_STYLE.test(attr.value)) { + el.removeAttribute(attr.name); + } } } }