opyeb SVGForge

Guides · Security

SVG is executable: the script-injection routes you have to close

Updated 28 Nov 2026 · 11 min read

An SVG file is XML, and XML can carry script. When the browser renders an SVG as an image, that script is inert. When the same markup is inlined into an HTML document, or opened as a top-level document, it is part of your origin and runs with your cookies, your storage and your DOM. That difference is the whole security story: the file is not the problem, the context it is placed in is.

The classic payload is a <script> element. It works when the SVG is inlined, and many upload filters miss it because they look only at the file extension. Event-handler attributes such as onload on the root element are just as effective and much easier to hide in a large export. A link with href="javascript:..." executes on click, which is enough if the icon is wrapped in an anchor.

VectorExampleDefence
Script element<script>alert(1)</script>Remove the element and its content
Event handler<svg onload="...">Remove every attribute starting with on
JavaScript URL<a href="javascript:...">Reject javascript: and vbscript: schemes
Embedded HTML<foreignObject>Remove the element with its subtree
Animation to a URL<animate attributeName="href">Reject URL-changing animation targets
Remote reference<image href="https://...">Block external loads with CSP
CSS expressionstyle="width:expression(...)"Remove the style attribute

What a sanitiser can do

A sanitiser parses the file as XML, walks the tree and removes the elements and attributes that can execute. The reliable rule set is small: drop script, foreignObject, iframe, object, embed and handler; drop every attribute whose name begins with on; drop href, xlink:href and src values whose scheme is javascript:, vbscript: or an HTML data URL; drop a style attribute containing expression() or a javascript URL.

Two things are harder than they look. First, namespaces and case: an attacker can use mixed case, character references such as &#106;avascript:, or leading whitespace inside the value, so the check must normalise the string before comparing. Second, nested SVG: an <svg> inside an <svg> is legal, and a rule that only inspects the root misses it. Walk every element, not the first one.

What a sanitiser cannot do

No sanitiser proves safety. A browser can add a new attribute or a new animation target next year, and a rule written today will not know it. The durable controls are deployment controls. Serve user-supplied SVG from a separate origin so that script, if it runs, cannot read your cookies or call your API. Send a Content-Security-Policy that forbids inline script and external connections. Send Content-Disposition: attachment when the file is meant to be downloaded rather than rendered.

For inline SVG that your own build produces, sanitising once at build time and reviewing the removed nodes is enough. For anything uploaded by a user, treat the file as hostile at every step: sanitise on upload, store it outside the web root or in object storage, serve it from the separate origin, and never inline it into a page with your session.

Opening an SVG directly in a browser tab is a real attack surface: the file runs as a document in the site it is served from. If a download link serves .svg from your main domain, a crafted file can act with that domain's origin.

Sources

Questions

Is an SVG inside an img tag safe?

An SVG loaded through img, object or as a CSS background is rendered in a restricted mode where script does not execute and external resources are not fetched. That is the safe way to display untrusted SVG. The danger appears when the markup is inlined into the page or opened directly as a document, because then it runs in your origin.

Why remove foreignObject entirely?

foreignObject embeds HTML inside SVG, and that HTML is parsed by the HTML parser with all of its capabilities, including event handlers and links. Sanitising it correctly means sanitising an HTML subtree with a different rule set. Removing the element is the only rule that stays correct as browsers change.

If the SVG comes from our own designer, do we still need this?

You need a policy, not necessarily a sanitiser for every file. Designers paste assets from many sources, and an export can include a tracking script or a remote font. Sanitise at the point where the file enters the build, keep the original, and review the diff of what was removed.

Load the unsafe sample in the optimizer to watch each script vector being removed and counted.

Open the optimizer

Written against the W3C SVG specifications, MDN and the React and WCAG documentation; every sample in the examples can be pasted into the optimizer linked above.

Back to the guides