From 1385b8076e0098ff1d8ce2d9020e24848d334a27 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 22 May 2026 17:01:30 +0000 Subject: [PATCH] Studio: sandbox SVG previews inside a CSP-locked iframe Reviewers flagged the inline SVG preview as a regression from the pre-PR data-URI path: DOMPurify's default SVG profile keeps `; + const clean = sanitizeSvgSource(svg).toLowerCase(); + expect(clean).not.toContain(" { + const svg = ``; + const clean = sanitizeSvgSource(svg).toLowerCase(); + expect(clean).toContain("/ tags so SVG cannot beacon to external URLs", () => { + const svg = ``; + const clean = sanitizeSvgSource(svg).toLowerCase(); + expect(clean).not.toContain("]|]|]|]|]/i; -// SVGs may legitimately reference fonts/images via href, so we keep those. -// We strip every event handler attribute (on*) and any tag DOMPurify would -// otherwise allow that could escape the SVG sandbox. +// SVG previews used to live inside a `` tag +// where the browser treats the SVG as an image and disables scripts and +// external resource loads. Mounting sanitized SVG directly into the host +// Studio document loses those guarantees, so we now (a) strip every node that +// can leak into the host page (`", + safeSvg, + ].join(""); +} + function SvgPreview({ source }: { source: string }) { - const safe = useMemo(() => sanitizeSvgSource(source), [source]); + const srcDoc = useMemo(() => buildSvgSrcDoc(sanitizeSvgSource(source)), [ + source, + ]); return ( -
); }