Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

What Is Cross-Site Scripting (XSS)?

Cross-site scripting (XSS) is a web vulnerability that lets an attacker inject JavaScript into a trusted page, so it runs in visitors’ browsers with the site’s own permissions.

Illustration for the cybersecurity term Cross-Site Scripting (XSS)
Home / Blog / Cybersecurity Terms / What Is Cross-Site Scripting (XSS)?
17 November 2026

What Is Cross-Site Scripting (XSS)?

What Is Cross-Site Scripting (XSS)?

Cross-site scripting (XSS) is a vulnerability in which an application includes untrusted input in a web page without handling it safely, so the browser treats that input as code. The attacker’s JavaScript then runs in the victim’s browser under the vulnerable site’s origin, with access to everything the page can see and do. Unlike server-side injection, XSS targets the people using the application rather than the server itself.

Why a Few Characters Matter So Much

Browsers trust any script delivered by a site as if the site wrote it. If a search page prints You searched for: <script>…</script> without encoding the angle brackets, the browser cannot tell that part of the page came from an attacker. Once running, the injected script can read page content, call internal APIs as the logged-in user, capture keystrokes, rewrite forms, or steal session tokens that are not protected by the HttpOnly flag.

The Samy worm of October 2005 showed the scale this can reach. A stored XSS payload on a MySpace profile added its author as a friend and copied itself to every visitor’s profile, spreading to more than a million accounts in under a day.

The Three Main Types

Reflected XSS

The payload travels in the request, usually a URL parameter, and the server echoes it straight back in the response. The victim has to be lured into opening a crafted link, which is why reflected XSS often pairs with phishing. Each attack typically hits one user at a time.

Stored XSS

The payload is saved by the application—in a comment, a profile field, a support ticket, a product review—and served to everyone who views it. No link is needed, and a single injection can affect every user, including administrators. A variant known as blind XSS fires in a back-office tool that the attacker never sees, such as a helpdesk console or log viewer.

DOM-Based XSS

The flaw sits entirely in client-side JavaScript. Code reads attacker-controlled data from a source such as location.hash or postMessage and passes it to a dangerous sink such as innerHTML, eval(), or document.write(). The server response may be identical for every visitor, so server-side scanners often miss it.

Where XSS Sits in the Bigger Picture

XSS is CWE-79 and falls under A03 Injection in the OWASP Top 10 2021; it remains within the Injection category in the 2025 edition. It is still one of the most frequently reported issues on bug bounty platforms because modern applications have so many places where data and markup meet.

A close relative is client-side script tampering of the Magecart kind. In the 2018 British Airways breach, attackers modified a script on the airline’s payment pages to skim card details, and the ICO fined the company £20 million in 2020. That was not classic XSS, but it shows the same point: whoever controls the JavaScript on your page controls what your customers type into it.

Defending Against XSS

No single control is enough, but the following combination stops the vast majority of attacks:

  1. Context-aware output encoding. Encode data for exactly where it lands: HTML body, attribute, JavaScript string, or URL. Modern template engines and frameworks such as React and Angular escape by default; the danger is in escape hatches like dangerouslySetInnerHTML or bypassSecurityTrustHtml.
  2. Sanitise rich content. Where users must submit HTML, pass it through a maintained sanitiser such as DOMPurify rather than writing your own filters.
  3. Content Security Policy. A strict CSP using nonces or hashes, without unsafe-inline, blocks injected scripts even when encoding fails. Trusted Types adds similar protection for DOM sinks in Chromium-based browsers.
  4. Protect cookies. Mark session cookies HttpOnly and Secure so scripts cannot read them, while remembering that XSS can still act as the user without the cookie.
  5. Validate input. Allow-list expected formats such as numbers, dates, and email addresses. Input validation reduces exposure but never replaces encoding.

Testing for XSS

Automated scanners such as Burp Suite and ZAP find many reflected cases, but stored, blind, and DOM-based flaws usually need a human who understands the application’s data flows and front-end code. Our web application security services combine both approaches, and developers who want to fix the root cause can build the skills through our web application security training.

FAQ – Frequently Asked Questions

Can XSS Be Used to Take Over an Account?

Often, yes. An injected script can read session tokens that lack the HttpOnly flag, change the account’s email address or password through the application’s own forms, or quietly add a new API key. Even with well-protected cookies, the script acts with the victim’s full permissions while the page is open. Stored XSS that reaches an administrator is especially dangerous because it can compromise the whole application.

Does a Content Security Policy Stop All XSS?

A strict, nonce-based Content Security Policy blocks most injected scripts, but it is a safety net rather than a fix. Weak policies that allow unsafe-inline, broad CDN domains, or JSONP endpoints can be bypassed. CSP also does not prevent HTML injection used for phishing or data exfiltration through other channels. Treat it as a second layer behind correct output encoding.

Are Modern JavaScript Frameworks Immune to XSS?

No, although they help a great deal. React, Angular, and Vue escape output by default, which removes many classic mistakes. XSS still appears when developers use raw HTML features, build URLs from user input without checking the scheme, render server-side templates unsafely, or rely on third-party components with their own flaws. Code review and testing remain essential.

Related Terms

You May Be Interested In These

What Is Metasploit?

Cybersecurity Terms

17 November 2026

What Is Metasploit?

Read More
What Is a Cloud Exit Strategy?

Cybersecurity Terms

17 November 2026

What Is a Cloud Exit Strategy?

Read More