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

# DOM XSS con innerHTML y location.search

### XSS en el DOM en el sumidero InnerHTML usando la fuente location.search

Vulnerabilidad de XSS en el DOM en la función de búsqueda del blog. El código recupera el valor de `location.search` y lo inserta directamente en el DOM mediante `innerHTML`, lo que permite que JavaScript se inyecte y se ejecute cuando el valor proviene de la URL.

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

**Extracto del código observado**

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

**Cargas útiles probadas**

* Carga útil simple (no interpretada): / `<script>alert("XSS")</script>`
* Prueba para insertar un elemento de imagen:/ `<img src="test"</img>`/ (esto no activa la ejecución de scripts ya que no está presente ningún atributo de evento)

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

<figure><img src="/files/87a562115188699eae12e29d913ccb94acdb290e" alt=""><figcaption></figcaption></figure>

Forzar un error de carga para activar un `onerror` controlador:/ `<img src=0 onerror=alert("XSS")>` / (Esta carga útil usa el `onerror` atributo de evento para ejecutar `alerta` cuando falla la carga de la imagen)

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

<figure><img src="/files/4fd21e0f806e41be36a5da49c67920504f6746cf" alt=""><figcaption></figcaption></figure>

El script afecta directamente al `innerHTML` con la `búsqueda` valor de la URL; por lo tanto, fragmentos HTML que contienen controladores de eventos (p. ej. `onerror`) o etiquetas ejecutables pueden llevar a la ejecución de código. La `<img src=0 onerror=alert("XSS")>` carga útil provoca un error de carga y ejecuta `alert("XSS")`, lo que demuestra la vulnerabilidad de XSS en el 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/es/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.
