Short Answer Cross-site scripting (XSS) is a web attack where someone hides a harmful script, a small piece of code, inside a trusted website. When visitors open that page, their browser, the app they use to open websites, runs the script as if the website wrote it. The script can then take over their signed-in account, show a fake sign-in box or act as that person on the site. Think of a fake notice pinned on a trusted office notice board that tells staff to hand over their ID cards.
What is Cross-Site Scripting?
In cyber security, cross-site scripting is a common weakness in websites and web apps, such as online shops and booking sites. It happens when a site takes text from a user, such as a comment or a search word. The site then shows that text on a page without checking it first. If that text hides a small program, the visitor’s browser runs it as if the website itself wrote it.
In simple words, the attacker uses a website you already trust as a delivery van. Your own computer is never broken into. Your browser trusts the site, so it also trusts the harmful script that came with it. As a result, the attacker can act as you on that site while you are signed in.
“Scripting” means running a script, a short piece of code, usually JavaScript. “Cross-site” means the script comes from an outsider but runs as part of someone else’s site. Security teams shorten it to XSS.
OWASP is a nonprofit project that publishes web security guidance. Its Top 10:2025 list of the most serious web app risks puts cross-site scripting in the Injection group. Injection means sneaking harmful commands into a site through normal text boxes.
Key Words to Know
- Script: a small program that a web page runs inside your browser, for example to open a menu or check a form.
- User input: anything a visitor types or sends to a site, like a review, a name or a search word.
- Session cookie: a small piece of data your browser keeps so a site knows you are signed in. Whoever holds it can often act as you without your password.
- Encoding (escaping): turning symbols that browsers read as code, such as angle brackets, into harmless text. The browser then shows them instead of running them.
- Sanitization: cleaning user input by removing anything that could act as code.
How Does Cross-Site Scripting Work?
Every XSS attack needs one weak spot: a page that shows user input as part of its own code. The attack then follows the same basic pattern.
- The site trusts input: a page shows what users type, such as reviews or search words, without encoding it.
- The attacker adds a script: they type text that hides a script, for example inside a product review.
- The site shows the script to others: the website saves the text or sends it straight back, now mixed into a normal page.
- The browser runs it: a visitor opens the page, and their browser treats the script as part of the trusted site.
- The attacker acts: the script can copy the session cookie, change what the page shows or do things on the site as the visitor.

An Example From an Online Shop’s Review Page
For example, imagine a shoe shop in Jaipur that sells online. Its product pages let buyers post reviews. But the review box shows whatever buyers type, exactly as typed. So an attacker posts a “review” that looks like normal praise but hides a script.
Next, a regular buyer opens that shoe page to read reviews. Their browser runs the hidden script without any warning. The script quietly sends the buyer’s session cookie to the attacker. Then the attacker uses it to open the buyer’s account and change the delivery details. The buyer only read a review on a site they trusted.
The fix for the shop is easy to understand. It should show every review as plain text, so a hidden script appears as harmless words on the screen.
Types of Cross-Site Scripting
Security teams sort cross-site scripting into three main types. The difference is where the harmful script lives and how it reaches the visitor.
- Stored XSS: the website saves the script, for example in a review or forum post, and serves it to every later visitor. One post can reach many people, because it stays on the page.
- Reflected XSS: the script travels inside a link. The site reads part of the link and “reflects” it straight back on the page. So the attacker must trick someone into clicking the link, often through phishing messages.
- DOM-based XSS: the flaw sits in the page’s own code that runs inside the browser. (DOM is the name for how the browser holds the parts of a page.) The page reads part of the web address and puts it on screen unsafely. The website’s main computer, called the server, may never see the script at all.
| Type | Where the Script Lives | Example in India |
|---|---|---|
| Stored | Saved on the website | A fake product review on a shop page that every buyer sees |
| Reflected | Inside a link | A link in a fake “order problem” SMS that opens the real shop’s search page |
| DOM-based | In the page’s own browser code | A travel portal’s welcome banner that shows a name taken from the web address |
Cross-Site Scripting Examples
XSS can appear anywhere a site shows text that came from a user. Here are a few made-up cases.
- A shop review page: as in the Jaipur example, a stored script in one review runs for every buyer who reads it.
- A search box that echoes text: a site shows “You searched for:” followed by the exact words. An attacker makes a special link that puts a script in those words, so the page runs it for whoever opens the link.
- A support chat window: a customer types a message that hides a script. Later, a support agent opens the chat in the company’s staff portal, and the script runs with the agent’s higher access.
- A staff profile page: an employee puts a script in the “About me” field of an internal HR portal. Then every colleague who views the profile runs it.
In each case, the website itself is the weak link. Even a fully updated phone or computer will still run the script.
What Can an Attacker Do With XSS?
Once the script runs, it can do almost anything the visitor could do on that page:
- Steal sessions: copy the session cookie and take over the account. This often leads to account takeover.
- Show fake forms: place a fake sign-in or payment box on the real site, so people type passwords into it.
- Act as the user: change an email address, post messages or place orders in the visitor’s name.
- Change the page: show false prices, false news or a link to a harmful download.
- Send visitors away: redirect them to a look-alike site run by the attacker.
Cross-Site Scripting vs SQL Injection
Both attacks start the same way: a site trusts what a user types. That is why security lists group both as injection attacks. However, they hit different targets. SQL injection tricks the website’s database into running commands. Cross-site scripting tricks the visitor’s browser into running a script instead.
| Question | Cross-Site Scripting (XSS) | SQL Injection |
|---|---|---|
| What is attacked | The visitor’s browser | The website’s database |
| Who gets hurt first | Visitors and their accounts | The company and all its stored data |
| What is often stolen | Session cookies, typed passwords | Customer records, often in large amounts |
| Main fix | Encode output before showing it | Keep typed text apart from database commands (parameterized queries) |
In short, SQL injection usually hurts the company’s data directly, while XSS hurts people one browser at a time. Both are serious, and the same app can have both flaws.
Ways to Prevent Cross-Site Scripting
No single fix stops every XSS flaw, so security experts advise several layers: fixes in the website’s code first, then safety nets in case one check is missed.
Encode Output and Check Input
- Output encoding: before a page shows user text, the code turns special characters into harmless text. So a hidden script appears on screen as plain words and never runs. This is the main defense.
- Input validation: the site accepts only what makes sense. For example, a PIN code field takes six digits and nothing else.
- Sanitization for formatted text: if users may add bold text or links, a well-kept cleaning tool removes anything that could act as code.
- Modern web frameworks: a framework is a ready-made base that developers build websites on. Most current ones encode output by default. Developers should not switch this safety off.
Browser Safety Layers
Two browser settings limit the damage if a script slips through.
- Content Security Policy (CSP): a rule the website sends to the browser, listing which scripts may run. As a result, the browser blocks scripts from unknown places. It is an extra layer on top of output encoding.
- HttpOnly cookies: a setting on the session cookie that stops page scripts from reading it. So even if a script runs, it cannot simply copy the cookie.

Testing and Filtering
Next, test before attackers do. Code reviews, automatic scanning tools and penetration testing find XSS flaws early. They are a key part of application security. Also, fix each flaw you find quickly, starting with pages that show user content.
Finally, a web application firewall can filter many known attack patterns before they reach the site. For example, AWS WAF can check web requests for XSS. Azure Web Application Firewall also has a cross-site scripting rule group in its default rule set. However, a filter cannot catch every case, especially DOM-based XSS, so the website’s code still has to be fixed.
Why Cross-Site Scripting Matters for Businesses
For a business, cross-site scripting is mostly a customer trust problem. Attackers use your own website against your own customers. So when accounts get taken over, customers blame your brand, because they never see the attacker.
- Account takeover: stolen accounts let attackers place orders, spend money kept in the site’s wallet or change delivery addresses.
- Staff access: a script that runs in an admin or support portal can reach far more data than a normal account.
- Data protection duty: India’s Digital Personal Data Protection Act, 2023 requires organizations that handle personal data to take reasonable security safeguards to prevent a personal data breach. An XSS flaw that leaks customer details can fall under that duty.
- Repair work: fixing one weak page is quick. Finding every other page with the same mistake takes planned testing.
If you run an online shop, ask your web developer or agency whether every box that shows customer text, such as reviews and search, displays it as plain text. Also ask when the site was last tested for XSS. Keep any add-ons (plugins) updated, because a weak add-on can bring in this flaw.
The good news is that XSS is well understood. Teams that encode output, use safe cookies and test regularly can close most of these gaps.
Frequently Asked Questions About Cross-Site Scripting (XSS)
What is cross-site scripting in simple words?
Is cross-site scripting a virus?
What is a cross-site scripting vulnerability?
Why is it called cross-site scripting?
What happens in a cross-site scripting attack?
Can a firewall alone stop XSS attacks?
Can XSS steal passwords?
References
- Definition, types and effects of XSS: OWASP, Cross Site Scripting (XSS)
- XSS in the Injection group: OWASP Top 10:2025, A05 Injection
- Duty to protect personal data: MeitY, The Digital Personal Data Protection Act, 2023
Get the Latest Terms & Insights.
New encyclopedia terms, threat intelligence, and architecture decisions, by email.
