Letting users upload SVG files? Stop them carrying scripts
Short answer: An SVG file looks like an image, but it's actually a small XML document that can contain scripts, links and event handlers. If people can upload SVGs to your site and anyone can open the uploaded file directly in a browser on your domain, a malicious one can run JavaScript as if it were part of your site. Shown through an <img> tag, browsers disable scripts in SVG, but a direct link removes that protection. Allow SVG only if you need it, then sanitise it, serve uploads from a separate domain with restrictive headers, or convert them to PNG.
Why SVG is different from JPG and PNG
JPG and PNG files are just pixels. SVG is code that describes shapes, and the format includes a <script> element; MDN describes it as equivalent to HTML's script element. SVG can also contain links, embedded HTML through foreignObject, and attributes such as onload that run code when the file is displayed.
That's harmless when you made the file yourself. It's a different matter when the file comes from a customer, a supplier or an anonymous visitor.
When the script runs and when it doesn't
MDN's guide to SVG used as an image explains that browsers apply restrictions when SVG is used in an image context: JavaScript is disabled and external resources can't be loaded. Image contexts include <img>, CSS backgrounds and the canvas drawImage function.
The same guide is clear that those restrictions don't apply when the SVG is viewed directly, or embedded as a document with <iframe>, <object> or <embed>. So the risky moments are:
- someone opens the uploaded file's URL in a new tab,
- your site links to the file, for example "View original",
- your CMS or theme embeds uploads with
<object>or inlines the SVG markup into the page, - an admin previews the file in a way that loads it as a document.
If the file is served from your main domain, a script running there can read pages, use the visitor's session and act on their behalf.
Option 1: don't accept SVG
The simplest route for many businesses is to accept only PNG, JPG and WebP for user uploads. If customers send vector artwork for printing or signage, accept it as a file for your team to download, not as something displayed on your website. This fits the general upload advice in OWASP's File Upload Cheat Sheet: allow only the types you need and validate what you receive.
Option 2: convert to a raster image
If you need people to upload logos as SVG for convenience, convert them on the server into PNG at a few sizes and display only the PNGs. Keep the original SVG, if you need it, in private storage that's never served to browsers. Converting removes scripts entirely because the result is just pixels. Most image libraries can do this, and you can generate the sizes your pages need at the same time.
Option 3: sanitise SVGs you must display
If you need to display uploaded SVGs as vectors, run them through a well-maintained SVG sanitiser on upload. A sanitiser removes <script> elements, event-handler attributes such as onclick, javascript: links, embedded foreignObject content and external references. Points to keep in mind:
- Sanitise on the server, not only in the browser.
- Keep the library updated, as OWASP recommends for any library handling uploads.
- Re-sanitise existing uploads when you adopt or update the sanitiser.
- Still display the result through
<img>, not by pasting the SVG markup into your page.
Serve uploads safely
Whatever you accept, how files are served matters as much as what you accept:
- Use a separate domain for uploads. Serving user files from a different domain to your main site means a script that does run can't touch your site's cookies or pages.
- Send
Content-Disposition: attachmentfor originals. MDN describes this as telling the browser to download the file rather than display it. Use it for any "download original" link. - Add a CSP sandbox header to uploaded files. MDN describes the
sandboxdirective as applying restrictions similar to a sandboxed iframe, including preventing script execution. It must be sent as an HTTP header; MDN notes it isn't supported in a<meta>tag. - Send the correct content type and
nosniff. Don't let the server guess types from file names. - Rename files on upload. OWASP recommends generating your own file names rather than keeping the uploaded ones.
Check your existing site
If you've allowed SVG uploads for a while, look for:
- where uploaded files are stored and which domain serves them,
- whether any page links directly to uploaded originals,
- whether your CMS inlines or embeds uploaded SVGs,
- existing SVG files containing
<script>,onattributes orjavascript:links.
A quick search through stored SVGs for those patterns shows whether you have a problem today.
An illustrative example
A print and signage firm in Wellington lets customers upload logos through an order portal, and staff preview them before printing. The portal stores uploads on the main domain and shows a "View full size" link to the original file. The developer moves uploads to a separate storage domain, converts every SVG to PNG previews for the portal, sends originals with Content-Disposition: attachment and a CSP sandbox header, and limits original downloads to signed-in staff. Customers can still upload vector artwork for printing.
FAQ
Is an SVG logo on my own site risky?
Not if you or your designer made it. The risk comes from files uploaded by other people.
Does a CDN or image service fix this?
Only if it converts SVGs to raster images or serves them with safe headers. Check how it handles SVG.
Can I just check the file extension?
No. Extensions are easy to fake. Check content and apply the controls above.
What about SVGs in emails or PDFs we generate?
Convert user-supplied SVGs to raster images before including them anywhere they might be rendered as a document.
If your site accepts uploaded logos or artwork and you want the SVG handling checked, tell us how uploads work today.
Sources
- https://developer.mozilla.org/en-US/docs/Web/SVG/Guides/SVG_as_an_image
- https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/script
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/sandbox
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Disposition
- https://cheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html