What is Cross-Site Scripting (XSS)?
Types, Examples and Prevention

Cross-site scripting (XSS) is a web attack where someone hides a harmful script, a small piece of code, in a trusted website, such as in a product review. When visitors open that page, their browser runs it. The script can then take over their account or show a fake sign-in box.

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.

  1. The site trusts input: a page shows what users type, such as reviews or search words, without encoding it.
  2. The attacker adds a script: they type text that hides a script, for example inside a product review.
  3. The site shows the script to others: the website saves the text or sends it straight back, now mixed into a normal page.
  4. The browser runs it: a visitor opens the page, and their browser treats the script as part of the trusted site.
  5. The attacker acts: the script can copy the session cookie, change what the page shows or do things on the site as the visitor.
A fake shop review carries a hidden script through the website into a shopper's browser, which leaks the login session
The shopper only reads a review, yet the trusted page runs the attacker’s script in their browser.

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.
Talk to an ExpertCould a comment box on your website run someone else’s code? Talk to our team about testing your web apps.

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.
Two pages compared: one runs typed text as code, the other encodes it so the browser shows only plain words
Encoding turns a hidden script into harmless text, and browser rules add a second safety net.

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?

Cross-site scripting means an attacker hides a small harmful program inside a trusted website. When you open that page, your browser runs the program, because it thinks the website wrote it. The program can then take over your signed-in account or show you a fake sign-in box. You do nothing wrong. The website simply forgot to check what other users typed.

Is cross-site scripting a virus?

No. A virus is a type of malware that copies itself onto computers and spreads from file to file. Cross-site scripting is a flaw in a website. The harmful script runs only inside your browser, while you have that page open. It usually does not install anything. However, an attacker can use it to send you to a harmful download.

What is a cross-site scripting vulnerability?

A cross-site scripting vulnerability is a weak spot in a website that lets outside text run as code. It appears when a page shows user input, such as a review, a name or a search word, without first turning it into plain text. Security testers look for these spots in every form, link and page that shows text supplied by users.

Why is it called cross-site scripting?

The name describes what happens. A script, which is a small program, comes from an outside person but runs as part of another website. So it crosses from the attacker into a trusted site. The short form is XSS. The name stayed even as new kinds of the attack appeared, such as one that happens fully inside the browser.

What happens in a cross-site scripting attack?

First, the attacker finds a page that shows user text without turning it into plain text. Then they place a script there, for example in a comment, or inside a link. Next, a visitor opens the page and their browser runs the script. Finally, the script takes over the visitor’s account, changes the page or does things on the site in that visitor’s name.

Can a firewall alone stop XSS attacks?

No. A web application firewall can block many known attack patterns, and it buys time while developers fix a flaw. However, attackers keep changing their tricks, and some XSS happens entirely inside the browser, where a firewall never sees it. So the real fix stays in the website’s code: encode output, check input and test every page that shows user text.

Can XSS steal passwords?

Yes, it can. A script can place a fake sign-in box on the real website, so visitors type their password into it without any doubt. It can also copy the small piece of data that keeps them signed in, so the attacker can skip the password. A second sign-in step, such as a phone code, limits this damage, but fixing the flaw matters most.
Use CaseFind and Fix Security Vulnerabilities

References

  1. Definition, types and effects of XSS: OWASP, Cross Site Scripting (XSS)
  2. XSS in the Injection group: OWASP Top 10:2025, A05 Injection
  3. Duty to protect personal data: MeitY, The Digital Personal Data Protection Act, 2023
Stay Updated

Get the Latest Terms & Insights.

New encyclopedia terms, threat intelligence, and architecture decisions, by email.

Book a Consultation

A practice principal will contact you within 1 business day.

Thank you! A Signisys principal will be in touch within 1 business day.