Skip to content
BENIBIX Learn

Cross-Site Scripting (XSS)

Development🔐 SecurityAttacks

Also known as XSS, XSS Attack, Script Injection, Stored XSS, Reflected XSS, DOM-based XSS

An attack that hides a script in a page. It runs for everyone who visits.

Think of it like

Think of it like a fake note pinned on a shop's board. Every shopper thinks it is real.

Example

Emma's blog shows comments as raw HTML. Someone posts a comment with a script tag, and it runs for every reader who opens the post.

Why it matters

A script on your page can do anything the user can. One comment box can expose every account.

Definition

Cross-site scripting gets a bad script to run on pages others visit. It happens when a site shows user text, like a comment, without making it safe first. The browser then treats the text as code. The script runs as if the site wrote it, so it can steal session cookies or act as the user. The fix is to escape all user text before showing it.

Example code

html
<p>Great post! <script>alert(1)</script></p>
If shown as raw HTML, the script runs for every reader.

Common mistake

Building HTML by joining strings with user text. Let the framework escape it, and never switch that off without care.

Key takeaways

  1. 1
    Text becomes code. In the browser.
  2. 2
    Runs as the site. Steals or acts as users.
  3. 3
    Escape on output. Every piece of user text.

Compare