The Hidden Danger in Scaled Vector Graphics: How JavaScript in SVG Files Can Be Used for Attacks
SVGs are flexible and scalable, but because they contain JavaScript code, attackers are exploiting them for malware delivery.
By Tim Uhlott|Last updated: August 25, 2025|7 minutes read
cybersecurity

SVGs, or Scalable Vector Graphics, are a type of image format that uses XML to describe two-dimensional graphics. Unlike raster formats like PNG or JPEG, which store images as a grid of pixels, SVGs use vectors, which are mathematical formulas that define shapes and lines, allowing SVGs to be scaled to any size without losing clarity.
Because they're text-based, SVGs can be easily compressed, indexed, and searched, making them highly useful in web development. But this flexibility also introduces serious security concerns. Since SVG files are essentially structured code, they can include JavaScript and interact directly with the web page's DOM (Document Object Model). This means that when a browser displays an SVG, it’s not simply showing a static image; it may also be executing code.
How an SVG file can be used in a phishing attack
1. Creating the Malicious SVG File
The attacker creates an SVG file that looks like a legitimate image (e.g., a logo or button) but contains embedded JavaScript code or links that lead to malicious sites. Some methods used:- Embedding
<script>
tags for JavaScript execution. - Using
<a xlink:href="...">
to create clickable areas. - Using CSS/SMIL animations to obfuscate or auto-redirect.
- Encoding malicious payloads with Base64 or character entities to evade detection.
2. Obfuscation of Code
To bypass security filters, the attacker obfuscates the code inside the SVG file. Common techniques include:- Encoding JavaScript inside
<foreignObject>
or<script>
tags. - Breaking up keywords using character entities (e.g.,
script
for script). - Using inline event handlers like
onload
,onclick
, oronmouseover
.
3. Delivering the SVG File
The attacker sends or hosts the malicious SVG through:- Email attachments (e.g., disguised as an invoice or report).
- Embedding in websites or web apps (especially in user-uploaded content).
- Chat platforms or messaging apps that allow file sharing or inline image previews.
- Social engineering to convince the victim to open the SVG file (e.g., “Click here to view your secure document”).
4. User Interaction and Execution
Once the victim opens the SVG in a browser (or it’s previewed inline in email/web), the malicious code executes:- A fake login page mimicking a real service (Gmail, Outlook, Facebook, etc.).
- The user is prompted to enter credentials, which are then sent to the attacker’s server.
- Alternatively, the SVG can auto-redirect the user to a malicious website using
<script>window.location=...
or similar.
5. Data Harvesting and Exploitation
Captured credentials or sensitive data are sent via:- HTTP POST requests embedded in the SVG.
- JavaScript fetching the data and exfiltrating it to the attacker's endpoint.
- Hijack accounts using stolen credentials.
- Pivot to further attacks (e.g., using the email account for Business Email Compromise).
- Distribute malware if the SVG is just the entry point to a more complex attack chain.
6. Evasion and Persistence
To avoid detection:- The attacker may rotate payload URLs (Command-and-Control domains).
- Use short-lived phishing pages that disappear after collecting credentials.
- Re-host the SVG on different services (like GitHub, Dropbox, or IPFS).
How the attack bypasses security tools
Many systems today still treat image files, especially formats like .svg as inherently safe. One common weakness is the reliance on MIME-type verification alone. Security tools often check the file’s declared MIME type (e.g.,image/svg+xml
) rather than performing a deep inspection of its actual content.
This means that even if an attacker embeds malicious JavaScript inside an SVG, the system may not detect it, since it assumes the file is a harmless image.
In many cases, SVGs are not displayed within sandboxed environments, which would otherwise isolate the content from the rest of the application. As a result, any active content within the SVG, such as embedded scripts, can execute in the context of the page, potentially leading to cross-site scripting (XSS), data leakage, or full compromise of the application.
How to Defend Against SVG-based Attacks
Inspect Emails Carefully
Always scrutinize the content of unexpected or unsolicited emails. Watch for:- A false sense of urgency (e.g., “Act now!” or “Account will be locked!”).
- Inconsistencies in the sender’s address or domain name (e.g.,
@amaz0n-support.com
instead of@amazon.com
).