PortSwigger

Description
This lab contains a DOM-based cross-site scripting vulnerability in the search query tracking functionality. It uses the JavaScript document.write function, which writes data out to the page. The document.write function is called with data from location.search, which you can control using the website URL.
Steps
Solution Steps
- Access the lab and locate the search box.
- First, test with a random string (eg.
haha) in the search box and submit it. - Rigth-click and inspect the element to see where your input appears. You should find it inside an
imgsrcattribute, like :
document.write('<img src="/resources/images/tracker.gif?searchTerms='+query+'">');

- Construct the payload to break out of the
srcattribute and inject JavaScript. Enter this as your search term:
"><svg onload=alert(1)>
-
Click “Search” or submit the query via the URL. The resulting HTML becomes:
<img src="/resources/images/tracker.gif?searchTerms="><svg onload=alert(1)>"> -
The alert fires, and the lab is marked as Solved
Solved

Why This Works
- The
document.writefunction writes user-controlledlocation.searchdata directly into the page. - The data is placed inside an
img srcattribute without proper escaping. - By injecting
">, you close the attribute and theimgtag. - The
<svg onload=alert(1)>injects a new element whoseonloadevent executes JavaScript.