Chapter 3
SVG, the trojan image
Stored cross-site scripting and server-side XML external entity injection.
To a tired developer, an .svg looks like just another harmless image format next to .png and .jpg. Under the hood, an SVG is an XML document that browsers parse as live code and server libraries parse with full XML engines. Allowing SVG uploads opens up both stored XSS in the browser and XXE or SSRF on the backend.
Client-side stored script execution
Section titled “Client-side stored script execution”Whenever an app serves an uploaded SVG directly in the browser on its main domain, any embedded JavaScript runs inside the victim’s session.
<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" onload="alert(document.domain)"> <circle r="50" fill="red"/></svg>Upload that snippet as a profile avatar, then copy its direct URL. When an admin or another user opens that link directly, your script runs inside the app origin. From there it can call authenticated APIs or hijack the account.
How the HTML tag changes execution
Section titled “How the HTML tag changes execution”Whether your script fires depends on how the browser loads the file:
- Inside an
<img>tag, modern browsers block SVG scripts completely. - On a direct URL visit, the browser renders the SVG as a full document and runs your JavaScript.
- Inside
<object>,<embed>, or<iframe>, embedded scripts run just as they do on a direct visit.
If the app serves uploads from its main domain without Content-Disposition: attachment or a strict Content Security Policy, stored XSS is on the table.
Server-side XML external entity injection
Section titled “Server-side XML external entity injection”Because every SVG is valid XML, it can declare external entities. Older server-side thumbnailers and PDF converters still resolve those entities by default, turning an image upload into local file read or SSRF.
<?xml version="1.0" standalone="yes"?><!DOCTYPE test [ <!ENTITY xxe SYSTEM "file:///etc/passwd" > ]><svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <text x="20" y="40" font-size="16">&xxe;</text></svg>When a server thumbnailer processes this file, it reads /etc/passwd.
The resulting image preview can render the system file contents directly into the image canvas.
This vector also enables server-side request forgery against cloud metadata endpoints.
Systematic SVG audit checklist
Section titled “Systematic SVG audit checklist”Test these conditions on every application that accepts SVG images.
- Upload a raw SVG file containing a simple
onloadalert payload. - Verify whether the server sets
Content-Disposition: attachmenton download responses. - Check whether the file is served from the main application origin or a separate sandbox domain.
- Inspect the application source to determine whether templates use
<img>,<object>, or inline SVG. - Send an SVG file containing external entities to test server thumbnail generators for file disclosures.
Next, Chapter 4 covers the parser libraries that process SVG and other formats after upload.
Open local graph around this page
Linked from here
Links out to
No outgoing internal links on this page.
File UploadFor authorized testing only.