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..b6cbe74b7 --- /dev/null +++ b/components/SvgBlock.test.mjs @@ -0,0 +1,114 @@ +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) { + for (const el of root.querySelectorAll(tag)) el.remove(); + } + // querySelectorAll("*") covers descendants only — the root itself can also + // carry an on* handler or a javascript: href, so sweep it too. + for (const el of [root, ...root.querySelectorAll("*")]) { + for (const attr of [...el.attributes]) { + if (UNSAFE_ATTRIBUTE.test(attr.name)) { + el.removeAttribute(attr.name); + continue; + } + 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); + } + } + } +} + +/** + * Parse + sanitize an SVG fence into embeddable markup, or null when the + * source is not a well-formed `` document (malformed input, or a fence + * still streaming half-written markup). Pure except for DOMParser / + * XMLSerializer, so tests drive it directly. + */ +export function parseSvgMarkup(code: string): string | null { + let doc: Document; + try { + doc = new DOMParser().parseFromString(code, "image/svg+xml"); + } catch { + return null; + } + if (doc.querySelector("parsererror")) return null; + const root = doc.documentElement; + if (!root || root.nodeName.toLowerCase() !== "svg") return null; + sanitizeSvg(root); + const style = root.getAttribute("style"); + root.setAttribute("style", `${style ? `${style};` : ""}max-width:100%;height:auto`); + if (!root.hasAttribute("fill")) root.setAttribute("fill", "currentColor"); + if (!root.hasAttribute("xmlns")) root.setAttribute("xmlns", "http://www.w3.org/2000/svg"); + return new XMLSerializer().serializeToString(root); +} + +export function SvgBlock({ code, isStreaming }: SvgBlockProps) { + const markup = useMemo(() => parseSvgMarkup(code), [code]); + + // Streaming generates half-written markup; fall back to the code block until + // the fence closes. + if (!markup) { + return ; + } + return ( +
+ ); +}