Skip to main content
XsiSec.com
HomeReposBlogProjectsPortfolio
© 2026 XsiSec.com
Security rules |security.txt
Updated 2026-11-17 · v1.0.0+2026-11-17.1cb4a2d · 1cb4a2d
← Back to overview
Security article

Reflected XSS into a JavaScript string with single quote and backslash escaped

Reflected XSS where input lands inside a JavaScript string with single quotes and backslashes escaped. Since < and > aren't filtered, we close the <script> tag with </script> and inject a fresh script to call alert().

2026-11-181 tag
Tags

Lab: Reflected XSS into a JavaScript string with single quote and backslash escaped

PRACTITIONER

This lab has a reflected XSS vulnerability in the search query tracking. The reflection lands inside a JavaScript string where both single quotes and backslashes are escaped. To solve it I had to break out of the JavaScript string and call alert.


What's actually going on

My input doesn't go into raw HTML here, it goes inside a JavaScript string in a <script> block. So the usual <img onerror=...> payloads don't work. The value I control sits between two single quotes.

The app escapes a few things:

  • Single quotes ' → escaped
  • Backslashes \ → escaped
  • Angle brackets < and > → not touched

So closing the string with a ' is a dead end. The server turns my ' into \' and the string holds.

But I don't need to break out of the string. I can break out of the whole <script> block instead.


Step 1 — Find the reflection

I searched for test123 first to see where it came back. It lands straight inside the tracking script:

html
<script>
    var searchTerms = 'test123';
    document.write('<img src="/resources/images/tracker.gif?searchTerms='+encodeURIComponent(searchTerms)+'">');
</script>

So the search term goes right into var searchTerms = '...'.

Then I searched for a single ' to see how it reacts:

html
var searchTerms = '\'';

There's the backslash. The quote got escaped, so I can't close the string that way. Same thing with a backslash. No quick win.


Step 2 — Check if angle brackets survive

Next I searched for <test> to see if < and > get filtered:

html
var searchTerms = '<test>';

Came back untouched. That's what I needed. If the browser's HTML parser sees </script>, it closes the script block right there — it doesn't care that it's technically inside a string literal. The HTML parser runs before the JavaScript, so </script> inside the string still ends the script element.


Step 3 — The payload

Since the angle brackets pass through, I close the current script tag and open a new one with my own code:

html
</script><script>alert("test")</script>

Reflected, the response becomes:

html
<script>
    var searchTerms = '</script><script>alert("test")</script>';
    document.write('<img src="/resources/images/tracker.gif?searchTerms='+encodeURIComponent(searchTerms)+'">');
</script>

The browser reads it like this:

  1. <script> ... var searchTerms = ' — first script block, now unterminated, browser doesn't care
  2. </script> — closes that first block early
  3. <script>alert("test")</script> — a fresh block that runs alert("test")

I used double quotes inside alert("test") on purpose. Single quotes would get escaped by the backend, double quotes are left alone.


Why it works

The app assumes that escaping quotes is enough to keep the input trapped inside the JavaScript string. It isn't. Escaping ' and \ stops me from breaking the string, but it does nothing about breaking the HTML element. Because the browser parses HTML first, </script> means "end of script" wherever it shows up, even in the middle of a string. Once the original script tag is closed, I can inject whatever I want after it.


Solving it

Drop the payload in the search box (or the search parameter directly):

text
</script><script>alert("test")</script>

The alert fires and the lab is solved.


Summary

  1. My input lands inside a JS string, and quotes and backslashes are escaped, so breaking out of the string doesn't work.
  2. But < and > aren't filtered, so I close the whole <script> element with </script>.
  3. Then I open a fresh <script>alert("test")</script> right after it and get code execution.

Comments

Comments

Loading comments…

Navigate

In this post

  1. 01Lab: Reflected XSS into a JavaScript string with single quote and backslash escaped
  2. 02What's actually going on
  3. 03Step 1 — Find the reflection
  4. 04Step 2 — Check if angle brackets survive
  5. 05Step 3 — The payload
  6. 06Why it works
  7. 07Solving it
  8. 08Summary
Search
Explore

Popular tags

Browse all 30 tags