Skip to content
XSS Write-up Intermediate

DOM XSS Exploit: Using postMessage and JSON.parse in iframe Attac

A web page listens for postMessage events and parses the data as JSON without checking the sender's origin. An attacker can send a crafted message that sets an iframe's src to a javascript: URL, leading to DOM XSS and arbitrary JavaScript execution.

Bash Overflow Follow

Published on infosecwriteups.com

This vulnerability involves DOM-based Cross-Site Scripting (DOM XSS) that occurs when a web page uses the postMessage API to receive data from another window (such as an iframe or popup), and parses the message usingJSON.parse() without properly validating the source of the message or its content.

Bash Overflow, on infosecwriteups.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 page does not verify the origin of incoming postMessage events and fails to sanitise the URL before using it as an iframe source.

Impact

An attacker could execute arbitrary JavaScript to steal sensitive user information such as cookies, tokens, and session IDs, and redirect users to malicious sites.

What the attacker needed

Victim visits attacker-controlled page; vulnerable page loaded in iframe

The fix

The page should verify the origin of incoming messages and validate or sanitise the URL before using it as an iframe source.

How it was found

  1. Open the lab and navigate to the main page.
  2. Scroll down to find the message event listener that uses JSON.parse on incoming data.
  3. Observe that if the parsed object's type is 'load-channel', the script sets an iframe's src to the url field.
  4. Go to the exploit server and create a payload that sends a postMessage to the vulnerable page.
  5. Test with a simple alert payload to confirm JavaScript execution.
  6. Replace with a payload that calls print() and deliver to the 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.

This is a DOM-based cross-site scripting (XSS) bug. The page accepts messages from any other window (like an iframe) and uses the data to set a link without checking who sent it. An attacker can trick the page into running harmful JavaScript, which could steal cookies or redirect the user.

What is XSS?

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

More by Bash Overflow

View all

35 more disclosed reports, best first.

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.