Why SVGs Are Fundamentally Code, Not Pixels
Unlike raster formats (JPEG, PNG, WebP) which store compressed matrices of color values, Scalable Vector Graphics (SVG) are full XML documents standardized by the W3C. An SVG uses mathematical coordinate markup (<path>, <rect>, <circle>) to describe shapes and styling.
Crucially, the SVG standard supports native scripting and event handlers: script tags, inline event attributes (onload, onerror, onclick), and hyperlink references (xlink:href with javascript pseudo-protocols).
When a web application allows users to upload an SVG file (such as a company logo or profile avatar) and serves it inline with a Content-Type: image/svg+xml header, the victim’s browser executes the embedded JavaScript in the security context of the application domain, resulting in stored Cross-Site Scripting (XSS).
SVGs are XML documents that can execute native JavaScript. Serving raw user-uploaded SVGs inline frequently leads to Stored XSS.
Common SVG Injection Vectors and Obfuscation
Adversaries utilize diverse XML execution payloads:
1. Direct Script Tags: embedding active script blocks inside the SVG XML document tree.
2. Event Attributes: using onload or onerror attributes inside elements to trigger remote telemetry calls.
3. XML External Entities (XXE): Embedding DOCTYPE declarations with external entity references to read local server files during backend XML parsing.
Auditing and Sanitizing SVGs with OverlayCheck
OverlayCheck parses SVG XML trees in a sandboxed iframe, detecting scripts, dangerous event attributes, XXE declarations, and foreign objects, providing a sanitized, safe SVG download.
SVG Security Defenses: OverlayCheck vs. Naive Regex Filters
Assessing protection against obfuscated SVG XSS and XXE payloads.
| Feature / Dimension | OverlayCheck (Client-Side) | Naive Regex Filtering (Grep for script tags) | DOMPurify XML Sanitizer |
|---|---|---|---|
| Bypass Resistance (Obfuscated Payloads) | Full AST XML tree inspection | Fails (Bypassed via event handlers or casing) | High (Thorough DOM sanitization) |
Command-Line Automation & CLI Workflows
For security engineers and forensic examiners automating pipelines or conducting terminal audits, the equivalent low-level commands can be executed natively:
grep -iE "(<script|onload|onerror|javascript:|<!ENTITY)" vector.svgFrequently Asked Technical Questions
Q1:Does loading an SVG inside an <img> tag execute JavaScript?
No. Browsers intentionally disable JavaScript execution inside standard <img src="vector.svg"> tags. However, if the user opens the SVG URL directly or it is embedded via <object>, <iframe>, or inline <svg>, scripts execute immediately.