Breaking Out of the String: Solving Reflected XSS in JavaScript
A reflected cross-site scripting vulnerability existed because user input was placed directly inside a JavaScript string without proper escaping. An attacker could break out of the string and execute arbitrary JavaScript in the victim's browser.
medium.com
Published on medium.com
This PortSwigger lab hides a Reflected XSS vulnerability inside a search tracking script.
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.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the application inserted user input into a JavaScript string without escaping it for that context, so the input could break out of the string.
Impact
An attacker could execute arbitrary JavaScript in a victim's browser, potentially stealing data or performing actions on behalf of the user.
What the attacker needed
Victim clicks a crafted link
The fix
The application should avoid concatenating user input into JavaScript strings and instead escape it for the correct context, and a Content Security Policy should be enforced.
How it was found
- Enter a random search term on the site and intercept the request with Burp Proxy.
- Send the request to Repeater and inspect the response to see where the input appears.
- Craft a payload that closes the string, injects code, and comments out the rest.
- Forward the payload through Burp and reload the page to confirm the alert executes.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
When a website puts something you typed directly into its code without cleaning it, an attacker can type special characters that trick the browser into running their own code. Here, the input went inside a JavaScript string, so the attacker could close the string and add their own JavaScript, which then runs in the victim's browser.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.