DOM XSS via postMessage: JavaScript Injection Inside Iframes
The vulnerable page listens for postMessage events and trusts the message data without validating the origin or sanitising it, then assigns it to location.href. An attacker can send a message containing a javascript: URL to execute arbitrary JavaScript in the context of the vulnerable application.
medium.com
Published on medium.com
This lab showcases a DOM-based cross-site scripting (DOM XSS) vulnerability triggered through postMessage web messaging combined with a JavaScript URL. The vulnerable page listens for messages from other windows using window.addEventListener('message', ...), and mistakenly trusts the content of the message without…
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 page used the message data directly in location.href without validating the sender's origin or sanitising the value, allowing a javascript: URL to be executed.
Impact
An attacker could execute arbitrary JavaScript in the context of the vulnerable application, which could lead to session hijacking, sensitive data theft, and redirection to malicious websites.
What the attacker needed
Victim visits attacker-controlled page; vulnerable page loaded in iframe
The fix
The page should validate the origin of incoming messages and avoid assigning untrusted data to location.href or other dangerous sinks.
How it was found
- Open the lab and observe the HTTP history in Burp Suite, specifically the request to the main page.
- In the response, notice a JavaScript function using addEventListener() that listens for web messages and checks for 'http:' or 'https:' before assigning to location.href.
- Craft a message using an HTML iframe on the exploit server to bypass origin validation and trigger a redirection.
- Click the 'Go to exploit server' button.
- Add the payload inside the request body, click 'Store', then 'View exploit' to confirm execution.
- Modify the payload to call print(), click 'Store' again, then 'Deliver exploit to victim'.
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.
A web page can receive messages from other windows using a feature called postMessage. This page took whatever message it received and used it as a web address to navigate to, without checking who sent it. Because the message could start with 'javascript:', the page would run that code as if it were part of the site, letting an attacker do things like steal session data.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.