> 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

Notas sobre XSS com CSP e Técnica de Bypass 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.

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

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

XSS refletido entre sites protegido por uma Content Security Policy (CSP). O objetivo é executar `alert()` contornando a CSP. A solução pretendida só funciona no Chrome.

#### Objetivo

Encontre um vetor de XSS criativo 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="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4df666884d538a9410518879d47be470afd0bb8a%2Fimage%20(3060).png?alt=media" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b9642d5b07f7003db5f0c0834ca752b0be79e4e9%2Fimage%20(3061).png?alt=media" 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="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-eec93269fef858daf8e2755dbd1a14d134a2c1ef%2Fimage%20(3062).png?alt=media" alt=""><figcaption></figcaption></figure>

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

```
&token=test
```

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d5e0690561df65a0a72aa7366285a64b8958a554%2Fimage%20(3063).png?alt=media" alt=""><figcaption></figcaption></figure>

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

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

a diretiva é levada em conta

<figure><img src="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-84559138b8a42b1b9e8d80ff39062992f4dac8a6%2Fimage%20(3064).png?alt=media" 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="https://783634792-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-819b3ab3004aaa25ac545128fc412e29f4d55437%2Fimage%20(3065).png?alt=media" 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 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/xss-with-csp-bypass.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.
