The comment section on your class blog works fine, until one morning everyone who opens a certain article sees a strange pop-up appear on its own. The server wasn't broken into. One comment contained code, and your page ran that code in every reader's browser.
That's what XSS (Cross-Site Scripting) means: text from a user gets read by the browser as code, meaning HTML or JavaScript. That code then runs in the browser of everyone else who opens the page, with the same rights as your site's own code. Picture an MC reading question cards from the audience. A good MC reads each card as it is: if a card says "turn off the lights", the MC reads that sentence out loud and does not walk over to the switch. Here the card is the text from a user, and the MC is your page. XSS is when your page becomes an MC who does what the card says, which means it runs the user's text as code.
Where the name comes from
XSS stands for Cross-Site Scripting: an attacker's script that gets into your site through data. The X is there so it isn't confused with CSS. The pattern is always the same: data from a user (a comment, a name, a search term, part of a URL) is mixed into HTML without being turned into plain text.
Example: a comment box
Suppose your class blog is called MyClass. MyClass's server builds the page by pasting the comment straight into the HTML:
// server: the comment is pasted raw into the HTML (vulnerable)
const comment = "<script>alert(1)</script>"; // sent by Budi through the form
const html = "<p>Comment: " + comment + "</p>";
res.send(html);
The browser that receives that page can't tell which part Budi wrote and which part you wrote. All the browser gets is this:
<p>Comment: <script>alert(1)</script></p>
- The HTML parser (the part of the browser that reads HTML and builds elements) meets
<script>. So the parser creates a script element and runs it right away. - The result: an alert box appears (a pop-up message box) in the browser of everyone who opens that page.
alert(1)is only a harmless proof. A real script in the same spot could read the page and send requests on behalf of the logged-in user.
The fix: show the comment as text. The next lesson, XSS Prevention, covers how in full. In browser JavaScript, the most basic tool is textContent.
textContent or innerHTML
There are two ways to put text into an element. The Lab uses both on the same comment box:
const box = document.getElementById("comment");
box.textContent = comment; // safe: always becomes text
box.innerHTML = comment; // vulnerable: read as HTML
textContentcreates one text node (one piece of plain text on the page). Every character, including<and>, shows as it is.innerHTMLhands the string to the HTML parser. Tags inside it become real elements, and attributes become real attributes.
Before you look at the table, guess: the comment <script>alert(1)</script> is put on the page with textContent. Does an alert box appear, the text as it is, or an empty box?
This is what the browser builds for the Lab's three comments:
| Comment | textContent |
innerHTML |
|---|---|---|
Hi, my name is Budi |
text, 19 characters | text, 19 characters |
<script>alert(1)</script> |
the exact text, 25 characters | a <script> element, but it does not run |
<img src=x onerror=alert(1)> |
the exact text, 28 characters | an <img> element: image x fails to load, onerror runs |
With textContent, what appears is the text <script>alert(1)</script> as it is, with no alert. That's the answer to the guess.
The second row of the table often surprises people: a script inserted through innerHTML is indeed not run by the browser. But that doesn't make innerHTML safe. The third row shows why: event attributes like onerror still run. An event attribute runs JavaScript when something happens, and onerror runs when the image x fails to load. So element.innerHTML = userInput is an XSS hole. For text from a user, use textContent.
A detector: an alarm, not a fence
In this lesson's exercise, you build detectXSS(input), a function that flags suspicious input. It returns true when the input contains one of these patterns, whether in upper or lower case:
<script- an event handler (an event attribute):
on, some letters, then=, such asonerror=,onload=,onclick= javascript:
function detectXSS(input) {
const lower = input.toLowerCase(); // <SCRIPT> becomes <script>
if (lower.includes("<script")) return true;
if (/on\w+\s*=/.test(lower)) return true; // onerror=, onload=, onclick=
if (lower.includes("javascript:")) return true;
return false;
}
console.log(detectXSS("<script>alert(1)</script>")); // true
console.log(detectXSS("Hi, my name is Budi")); // false
console.log(detectXSS("<img onerror=alert(1)>")); // true
toLowerCase()turns the text into lower case, so<SCRIPT>and<script>both get caught./on\w+\s*=/is a regex (a pattern for searching text). It means:on, one or more letters, an optional space, then=..test(lower)istruewhen the pattern is found inlower.includes("javascript:")catches links like<a href=javascript:alert(1)>click</a>.
A function like this is useful for flagging, such as writing to a log or asking an admin to review a comment. But it is not a defence, because a list of patterns can always miss something. The real defence is in how data is displayed, which means textContent and escaping (turning signs like < into plain text, covered in the next lesson).
⚠️ Wrong ideas about innerHTML and detectors
- Thinking
innerHTMLis safe because its<script>doesn't run.<img src=x onerror=alert(1)>still runsonerror. For text from a user, usetextContent. - Forgetting upper case.
<SCRIPT>alert(1)</SCRIPT>is a script tag too. Turn the text into lower case before looking for patterns. - Relying on a detector as the only fence. There is always a way of writing it that isn't on the list. A detector is only an alarm.
- Forgetting that a URL is user input too. A search term in
?q=and the part after#can't be trusted, the same as a form field.
TL;DR: XSS (Cross-Site Scripting) happens when text from a user is read by the browser as code, then runs in other people's browsers. A server that pastes <script>alert(1)</script> raw into the HTML makes an alert box (a pop-up) appear. element.innerHTML = userInput is an XSS hole: a <script> inserted through innerHTML doesn't run, but the onerror attribute on an <img> still runs when the image fails to load. textContent always makes plain text. detectXSS, the detector function from the exercise, looks for <script, on...= and javascript: in any case, and is useful as an alarm, not as a defence.
⚡ Text read as code
Write:
<p>Comment: <script>alert(1)</script></p>
Guess first: this is the HTML the server makes after pasting a comment in raw. What happens in the reader's browser?
An alert box (a pop-up) appears. The HTML parser, the part of the browser that reads HTML, meets <script>, creates a script element, and runs it right away.
Meaning: XSS (Cross-Site Scripting): text from a user is read by the browser as code and runs in the browser of everyone who opens that page.
⚡ textContent or innerHTML
Write:
box.textContent = comment; // safe: always becomes text
box.innerHTML = comment; // vulnerable: read as HTML
Meaning: box is the comment box on the page, and comment is the text from a user. textContent always makes one text node (a piece of plain text): < and > show as they are. innerHTML hands the string to the HTML parser, so tags inside it become real elements.
⚡ The script doesn't run, but it's still vulnerable
Meaning: A <script> inserted through innerHTML is indeed not run by the browser. But <img src=x onerror=alert(1)> still runs onerror, an attribute that runs when an image fails to load. There is no image x, so alert(1) runs. For text from a user, use textContent.
⚡ A detector is an alarm, not a fence
Write:
const lower = input.toLowerCase(); // <SCRIPT> becomes <script>
if (/on\w+\s*=/.test(lower)) return true; // onerror=, onload=, onclick=
Meaning: detectXSS is a function that detects suspicious input. It turns input into lower case, so upper or lower case makes no difference. Then it looks for <script, javascript:, and on...=. The second line looks for on...= with a regex (a pattern for searching text). The detector is useful for flagging, but it is not a defence: there's always a way of writing it that isn't on the list.