Skip to content

Web & Application Security

Cross-Site Scripting (XSS):
Understanding the Risk & How to Prevent It

How XSS appears in web applications, why it matters to users and organisations, and the controls that prevent untrusted content from running in the browser.

August 2026·7 min read

Browser and application view showing how untrusted scripts can reach the page DOM

Cross-site scripting, or XSS, occurs when a web application includes untrusted data in a page in a way that the browser treats as executable script. The usual language is JavaScript, and the usual cause is a failure to validate input and encode output before content is rendered.

Unlike many server-side flaws, XSS runs in the victim's browser, in the context of the vulnerable application. That is why it remains relevant to session security, data integrity, and user trust.

What XSS is

Modern web pages are documents the browser interprets. HTML structure, styles, and scripts combine to produce the interface. If an application takes content from a user — a search term, a comment, a profile field — and writes it into the page without encoding, the browser may execute that content as code rather than display it as text.

The result is that a third party can run script with the same origin privileges as the legitimate application. From the user's point of view, the page still looks like the trusted site.

Reflected XSS

Reflected XSS is typically delivered through a request and immediately returned in the response. Search fields, error messages, and URL parameters are common locations. The injected content is not stored by the application; it is reflected back to the person who submitted it, or to anyone who can be persuaded to open a crafted link.

Testers look for parameters whose values reappear in HTML, attributes, or script contexts, then confirm whether the application encodes those values before rendering.

Stored XSS

Stored XSS persists. The untrusted content is saved — in comments, reviews, support tickets, display names — and later served to other users. Anyone who opens the affected page can be exposed, which is why stored XSS is often treated as higher impact than a one-off reflected case.

Forums, product reviews, and other multi-user content features deserve particular attention during testing because one stored value can reach many sessions.

DOM-based XSS

DOM-based XSS happens entirely in the browser. Client-side JavaScript reads data from the URL, the page, or storage, then writes it into the Document Object Model in an unsafe way. The server may never see the malicious value. Reviewing server responses alone will miss this class of issue.

Assessment therefore includes client-side code paths: how the application reads location data, how it updates the DOM, and whether those updates use safe APIs.

Business and security impact

XSS is often underestimated because it does not always produce a dramatic server-side error. The impact is still material.

  • Session abuse where cookies or tokens are accessible to script and not marked HttpOnly
  • Unauthorised actions performed in the user's session, including changes to account data
  • Content injection that alters what users see, including phishing inside a trusted origin
  • Delivery of further client-side attacks, such as unexpected navigation or embedded third-party content

For organisations, XSS can undermine authentication, damage brand trust, and create a path into otherwise well-protected accounts. The application did not need to give the attacker a password. It only needed to render their content unsafely.

How security testers identify XSS

Testers map input points, then observe whether submitted values appear in the response or in client-side rendering. They pay attention to context: text nodes, HTML attributes, JavaScript strings, and URL fragments each require different encoding.

A finding is only confirmed when the tester can show that untrusted input is interpreted as script, not merely that special characters are echoed. Manual review of encoding, sanitisation, and DOM sinks is essential. Scanner alerts should be treated as leads, not conclusions.

Prevention and secure development

Input validation

Accept only the data the feature needs. Constrain length and format. Do not rely on rejecting a short list of characters; attackers change encoding and context faster than blocklists can keep up.

Output encoding

Encode on the way out, according to context. HTML-encode content placed in the document body. Use attribute encoding for values inside tags. JavaScript contexts require their own encoding rules. A single global filter is rarely sufficient for every sink.

Content Security Policy

A well-designed Content Security Policy (CSP) reduces the damage of a missed encoding bug by restricting which scripts the browser will execute. CSP is a defence in depth control, not a replacement for encoding. It works best when inline script is limited and trusted sources are explicit.

  • Prefer framework-safe rendering that encodes by default
  • Set HttpOnly and Secure on session cookies so script cannot read them
  • Review third-party widgets that insert HTML into the page
  • Test both server-rendered and client-rendered paths

XSS prevention is a design habit: treat every piece of untrusted content as data until the last moment it is displayed, and never let the browser confuse that data with code.

Need More Than an Insight?

Our security practitioners help organisations identify vulnerabilities, strengthen defences, investigate incidents, and build secure technology.