Host header injection: when the server believes the name it is given

Host header injection is an attack on applications that read the HTTP Host header and then act on it as if it were trustworthy: building links, choosing a site, keying a cache or forwarding a request. The header comes from the client, so whoever sends the request decides what it says. The flaw is in the application or proxy that trusts it, not in HTTP.

Section Web attacks Updated Weakness CWE-644

Quick facts

The header
Host carries the host and port of the target URI, defined in RFC 9110 section 7.2
Malformed Host
An HTTP/1.1 server must answer 400 to a request with no Host, several Host lines or an invalid value (RFC 9112 section 3.2)
What the RFC warns about
Host acts as an application-level routing mechanism and is a frequent target for cache poisoning and misdirection (RFC 9110 section 7.2)
Main fix
Build absolute links from configuration, answer only for host names you serve, and do not honour override headers you did not set up

Weakness classification is imperfect here. CWE-644 is titled "Improper Neutralization of HTTP Headers for Scripting Syntax" and concerns raw header values carrying script, so it covers only one outcome of trusting a header. The wider failure, using client-supplied data without checking it, is input validation in general. The attacks below are described in the OWASP and PortSwigger material linked above.

One IP address often serves many sites. The Host header tells the server which one the request is for, so it can pick the right virtual host. In HTTP/2 and HTTP/3 the same information may travel as the :authority pseudo-header instead, and RFC 9110 says Host is supplanted by it in some cases. A user agent must send one or the other.

Servers do check the syntax. A missing, repeated or malformed Host gets a 400 in HTTP/1.1. That check is easy to mistake for protection. A request with Host: some-other-domain.example is perfectly well formed, so it passes. Syntax is valid; whether the name is one you serve is a separate question that only your configuration can answer. A related misconfiguration at the DNS level is subdomain takeover.

The RFC is frank about the risk. Section 7.2 notes that because host and port act as an application-level routing mechanism, they are a frequent target for malware seeking to poison a shared cache or redirect a request to an unintended server, and that an intercepting proxy is especially exposed if it uses them for routing or as a cache key without checking the connection. Section 7.4 adds that a Host that disagrees with the connection can indicate an attempt to bypass security filters, reach non-public content or poison a cache, and defines the 421 (Misdirected Request) status for rejecting such requests.

How the header gets abused

OWASP lists the outcomes of unvalidated Host values: dispatch to the first virtual host on the list, a redirect to an attacker-controlled domain, web cache poisoning, manipulated password reset, and access to virtual hosts that were never meant to be reachable from outside. PortSwigger's list is longer and adds routing-based SSRF, authentication bypass and classic server-side bugs when the value reaches a query or a template. Four of these come up most often. Checking Host also helps against DNS rebinding.

Password reset links

Many applications write the reset email like this: take the Host of the current request, put it in front of a path, append a secret token. If the attacker submits the reset form for a victim's account with their own domain in Host, the genuine email arrives from the genuine sender, but the link points at the attacker's server. The victim who clicks hands over the token. OWASP walks through this case with a PHP example that reads the host from the request.

The attacker's request is normally sent directly to the application, so the victim's browser never sends the altered header. That is why the flaw survives in applications that look fine in a browser.

Cache keys and cached links

If a page embeds the Host (or a forwarded equivalent) in absolute URLs, and a shared cache stores that page under a key that ignores the header, the poisoned copy is served to everyone who requests the page. Both OWASP and PortSwigger describe this chain, and it is the reason this page has a sibling on web cache poisoning, where unkeyed inputs of all kinds are covered.

Routing and virtual hosts

A front-end proxy or load balancer often forwards a request to a back end chosen from the Host. If it forwards to whatever the header names, the attacker may steer it at internal systems, which PortSwigger calls routing-based SSRF; see server-side request forgery for the general problem. An internal-only site sharing a server with a public one can likewise be reached by naming it, because virtual-host selection happens on the same header.

Redirects and absolute URLs

A redirect that echoes the Host sends visitors to a domain of the attacker's choosing, the simplest case in the OWASP guide. It overlaps with open redirect. Canonical tags, sitemap entries, OAuth callback URLs and email footers built the same way carry the same risk.

X-Forwarded-Host and its relatives

Behind a reverse proxy, the Host the application sees may be the proxy's internal name. To recover the one the visitor used, proxies add a header. X-Forwarded-Host is, in MDN's words, a de-facto standard, not part of any current specification. The standardised form is Forwarded, in RFC 7239, which MDN describes as much less frequently used.

Here the trust problem moves. Applications and frameworks that read these headers must only believe them when they come from your own proxy. If the proxy passes along a client-supplied copy, or the application reads the header on requests that never went through a proxy, the attacker sets the value. OWASP notes the bypass: when plain Host values are checked, the same value supplied in X-Forwarded-Host can still reach output. PortSwigger's advice is to make sure such override headers are not honoured unless you rely on them, since support may be switched on by default in a framework.

Checking your own application

Test only systems you own or are authorised to assess. The OWASP guide starts simply: send a request with a different domain in Host and see what the application does with it. Then look for the consequences, not the status code:

  • Redirects. Does a Location header contain the supplied name?
  • Generated output. Do links, script sources, canonical tags or email bodies repeat it? Trigger a reset for a test account that you control.
  • Routing. Does an unknown name get the default site, an error, or a different application?
  • Cache behaviour. Does a response built from the altered value get stored, and for whom? Use a private cache or a test path, never a shared production page.
  • Forwarded variants. Repeat with X-Forwarded-Host and Forwarded.

Every item that changes behaviour is a finding, even if the output looks harmless today. A static scan of source for reads of the host from the request (framework request-host accessors, HTTP_HOST, req.hostname) finds the same places faster.

Where a WAF helps and where it does not

A WAF inspects requests before they reach the origin. That makes it a reasonable place to notice a Host value that is not one of yours, or a forwarding header arriving from a client, since both are visible in the request. Bridge WAF matches named categories on the request and records what matched. It works in three modes, off, monitor and block, and which categories flag a given Host value depends on the rules in use; the activity log shows what was flagged.

It cannot see what the application does with the header. Whether a reset link is built from the request or from configuration is decided in your code, and a request with a plausible Host and a valid session looks like any other. A filter does not repair a link generator, a cache key or a proxy that routes on the header. It also cannot tell a legitimate use of an unusual Host from an attack. Staging hosts, preview domains and health checks that call an origin by IP all have Hosts you may not expect.

The sensible order is the usual one. Start in monitor mode, so matches are recorded and every request is still served. Read the log for a few days of real traffic and decide which Host values are genuine; the notes on WAF monitor mode and false positives cover that review. Only then consider block. The application fix comes first either way; the WAF is a second line that makes attempts visible.

Host-related misconfiguration also sits in an OWASP category of its own, security misconfiguration, which is where an open default virtual host or an honoured override header belongs. A neighbouring problem is CRLF injection, which uses a line break to write a header, whereas Host injection needs none.

How to prevent host header injection

PortSwigger's prevention list is a good base. In order of effect:

  1. Do not use the Host header to build links. Use relative URLs where you can. Where an absolute URL is needed, read the site address from configuration.
  2. Keep an allow-list of host names. Reject or redirect anything else. Django's ALLOWED_HOSTS setting is the example PortSwigger gives; most frameworks and servers have an equivalent, and a default virtual host that returns an error is the server-level version.
  3. Turn off override headers you do not use. Accept X-Forwarded-Host or Forwarded only from your own proxy, and have that proxy replace any client-supplied copy.
  4. Constrain proxies and load balancers. Forward only to the back ends you list, never to a destination taken from the header.
  5. Separate internal sites. Do not host internal-only applications on the same server or virtual-host set as public ones.
  6. Key caches carefully. If a response depends on a header, that header belongs in the cache key, or the response should not be cached. The host in the key should be validated first.
  7. Reject misdirected requests. Where a server can tell the Host does not match the connection, a 421 is the answer the RFC defines.

None of these requires a security product. They are configuration and a few lines of code, and they remove the cause rather than the symptom.

Questions

What is host header injection?

Host header injection is an attack on an application or proxy that trusts the client-supplied HTTP Host header, for example to build links in password reset emails, choose a virtual host, route a request or key a cache.

Why does the server accept a Host value it does not serve?

HTTP only requires the Host value to be well formed. RFC 9112 makes a server answer 400 for a missing, repeated or invalid Host, but a valid name belonging to another domain passes that check, so the application or proxy must compare it with the names it actually serves.

Is X-Forwarded-Host also a risk?

Yes. X-Forwarded-Host is a de-facto standard, not part of a current specification, and an application that honours it from any client lets the attacker set the host again. Accept it, or the standard Forwarded header, only from your own proxy.

How do you prevent host header injection?

Build absolute links from configuration instead of the request, keep an allow-list of host names the site answers for, disable override headers you do not use, restrict where proxies forward, and keep internal sites off public servers.

Can a WAF stop host header injection?

A WAF can flag or refuse requests with unexpected Host or forwarding headers, which makes attempts visible, but it cannot change how your application builds links or routes requests. Fix the code and configuration first, and use monitor mode to learn which Host values are legitimate before blocking.