No. An SVG loaded through <img> cannot run JavaScript. Browsers render SVG images in a restricted mode where scripts don't execute, event handlers don't fire, and external resources aren't fetched. The same file can run scripts when it is embedded with <object>, <embed> or <iframe>, inlined as <svg>, or opened directly by URL.
So <img> is safe for displaying an SVG, but it doesn't make an uploaded file safe. Whoever opens the file's URL gets a live document.
Script execution by embed method
| Embed method | Scripts run? | External resources load? | Risk with untrusted SVG |
|---|---|---|---|
<img src="x.svg"> | No | No | Low |
CSS background-image | No | No | Low |
<object data="x.svg"> | Yes | Yes | High |
<embed src="x.svg"> | Yes | Yes | High |
<iframe src="x.svg"> | Yes (unless sandboxed) | Yes | High |
Inline <svg>…</svg> | Yes, in your page's origin | Yes | Highest |
Direct navigation to the .svg URL | Yes | Yes | High |
The restricted image behavior comes from the W3C's SVG Integration spec ("secure animated" and "secure static" modes). MDN summarizes the image-context limits in its SVG as an image guide. There is a longer walkthrough in our SVG as image guide.
Why <img> vs <object> behaves differently
<img> asks the browser for a picture, so the SVG is rendered once as a sealed image. <object>, <embed> and <iframe> ask for a document, which gets a DOM, script engine and network access, just like an HTML page. Inline SVG goes further and joins your page's DOM, so a malicious onload attribute runs as your site. Inline SVG vs img tag compares the performance and styling trade-offs.
A minimal proof
<svg xmlns="http://www.w3.org/2000/svg" onload="alert(document.domain)">
<circle cx="50" cy="50" r="40"/>
</svg>
<img src="poc.svg">: a circle is drawn. No alert.<object data="poc.svg">or openingpoc.svgdirectly: the alert fires.
How to serve untrusted SVGs safely
- Sanitize on upload with an allowlist sanitizer such as DOMPurify. Remove
<script>,on*attributes,javascript:URLs and<foreignObject>. See the SVG XSS sanitize guide. - Display with
<img>, never<object>or inline, for user content. - Send restrictive headers:
Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox, plusX-Content-Type-Options: nosniff. See SVG CSP headers. - Isolate the origin. Serve uploads from a separate, cookieless domain.
- Force a download for "view original" links with
Content-Disposition: attachment.
The full checklist is in SVG upload security. For individual attack vectors, see event handlers, foreignObject, external references and polyglot files.
Quick answers for end users
Opening an SVG someone emailed you? Treat it like an HTML file. Can SVG files contain malware? and how to open SVG files safely explain the risk. To inspect or clean a file without running it, use the SVG validator and SVG optimizer.
SVG Genie outputs plain vector shapes, with no scripts, event handlers or embedded HTML. Even so, run any third-party SVG through a sanitizer before you inline it.
Start with the full SVG security best practices guide.
Create your own SVG graphics with AI
Describe what you need, get a production-ready vector in seconds. No design skills required.
About This Article
This article was written by SVG Genie Team based on hands-on testing with SVG Genie’s tools and years of experience in vector design and web graphics. All recommendations reflect real-world usage and are reviewed by the SVG Genie editorial team for accuracy.
About the Author
SVG Genie Team
SVG Design Expert & Technical Writer at SVG Genie
SVG Genie Team is a vector design specialist and technical writer at SVG Genie with years of hands-on experience in SVG tooling, AI-assisted design workflows, and web graphics optimization. Their work focuses on making professional vector design accessible to everyone.
More articles by SVG Genie Teamarrow_forward