Guide

Check an SVG file: scripts, external resources and a safe view

An SVG looks like an image but is XML text that can carry scripts, references and hidden content. Inspect the source before you open the file in a browser. ScanBeforeOpen reads it locally and does not render the graphic.

Responsible operator: Tom Silas Helmke, operator of ScanBeforeOpen. Content revised: .

Open scanner

Why an SVG is more than an image

SVG is an XML text format. Besides lines and colors it may contain script elements, event attributes such as onload, references to foreign addresses, embedded styles and even embedded HTML. Whether any of it takes effect depends on the context in which the file is opened.

If the file is opened directly in a browser as a document, or inserted as inline SVG into a web page, script code can run. If it is loaded through an img element or as a background image, the browser disables scripts and blocks external resources (see MDN on SVG as an image). The same file is harmless or risky depending on how it is embedded.

How the same SVG behaves depending on embedding
SVG fileXML text with drawing commandsOpen directly in the browserscripts can runLoad as an img elementscripts and loading blockedScanBeforeOpenreads source, draws nothingFindingpatterns, comments, hidden text

The sequence in words

  1. An SVG is text in XML format. Besides lines and colors it may contain script elements, event attributes and references.
  2. If you open the file directly or embed it inline in a page, the browser treats it like a document. Scripts can run.
  3. If you load it through an img element, the browser disables scripts and blocks external resources (MDN: SVG as an image).
  4. ScanBeforeOpen reads only the source as text and searches for patterns. The graphic is not drawn, so no file code runs.
  5. Findings name script and event patterns, external references and hidden text. Patterns only inside comments count as a low-level hint.

Analysis, preview and execution are different things

Analysis reads the source as a string and searches for known patterns, such as script, iframe, object, embed, event attributes, meta refresh, DTD and ENTITY declarations, external addresses in href, src or CSS url, and styles that hide content. Nothing is executed.

The preview in ScanBeforeOpen shows the source as escaped text. The graphic is deliberately not drawn, so neither scripts nor reloading are possible. Control characters are made visible. Execution would begin only when the original is opened in a browser or graphics program, which is exactly what should be avoided beforehand.

The export for this file type is a limited text excerpt, not a working SVG. It is for reading and passing on the source text, not a cleaned image.

Typical patterns and what they mean

A script element or an event attribute is the clearest warning sign, because either can start code in an ordinary browser window. An external address in href or src may be an ordinary link, but it may also be a request that reveals the file was opened.

Hidden text (display:none, opacity 0, font size 0) is invisible to people but readable by programs. That matters when the text is later handed to an AI system. Declarations such as DOCTYPE or ENTITY rarely belong in a simple graphic.

Pattern hits are not proof. Patterns that appear only inside XML comments are reported separately as a low-level hint, because comments are not executed. A hit in sample text or an attribute value is treated like real code, and deliberately obfuscated code can slip past the pattern.

How to handle an unknown SVG

Save the file without clicking it and confirm where it came from. Then choose it in the scanner and read the recommendation and inspection coverage before looking at single hits. With script or event hits, do not open the file in a browser.

If you still need the graphic, load it only through an img element on your own trusted page or render it with a program that does not run scripts. Anyone who accepts SVG uploads should sanitize them on the server with a vetted library such as DOMPurify, set a Content Security Policy and serve user files with Content-Disposition attachment. No single step is enough.

Reproducible synthetic example

The SVG names script and onload= only as text inside a comment. That yields the low-level hint markup.comment, not an executable threat. The text hidden with display:none produces markup.hidden and is a genuine, harmless hint.

The findings come from an actual local scan of this test file. Download it and choose it in the scanner. No real personal data or malware.

Download test file: markup-demo.svg

Example inspection coverage: complete.

Practical checklist

  • Confirm the origin of the SVG independently
  • Do not double-click it or open it in a browser
  • Read the recommendation and coverage first, then the hits
  • Cross-check script, event and external references in the source
  • Display it only through an img element or a program that runs no scripts
  • Do not confuse the text excerpt with a cleaned SVG

Clear limits

The check is a pattern search in the source, not an XML analysis with resolved entities, styles or references. Obfuscated code may go unnoticed. Neither a green result nor the text excerpt is a safety clearance.

FAQ

Can an SVG file really run malicious code?

Yes. A directly opened or inline-embedded SVG can run scripts in a browser. When loaded through an img element, the browser disables scripts.

Does ScanBeforeOpen render the SVG for viewing?

No. It shows the source as escaped text and does not draw the graphic. That prevents file logic from running, but you also do not see what the graphic looks like.

Does a finding mean the file is malicious?

No. The scanner finds text patterns. Patterns only inside comments are reported as a low-level hint, while other hits may concern harmless text. A missing hit does not prove harmlessness either.

Related reading

Technical sources