---
title: "Why your password reset link can point to an attacker's domain"
description: "Your app builds the link from the Host header that the request itself sends. Limit the domains you accept, and build email links from a fixed address in your settings."
url: https://ihsanalapsi.dev/articles/host-header-injection
lang: en
date: 2026-11-28
updated: 2026-11-28
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Why your password reset link can point to an attacker's domain

One line in the request, which the user never sees and the browser writes on its own, can decide where the password reset link in your customer's inbox points. If your app trusts that line, an attacker can make it send your customer a real link from your real email address, with the attacker's domain inside it. By the end of this article you will know where this happens in your code and how to close it.

## What is a Host header attack?

A Host header attack is when an attacker sends a request to your site with a `Host` header that names another domain, and your app uses that value to build links. The `Host` header is the line where a request says which domain it is for.

The browser always writes it correctly, but anyone who sends the request by hand can put anything in it. According to [Django's security documentation](https://docs.djangoproject.com/en/stable/topics/security/), Django uses the header the client sends to build URLs in some cases, and a fake value can be used for CSRF, cache poisoning and poisoning links in emails.

## How does a password reset link end up on the attacker's domain?

A password reset link ends up on the attacker's domain when the app takes the domain from the request header while it writes the email. The link and the token are real, but the domain inside it is not yours.

The attack goes like this:

1. The attacker opens the "forgot password" page and types the victim's email.
2. They send the request with a `Host` header that holds their domain instead of yours.
3. Your app creates a new token, builds the link from the header, and sends it to the victim's inbox from your server.
4. The victim sees a real email from your site, clicks the link, and the token reaches the attacker's server in the link itself.
5. The attacker uses the token on your real site and changes the password.

The email passes every mail check because it really comes from your server, so nothing catches the victim's eye except the domain. And if the token has no short lifetime and is not cancelled after its first use, the damage grows (details in [the password reset link article](/articles/password-reset-token-flow)).

## Why is web server configuration alone not enough?

Web server configuration alone is not enough because many setups look like they validate the header when they do not. That is why Django now asks for an explicit list in the app itself.

The same documentation page gives an example: even if Apache is set up to serve your site from a non-default virtual host with a `ServerName`, a request can still match that virtual host and send a fake header. The docs still recommend checking on the server, but they do not rely on it.

## How do you close it in Django?

In Django you close it with the `ALLOWED_HOSTS` setting: the list of domains your site serves, and any request whose header is not on it is refused. And you always read the domain from `get_host()`, never from the header directly.

According to [Django's settings reference](https://docs.djangoproject.com/en/stable/ref/settings/), the default for `ALLOWED_HOSTS` is an empty list. A value that starts with a period matches the domain and every subdomain under it, and a star matches anything, in which case you are responsible for the check. If the header matches no value, `get_host()` raises `SuspiciousOperation`.

```python
# settings.py
ALLOWED_HOSTS = ["example.com", ".example.com"]
```

The check happens through `get_host()` only. If your code reads the header directly from `request.META`, you are skipping the protection:

```python
host = request.META["HTTP_HOST"]  # skips ALLOWED_HOSTS
host = request.get_host()         # checked against ALLOWED_HOSTS
```

And if your app sits behind a proxy that sends `X-Forwarded-Host`, Django reads it only if you turn on `USE_X_FORWARDED_HOST` yourself. Turn it on only if the proxy is the one that writes it, because then it is the header that gets checked instead of `Host`.

## What if your app is not Django?

The idea is the same in any framework: do not build a link in an email from a header the request sent. A link that leaves your app should come from a fixed address you wrote in your settings.

1. Put your site's address in one settings variable (for example `APP_URL`), and build every email link from it.
2. Search your code for every place that reads the `Host` or `X-Forwarded-Host` header, and check whether its value leaves the request.
3. Have the server refuse any request for a domain that is not yours, as an extra layer on top of the app check.
4. Watch the cache: a page built from a fake header and then cached can reach other visitors (see [Cache-Control and no-store](/articles/cache-control-no-store)).

The same habit protects you from [open return-URL redirects](/articles/open-redirect-return-url): any address that comes from the request, check it against a list you wrote.

## Where do you start today?

Your app knows its own domain better than any request that reaches it, so let your settings name the domain, not the header.

Try it now on your test environment: request a password reset for a test account, change the `Host` header with a tool like curl, and see which domain shows up in the email. If your fake domain shows up, you have work to do today. And if you want someone to test this and more on your platform before an attacker does, [Muhassan](/muhassan) does that check. Which domain showed up for you?

## FAQ

**What happens if I set ALLOWED_HOSTS to a star?**

A star accepts any domain, and the docs say you are then responsible for validating the header yourself, for example in a middleware that must be listed first.

**My app is behind a load balancer. Should I enable X-Forwarded-Host?**

Only if your proxy sets it itself and drops any value coming from outside. Django reads it only when you enable USE_X_FORWARDED_HOST, and then it is the header that gets validated instead of Host.

**Does the attacker need access to the victim's account?**

No. It is enough to request a password reset for the victim's email with a fake header. The victim clicks the link, and the token reaches the attacker.
