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().
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:
<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:
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:
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:
</script><script>alert("test")</script>Reflected, the response becomes:
<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:
<script> ... var searchTerms = '— first script block, now unterminated, browser doesn't care</script>— closes that first block early<script>alert("test")</script>— a fresh block that runsalert("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):
</script><script>alert("test")</script>The alert fires and the lab is solved.
Summary
- My input lands inside a JS string, and quotes and backslashes are escaped, so breaking out of the string doesn't work.
- But
<and>aren't filtered, so I close the whole<script>element with</script>. - Then I open a fresh
<script>alert("test")</script>right after it and get code execution.
Comments
Comments
Loading comments…