Skip to content

Host Header Injection: How a Spoofed Header Hijacks Password-Reset Links

Applications that trust the Host header to build absolute URLs can be tricked into emailing attacker-controlled reset links and poisoning caches. Here is how it works and how to validate your way out.

SelfSec Team 8 min read

Part 1 of 2From the Host Header Injection series

At a glance

CWE-644
Interpreter
Application URL builder, router and cache
Required condition
The application builds links, redirects or routing decisions from a client-supplied Host or forwarding header.
Potential impact
Password-reset token theft, cache poisoning, and links or redirects that point at an attacker's domain.
Primary defense
Build absolute URLs from a configured canonical origin and reject hosts outside an allowlist.

Safe practice: use benign proof only, in a disposable local lab or on a system you are explicitly authorized to test.

Every HTTP request carries a Host header naming the site the client wants — Host: shop.example. It is essential for virtual hosting, where one server answers for many domains. The problem is that the Host header is client-supplied: the value comes from whoever made the request, not from the server's own configuration. Treating it as trustworthy is the mistake at the heart of host header injection.

When an application reflects that header into application logic — building absolute URLs, generating password-reset links, or computing cache keys — an attacker who controls the header controls those outputs. The result is poisoned links pointing at attacker domains, hijacked account-recovery flows, and cache poisoning that affects every later visitor. The OWASP Top 10:2025 maps this weakness (CWE-644) to A05 Injection.

The mental model: who decides what your site is called

An application needs its own name to build absolute links. There are several places it can get that name, and only some of them are under its control.

Source of the host name Who sets it Safe to build links from?
A configured canonical origin such as https://shop.example You, at deploy time Yes
Host, after it is checked against the names you serve The client, then validated Yes, once validated
X-Forwarded-Host, X-Host, Forwarded: host= Whoever sent the request, unless your proxy overwrites it Only when your own proxy sets it
Host with no validation The client No

A spoofed Host often fails at the front door, because a CDN or virtual-host setup will not route evil.example to your application at all. Forwarding headers face no such check: a proxy that does not overwrite them passes them straight through, and a framework configured to honor them hands the attacker's value to the application as if it were the host. In practice that makes X-Forwarded-Host the more common real-world vector.

How it happens

The root cause is code that asks the request "what host am I?" instead of reading a configured value. Absolute-URL construction is the classic offender, and password reset is where it bites hardest:

@app.route('/forgot-password', methods=['POST'])
def forgot_password():
    user = find_user(request.form['email'])
    token = create_reset_token(user)
    host = request.headers.get('Host')
    link = f'https://{host}/reset?token={token}'
    send_email(user.email, f'Reset your password: {link}')
    return 'Check your email'

The reset link's domain is taken straight from the inbound Host header. The developer assumed it would always be shop.example. Nothing guarantees that. Reverse proxies make it worse: many apps also trust X-Forwarded-Host, X-Host or Forwarded, any of which an attacker can set, and which can override the apparent host even when the front-end looks correct.

A concrete attack

The attacker triggers a password reset for the victim's account but rewrites the Host header to point at their own server:

POST /forgot-password HTTP/1.1
Host: evil.example
Content-Type: application/x-www-form-urlencoded

[email protected]

When the front end refuses unknown hosts, the same request often works through a forwarding header the application honors instead:

POST /forgot-password HTTP/1.1
Host: shop.example
X-Forwarded-Host: evil.example
Content-Type: application/x-www-form-urlencoded

[email protected]

Either way, the application generates a valid reset token for the victim, then builds the link using the spoofed host:

https://evil.example/reset?token=eyJhbGciOi...

That link is emailed to the real victim. When they click it — trusting the email because they really did request a reset — their browser sends the genuine token to evil.example. The attacker captures it, visits the legitimate /reset?token=... endpoint, and takes over the account. The same trick poisons caches: if a shared cache stores a page whose absolute links were built from the attacker's host, every subsequent visitor is served those rewritten links. The cache poisoning deep-dive follows that variant end to end.

The token was valid. The email was genuine. The only thing the attacker controlled was a header the application should never have trusted — and that was enough to redirect the secret to them.

What an attacker gains

Trusting the Host header turns a request header into a lever over the whole application:

  • Account takeover via password-reset links that deliver a valid token to an attacker domain.
  • Web cache poisoning when absolute URLs or resources built from the spoofed host get cached and served to others.
  • Phishing with a legitimate footprint — emails and pages that originate from the real backend but point victims at malicious hosts.
  • Routing and access-control abuse where downstream logic keys off the unvalidated host value.

The fix: validate the host against an allowlist

The header is untrusted input. Compare it against a configured set of canonical domains you actually serve, and reject anything else — then build URLs from a trusted, configured base rather than from the request. Flask 3.1 and later do the comparison for you with TRUSTED_HOSTS:

app.config['TRUSTED_HOSTS'] = ['shop.example', 'www.shop.example']
CANONICAL_BASE = 'https://shop.example'

@app.route('/forgot-password', methods=['POST'])
def forgot_password():
    user = find_user(request.form['email'])
    token = create_reset_token(user)
    link = f'{CANONICAL_BASE}/reset?token={token}'
    send_email(user.email, f'Reset your password: {link}')
    return 'Check your email'

Flask validates the request host against that list during routing and answers anything else with 400 Bad Request; on older versions the same check can run in a before_request hook. More importantly, the reset link no longer depends on the request at all.

ASP.NET Core ships host filtering, but it accepts every host until you list the ones you serve. In appsettings.json:

{
  "AllowedHosts": "shop.example;www.shop.example"
}

Requests for any other host are rejected before they reach the application. Do not leave the template's "*" in production. Behind a reverse proxy, the forwarded headers middleware has its own allowlist, and it should accept forwarding headers only from the proxy you run:

builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor
        | ForwardedHeaders.XForwardedProto
        | ForwardedHeaders.XForwardedHost;
    options.KnownProxies.Add(IPAddress.Parse("10.0.0.10"));
    options.AllowedHosts.Add("shop.example");
    options.AllowedHosts.Add("www.shop.example");
});

var app = builder.Build();
app.UseForwardedHeaders();

If the proxy preserves the original Host, leave XForwardedHost out entirely. When you do enable it, always pair it with AllowedHosts: an empty list allows every host.

At the edge, make the reverse proxy refuse names it does not serve and set forwarding headers itself instead of passing the client's values through. In nginx:

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    ssl_reject_handshake on;
    return 444;
}

server {
    listen 443 ssl;
    server_name shop.example www.shop.example;
    ssl_certificate     /etc/nginx/tls/shop.example.crt;
    ssl_certificate_key /etc/nginx/tls/shop.example.key;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

A request whose Host matches no server_name lands in the default server, which refuses TLS handshakes for unknown names and closes the connection with nginx's non-standard 444 status. In the real server, proxy_set_header replaces whatever X-Forwarded-Host the client sent with a name that already matched server_name.

The rules that keep this safe:

  • Validate Host against an explicit allowlist of expected domains and reject requests that do not match.
  • Build absolute URLs from a configured canonical host, never from the inbound header.
  • Do not trust forwarding headers — X-Forwarded-Host, X-Host and Forwarded are attacker-controllable; only honor them from a proxy you control, and validate them the same way.
  • Most servers and frameworks offer an allowed-hosts setting — Django's ALLOWED_HOSTS, Flask's TRUSTED_HOSTS, ASP.NET Core's AllowedHosts — turn it on so spoofed hosts are rejected before any handler runs.

Fixes that do not hold

  • Validating only Host: an application that honors X-Forwarded-Host, X-Host or Forwarded lets the attacker route around the check.
  • Trusting forwarding headers from any client: they mean something only when your own proxy sets them and the application accepts them only from that proxy's address.
  • Substring or suffix matching: shop.example.evil.example contains your name; compare exact host names.
  • An allowlist of *: it is the same as no allowlist.
  • Short-lived reset tokens alone: expiry narrows the window, but a victim who clicks within it still hands over a valid token.
  • Fixing only reset emails: the same header feeds redirects, canonical links, absolute asset URLs and anything a cache stores.

A developer review checklist

  1. Configure one canonical origin per environment and build every absolute URL, email link and redirect from it.
  2. Turn on the framework's host allowlist and remove any wildcard entry.
  3. Make the edge proxy drop unknown hosts and overwrite forwarding headers rather than pass them through.
  4. Process forwarding headers in the application only from known proxy addresses, with their own host allowlist.
  5. Include every header that can change a response in the cache key, or stop reflecting it.
  6. Test reset and invitation emails with a forged Host and X-Forwarded-Host and confirm the links still point at the canonical origin.

References

Module: Host Header Injection. How it probes: With header attacks enabled, sends an attacker-style domain in the X-Forwarded-Host, X-Forwarded-For, True-Client-IP, Referer and User-Agent request headers of each crawled endpoint. How it confirms: The domain has to appear in the response body or a response header, and the same request with a benign value in that header must not produce it. A finding moves from Possible to Confirmed only after this check. Reported as CWE-644 · SARIF 2.1.0.
How SelfSec proves this one
Module
Host Header Injection
How it probes
With header attacks enabled, sends an attacker-style domain in the X-Forwarded-Host, X-Forwarded-For, True-Client-IP, Referer and User-Agent request headers of each crawled endpoint.
How it confirms
The domain has to appear in the response body or a response header, and the same request with a benign value in that header must not produce it.
Reported as CWE-644 · SARIF 2.1.0

A reflected response alone is not enough to promote a Host Header Injection finding. The classical engine has to confirm the behavior before it reaches the report. See the detection engine

SelfSec scanner

Find Host Header Injection in your own app

Reading about the class is one thing. SelfSec tests for it against an application you are authorized to assess, and only calls it real when independent evidence agrees.

Join the launch list