OnChainScans
SVG Stored Cross-Site Scripting (XSS)•W3C SVG 1.1 / OWASP XSS Prevention Cheat Sheet• LIVE SCANNER EMBEDDED

How to Detect & Sanitize Stored XSS Script Injections in SVG Files

Audit vector graphics for malicious JavaScript execution triggers before uploading to CDNs.

Live Diagnostic Tool: SVG Security & Bloat ScannerFull Tool Page
Executive Answer & Impact Analysis

SVG Stored XSS occurs because Scalable Vector Graphics are XML documents capable of executing arbitrary JavaScript via '<script>' elements, '<foreignObject>', or DOM event handlers (onload, onerror). When an SVG is served directly with 'image/svg+xml', the script executes in the user's browser session.

Root Cause Analysis

Web applications treat SVG files as regular image uploads (like PNG or JPEG) and serve them from primary domains without sanitization, giving attackers access to cookies and authenticated sessions.

Step-by-Step Resolution Procedure

1Scan SVG Vector XML

Upload or paste the SVG into the SVG Security Scanner above to detect script tags, event listeners, and external references.

2Serve SVGs with Safe Content-Disposition

Serve uploaded user SVGs with 'Content-Disposition: attachment' or sandbox them on an isolated CDN domain.

3Sanitize XML with DOMPurify

Run SVGs through DOMPurify with SVG profile enabled before storing or rendering in DOM.

Verification Command or Config Pattern

TERMINAL / CONFIGRFC W3C SVG 1.1 / OWASP XSS Prevention Cheat Sheet
import DOMPurify from "dompurify";

// Strip scripts, foreignObject, and inline event handlers from SVG
const cleanSvg = DOMPurify.sanitize(rawSvgString, {
  USE_PROFILES: { svg: true, svgFilters: true },
  ADD_TAGS: ["use"],
});

Frequently Asked Remediation Questions

Why can SVGs execute JavaScript?

The W3C SVG specification includes full scriptability via JavaScript to support interactive diagrams and animations.

Does an <img> tag execute SVG scripts?

When embedded as '<img src="image.svg">', browsers disable script execution. However, navigating directly to the SVG URL or embedding via '<object>' or '<iframe>' executes scripts fully.

What is foreignObject in SVG?

'<foreignObject>' allows embedding raw HTML elements inside SVG, creating an additional injection vector for malicious payloads.

Does OnChainScans clean SVG bloat?

Yes. The SVG scanner provides a 1-click cleaner that removes Adobe Illustrator/Inkscape metadata bloat and strips scripts.