Security glossaryCross-site scripting (XSS) is an attack that injects malicious JavaScript into a page other visitors load, so it runs in their browsers. It can steal sessions or act as the user.
Cross-site scripting, almost always shortened to XSS, happens when a website takes input from someone and puts it into a page without making it safe. If that input contains a script, the browsers of everyone who views the page run it as if the site itself had sent it. The attacker never breaks into the server; the site delivers the attack for them.
Because the script runs with the site's own permissions in the victim's browser, it can read whatever the page can read, change what the page shows, and send requests as the logged-in user.
The three types
- Stored XSS: the script is saved by the site, for example in a comment, a form entry, or a user profile field, and served to everyone who views it. It is the most dangerous kind because it needs no trick to reach victims.
- Reflected XSS: the script arrives in the request itself, usually a URL parameter, and the page echoes it back. The attacker has to get the victim to open a crafted link, often through phishing.
- DOM-based XSS: the flaw is in the page's own JavaScript, which takes data from the URL or another source and writes it into the page unsafely. The server may never see the payload.
Why it matters for WordPress
XSS is one of the most commonly reported vulnerability types in WordPress plugins. On a WordPress site, the most valuable victim is an administrator: a stored XSS payload that runs while an admin is logged in can create a new admin account or edit plugin files, which turns a small bug into a full takeover. Other uses include injecting spam links, redirecting visitors, and adding card-skimming code to checkout pages.
How to prevent it
For developers
- Escape on output, at the last moment, for the context the data lands in. WordPress provides
esc_html(),esc_attr(),esc_url(), andesc_js(), andwp_kses()for when some HTML must be allowed. - Sanitize and validate input with functions such as
sanitize_text_field(), and reject what does not fit. - Avoid writing raw strings into the page from JavaScript; set
textContentrather thaninnerHTML. - Check capabilities and nonces on every action. Nonces defend against cross-site request forgery rather than XSS, but the two attacks are often chained.
For site owners
- Keep plugins and themes updated, and delete ones you do not use.
- Give users the lowest role they need. On a single site, administrators and editors can post unfiltered HTML; the
DISALLOW_UNFILTERED_HTMLconstant inwp-config.phpremoves that ability. - Add a Content Security Policy header to limit where scripts may load from, testing it in report-only mode first.
- A web application firewall can block known attack patterns while a patch is pending.
Our guide to preventing XSS attacks in WordPress goes through these steps in more detail.
Source: OWASP, Cross Site Scripting (XSS).