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

# XSS DOM com innerHTML e location.search

Notas sobre XSS DOM com innerHTML e location.search para laboratórios da PortSwigger Web Security Academy, incluindo fluxo de exploração, teste de payloads, validação no Burp Suite e contexto de remediação.

### DOM XSS no destino innerHTML usando a origem location.search

Vulnerabilidade de DOM XSS no recurso de busca do blog. O código recupera o valor de `location.search` e o insere diretamente no DOM via `innerHTML`, permitindo que JavaScript seja injetado e executado quando o valor vem da URL.

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b8aa13280eb3315c3528a9d2f7eb497be0dd6550%2F%7B444963A0-370C-4F85-93C2-2326197EB4F0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Trecho do 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 úteis testadas**

* Carga útil simples (não interpretada): / `<script>alert("XSS")</script>`
* Teste para inserir um elemento de imagem:/ `<img src="test"</img>`/ (isso não aciona a execução de script, pois nenhum atributo de evento está presente)

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9d9efd80509a8f8ce27bb73dd64d4adfb8d92fc2%2F%7B50C1DB2A-BBDD-44E0-928A-257F04334961%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7b594a4e05aaaf7c0f0ede7df51965c9e9a42931%2F%7B24AED474-87F4-494A-AF22-B2D9ACC0341F%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Forçar erro de carregamento para acionar um `onerror` tratador:/ `<img src=0 onerror=alert("XSS")>` / (Esta carga útil usa o `onerror` atributo de evento para executar `alerta` quando o carregamento da imagem falha)

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

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8c45da08fcdb3080fc6cb4f6629f73c9ec937ec0%2F%7B3A8EB142-F97F-4D96-9104-AFF814B2380C%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

O script afeta diretamente `innerHTML` com a `parâmetro de busca` o valor da URL; portanto, fragmentos HTML contendo manipuladores de evento (por ex. `onerror`) ou tags executáveis podem levar à execução de código. A `<img src=0 onerror=alert("XSS")>` carga útil causa um erro de carregamento e executa `alert("XSS")`, demonstrando a vulnerabilidade de DOM XSS.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/pt-br/web/xss/dom-xss-with-innerhtml-and-location-search.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
