What is Cross-Site Scripting (XSS)?
What you'll learn: How attackers inject malicious scripts into trusted websites that execute in victims' browsers.
Cross-Site Scripting (XSS) is a web security vulnerability where an attacker injects malicious JavaScript (or other client-side code) into a legitimate website. When other users visit that page, their browsers trust the website and execute the attacker's script—giving the attacker control within the victim's browser session.
The Core Idea
Think of a trusted website like a restaurant menu. You expect everything printed on it to be safe. XSS is like an attacker sneaking into the kitchen and adding poison to one item—but the menu still looks official, so you consume it without suspicion.
In technical terms: websites often accept user input (search boxes, comment fields, profile names) and display it back to users. If the website doesn't properly validate or sanitize this input, an attacker can submit malicious JavaScript instead of normal text. When the site displays this "data," the browser interprets it as legitimate code and executes it.
Why It's Dangerous
Because the malicious script runs in the context of the trusted website, it can:
- Steal session cookies and authentication tokens
- Capture keystrokes or form data
- Deface the website for specific users
- Redirect victims to phishing sites
- Perform actions on behalf of the victim
Unlike SQL injection (which attacks the database server), XSS attacks the client side—the victim's browser becomes the weapon against itself.
Real-World Scenario
A forum allows users to post comments. An attacker posts a comment containing <script> tags with malicious code. When you view that comment thread, your browser executes the attacker's script, potentially sending your login cookies to their server.
Key Takeaway: XSS exploits trust relationships between users and websites by injecting malicious scripts that execute in victim browsers under the guise of legitimate website content.