How to Sanitize an SVG Before Using It
Learn what makes SVG different from ordinary image files, which features deserve attention and how to clean SVG markup before using it in a website or sharing it with another system.
SVG is a useful format because it describes graphics with XML markup rather than storing every pixel as a bitmap. That makes it easy to scale, edit and integrate into web projects. It also means that an SVG can contain more than paths and shapes. Depending on the file, the markup can include scripts, event handlers, links, styles, references and other XML elements. When an SVG comes from an untrusted source, those extra capabilities deserve the same careful treatment you would give other active or structured content.
Why SVG is different from a normal image
A PNG or JPEG is normally treated as raster image data. SVG is text-based XML that a browser can parse as a document. The format therefore has features that can affect how the browser interprets the file. An SVG may contain elements such as <script>, event attributes such as onload, references through href and CSS, or elements designed to embed other content.
This does not mean every SVG is dangerous. Most everyday icons, logos and illustrations contain ordinary drawing elements such as paths, rectangles, circles, gradients and text. The security question is whether content that is unnecessary for the intended graphic is also present.
What an SVG sanitizer does
An SVG sanitizer applies an allowlist or another explicit security policy to the document. Instead of trying to guess whether a particular piece of markup is trustworthy, it removes classes of content that the application does not need. A browser-based sanitizer can parse the XML, inspect every element and attribute, remove disallowed content and serialize the remaining SVG for download.
A practical sanitizer commonly removes script elements, event-handler attributes, dangerous URL schemes and unsupported embedded content. It can also restrict references so that a graphic cannot unexpectedly point to another document or resource.
Common SVG features to review
Script elements
The most obvious example is <script>. A normal static illustration does not need executable JavaScript inside the SVG, so a sanitizer can remove script elements rather than attempting to make them safe.
Event-handler attributes
Attributes such as onload, onclick and similar on... handlers can attach behavior to elements. Removing these attributes is a straightforward way to eliminate a class of executable content.
URLs and references
SVG attributes can point to other resources. A security policy should distinguish safe internal references, such as #gradient1, from schemes or destinations that could load active content. A sanitizer can allow only the reference forms required by the graphic.
Embedded HTML and external content
The <foreignObject> element can introduce HTML-like content into an SVG. Other elements can also reference external files. If the application only needs ordinary vector artwork, removing these features reduces the amount of content that must be trusted.
What should usually be preserved?
Removing everything except <path> is not practical for many real SVG files. Common artwork can rely on groups, shapes, gradients, masks, clipping paths, filters and text. A useful sanitizer therefore keeps a deliberately chosen set of drawing elements and presentation attributes while removing features that are not needed.
There is always a trade-off. The broader the allowlist, the more SVG features you preserve, but the more carefully you must define their allowed attributes and references. The narrower the allowlist, the simpler the security policy, but the more likely you are to change legitimate artwork.
How to sanitize an SVG before uploading it
- Keep the original SVG as a backup.
- Open the SVG in a sanitizer that processes the markup locally when possible.
- Review the sanitization report and the resulting SVG.
- Open the cleaned file in your normal graphics or browser workflow.
- Check important visual features such as gradients, masks, text and filters.
- Use the sanitized copy when the SVG will be handled by a system that receives content from users or other untrusted sources.
Sanitizing is not the same as removing metadata
Security sanitization and metadata cleanup have different goals. A sanitizer focuses on content that can affect parsing, execution or external references. Metadata cleanup focuses on information such as authoring details or document properties. An SVG can be sanitized without removing every piece of descriptive metadata, and a metadata remover does not automatically make every SVG feature safe.
Why local processing can be useful
SVG files can contain proprietary artwork, design work or internal project information. A browser-only workflow lets you inspect and transform the markup without sending the source to a remote processing server. That does not make the output automatically safe in every context, but it can reduce unnecessary data transfer during the sanitization step.
What sanitization cannot guarantee
No generic browser tool should be described as a universal security guarantee. The final risk depends on what happens after sanitization. A website that accepts SVG uploads should still use appropriate response headers, content security policies, storage isolation and safe embedding practices. If an application has a very specific SVG use case, its sanitizer policy should be designed around the exact features that application needs.
Practical checklist before using an SVG
- Remove executable scripts and event-handler attributes.
- Reject dangerous URL schemes and unnecessary external references.
- Remove embedded HTML or other unsupported content when it is not required.
- Keep a backup of the original file before sanitization.
- Test the cleaned SVG visually after processing.
- Apply website-level security controls in addition to sanitization.
For a quick browser-based workflow, NeroTool's SVG Sanitizer can parse an SVG locally, remove disallowed elements and attributes, show a compact sanitization report and let you download the cleaned markup.