CCyberwiki

Host header attacks

Host header attacks
ClassInjection
Also known asHost header injection, Host header poisoning
Typical severityHigh
AffectsCode that builds links or makes decisions from the Host or X-Forwarded-Host header; reverse proxies and load balancers that route by it

An HTTP/1.1 request for https://example.com/login carries the line Host: example.com; HTTP/2 and HTTP/3 send the same value as the :authority field. The value exists because one server, or one reverse proxy, often answers for many names at the same address, and the Host value tells it which site the request is for.1 A client can write any name there. A Host header attack uses that freedom against an application, cache or proxy that treats the value as a fact about itself: the attacker sends a forged host name and the system puts it into a link, a cached page or a routing decision.2

Why applications read the header

The header has been required since HTTP/1.1. A server must reject with status 400 a request that has no Host line, has two, or has an invalid value.3 Shared hosting and CDNs depend on it: many sites resolve to one IP address, and the front server picks the right back end by the name in the request.2

A web app does not know which domain it is deployed on unless someone writes it into a configuration file, and code that needs a full URL, for an email or a redirect, often takes the domain from the incoming Host header instead.2 The weakness comes from treating that value as if users could not set it.2

How the forged value gets in

Where the Host header itself is checked, other headers may override it. A front end can add X-Forwarded-Host with the name the visitor typed, and many frameworks prefer that header whenever it is present, even when no proxy in front of them sets it. X-Host and X-Forwarded-Server play the same part in some software.4

A third route uses requests that two servers read differently: two Host lines, a full URL in the request line next to a Host header, or a Host line indented so that one parser skips it. When the front end routes by one value and the application reads the other, the request reaches the real site and still carries the attacker's name into the code.4 Many of these flaws come from proxy and server configuration rather than from application code.2

Attacks

Password reset poisoning

The attacker asks for a password reset for Anna's account and sends the request with Host: attacker.example. The site emails Anna a genuine message with a valid token, but the link starts with the attacker's domain. When Anna clicks it, or an antivirus scanner fetches it, the token arrives at the attacker's server, and the attacker uses it on the real site to set a new password.5 The wider subject of reset flows is in Account recovery.

Cache poisoning

A Host value echoed into a page, for example in a script address, cannot be used against other people on its own, because an attacker cannot make another person's browser send a wrong host. A cache, which replays a stored response to later visitors, removes that limit: if the response with the forged value is stored under a cache key that ordinary visitors' requests also match, every later visitor receives it, and a reflected flaw becomes a stored one.4 Standalone caches usually put the host in the key, so caches built into the application are the likelier target.4

Routing and internal sites

A load balancer or reverse proxy receives each request from the internet and forwards it to a back end. One that forwards to whatever host the request names can be pointed at internal systems, a form of server-side request forgery.4 Cloud instance metadata service describes the cloud credentials such a request can reach. On a server that hosts an internal site next to public ones, guessing the internal name in the Host header can open it, even when the name has no public DNS record.4

Known vulnerabilities

Django added its ALLOWED_HOSTS setting in version 1.4.4 on 19 February 2013. Parts of the framework built full URLs from the Host header, and the project had been told that common web servers could be made to pass a malicious Host value even with the configuration its documentation recommended. The first default, ['*'], still matched every host, for backward compatibility.6

Umbraco before 9.2.0 built password reset links from the request's host name (CVE-2022-22691), and a related flaw, CVE-2022-22690, could make the forged address stick for all later reset links.7 AzuraCast before 0.23.6 trusted X-Forwarded-Host from any client; a poisoned reset link let an attacker reset a victim's password and remove their two-factor setup (CVE-2026-42606).8

Defences

The surest fix is to stop reading the header in server code. Relative links need no host, and where a full URL is needed, such as the link in a reset email, take the domain from configuration.2 Where the header must be read, compare it with a list of the app's own names, reject anything else, and turn off support for X-Forwarded-Host unless a proxy you control sets it, since some frameworks honour it by default.2 Proxies should forward only to an allowed list of hosts, and internal sites belong on servers that do not also serve the public.2

Django

# settings.py
ALLOWED_HOSTS = ["example.com", "www.example.com"]
USE_X_FORWARDED_HOST = False  # the default; True only behind a proxy that sets it

request.get_host() raises SuspiciousOperation for a name not on the list. Code that reads request.META directly skips the check.9

Express

With trust proxy turned on, req.hostname comes from X-Forwarded-Host, which a client can also send. The setting is off by default; turn it on only for the proxy's own address, and only when that proxy overwrites the forwarded headers.10

app.set('trust proxy', 'loopback'); // the proxy runs on the same machine
const resetUrl = `${process.env.APP_URL}/reset?token=${token}`; // not req.hostname

nginx

nginx compares the Host value with each server_name and sends requests that match none to the default server for the port, which is the first one unless default_server names another.11 A catch-all block that closes those connections keeps unknown names away from the app:

server {
    listen 80 default_server;
    return 444;  # a code of nginx: close the connection
}

Prompt for a coding agent

Find every place in this project that reads the request host: the Host header, X-Forwarded-Host, req.hostname, req.headers.host, request.get_host() or request.META["HTTP_HOST"], and framework helpers that build absolute URLs from the request. Where the value goes into a link, an email, a redirect, a cache or a database query, replace it with a base URL read from configuration. Make sure X-Forwarded-Host is trusted only from our own proxy, and that the framework rejects host names that are not ours (for example ALLOWED_HOSTS in Django). List each file and line you changed.

In SecFlash

SecFlash reports SF-SEC-619 on the Security page when its ZAP scan sends a request with the Host header set to the cloud metadata address and gets a success back, the sign of a proxy that forwards to any host it is given. SF-SRV-105 on the Server page marks a server that shows a site to requests carrying only its bare IP address and no matching host name.

See also

References

  1. back Fielding, R., Nottingham, M. and Reschke, J. (June 2022). "HTTP Semantics", section 7.2. RFC 9110, IETF.
  2. a b c d e f g h PortSwigger. "HTTP Host header attacks". Web Security Academy.
  3. back Fielding, R., Nottingham, M. and Reschke, J. (June 2022). "HTTP/1.1", section 3.2. RFC 9112, IETF.
  4. a b c d e f PortSwigger. "How to identify and exploit HTTP Host header vulnerabilities". Web Security Academy.
  5. back PortSwigger. "Password reset poisoning". Web Security Academy.
  6. back Django Software Foundation (19 February 2013). "Django 1.4.4 release notes". Django documentation.
  7. back GitHub (21 January 2022). "Umbraco Persistent Password Reset Poison" (CVE-2022-22691). GitHub Advisory Database.
  8. back GitLab. "AzuraCast has Password Reset Poisoning via Untrusted X-Forwarded-Host Header" (CVE-2026-42606). GitLab Advisory Database.
  9. back Django Software Foundation. "Settings: ALLOWED_HOSTS". Django 5.2 documentation.
  10. back Express. "Express behind proxies". Express documentation.
  11. back nginx. "How nginx processes a request". nginx documentation.