Skip to content

XSS

In one line

XSS (Cross-Site Scripting) is a security vulnerability where an attacker injects malicious scripts into a web page viewed by other users.

In simple words

XSS is one of the most common web security vulnerabilities. It happens when user input is displayed on a page without proper escaping, allowing an attacker to inject JavaScript that runs in other users' browsers. This can steal cookies, session tokens, or sensitive data.

There are three types: Stored XSS (malicious input saved to the database and shown to others), Reflected XSS (input embedded in the URL and reflected on the page), and DOM-based XSS (client-side JavaScript injects unescaped data into the DOM).

Prevention: never use innerHTML with user input (use textContent instead), escape all user-generated content, implement a Content Security Policy (CSP), and use HttpOnly cookies so injected scripts cannot steal them. Always treat user input as untrusted.

Example

javascript
// VULNERABLE - XSS risk:
element.innerHTML = userInput;

// SAFE - treats input as text:
element.textContent = userInput;

innerHTML parses the input as HTML (allowing script injection). textContent treats it as plain text, preventing XSS.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary