SVG and File Upload XSS

XSS where the injected file is the script: an uploaded SVG or HTML document served same-origin with a renderable content type executes its embedded JavaScript when a victim opens it — a common Stored XSS sink that input-field sanitizers never see.

How it works

An SVG is not an image, it is executable XML: it can carry <script>, on* handlers, javascript: links, <foreignObject> HTML, external CSS references, a DOCTYPE (XXE) and <use> expansion. A browser that renders an uploaded SVG same-origin runs all of it in the app’s origin. The same applies to any uploaded file the server lets the browser render as HTML. Two conditions make it exploitable:

  1. The file is served from the application’s own origin (not a sandboxed/separate domain), and
  2. It is served inline with a renderable Content-Type (image/svg+xml, text/html) and no Content-Disposition: attachment / X-Content-Type-Options: nosniff.

Impact is typically token/cookie theft → Account Takeover, since the SVG runs as the origin and can read document.cookie/localStorage and call authenticated APIs.

Techniques / payloads

  • Minimal scriptable SVG: <svg xmlns="http://www.w3.org/2000/svg" onload="alert(document.domain)"/> or an embedded <script>alert(document.cookie)</script>.
  • Rendered via <object>/<embed>/direct navigation: an SVG shown with <object data=…> executes its script (the $750 HackerOne pattern reproduced in the bblabs lab).
  • MIME allowlist bypass: set Content-Type: image/png on a multipart upload of xss.html; servers that trust the declared type (not the bytes) store it, then serve it as HTML (Netlify CDN upload → used in a CSP chain, see Content Security Policy).
  • SVG sanitizer bypasses:
    • Namespace-prefixed elements/attributes dodge blocklists: x:script, x:onload (Penpot CVE-2026-133704, bypassing sanitize-svg-element).
    • svg-hush (Cloudflare /cdn-cgi/image/): the XML writer serialized namespace-declaration URIs without escaping, smuggling markup past a Rust sanitizer; chained with a Chromium cache/CSP-stripping gadget.
    • SVG animation/MathML attributes bypass Angular’s sanitizer schema (CVE-2025-66412).
  • Inline-served uploads generally: Atomic Server served inline HTML uploads without Content-Disposition: attachment, nosniff, or CSP → stored XSS from any uploaded file.
  • Forgotten SVG surfaces beyond <script>: external references in CSS, XXE via DOCTYPE, and <use> expansion bombs.

Notable cases

  • Penpot 2.18 (CVE-2026-133704) — namespace-prefixed SVG elements bypass the element blocklist (authenticated editor).
  • Cloudflare /cdn-cgi/image/ — svg-hush namespace serialization bug + Chromium gadget → XSS on a CDN used by many sites.
  • Atomic Server — inline-served uploads missing all three header defenses.
  • $~700 (SAR 2,629) stored XSS via SVG avatar → ATO — same-origin SVG, missing CSP, exfiltrated localStorage auth tokens; the canonical avatar-upload ATO.
  • SnapOtter < 2.3.0 (GHSA-9c7h-xjrp-8gh3): upload routes sanitized only when isValidImage && isSvgBuffer(buffer). A DOCTYPE declaring an external entity made librsvg (via Sharp) fail to decode, so the && short-circuited past sanitizeSvg() and the raw SVG was stored. The sanitizer also missed namespace-prefixed elements, and its DOCTYPE regex broke on ] inside quoted entity values. In the shipped app, Content-Disposition: attachment, nosniff and CSP limited the impact; the reported XXE and SSRF did not reproduce.

Defenses

  • Serve all user uploads from a separate, cookieless origin (sandbox domain), or force download: Content-Disposition: attachment + X-Content-Type-Options: nosniff.
  • For SVG that must render: sanitize with DOMPurify (mind Mutation XSS), strip scripts/handlers/external refs, disable DOCTYPE/external entities; or rasterize to PNG.
  • Validate by content, not the client-supplied Content-Type; set a restrictive Content Security Policy on upload-serving paths.
  • Never make sanitization conditional on a validator or decoder succeeding; sanitize every SVG-bearing upload (the SnapOtter fix).

Sources