Skip to content

Chapter 3

SVG, the trojan image

Stored cross-site scripting and server-side XML external entity injection.

2 min readFile Upload

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.

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.

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.

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.

Test these conditions on every application that accepts SVG images.

  1. Upload a raw SVG file containing a simple onload alert payload.
  2. Verify whether the server sets Content-Disposition: attachment on download responses.
  3. Check whether the file is served from the main application origin or a separate sandbox domain.
  4. Inspect the application source to determine whether templates use <img>, <object>, or inline SVG.
  5. 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.

Connections3 backlinks · 0 outgoing · 2 cross-guideFull graph →
Open local graph around this page
Open full graph →
Click a node to open · Drag to reposition · Scroll to zoom

File UploadFor authorized testing only.