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.
| Type | Where the script comes from | Simple example |
|---|---|---|
| Stored XSS | Saved in your database and shown to others | A product review contains a script. Every shopper who opens the page runs it. |
| Reflected XSS | Sent in a link and echoed straight back | A search page prints "You searched for ..." from the web address. The attacker emails a crafted link. |
| DOM-based XSS | Handled only by code in the browser | Page 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.
- 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.
- 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. - Watch the escape hatches. Each framework has a way to switch this off:
v-htmlin Vue,dangerouslySetInnerHTMLin React and thebypassSecurityTrustfunctions in Angular. Treat every use as a code review flag. - Use safe browser functions. In plain JavaScript,
element.innerHTML = nameis risky.element.textContent = nameis safe, because it always treats the value as text. - 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.
- 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.
- 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.
- 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.
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

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 AntonioSecurity 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.
