DOM-XSS via postMessage API

Vulnerable Receiver Example

  • Listens for messages with addEventListener('message')

  • Updates the paragraph content with the message without sanitization

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Receiver</title>
</head>
<body>
    <p>Message will appear here</p>
    <script>
        // Listen for incoming messages
        window.addEventListener('message', (event) => {
            // Validate the source and process data
            document.querySelector('p').textContent = event.data;
        });
    </script>
</body>
</html>

Messages identification

  • Hook messages in your web application copying the following JS code in your Browser console:

  • Analyze the postMessage IN messages identified by the previous Javascript code to understand which data are processed by the Web page listener

Exploitation

  • Create the attacker web page with the XSS in the message of sent using the postMessage function. To do it, open the target URL and perform postMessage with 5 seconds delay, to be sure that the web application loads completely and starts all the workers

  • The user visits the attacker web page and the target site will be opened by the script, allowing the execution of the DOM XSS

Last updated