Cross-Site Scripting (XSS)
Cross-site scripting (XSS) is a web vulnerability that lets an attacker inject malicious script into pages other users view, so the script runs in their browsers and can steal sessions, change content or redirect them.
Also called: XSS, cross site scripting, stored XSS, reflected XSS, DOM-based XSS
What is cross-site scripting?
In an XSS attack, the attacker gets a website to include their JavaScript in a page that other people load. Because the script appears to come from the trusted site, the victim's browser runs it with full access to that site, including the victim's session, page content and any form they fill in.
Types of XSS
- Stored XSS: the script is saved on the site, for example in a comment, review or profile field, and runs for everyone who views it.
- Reflected XSS: the script is in a link; when the victim clicks it, the site reflects it back into the page.
- DOM-based XSS: the flaw is in the page's own JavaScript, which inserts untrusted data unsafely.
What attackers do with XSS
Steal session cookies to hijack accounts, capture keystrokes and passwords on the page, show fake login forms, deface the site, or redirect visitors to malware.
How to prevent XSS
CISA and the FBI issued a Secure by Design alert in 2024 urging software makers to eliminate XSS. OWASP's prevention guidance:
- Encode output so user-supplied data is displayed as text, never run as code.
- Use modern web frameworks that encode by default, and avoid features that bypass it.
- Sanitize any HTML that users are allowed to submit.
- Add a Content Security Policy to limit which scripts can run.
- Mark session cookies HttpOnly so scripts can't read them.
Related terms
Sources
Drafted with AI assistance, then edited and fact-checked by a person. How we use AI.
