Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Cross-site scripting (XSS) is a web security flaw that makes a site run attacker-controlled code in a visitor’s browser as if it came from that trusted site. Depending on the site and the visitor’s access, the code may read or change page content or send requests using the visitor’s credentials. XSS does not necessarily steal cookies, and it is not code execution on the web server.

How cross-site scripting works

A typical XSS flaw begins when attacker-controlled data reaches a page without being made safe for the exact place it appears. If the browser interprets that data as executable content, it runs in the vulnerable site’s context. The browser’s trust in the site is what makes the flaw dangerous: injected code may act on page content and functionality available to that visitor.

The name is historical. An attack does not have to cross between two separate sites; the essential problem is unsafe code execution in the context of a trusted target site. See MDN’s explanation of XSS and OWASP’s XSS overview.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What are the main types of XSS?

Reflected and stored XSS describe how attacker-controlled content reaches a victim and whether the application keeps it. DOM-based XSS describes unsafe handling in client-side code. These labels can overlap rather than forming three exclusive categories.

Type Where the unsafe handling occurs Is the payload stored? How it reaches a victim
Reflected In data from a request that the server includes unsafely in its response, such as a result or error page. No; the application does not persist it. Often through a crafted link or request that the victim opens.
Stored When saved content is later included unsafely in a page. Yes; the application retains the content. A later viewer encounters it on a page, for example in a comment or forum post.
DOM-based In client-side code that handles attacker-controlled data unsafely and sends it to a dangerous DOM or other execution sink. Not defined by this label; the data may be reflected or stored. Through the client-side data flow that causes the browser to process it unsafely.

Stored content may reach multiple later viewers, while a reflected payload is commonly tied to a particular request. DOM-based issues concern what browser-side code does with data, so a DOM-based vulnerability can also have a reflected or stored delivery path. OWASP’s XSS types overview and reflected XSS testing guidance describe these distinctions.

Why XSS matters—and what it does not automatically mean

Injected code runs in a visitor’s browser under the vulnerable site’s context, so it may be able to interact with content or functions the visitor can access. The specific impact depends on the application, the visitor’s privileges, and browser protections. Do not assume that every XSS flaw exposes cookies or has the same consequences.

XSS is different from server-side code execution: the injected code runs in the victim’s browser, not on the site’s server. Its effects therefore depend on the browser-side context and the site’s behavior.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to prevent XSS

Prevent XSS by controlling how untrusted data is handled at the point where it is rendered or passed to browser APIs. Input filtering by itself is not a substitute for making data safe for its exact output context. OWASP’s guidance on avoiding cross-site scripting vulnerabilities explains why the right defenses depend on where data goes.

Encode for the exact output context

HTML text, quoted attributes, URLs, JavaScript, CSS, and DOM operations have different safety requirements. Use context-appropriate output encoding rather than assuming one generic transformation is safe everywhere.

Use safe DOM APIs for text

For ordinary text, use textContent and create elements with DOM APIs instead of inserting untrusted strings through innerHTML. If a feature genuinely needs to accept user-provided HTML, sanitize it with a maintained allowlist sanitizer before rendering it.

Keep framework protections intact

Templating frameworks that escape output by default can help, but unsafe escape hatches, raw HTML rendering, unsafe URLs, or outdated components can undermine that protection. Review how untrusted data reaches the rendered page rather than treating framework use as a guarantee.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add supporting browser and policy controls

A Content Security Policy and browser controls can provide defense in depth, but neither replaces safe data handling. A web application firewall is not a reliable root-cause fix for XSS, particularly when the flaw lies in client-side DOM processing.

Best Value
Sale
The Web Application Hacker's Handbook: Finding and Exploiting Security Flaws
  • Comes with secure packaging
  • It can be a gift item
  • Easy to read text

Consider Trusted Types where supported

Trusted Types lets developers require data to pass through a developer-defined transformation before it reaches browser APIs that may execute it. MDN marks the API broadly available since February 2026, while noting that older browsers or devices may lack support. Check compatibility against the browsers your application needs to serve.

Quick Recap

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.