DOM XSS in document.write sink using source inside a select element


The website in this lab contains a DOM-based cross-site scripting vulnerability in the stock checker functionality. It uses the JavaScript document.write function, which writes data out to the page. The document.write function is called with data from which you can control using the website URL. The data is enclosed within a select element.

Reproduction and proof of concept

  1. On the product pages, notice that the dangerous JavaScript extracts a storeId parameter from the source. It then uses document.write to create a new option in the select element for the stock checker functionality.

<form id="stockCheckForm" action="/product/stock" method="POST">
    <input required type="hidden" name="productId" value="4">
        var stores = ["London","Paris","Milan"];
        var store = (new URLSearchParams('storeId');
        document.write('<select name="storeId">');
        if(store) {
            document.write('<option selected>'+store+'</option>');
        for(var i=0;i<stores.length;i++) {
            if(stores[i] === store) {
    <button type="submit" class="button">Check stock</button>
  1. Add a storeId query parameter to the URL and enter a random alphanumeric string as its value. Request this modified URL.
  1. In the browser, the random string is now listed as one of the options in the drop-down list.

  2. Right-click and inspect the drop-down list to confirm that the value of the storeId parameter has been placed inside a select element.

  3. Create a suitable XSS payload inside the storeId parameter:<script>alert(0)</script>