Skip to content
XSS Write-up Beginner

DOM XSS in innerHTML Sink (location.search → innerHTML)

The blog's search feature takes user input from the URL's query string and inserts it directly into the page as HTML using innerHTML. An attacker can craft a link that injects an SVG element with an onload handler, causing JavaScript to run in the victim's browser.

medium.com

Published on medium.com

This PortSwigger lab contains a DOM-based XSS vulnerability inside the blog’s search feature. The application takes user input from location.search and directly injects it into HTML using innerHTML, which immediately makes it exploitable.

Aditya Bhatt, on medium.com

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

The application reads user input from location.search and assigns it to innerHTML without sanitisation, so the browser parses it as HTML and executes event handlers.

Impact

An attacker could execute arbitrary JavaScript in a victim's browser, potentially stealing tokens or injecting fake forms for phishing.

What the attacker needed

Victim clicks a crafted link

The fix

The application should avoid innerHTML and use textContent or safe DOM APIs, or sanitise the HTML with a library such as DOMPurify.

How it was found

  1. Open the lab and inspect how the search box behaves.
  2. Enter a harmless test string in the search box and submit it.
  3. Inspect the page's HTML to see the search term inserted via innerHTML.
  4. Inject a payload that breaks out of the existing HTML and adds an SVG element with an onload handler.
  5. Observe the alert firing, confirming the DOM XSS.

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 text from the address bar into the page without cleaning it, a malicious link can make the browser run code. Here, the search term is placed into the page as if it were part of the website's own code, so an attacker can add a tag that runs JavaScript when the page loads.

What is XSS?

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.