Cross-Site Scripting (XSS)
A web vulnerability where attackers inject malicious scripts into web applications, enabling theft of user data or unauthorized actions.
Cross-Site Scripting (XSS) is one of the most prevalent and dangerous web security vulnerabilities, ranked in the OWASP Top 10. In the context of Web3 and DeFi applications, XSS attacks are particularly devastating—they can manipulate transaction data before signing, steal wallet credentials, or trick users into approving malicious transactions. While smart contract audits focus on on-chain code, XSS vulnerabilities in dApp frontends can completely bypass secure contracts.
How XSS Works
XSS occurs when an application includes untrusted data in a web page without proper validation or escaping. The browser executes the injected script with full access to the page context:
1// Vulnerable code: directly inserting user input into HTML2const username = getUserInput(); // User enters: <script>stealWallet()</script>3document.getElementById('welcome').innerHTML = `Welcome, ${username}`;4// Result: The malicious script executes!
Types of XSS
Reflected XSS
The malicious script comes from the current HTTP request:
1https://dapp.example.com/search?q=<script>stealCookies()</script>
The server includes the search query in the response without sanitization, and the script executes when the victim clicks the crafted link.
Stored XSS
The malicious script is permanently stored on the target server:
1// Attacker submits profile bio containing:2// <img src=x onerror="sendWalletToAttacker()">34// When other users view the profile, the script executes
More dangerous than reflected XSS because it affects all users who view the stored content.
DOM-Based XSS
The vulnerability exists entirely in client-side code:
1// Vulnerable: Using URL fragment directly2const tokenAddress = window.location.hash.substring(1);3document.write(`Loading token: ${tokenAddress}`);45// Attacker crafts: https://dapp.com/#<script>drainWallet()</script>
XSS in Web3: Why It's Critical
In traditional web apps, XSS might steal session cookies. In Web3 applications, the stakes are higher:
Transaction manipulation: Scripts can modify transaction parameters before signing:
1// Attacker's injected script2const originalSend = wallet.sendTransaction;3wallet.sendTransaction = async (tx) => {4 tx.to = "0xATTACKER_ADDRESS"; // Redirect funds5 return originalSend(tx);6};
Approval hijacking: Trick users into signing unlimited token approvals:
1// Injected script requests MAX approval to attacker's contract2await token.approve(attackerContract, ethers.MaxUint256);
Wallet draining: Execute transactions directly using connected wallets:
1// If wallet is connected, attacker can initiate transfers2const signer = await provider.getSigner();3await signer.sendTransaction({4 to: attackerAddress,5 value: ethers.parseEther("10")6});
Real-World Web3 XSS Examples
NFT metadata attacks: Malicious scripts embedded in NFT metadata that execute when viewed on marketplaces.
DeFi dashboard exploits: XSS in portfolio trackers that could manipulate displayed balances or inject malicious transaction requests.
Bridge interface attacks: Scripts modifying destination chains or addresses in cross-chain bridge UIs.
Prevention Techniques
Output Encoding
Encode data based on context:
1// HTML context: escape HTML entities2function escapeHtml(str) {3 return str.replace(/[&<>"']/g, char => ({4 '&': '&',5 '<': '<',6 '>': '>',7 '"': '"',8 "'": '''9 }[char]));10}1112document.getElementById('welcome').innerHTML =13 `Welcome, ${escapeHtml(username)}`;
Content Security Policy (CSP)
HTTP headers that restrict script sources:
1Content-Security-Policy: default-src 'self'; script-src 'self' 'sha256-abc123...'
This prevents inline scripts and restricts external script sources.
Input Validation
Validate and sanitize all user inputs:
1import DOMPurify from 'dompurify';23// Sanitize HTML content4const cleanHtml = DOMPurify.sanitize(userInput);56// Validate addresses7function isValidAddress(addr: string): boolean {8 return /^0x[a-fA-F0-9]{40}$/.test(addr);9}
Use Framework Protections
Modern frameworks provide automatic escaping:
1// React automatically escapes values in JSX2function Welcome({ username }) {3 return <div>Welcome, {username}</div>; // Safe!4}56// DANGER: dangerouslySetInnerHTML bypasses protection7<div dangerouslySetInnerHTML={{ __html: userInput }} /> // Vulnerable!
XSS Testing in Audits
TypeScript audits should include:
- Identify injection points: All places where user input is rendered
- Test encoding: Verify proper context-aware encoding
- Check CSP: Review Content Security Policy headers
- Test DOM manipulation: Look for unsafe innerHTML, document.write, eval
- Review third-party integrations: Libraries rendering user content
Audit Checklist
- All user inputs sanitized before rendering
- Content Security Policy implemented
- No use of
innerHTMLwith untrusted data - No
eval()ornew Function()with user data - Framework security features enabled (not bypassed)
- Third-party widgets/embeds sandboxed
- Wallet interactions protected from script injection
XSS remains a critical vulnerability in Web3 applications. While smart contracts may be secure, a single XSS vulnerability can enable attackers to manipulate transactions, steal funds, or drain connected wallets—making frontend security audits essential for comprehensive dApp protection.
Articles Using This Term
Learn more about Cross-Site Scripting (XSS) in these articles:
Related Terms
Cross-Site Request Forgery (CSRF)
An attack that tricks authenticated users into executing unwanted actions on a web application where they're logged in.
Insecure Direct Object Reference (IDOR)
A vulnerability where applications expose internal object references without proper authorization checks, allowing unauthorized access to data.
Input Validation
The process of verifying that user-supplied data meets expected formats and constraints before processing, preventing injection attacks and logic errors.
Frontend Security
Security practices protecting web application client-side code from attacks like XSS, CSRF, and malicious script injection.
Need expert guidance on Cross-Site Scripting (XSS)?
Our team at Zealynx has deep expertise in blockchain security and DeFi protocols. Whether you need an audit or consultation, we're here to help.
Get a Quote

