Skip to content
XSS Write-up Intermediate

A small error destroyed the protection against XSS vulnerability.

User input reflected inside a script tag was escaped for JavaScript special characters but left angle brackets untouched, so the closing script tag could be injected. An attacker could break out of the script context and run their own JavaScript in the victim's browser.

omarelza3im44 Follow

Published on medium.com

Cybersecurity

Omar Zaky, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

This is a short account of an XSS finding made while hunting on an application whose name, product and feature the report does not identify. What the report does describe is the behaviour of the reflection: the same user input was reflected in several places, most of them inside HTML, and one of them inside a JavaScript context. The interesting part is not the payload but the difference in how each context was handled.

Setup

The report gives no details about accounts, roles, tools, versions or preconditions. It only states that the researcher was hunting for XSS and that the input was reflected in several locations in the application.

Finding it

Most of the reflections sat inside HTML, and there the application was properly encoding the input. That is the expected behaviour and nothing looked wrong.

One reflection, however, landed inside a JavaScript context. In that location the application did not HTML-encode the input. Instead it escaped special characters by prefixing them with a backslash (\).

The researcher first tried to defeat that escaping by escaping the backslash itself, but the application escaped that character too, so the approach failed. The escaping logic held for the characters it was designed to cover.

Then came the detail that mattered: although the JavaScript characters were being escaped, < and > were still reflected unchanged.

Exploitation

Step 1: look at the context rather than the string

Instead of continuing to attack the JavaScript string and trying to break its logic, the researcher looked at the surrounding context. The input was inside a <script> tag, and the characters needed to close that tag were not being encoded. That opened a different route: leave the script context entirely instead of escaping the string.

Step 2: break out of the script tag

The payload that was accepted is shown in the report as an image:

the accepted payload From the original on medium.com

the accepted payload

The report shows the payload only as an image, so the exact characters are not reproduced here. What the report does explain is the mechanism: the browser interpreted the first </script> as the end of the original script, then parsed the injected <script> and executed the JavaScript inside it.

In other words, the payload did not need to survive the backslash escaping at all. It only needed the two characters that were not being encoded, < and >, to close the existing script element and open a new one.

Root cause

The application had security controls, and from a quick look it could easily seem secure: HTML reflections were encoded, and the JavaScript reflection was escaping special characters. The mistake was that the JavaScript context escaped special characters, but < and > were still reflected as they were. Because the reflection sat inside a <script> element, those two characters were enough to terminate the script block and start a new one, so the escaping of the string contents became irrelevant.

In short, the defence was chosen for the string context and not for the surrounding HTML context. The report does not show the server-side code, so the exact implementation of the escaping routine is not known.

Impact

The report does not describe the affected users, the data involved or any exploitation beyond the proof that the injected script executed. What it does establish is that script execution was possible through the reflected input, which is the definition of reflected XSS. The practical consequences would depend on who visits the crafted URL and what the application allows in the victim's session, none of which the report covers.

Fix

The lesson it draws is about the approach: understand where the input is reflected, how it is transformed, what context it sits in, and how the browser will parse it. The report draws no fix from this; its lesson is to understand where input is reflected, how it is transformed, what context it sits in, and how the browser will parse it.

Variants to test

  1. Reflection inside a <script> block. Send a value containing </script> followed by a new script element and check whether the response leaves < and > untouched while escaping quotes and backslashes.
  2. Escaping that covers quotes but not angle brackets. Try the same input in any context where the application uses backslash escaping rather than encoding.
  3. Multiple reflections of the same parameter. Submit one value and inspect every place it appears. A positive result is one reflection that is handled differently from the others, as happened here where the HTML reflections were encoded and the JavaScript reflection was escaped.
  4. Backslash escaping itself. Try to escape the backslash (\\) to see whether the escaping routine can be unbalanced.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 9 min read.

What happened, in plain words

A security researcher was testing a website for a common weakness called cross-site scripting, usually shortened to XSS. The idea behind XSS is simple: if a website takes something a visitor types and later shows it back on a page without handling it carefully, an attacker can type something that the browser treats as code rather than as text. When another person opens the page, that code runs in their browser, as if the website itself had written it.

The researcher typed a value into the application and looked at every place where that value came back on the page. Most of those places were ordinary HTML, the language used to build web pages, and there the application handled the value safely. One place was different: the value came back inside a block of JavaScript, the programming language that browsers run. There the application used a different technique to make the value safe, and that technique had a gap. The researcher found the gap and described how a script could be made to run.

The report does not name the application, the product or the feature being tested. It also does not describe accounts, tools or versions. What it does describe, in detail, is how the input was handled in each place it appeared, and that is the part worth understanding.

The words you need first

Before walking through the steps, here are the ideas the rest of the article relies on.

  • HTML is the language that describes what is on a web page: headings, paragraphs, links, images. It is written with tags, which are instructions wrapped in angle brackets, like <p> for a paragraph. Most tags come in pairs: an opening tag such as <p> and a closing tag such as </p>.
  • A browser is the program you use to visit websites. Its job is to read the HTML it receives and turn it into the page you see. It also runs any JavaScript it finds.
  • JavaScript is a programming language that runs inside the browser. It makes pages interactive: buttons that respond, menus that open, forms that check what you typed. On a page, JavaScript is usually placed inside a <script> tag.
  • A reflection is when a website takes a value you sent and writes it back into the page it returns. A search box that shows "results for: your words" is reflecting your words. Reflections are everywhere and are not a problem by themselves.
  • Encoding (or escaping) is how a website makes a reflected value safe. The basic idea is to replace characters that have a special meaning with a harmless stand-in, so the browser shows them as text instead of acting on them. Think of writing a shopping list and putting quotation marks around a word so nobody mistakes it for an instruction: the word is still there, but it is clearly just a word.
  • XSS (cross-site scripting) is the weakness that appears when that safety step is missing or incomplete. The attacker's text is treated as code, and the browser runs it.
  • A payload is the specific piece of text an attacker sends to try to trigger the weakness. It is not a magic string; it is whatever fits the situation.

The setup

The report gives no details about accounts, roles, tools, versions or preconditions. It only says that the researcher was hunting for XSS and that the input was reflected in several locations in the application.

From the original on medium.com

Finding it

Most of the reflections sat inside HTML, and there the application was properly encoding the input. That is the expected behaviour and nothing looked wrong. If you type <b>hello</b> into a well-built search box, the page should show you the literal text <b>hello</b>, not a bold word, because the angle brackets have been turned into harmless stand-ins.

One reflection, however, landed inside a JavaScript context. In that location the application did not HTML-encode the input. Instead it escaped special characters by prefixing them with a backslash (\).

A backslash is the character \. In many programming languages it is used as an escape character: put it in front of a special character and that character loses its special meaning and becomes ordinary text. If a quote normally ends a piece of text, writing \" tells the language "this is just a quote character, not the end". The application was using that trick on the researcher's input.

The researcher first tried to defeat that escaping by escaping the backslash itself, but the application escaped that character too, so the approach failed. The reasoning behind the attempt is worth spelling out: if the application adds a backslash before every special character, then sending a backslash of your own might produce two backslashes in a row, and the second one might cancel out the protection on the character that follows. The application was one step ahead and escaped the backslash as well, so the escaping logic held for the characters it was designed to cover.

Then came the detail that mattered: although the JavaScript characters were being escaped, < and > were still reflected unchanged.

Exploitation

Step 1: look at the context rather than the string

Instead of continuing to attack the JavaScript string and trying to break its logic, the researcher looked at the surrounding context. The input was inside a <script> tag, and the characters needed to close that tag were not being encoded. That opened a different route: leave the script context entirely instead of escaping the string.

This is the key shift in thinking. The escaping was doing its job on the characters inside the JavaScript text. But the input was not only JavaScript text: it was sitting inside an HTML element, and HTML has its own rules. The < and > characters belong to those HTML rules, and they were passing through untouched. If you can send </script>, the browser reads it as the end of the script block, no matter what the JavaScript inside was supposed to do.

A comparison: imagine a letter where every word is carefully checked, but the envelope is not. You can write anything you like inside, and it will be inspected, but if you can also write on the outside of the envelope, you can address it somewhere else entirely. The escaping was guarding the letter; nobody was guarding the envelope.

Step 2: break out of the script tag

The payload that was accepted is shown in the report as an image:

the accepted payload From the original on medium.com

the accepted payload

The report shows the payload only as an image, so the exact characters are not reproduced here. What the report does explain is the mechanism: the browser interpreted the first </script> as the end of the original script, then parsed the injected <script> and executed the JavaScript inside it.

In plain words, the browser reads a page from top to bottom and reacts to tags as it meets them. When it sees </script>, it stops treating what follows as JavaScript and goes back to reading HTML. When it then meets a fresh <script> tag, it starts running JavaScript again, and this time the JavaScript is whatever the attacker put there. The original script block is closed early, and a new one is opened under the attacker's control.

In other words, the payload did not need to survive the backslash escaping at all. It only needed the two characters that were not being encoded, < and >, to close the existing script element and open a new one.

Root cause

The application had security controls, and from a quick look it could easily seem secure: HTML reflections were encoded, and the JavaScript reflection was escaping special characters. The mistake was that the JavaScript context escaped special characters, but < and > were still reflected as they were. Because the reflection sat inside a <script> element, those two characters were enough to terminate the script block and start a new one, so the escaping of the string contents became irrelevant.

In short, the defence was chosen for the string context and not for the surrounding HTML context. The report does not show the server-side code, so the exact implementation of the escaping routine is not known.

Impact

What it does establish is that script execution was possible through the reflected input, which is the definition of reflected XSS. The practical consequences would depend on who visits the crafted URL and what the application allows in the victim's session, none of which the report covers.

Fix

The lesson it draws is about the approach: understand where the input is reflected, how it is transformed, what context it sits in, and how the browser will parse it. The report draws no fix from this; its lesson is to understand where input is reflected, how it is transformed, what context it sits in, and how the browser will parse it.

Variants to test

These are the same ideas the researcher used, written as things you could try on an application you are allowed to test.

  1. Reflection inside a <script> block. Send a value containing </script> followed by a new script element and check whether the response leaves < and > untouched while escaping quotes and backslashes.
  2. Escaping that covers quotes but not angle brackets. Try the same input in any context where the application uses backslash escaping rather than encoding.
  3. Multiple reflections of the same parameter. Submit one value and inspect every place it appears. A positive result is one reflection that is handled differently from the others, as happened here where the HTML reflections were encoded and the JavaScript reflection was escaped.
  4. Backslash escaping itself. Try to escape the backslash (\\) to see whether the escaping routine can be unbalanced.

The main lesson

The researcher's own summary is that XSS hunting should not be just trying one payload after another until something works. It is more useful to ask, in order: where is my input reflected, how is it transformed, what context is it in, and how will the browser parse it. Once those questions are answered, the payload is often the easy part. Understand the application first; do not just throw payloads at it.

Glossary

  • HTML: the language that describes the structure of a web page, built from tags in angle brackets such as <p> and </p>.
  • Tag: an instruction in HTML, written between < and >. Most tags have an opening and a closing form.
  • Browser: the program used to visit websites, which reads HTML and runs JavaScript.
  • JavaScript: the programming language that runs in the browser and makes pages interactive.
  • <script> tag: the HTML element that marks a block of JavaScript for the browser to run.
  • Reflection: when a website writes a value you sent back into the page it returns.
  • Encoding: replacing characters that have a special meaning with harmless stand-ins so they are shown as text.
  • Escaping: putting a special marker, here a backslash, in front of a character so it loses its special meaning.
  • Backslash (\): the character used as the escape marker in many programming languages.
  • XSS (cross-site scripting): a weakness where input from an attacker is treated as code by a victim's browser and runs there.
  • Reflected XSS: XSS where the malicious value is sent in a request and immediately written back into the response page.
  • Payload: the specific piece of text sent to try to trigger a weakness.
  • Context: the surroundings in which a value appears on a page, such as inside HTML text or inside a JavaScript block, which decides how the browser will interpret it.
  • Parameter: a named value sent with a request, for example the text typed into a search box.
  • Session: the period during which a website recognises a visitor as the same person across requests.
  • Root cause: the underlying mistake that makes a weakness possible, as opposed to its visible symptom.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.