Why WordPress blocks SVG uploads, and how to allow them safely
SVG logos are sharp and tiny, but an SVG file can carry JavaScript. Why WordPress refuses them by default and what a safe SVG upload actually has to do.
Designers love SVG. A logo or icon stays crisp at any size and often weighs a few kilobytes. So the first time someone tries to upload one to WordPress and sees "Sorry, you are not allowed to upload this file type", the natural reaction is to find a snippet online that switches the check off.
That snippet is how a lot of sites end up with a stored cross-site scripting hole.
Why WordPress says no
An SVG is not a picture in the way a JPEG is. It is an XML document that describes shapes, and the SVG format allows it to contain scripts, event handlers such as onload, links and references to external resources. MDN documents the SVG <script> element alongside the rest of the format.
When an SVG is shown with an <img> tag, browsers do not run its scripts. But uploaded files can also be opened directly, at their own address on your domain. Opened that way, a malicious SVG runs its script as part of your site, with access to whatever a logged-in administrator's browser can do there. That is stored cross-site scripting, one of the most common vulnerability classes OWASP describes.
WordPress core has discussed allowing SVG for over a decade; the long-running ticket is still open because there is no safe way to do it without cleaning every file. So core leaves SVG out of the allowed file types.
What "safe SVG" has to mean
Allowing the file type is the easy part. A safe setup must do all of the following:
- Sanitise every upload on the server. Parse the XML and keep only an allow list of shape and style elements and attributes. Remove
<script>, everyon*event attribute,javascript:links,<foreignObject>and references to external files. Blocking a list of bad things is not enough; attackers find new ones. Keep a list of good things instead. - Reject what cannot be parsed. A file that is not valid XML should not be "repaired" and accepted.
- Limit who can upload. Administrators and editors, typically. Not every contributor or customer account.
- Check files that already exist. If the snippet was in place before, older uploads were never cleaned.
Alternatives worth considering
- Inline SVG in the theme, written or reviewed by a developer, for logos and icons that rarely change.
- PNG or WebP for anything uploaded by many people. A 2x PNG logo is usually still small.
- Icon fonts or sprite sheets for large icon sets, though inline SVG has largely replaced them.
Our recommendation
If people on your site need to upload SVG, use a solution that sanitises on upload with an allow list and restricts the roles. If nobody needs it, keep WordPress's default. Never use the one-line snippet that only changes the allowed file types.
In Ironhelm
Safe SVG uploads lets the roles you choose upload SVG logos and icons, and cleans each file of anything that could run code before it is stored. By default only administrators and editors can upload them.
Sources
- MDN Web Docs, SVG
<script>element - OWASP, Cross Site Scripting (XSS)
- OWASP, Cross Site Scripting Prevention Cheat Sheet
- WordPress Trac, #24251: Reconsider SVG inclusion to get_allowed_mime_types
Ironhelm Suite is one WordPress plugin for security, speed, SEO and analytics. See plans or tour the features.