Cross-Site Scripting
Also known as: XSS
Web vulnerability that lets attackers inject malicious scripts into pages viewed by other users, which then run with the trust of the affected site.
Cross-site scripting (XSS) is a web application vulnerability in which a site includes untrusted content in a page without properly handling it, so that a browser treats attacker-supplied text as executable JavaScript. Because the script runs in the context of the vulnerable site, it can act with that site's authority: reading information shown on the page, performing actions as the logged-in user or altering what the user sees. The name is somewhat misleading, since many XSS attacks involve only a single site.
Microsoft security engineers are credited with coining the term around 2000. Three main types are recognized. Stored XSS saves malicious content on the server, for example in a comment, so it affects every visitor who views it; reflected XSS returns input from a crafted link in the response; and DOM-based XSS occurs entirely in client-side code that writes untrusted data into the Document Object Model. The 2005 Samy worm on MySpace, which spread to over a million profiles in about a day, showed how stored XSS could propagate across a social platform.
Prevention centers on treating all external data as text, not code. Frameworks such as React escape output by default, and developers should apply context-appropriate encoding wherever data appears in HTML, attributes, URLs or scripts. A Content Security Policy lets sites restrict which scripts may run, limiting the damage of any flaw, and HttpOnly cookies keep session tokens out of reach of scripts. Like SQL injection, XSS has featured in successive editions of the OWASP Top 10, and both persist mainly through legacy code and inconsistent practices.
Key points
- XSS lets attacker-controlled scripts run in other users' browsers under a trusted site's authority.
- The three main types are stored, reflected and DOM-based cross-site scripting.
- Context-aware output encoding and frameworks that escape by default prevent most XSS.
- Content Security Policy and HttpOnly cookies limit the damage if a flaw slips through.
Related entries
SQL Injection
Web application vulnerability in which untrusted input is interpreted as part of a database query, letting attackers read, change or delete data.
JavaScript
High-level programming language of the web, created at Netscape in 1995, that runs in every major browser and on servers through runtimes like Node.js.
OWASP Foundation
Nonprofit foundation that produces free, community-built resources for improving software security, including the widely used OWASP Top 10.
Document Object Model
Programming interface that represents a web page as a tree of objects, letting scripts read and change its content, structure and style.
Vulnerability
A weakness in software, hardware, configuration or process that an attacker could exploit to compromise confidentiality, integrity or availability.
Cross-Origin Resource Sharing
Browser security mechanism that uses HTTP headers to let a server state which other websites may read its responses through scripts.
React
Open-source JavaScript library from Meta for building user interfaces out of reusable components that update efficiently when data changes.
Penetration Testing
An authorized, simulated attack on a system, network or application, performed to find and fix security weaknesses before real attackers do.
Referenced by
Other entries that discuss or point to Cross-Site Scripting.