Penetration Testing

Cross-Site Scripting (XSS) Explained: Types, Examples and How to Prevent It

By Vince AntonioPublished 6 min read

Cross-site scripting (XSS) is one of the most common flaws found in web apps. This guide explains how an XSS attack works, the three main types with simple examples, what attackers can do with it and the layered fixes that stop it.

In plain English

  • Cross-site scripting lets an attacker run their own code in your users' web browsers.
  • It can be used to take over accounts, show fake login forms or act as the user.
  • Most fixes are built into modern web tools, as long as developers do not switch them off.
  • Ask your developers how they handle user content, and test your app regularly.

What is cross-site scripting (XSS)?

Cross-site scripting (XSS) is a flaw that lets an attacker run their own script in another user's browser, inside your website. A script is a small program that web pages run, written in JavaScript. Because it runs on your site, the browser trusts it as if you wrote it.

XSS is part of A05:2025 Injection in the OWASP Top 10. OWASP notes that XSS is found very often, which is why Injection has so many published flaws. Its formal ID is CWE-79 (CWE is a public catalog of software flaw types).

How an XSS attack works

Web pages mix two things: content (text, names, comments) and code (HTML that shapes the page and JavaScript that runs in it). XSS happens when user content lands in the page and the browser reads it as code.

Picture a page that shows a user's display name. If the name is written into the page as raw HTML, someone could set their name to a script tag, such as <script>...</script>. Every browser that shows that name would run whatever is inside.

It is the same root mistake as SQL injection: data and code get mixed. With SQL injection, the database is fooled. With XSS, your users' browsers are.

Types of XSS with simple examples

There are three main types. They differ in where the bad input is kept and how it reaches the victim.

TypeWhere the script comes fromSimple example
Stored XSSSaved in your database and shown to othersA product review contains a script. Every shopper who opens the page runs it.
Reflected XSSSent in a link and echoed straight backA search page prints "You searched for ..." from the web address. The attacker emails a crafted link.
DOM-based XSSHandled only by code in the browserPage JavaScript reads part of the web address and writes it into the page as HTML.

Stored XSS is usually the most serious, because one bad entry can hit every visitor. It is worse when staff or admins view the content, for example in a support ticket queue.

Reflected XSS needs the victim to click a link, so attackers pair it with phishing (fake emails or messages that trick people into acting).

DOM-based XSS may never touch your server. That makes it easy for server-side filters and some scanners to miss.

What attackers can do with XSS

Once a script runs in a user's browser on your site, it can do almost anything that user can do. Common results include:

  • Session theft: stealing the login cookie (a small file that keeps you signed in) and using it to take over the account.
  • Fake forms: showing a convincing login or payment form on your real site and sending what is typed to the attacker.
  • Account actions: changing the email address, adding a new admin or making a transfer, all as the victim.
  • Page changes: altering prices, messages or links that users see.

If the victim is an admin, one XSS flaw can lead to control of the whole app.

How to prevent XSS

OWASP stresses that no single fix stops XSS. You need layers, and they must cover every place user content appears.

  1. Encode output for its context. Before user content goes into a page, convert special characters so the browser shows them as text. The right encoding depends on where it goes: HTML text, an attribute, a web address or JavaScript each need their own.
  2. Rely on your framework's auto-escaping. Modern tools such as React, Vue and Angular encode content by default. In Vue, {{ comment }} is safe because it is shown as text.
  3. Watch the escape hatches. Each framework has a way to switch this off: v-html in Vue, dangerouslySetInnerHTML in React and the bypassSecurityTrust functions in Angular. Treat every use as a code review flag.
  4. Use safe browser functions. In plain JavaScript, element.innerHTML = name is risky. element.textContent = name is safe, because it always treats the value as text.
  5. Sanitize rich text with a vetted library. If users must post formatted text, encoding would break it. Clean it with a well-maintained sanitizer instead. OWASP recommends DOMPurify. Keep it updated.
  6. Add a Content Security Policy. CSP is a header that tells the browser which scripts it may run. A strict policy can block injected scripts. It is an extra layer, not a main defense.
  7. Mark session cookies HttpOnly. This stops JavaScript from reading the cookie, so a script cannot simply copy it. The attacker can still act inside the page, so this limits damage rather than preventing XSS.
  8. Validate input. Reject values that do not match the expected format, such as letters in a phone number field. Like the other layers, it helps but does not replace output encoding.
Do not rely on a web application firewall or a filter that strips "bad words". They miss DOM-based XSS and are often bypassed.

How to find XSS in your app

XSS hides wherever user content is shown: profiles, comments, search pages, file names, error messages and admin dashboards. Two kinds of tools help.

SAST tools (which scan source code) can flag risky patterns such as v-html or innerHTML. DAST tools (which probe the running app) send test input and check what comes back. Our guide to SAST vs DAST explains when to use each.

A penetration test (pentest) goes further. A tester follows each input to every place it appears, including emails, PDFs and internal admin pages. They check each one in the right context and show how far an attacker could get.

How NextGarrison helps

NextGarrison tests web apps and APIs for every type of XSS, plus SQL injection and the rest of the OWASP Top 10. You get clear findings with the exact place, the risk and the fix for your framework, and a retest once it is fixed. See our penetration testing services or ask for a scoping call.

Sources

Topics:Application SecurityPenetration Testing

Need a hand with this?

Talk to a senior consultant. We reply within one business day with clear next steps and a scoped quote.

Prefer to just ask? Contact us and a consultant will reply.

Written by

Vince Antonio

Security writer and consultant, NextGarrison

Vince writes NextGarrison's security news, technical vulnerability write-ups and guides. He covers actively exploited flaws, penetration testing, SOC monitoring and getting ready for SOC 2, ISO 27001 and PCI DSS, in plain English with sources for every claim.

Want the next alert like this one?

New vulnerability alerts as soon as we publish them. Free, no sign-up, works in Feedly, Outlook, Slack and Teams.

Frequently Asked Questions

Is XSS in the OWASP Top 10?

Yes. Cross-site scripting is part of A05:2025 Injection in the current OWASP Top 10. It used to have its own entry in older editions.

What is the difference between stored and reflected XSS?

Stored XSS is saved on your site and runs for everyone who views it. Reflected XSS is carried in a link and only runs for the person who clicks it.

Does a Content Security Policy stop XSS?

A strict CSP can block many injected scripts, but it is easy to set up loosely. Use it as an extra layer on top of output encoding and safe framework use.

Are React and Vue apps safe from XSS?

They are safer by default because they encode content automatically. But features like dangerouslySetInnerHTML and v-html, unsafe links and plain JavaScript can still introduce XSS.

Keep reading