> 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/xss/dom-xss-with-innerhtml-and-location-search.md).

# XSS DOM avec innerHTML et location.search

### XSS DOM dans le réceptacle InnerHTML en utilisant la source location.search

Vulnérabilité XSS DOM dans la fonctionnalité de recherche du blog. Le code récupère la valeur de `location.search` et l'insère directement dans le DOM via `innerHTML`, ce qui permet d'injecter et d'exécuter du JavaScript lorsque la valeur provient de l'URL.

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

**Extrait du code observé**

```javascript
function doSearchQuery(query) {
    document.getElementById('searchMessage').innerHTML = query;
}
var query = (new URLSearchParams(window.location.search)).get('search');
if(query) {
    doSearchQuery(query);
}
```

**Payloads testés**

* Payload simple (non interprété) : / `<script>alert("XSS")</script>`
* Test pour insérer un élément image : / `<img src="test"</img>`/ (ceci ne déclenche pas l'exécution du script car aucun attribut d'événement n'est présent)

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

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

Forcer une erreur de chargement pour déclencher un `onerror` gestionnaire : / `<img src=0 onerror=alert("XSS")>` / (Ce payload utilise l'attribut `onerror` d'événement pour exécuter `alerte` lorsque le chargement de l'image échoue)

```javascript
<img src=0 onerror=alert("xss")>
```

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

Le script affecte directement `innerHTML` avec le `paramètre de recherche` la valeur de l'URL ; par conséquent, des fragments HTML contenant des gestionnaires d'événements (par ex. `onerror`) ou des balises exécutables peuvent conduire à l'exécution de code. Le `<img src=0 onerror=alert("XSS")>` payload provoque une erreur de chargement et exécute `alert("XSS")`, démontrant la vulnérabilité XSS DOM.


---

# 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/xss/dom-xss-with-innerhtml-and-location-search.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.
