XSS explained in plain terms means an attack in which a malicious script is smuggled into a website other people trust, so the victim’s own browser runs the attacker’s code as if the site itself had written it. The central question is why a browser obeys code from a stranger at all, and the short answer is trust: the browser cannot tell where a page’s script came from, so it assumes anything the trusted site sends is safe to run.
Think of a noticeboard in a busy office. Anyone may pin up a note, and everyone who reads it assumes the office approved it. An attacker pins up a note that says, in effect, hand me your keys, and the readers comply because the board itself looks official. XSS turns a trusted website into that noticeboard.
The name stands for cross-site scripting, spelled XSS because CSS already means something else. It has been one of the web’s most persistent flaws since the late 1990s, and it still appears in security rankings of the most common web weaknesses today. It survives because so many sites display text typed in by their own users.
Key takeaways
- XSS injects malicious scripts into pages that victims already trust.
- The browser runs the injected script with the trusted site’s full privileges.
- Stored, reflected, and DOM-based XSS differ in where the payload lives.
- Stolen session cookies are the most common prize for an attacker.
- Output encoding, input validation, and content security policy stop most attacks.
What XSS is
XSS is a web security flaw in which an attacker gets a website to include untrusted code in a page it sends to other users. The code is usually JavaScript, the language browsers run inside web pages, and it executes in the victim’s browser with all the privileges of the site itself. It can read what the page shows, change what the page displays, and send requests as if the victim had made them.
The flaw exists because websites are built to show user content. Comments, reviews, search boxes, profile names, and forum posts all take text from one person and display it to another. If the site fails to treat that text as plain data, and instead lets the browser interpret part of it as code, an attacker can slip a script into the page.
The attack matters because of the same-origin policy, the browser rule that a page from one site cannot read data from another. That rule is what keeps a random website from reading your email. But a script injected into your email provider’s own pages is no longer from another site: it is from the trusted site itself, so the browser lets it read whatever the site can see.
XSS is not the same as breaking into a server. The attacker does not need the site’s passwords or its database; they only need a place where the site echoes their input back to other people. Our guide to network security explains the wider defences that protect systems and traffic, of which fixing XSS is one important part.
How XSS works
It begins with a place where user input becomes page content. A comment form, a search field that repeats the search term on the results page, or a profile page that shows a display name are all candidates. The attacker types in text that contains script tags or event handlers, the kind of markup that tells a browser to run code, and submits it.
If the site stores that input and shows it to later visitors, the attack is stored XSS. The malicious comment sits in the database, and every person who opens the page runs the script. This is the most dangerous form, because one injection reaches everyone, and the attacker does not even need to be present when the victims arrive.
If the site only echoes the input back in its immediate response, the attack is reflected XSS. The script lives in a crafted link, and the victim must be persuaded to click it, perhaps in a message that looks like it comes from the site itself. When they click, the site reflects the script into the page it returns, and their browser runs it.
The third form, DOM-based XSS, never reaches the server at all. The page’s own JavaScript takes data from the address bar or another source and writes it into the page without checking it, so the attack happens entirely inside the victim’s browser. This form is easy to miss, because scanning the server’s responses will not reveal it.

Comparing the three types of XSS
| Aspect | Stored XSS | Reflected XSS |
|---|---|---|
| Where the payload lives | In the site’s database | In a crafted link |
| Who it reaches | Every visitor to the page | Only people who click the link |
| Attacker effort | One injection, many victims | Must lure each victim |
| How it is found | By testing stored fields | By testing echoed input |
| Typical impact | Highest, affects all users | High, but needs a click |
Where XSS is used
Session theft is the classic goal. A website recognises a logged-in user by a session cookie, a small token stored in the browser, and an injected script can read that cookie and send it to the attacker. With the token in hand, the attacker can impersonate the victim, opening their account without ever knowing their password.
Phishing inside a trusted site is another use. The script can rewrite the page the victim sees, adding a convincing fake login form or a warning that asks them to enter payment details. Because the address bar still shows the genuine site, the victim has little reason to doubt what they see.
Attackers also use XSS to spread further. A script running in one user’s browser can post comments, send messages, or change settings on their behalf, turning each victim into a new source of infection. Worms built this way have spread through social networks in hours, because every infected account reaches all of its contacts.
Sometimes the goal is simply defacement or disruption. A script can replace a page’s content with a message, redirect visitors to another site, or silently mine cryptocurrency in the victim’s browser. These attacks are less profitable than session theft, but they are easy to launch and hard for a site owner to notice until users complain.
Strengths and limits
From the attacker’s point of view, XSS is powerful because it borrows trust. The victim’s browser, the site’s cookies, and the site’s reputation all work in the attacker’s favour, and the attack needs no special software on the victim’s machine. A single overlooked input field can be enough to compromise every user of a site.
It is also cheap to attempt. The attacker needs only a browser and a place to type, and automated scanners probe millions of sites for unprotected input fields every day. Where a flaw exists, it is usually found quickly, and the same payload can be reused against any site with the same weakness.
The limits, for the attacker, come from modern browser defences. Browsers now block some of the oldest tricks, and sites that set a content security policy can stop injected scripts from running or from contacting outside servers. Cookies marked HttpOnly cannot be read by JavaScript at all, which closes off the simplest form of session theft.
For defenders, the limit is that XSS is a design problem, not a single bug. Every place a site displays user input is a potential hole, including places developers forget, such as error messages and file names. Fixing it means treating all user input as data by default, which takes discipline across a whole codebase. Our guide to SSL certificates explains the encryption that protects data in transit, which is a different layer of defence: it keeps outsiders from reading traffic, but it does nothing against a script the site itself delivers.
XSS checklist
- Encode all user input when it is displayed, so browsers treat it as text, not code.
- Validate input on the server, allowing only the characters each field genuinely needs.
- Set a content security policy that limits which scripts a page may run.
- Mark session cookies HttpOnly so JavaScript cannot read them.
- Use modern frameworks that escape output by default, and do not bypass them.
- Never insert untrusted data into a page with raw HTML methods.
- Test stored fields, reflected fields, and DOM sinks, not just forms.
- Keep libraries and frameworks updated so known flaws are patched.
- Log and review unexpected script errors, which can reveal probing.
Frequently asked questions
What is XSS?
XSS, or cross-site scripting, is a flaw in which an attacker gets a trusted website to include malicious code in pages it sends to other users. The victim’s browser runs the code with the site’s own privileges.
What is the difference between stored and reflected XSS?
Stored XSS saves the malicious script in the site’s database, so it runs for every visitor to the page. Reflected XSS echoes the script back in an immediate response, so it only runs for people who click a crafted link.
What is DOM-based XSS?
DOM-based XSS happens entirely in the victim’s browser, when a page’s own JavaScript takes untrusted data and writes it into the page without checking it. The malicious payload never passes through the server.
Can XSS steal my password?
It can steal a session cookie, which lets an attacker use your logged-in session without your password. It can also fake a login form inside the trusted site to trick you into typing your password directly.
Does HTTPS protect me from XSS?
No. HTTPS encrypts traffic between you and the site, so outsiders cannot read or alter it. But XSS comes from the site itself, inside that encrypted connection, so the browser trusts it completely.
How do developers prevent XSS?
By encoding output so user input is displayed as text, validating input on the server, setting a content security policy, and marking session cookies HttpOnly. Modern frameworks escape output by default, which removes most of the risk.
The bottom line
XSS explained in one line is this: an attacker smuggles code into a website you trust, and your browser runs it, because it cannot tell the attacker’s script from the site’s own.
Defending against it means treating every piece of user input as data until proven otherwise, encoding it on output, and layering policies and cookie protections so that even a slipped-through script finds little to steal. Trust is the web’s foundation, and XSS is what happens when a site lends it to the wrong text.
This article is general information about how cross-site scripting works, not advice about securing a particular application.







