FlawOpen

flawopen.com/XSS/SVG upload

Is SVG upload an XSS risk?

Reference page
Short answer

An uploaded SVG is a document, not merely pixels: it can contain active elements such as scripts and event handlers. Whether script runs depends on how the browser receives it — SVG used through <img> is placed in a restricted image context, while inline SVG, direct navigation, or document embedding can be active.

What makes SVG different from other image formats

Unlike PNG or JPEG, SVG is XML and can carry active content. Browsers disable scripting in restricted image contexts such as <img>, but that restriction does not apply when SVG is viewed directly or embedded as a document through <object>, <iframe>, or <embed>, or when markup is inserted inline. A user-uploaded “profile picture” can therefore become stored XSS if the application later serves it in an active context under the application origin. Browser behavior depends on context; do not treat an extension or image MIME type as sanitization.

Doing it safely

FAQ

Does validating the file extension or MIME type solve this?

No. An extension and declared MIME type describe metadata, not the document’s active content. Verify the actual file format, then sanitize with an SVG-aware allowlist or convert it to a raster image if SVG features are unnecessary.

References

See also Cross-Site Scripting (XSS, CWE-79): How It Works and How to Prevent It in Every Language — flawopen.com Vue v-html XSS Risk and Safe Rendering Is WKWebView loadHTMLString Safe for Untrusted Content? Reflected vs. Stored vs. DOM-Based XSS Android addJavascriptInterface Security Risks Angular XSS Protection: Safe Defaults and Bypass APIs Content-Type Sniffing XSS: MIME Types and nosniff