Cross-site scripting (XSS)
An injection flaw in which a web application places untrusted input in a page without proper encoding, so an attacker's script runs in other users' browsers with the site's privileges.
Cross-site scripting (XSS) occurs when a web application places untrusted data in a page without proper output encoding, so the browser runs that data as script. The injected script runs with the same privileges as the site’s own code, so it can read cookies not marked HttpOnly, alter the page or send requests as the user. It is catalogued as CWE-79, and the OWASP Top 10 (2025 edition) includes it under Injection.
Three types are commonly named. Reflected XSS bounces the script off the server in an immediate response, often through a crafted link. Stored (persistent) XSS saves the script on the server, for example in a comment or profile field, so it runs for everyone who views that content. DOM-based XSS happens in client-side JavaScript that writes untrusted data into the page. The main defence is output encoding suited to the context, supported by input validation, the HttpOnly flag and a Content Security Policy. It is often paired with cross-site request forgery, which works the other way round.
Exam relevance: a scenario about script typed into a comment field running in other visitors’ browsers is likely to point to stored XSS. Candidates are expected to name output encoding as the primary fix, and to keep XSS apart from SQL injection, which targets the database rather than the browser.