Skip to content
Tools Without AI

Prefer toolswithoutai.com in Google Search so our tools and guides show up more often. Google has to confirm it — we cannot set this for you.

SVG sanitizer

Sanitize SVG code, remove scripts and bloat locally. 100% private, no-upload processing with zero server latency.

No uploads. No account. Free to use.

Removes script nodes, event handlers, javascript: URLs, and optionally foreignObject. Artwork never leaves the device.

Drop an SVG

Nothing is uploaded. Processing stays on this device.

Prefer toolswithoutai.com

Opens Google’s picker with toolswithoutai.com pre-filled.

Under the Hood: The Architecture of Vector SVG Code Sanitizer & Bloat Stripper

Cloud SVG optimizers take proprietary artwork and often execute or store the XML. That is the wrong trust boundary. Sanitizing vector graphics is XSS prevention: SVGs are XML documents that browsers may execute when inlined. Attackers use <script>, onload=, xlink:href=javascript:…, and HTML inside foreignObject. Security write-ups keep showing that deleting only <script> tags is insufficient. This sanitizer is a local DOM rewrite, not a “prettier” that uploads to a compress API.

Optimize SVG code is a secondary pass: comments, editor metadata, and extra whitespace. That pass is optional so you can keep <title> for accessibility. DOMParser + XMLSerializer + FileReader/Blob are the native APIs. No canvas rasterization—output remains vector.

Memory Allocation & Browser Threading Mechanics

Parsing allocates a document tree proportional to node count. DFS walk is stack-recursive over children; icon SVGs are shallow. Attributes are copied with spread ([...node.attributes]) because live NamedNodeMap mutates while you delete on* names. Serialization emits one string. Main thread is correct; a Worker cannot use DOMParser in all browsers the same way without a document. For multi-megabyte map SVGs, expect a brief parse hitch rather than a network round-trip.

Algorithmic Deep-Dive: Code Snippet & Logical Analysis

function sanitizeSvg(source, stripForeign, stripBloat) {
  const doc = new DOMParser().parseFromString(source, "image/svg+xml");
  if (doc.querySelector("parsererror")) throw new Error("Could not parse SVG.");
  doc.querySelectorAll("script").forEach((n) => n.remove());
  if (stripForeign) doc.querySelectorAll("foreignObject").forEach((n) => n.remove());
  const walk = (node) => {
    for (const attr of [...node.attributes]) {
      if (/^on/i.test(attr.name)) node.removeAttribute(attr.name);
      if (/href$/i.test(attr.name) && /^\s*(javascript|data):/i.test(attr.value)) {
        node.removeAttribute(attr.name);
      }
    }
    if (stripBloat && (node.localName === "metadata" || node.localName === "title")) node.remove();
    [...node.children].forEach(walk);
  };
  walk(doc.documentElement);
  let xml = new XMLSerializer().serializeToString(doc);
  if (stripBloat) xml = xml.replace(/<!--[\s\S]*?-->/g, "");
  return xml;
}

parsererror is how Gecko/WebKit surface XML failures; throwing beats silently serializing an error document as if it were art. data: hrefs are removed because they can embed HTML or SVG-in-SVG. External http(s) hrefs on <use> are a remaining policy choice—this pass focuses on executable schemes. Always re-review SVGs that reference remote sprites.

The Local Storage Sandbox & Volatile Data Pipelines

Output is a Blob image/svg+xml downloaded via object URL, then revoked. The parsed Document becomes unreachable after the function returns. Input textarea state is the only persistence, and it is RAM-only.

Verifying True Privacy: How to Audit Your Browser's Network Tab

Clear Network, paste an SVG containing <script>alert(1)</script>, sanitize, download. The markup must not appear in any POST. If the SVG referenced an external https:// image, a later preview might fetch it—this sanitizer’s serialize step does not. Audit both Network and the output source.

  • Files never leave your device
  • Runs entirely in your browser
  • No signup required
  • Free

What is SVG sanitizer?

An SVG sanitizer that uses the DOM to strip XSS vectors (script, event handlers, foreignObject, external resources) and optional editor bloat.

Key features

  • Strip script and event handlers
  • Optional foreignObject removal
  • Bloat / metadata stripping
  • No upload of artwork

How to use it

  1. Paste SVG markup or drop an .svg file.
  2. Scripts, event handlers, javascript: URLs, and optional foreignObject nodes are stripped.
  3. Download the sanitized SVG. DOMParser and XMLSerializer run locally.

When to use it

SVGs can carry script and foreignObject payloads. Removing only script tags is not enough; this tool applies explicit rules locally.

Frequently asked questions

Are my files uploaded?

No. This tool runs in your browser. Files and text stay on your device and are not sent to our servers.

Does this use AI?

No. It is a deterministic utility. There is no model and no third-party processing of your data.

How do I sanitize an SVG before using it on my website?

Run it through this sanitizer so executable markup is removed before you inline or host the graphic.

How do I remove JavaScript and event handlers from an SVG?

Every script node and on* attribute is dropped, including xlink and href javascript: values.

How can I reduce SVG file size without uploading proprietary artwork?

Optional bloat stripping removes comments, unused namespaces, and metadata locally.

Related tools

More in Image tools.