Cross-site scripting (XSS) presents the threat of an attacker running malicious script in a victim's browser with the full trust of the vulnerable website. That script can steal session cookies and logins, take over accounts, redirect users, deface pages, log keystrokes and deliver malware — all without the attacker ever breaking into the server itself.
Key takeaways
- XSS lets an attacker's script run inside a trusted site, so the browser treats it as legitimate code.
- Stored XSS is the most dangerous type because one injection can hit every visitor; reflected and DOM-based XSS target victims one at a time.
- The fix is context-aware output encoding first, with a strict Content Security Policy and HttpOnly cookies as defense in depth.
You visit a website you trust, log in, and browse normally. Nothing looks wrong — yet a script planted by an attacker is running in your browser, reading your session cookie and acting as you. That is the threat cross-site scripting presents, and it needs no break-in to the server.
What threat is presented by cross-site scripting attacks?
The core threat is code execution inside a trusted origin: an attacker's script runs in your browser as if the website itself wrote it. The browser cannot tell the difference, so the script inherits the site's trust, your login session and your permissions on that site.
OWASP defines XSS as an injection attack in which malicious scripts are injected into otherwise benign and trusted websites. The flaw appears anywhere a web application uses user input in the output it generates without validating or encoding it.
That single flaw unlocks a long list of harms. OWASP lists session stealing, account takeover, MFA bypass, DOM node replacement or defacement such as trojan login panels, malicious software downloads, keylogging and other client-side attacks. The most severe attacks disclose the user's session cookie, letting the attacker hijack the session and take over the account.
The damage is not limited to one user. A stored XSS flaw in a popular comment field or forum can hit thousands of visitors from a single injection. And because the attack runs in the victim's browser, the website's own server logs may show nothing unusual at all.
What is cross-site scripting (XSS)?
Cross-site scripting is a web vulnerability in which an application includes untrusted input in a page without properly encoding it, so the browser treats that input as executable code. The malicious content is usually JavaScript, but it can also be HTML or other code the browser can execute.
The attack has two ingredients. First, data enters the application from an untrusted source, most often a web request. Second, that data is included in dynamic content sent to a user without being validated for malicious content. Fix either step and the attack fails.
It helps to contrast XSS with phishing. A phishing email pretends to be a trusted sender; XSS makes the trusted site itself deliver the attack. The victim did everything right — they typed the real address and checked for HTTPS — and still got compromised. That is why XSS is a site-owner problem first and a user problem second.
XSS is classified as CWE-79, improper neutralization of input during web page generation. In the OWASP Top 10 it falls under the Injection category, and OWASP's 2017 assessment found XSS in around two thirds of all applications, making it the second most prevalent issue in the Top 10 at the time.
What are the three types of XSS?
The three types are stored, reflected and DOM-based XSS. They differ in where the payload lives and how it reaches the victim, but the consequence is the same: attacker code running in the victim's browser with the site's trust.
| Type | Where the payload lives | Who it hits | How it is delivered | Severity |
|---|---|---|---|---|
| Stored | Server database, comment, profile | Every viewer of the page | Victim visits the infected page | Severe — mass compromise |
| Reflected | A URL parameter or form field | One victim per click | Crafted link in email or a post | Moderate — needs a click |
| DOM-based | Client-side JavaScript only | One victim per visit | Malicious link or page input | Moderate — hard to detect |
| Blind | Stored, fires in an admin panel | Staff or admins | Form submission viewed later | High — targets privileged users |
Stored XSS: inject once, hit everyone
Stored XSS, also called persistent XSS, saves the attacker's script on the target server — in a database, message forum, visitor log or comment field. Every user who later views that content receives the script from the server.
OWASP calls stored XSS a high or critical risk, and for good reason. The attacker does no further work after the injection, and the indirection through the data store makes the threat harder to trace. Classic targets are guestbooks, comments, product reviews, forum posts and user profiles.
Blind XSS is a stored variant where the payload fires in a different application, such as an admin panel or support tool, that the attacker cannot see. A payload left in a feedback form executes when a staff member opens it — and staff sessions are often the most valuable ones on the site.
Reflected XSS: the boomerang
Reflected XSS is non-persistent. The injected script is reflected off the web server, for example in an error message or a search-results page that echoes the input back in the response. The victim must be tricked into clicking a malicious link or submitting a crafted form.
OWASP's own example is a page that reads an employee ID from a request and prints it straight into the response. That looks harmless — why would anyone attack their own browser? The danger is that the attacker builds the malicious URL and lures victims to it by email or social engineering, often as part of a phishing campaign.
Reflected XSS is the most common type in the wild because so many pages echo search terms, error messages and form values. It is also the easiest to test for: send a distinctive string, then check whether it comes back unencoded in the response.
DOM-based XSS: the server never sees it
DOM-based XSS lives entirely in client-side JavaScript. The page's own script reads attacker-controlled input — the URL fragment after the hash, document.location or window.name — and writes it into the page through a dangerous sink such as innerHTML or document.write, without sanitizing it.
This type was identified by Amit Klein in 2005. It is the hardest to find because the payload may never reach the server, so request and response logs show nothing. Detection needs client-side code review and CSP violation reports, not just server-side scanning.
What can an attacker actually do with XSS?
Once a script runs in your browser under a trusted origin, it can do almost anything you can do on that site. The browser assumes the code belongs to the site, so the same-origin policy — the rule that normally keeps sites apart — works in the attacker's favor.
- Steal your session. The script reads document.cookie and sends your session token to the attacker, who replays it to take over your logged-in account without your password.
- Take over the account. With a live session, the attacker can change your email, password or payment details, or act in your name.
- Fake the page. The script can rewrite the page, replace a login form with a trojan panel, or inject a fake payment form that harvests card details.
- Redirect you. Victims can be sent to a look-alike site or a malware download, still believing they are on the real one.
- Log keystrokes. A keylogger injected into the page captures passwords and card numbers as you type them.
- Deface or manipulate content. OWASP notes that changing a press release or news item could move a company's stock price or damage consumer confidence — and on a pharmaceutical site, altered dosage information could cause an overdose.
- Spread further. A stored XSS payload can post itself to other users' pages, turning the attack into a self-propagating worm.
The Samy worm of 2005 is the classic demonstration: a stored XSS payload on MySpace added the attacker as a friend of everyone who viewed an infected profile, and spread to over a million users in about 20 hours. No server was hacked — the site's own pages did the spreading.
Who is at risk from XSS?
Almost everyone who uses the web is at risk as a victim, and almost every site that accepts user input is at risk as a carrier. OWASP warns not to be fooled into thinking a read-only or brochureware site is safe: serious reflected XSS attacks can hit even sites that never store user data.
For users, the risk is highest on sites where you are logged in: webmail, banking, social media, admin panels and shopping accounts. A script running there can spend your money, read your messages or change your settings.
For site owners, the risk is reputational and legal as well as technical. An XSS flaw that lets attackers deface your site, steal your customers' sessions or serve malware from your domain can cost you users, search rankings and, under data protection law, regulatory trouble.
Small businesses are not exempt. Most small sites run the same frameworks, plugins and comment systems as large ones, and attackers scan the whole internet for known flaws automatically. Our small business cybersecurity checklist covers the basics that keep a small site out of that sweep.
How does an XSS attack work in practice?
The mechanics are simple, which is why the flaw is so common. Here is the reflected version, step by step, following OWASP's example of a page that prints a request parameter straight into its output.
- The attacker finds an echo. A search page, error page or profile field prints user input back into the HTML without encoding it.
- The attacker crafts a link. The script is hidden in a URL parameter, often encoded so it does not look suspicious.
- The victim clicks. The link arrives by email, chat or a social post — often as part of a phishing message. See our phishing email examples for what that delivery looks like.
- The site reflects the script. The server returns the input as part of the page, and the browser executes it because it came from a trusted server.
- The script phones home. It reads the session cookie or performs actions as the victim, sending the results to the attacker's server.
Stored XSS is worse because steps 2 and 3 disappear: the attacker injects once, and every visitor who loads the infected page is attacked automatically. DOM-based XSS is sneakier still because the payload never crosses the server at all.
How is XSS different from CSRF and SQL injection?
These three attacks are often confused because all of them abuse a website's trust. The difference is what the attacker targets: XSS targets the user's browser, SQL injection targets the site's database, and cross-site request forgery (CSRF) targets the user's authenticated session from another site.
| Attack | Target | What the attacker does | Main defense |
|---|---|---|---|
| XSS | Victim's browser | Runs script as the trusted site | Output encoding, CSP |
| SQL injection | Site's database | Injects database commands | Parameterized queries |
| CSRF | Victim's session | Tricks browser into a forged request | Anti-CSRF tokens, SameSite cookies |
| Phishing | The user | Impersonates a trusted sender | DMARC, verification habits |
XSS and CSRF are sometimes combined: an XSS script can forge requests from inside the trusted origin, bypassing the very defenses that would stop a CSRF attack from outside. That is one reason XSS is treated as a critical class of flaw rather than a cosmetic bug.
How do you prevent cross-site scripting?
The primary defense is context-aware output encoding: encode every untrusted value at the moment it is inserted into a response, using the encoding for the context it lands in — HTML body, HTML attribute, JavaScript, CSS or URL. Encoding turns the attacker's code back into harmless text.
OWASP's XSS Prevention Cheat Sheet sets out the layered approach. No single technique solves XSS; the combination does. For developers, the checklist is:
- Encode output for its context. Use a trusted encoding library for each output context instead of hand-rolled escaping.
- Keep framework auto-escaping on. Modern frameworks escape template output by default. Never disable it, and never mark untrusted data as safe HTML.
- Sanitize user-authored HTML. When users legitimately need formatting, pass their HTML through a maintained sanitizer such as DOMPurify.
- Validate input on arrival. Allowlist validation helps, but it is defense in depth — encoding at output is the real fix.
- Use safe DOM APIs. Prefer textContent over innerHTML, and avoid document.write and eval on untrusted data.
- Set cookie flags. Mark session cookies HttpOnly so scripts cannot read them, Secure so they only travel over HTTPS, and SameSite to limit cross-site use.
- Deploy a strict Content Security Policy. CISA recommends implementing a CSP because it lessens the chance an attacker can load and run malicious JavaScript on the end user's machine. Use nonces or hashes, not a broad allowlist, and start in report-only mode.
- Turn off HTTP TRACE. OWASP notes that an attacker can steal cookie data through an asynchronous TRACE call even when document.cookie is disabled, so disable TRACE support on all web servers.
CISA's website security guidance adds the same themes for site owners: sanitize all user input at both the client and server ends, implement XSS protections, deploy a CSP, and audit third-party code such as ads and analytics, since a compromised third-party script runs with your site's trust too. You can check your own site's headers in seconds with our free security headers checker.
What can users do to protect themselves from XSS?
Users cannot fix a vulnerable website, but they can shrink the damage. Keep your browser and extensions updated, since browsers add XSS mitigations over time. Use a password manager and passkeys where available, so a stolen session is less useful and your credentials are never typed into a fake form.
Be careful with links in email and chat, especially links that lead to a site where you are logged in. A reflected XSS attack needs that click. If a link looks odd or arrives unexpectedly, open the site yourself from a bookmark instead. Our guide to knowing if your phone is hacked covers the signs that something has already gone wrong on a device.
Log out of sensitive sites when you finish, and do not stay signed in to admin panels in a tab you leave open all day. Short sessions and re-authentication for sensitive actions limit what a stolen cookie can do. If a site you use looks defaced or behaves strangely — forms that look different, unexpected redirects — stop, do not enter credentials, and tell the site owner.
The bottom line
Cross-site scripting presents a simple, serious threat: code the attacker wrote runs in your browser with the trust of a website you chose. From there it can steal sessions, take over accounts, fake pages, log keystrokes and spread to other users — and the site itself may never notice.
For site owners, the fix is unglamorous but proven: encode output for its context, keep framework protections on, sanitize user HTML, harden cookies, and back it all with a strict Content Security Policy. CISA and the FBI have said XSS vulnerabilities are preventable and should not be present in software products. Check your own site's headers with the free security headers checker and start there.
Frequently asked questions
What threat is presented by cross-site scripting attacks?
The main threat is that an attacker can run code in your browser as if it came from a trusted website. That code can steal your session cookie, take over your account, redirect you to a fake page, or install malware, all while the site itself looks normal.
What is cross-site scripting in simple terms?
It is an injection attack where a website includes untrusted input in a page without properly encoding it, so the browser treats the attacker's text as code. The malicious script then runs with the trust and permissions of the real site.
What is the difference between stored, reflected and DOM-based XSS?
Stored XSS saves the script on the server and serves it to every visitor. Reflected XSS bounces the script back in a single response, usually via a crafted link. DOM-based XSS never touches the server: the page's own JavaScript writes attacker input into the page.
Can XSS steal my password?
Yes, indirectly. XSS can steal session cookies, which lets an attacker take over a logged-in account without the password. It can also replace a login form with a fake one, log keystrokes, or trick you into actions on the real site.
Does HTTPS protect against XSS?
No. HTTPS encrypts traffic between you and the site, but it does nothing about malicious code the site itself delivers to your browser. An XSS attack works perfectly well over HTTPS.
Can a Content Security Policy stop XSS completely?
No. A strict CSP with nonces greatly reduces the damage an XSS flaw can cause, but it is defense in depth, not a fix. The real fix is encoding untrusted data for its output context and using safe JavaScript APIs.
Is XSS still a serious threat in 2026?
Yes. XSS is classified as CWE-79 and sits inside the Injection category of the OWASP Top 10. CISA and the FBI have stated that XSS vulnerabilities are preventable and should not be present in software products, which shows how seriously the problem is still taken.
How do I know if a website has an XSS vulnerability?
As a visitor you usually cannot tell, which is part of the danger. As a site owner, review code wherever untrusted input reaches HTML output, scan with a web vulnerability scanner, and watch Content Security Policy violation reports for blocked scripts.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against OWASP's XSS guidance, CISA website security guidance and the CWE-79 weakness definition.
How we write and fact-check →