Skip to main content
Security guideReviewed

Host header injection: when your application takes its own name from the request

Every request tells the server which site it is asking for, and whoever sends the request writes that name.

Host header injection is what follows when an application uses the name to build its own links: on a page, in a redirect or in a password reset email. This guide covers who sets the Host header, how a proxy changes what the application sees, how six frameworks and two web servers decide their own name, the four controls, and a trusted-host test pack with a lab you can run on your own computer.

Trusted-host test pack and local lab

Part 1 is a test pack with four rows, absolute URL generation, password reset links, cache variation and proxy forwarding: the request to send, what to observe, the result to expect after remediation and the source behind it, with spaces for your own results. Parts 2 and 3 are an illustration you can run with Node.js and no installs: an invented booking application, a reverse proxy and a cache, each in a careless and a careful version, with seven cases and seven regression tests, and every output copied from one recorded run on Node.js 26.0.0.

How to use it

Part 4 lists the same tests for your own application.

host-header-injection.txt

Introduction21 lines

TRUSTED-HOST TEST PACK AND LOCAL LAB ILLUSTRATION ONLY. Part 1 is a test pack for your own application. Parts 2 and 3 are a lab written for this page and run on one computer: an invented booking application, Tideline Tours, a small reverse proxy and a small cache, each in a careless and a careful version. The application, its customer, its host names and its messages are invented, and nothing in the lab is a client's system or a client's result. The lab shows a pattern. It does not show how any real framework, proxy, load balancer, cache or CDN behaves. Use what it teaches only on systems you own or are authorised in writing to test. WHAT THE LAB NEEDS Node.js. Apart from each other, the lab files import only node:assert/strict, node:http, node:net and node:test, and the lab installs no packages. The run in Part 3 was recorded on macOS with Node.js 26.0.0, the only version and the only system it was run on. Timings differ from run to run. Steps 2 and 4 set the LAB_VERSION environment variable in front of the command, which is how the run was made, in bash on macOS. In another shell, set the variable the way that shell does. WHAT THE LAB DOES NOT DO Every server in the lab listens on 127.0.0.1, on a port the system picks, and no port is printed. The lab client opens every connection to the literal address 127.0.0.1 and writes each request as text. It sends its three host names only as header values inside requests and never connects to one. The application sends no email: a reset message is an entry in a list in memory, which the lab prints. The lab writes nothing to disk. Each command starts its servers inside its own process and stops them before it ends. THE NAMES bookings.tideline.example the name in the configured origin, https://bookings.tideline.example tideline-tours.example a second name the application also serves other-host.test stands for any name the application does not serve All three are invented. They sit under .example and .test, two of the four top level names that RFC 2606 reserves. WHAT IS HERE Part 1 The trusted-host test pack: four rows to fill in for your own application Part 2 The lab files: ten files Part 3 The recorded run: four commands and what each printed Part 4 The same tests on your own application

What host header injection is

A web application sometimes has to write out its own address in full: in the link inside a password reset email, in a canonical tag, in a redirect to the sign-in page.

That address is a fact about where the application is deployed, so it belongs in configuration. Host header injection is the weakness that appears when the application reads it from the request instead. Every request names the site it is meant for, in the Host header or in a forwarded copy of it, and the name is written by whoever sends the request.

An application that builds a link from that name has let a stranger decide what the link says.

OWASP's Web Security Testing Guide (WSTG) has a test for it. In version 4.2, the current stable release, it is 4.7.17 Testing for Host Header Injection, with the id WSTG-INPV-17. Its summary says a web server commonly hosts several web applications on the same IP address, referring to each application via the virtual host, and that web servers often dispatch an incoming request to the target virtual host based on the value supplied in the Host header.

Without proper validation of the header value, it says, the attacker can supply invalid input to cause the web server to do four things: dispatch requests to the first virtual host on the list, cause a redirect to an attacker-controlled domain, perform web cache poisoning, and manipulate password reset functionality.

The latest edition of the guide, which its site says represents the latest contributions and may frequently change, files the test under a different id: 4.7.17 Host Header Injection, WSTG-INJT-17, in a section named Injection. It keeps the same two test objectives and adds a fifth outcome to the list: allowing access to virtual hosts that were not intended to be externally accessible. This page uses version 4.2 and its id unless it says otherwise.

PortSwigger's Web Security Academy, which is one vendor's teaching material and not a standard, uses the wider name HTTP Host header attacks for attacks that exploit websites handling the value of the Host header in an unsafe way.

It says attacks that involve injecting a payload directly into the Host header are often known as Host header injection attacks, and that the vulnerabilities typically arise due to the flawed assumption that the header is not user controllable.

On this page a host header attack and host header injection are the same problem, named once for what the sender does and once for where the value lands.

This guide owns one mistake and the controls that stop it. The mistake: the application takes its own name from the request when it builds an absolute URL, chooses a tenant or virtual host, or writes a link into an email. The controls: one configured origin for every generated link, an allow-list of host names enforced in the framework and at the proxy, a default virtual host that refuses unknown names, and a proxy that overwrites forwarded host headers instead of passing a client's copy on.

What the Host header is for, and who sets it

One server address can answer for many sites, so each request has to say which one it wants.

RFC 9110, HTTP Semantics, published in June 2022, says in section 7.2 that the Host header field in a request provides the host and port information from the target URI, enabling the origin server to distinguish among resources while servicing requests for multiple host names.

The same section says a user agent MUST generate a Host header field in a request unless it sends that information as an ":authority" pseudo-header field, which in HTTP/2 and HTTP/3 supplants the Host header in some cases.

RFC 9112, the HTTP/1.1 standard, adds in section 3.2 that a client MUST send a Host header field in all HTTP/1.1 request messages and, when the target URI includes an authority component, MUST send a value identical to that component, leaving out any userinfo. In plain terms, a browser copies the name from the address it was asked to open.

Those rules bind well-behaved clients. They do not bind the person at the keyboard, who can write a request with any name in it. The standards acknowledge this in their own way. Section 7.2 of RFC 9110 warns that since the host and port information acts as an application-level routing mechanism, it is a frequent target for malware seeking to poison a shared cache or redirect a request to an unintended server.

Apache HTTP Server's documentation says as much in a warning about CGI scripts under its UseCanonicalName directive: the client is essentially free to give whatever value they want as a hostname.

What the standards say about the server

Four passages in the two RFCs cover what a server does with the name it is given. 2 min read

Where the standards stop

Section 7.4 of RFC 9110 makes one kind of rejection a requirement: unless the connection is from a trusted gateway, an origin server MUST reject a request if any scheme-specific requirements for the target URI are not met. 1 min read

Why the application behind a proxy sees a different name

An application does not have to face the internet itself.

Where a CDN, a load balancer or a reverse proxy takes the visitor's connection and opens its own connection to the application, the application sees the request that intermediary wrote. RFC 9110 section 3.7 calls this kind of intermediary a gateway, also known as a reverse proxy: one that acts as an origin server for the outbound connection but translates received requests and forwards them inbound to another server or servers.

What the application then finds in the Host header depends on how the intermediary is set up.

Of the three sources below, two are web servers documenting their own proxy modules, and the third is a framework's documentation saying what proxies usually do.

  • nginx: its proxy module documentation gives the default as proxy_set_header Host $proxy_host, and defines $proxy_host as the name and port of a proxied server as specified in the proxy_pass directive. By default, it says, the Host header field from the original request is not passed to the proxied server. It shows ways to pass a name from the request instead, among them $http_host, for an unchanged Host request header field, and $host, whose value equals the server name in the Host request header field or the primary server name if this field is not present.
  • Apache HTTP Server 2.4: the ProxyPreserveHost directive defaults to Off. When enabled, the mod_proxy documentation says, the option passes the Host line from the incoming request to the proxied host, instead of the hostname specified in the ProxyPass line. It adds that the option should normally be turned Off.
  • ASP.NET Core 10.0: Microsoft's documentation on proxy servers and load balancers says that usually proxies do not modify the Host header.

What that means for the application

On this page's reading of those three, the application may receive the public name, an internal name such as the one in a proxy_pass line, or whatever the client typed, depending on a setting in a different layer. Code that builds a link from the Host header can be right behind one proxy and wrong behind another without a line of it changing.

Forwarded host headers

When the proxy rewrites the Host header, the original name has to travel some other way. RFC 7239 defines the standard carrier: the Forwarded header field, an OPTIONAL field whose parameters disclose information that is altered or lost when a proxy is involved in the path of the request.

Section 5.3 defines its host parameter, which is used to forward the original value of the Host header field, and gives a use for it: by the origin server, for example, if a reverse proxy is rewriting the Host header field to some internal host name.

The X-Forwarded-Host header does the same job under another name, and this page takes its meaning from products that document it. Apache's mod_proxy documentation says that in reverse-proxy mode mod_proxy_http adds several request headers in order to pass information to the origin server, and lists three: X-Forwarded-For, X-Forwarded-Host, which it describes as the original host requested by the client in the Host HTTP request header, and X-Forwarded-Server.

Microsoft's ASP.NET Core documentation describes X-Forwarded-Host as the original value of the Host header field.

PortSwigger calls it the de facto standard for this behaviour and says other headers serve a similar purpose, including X-Host, X-Forwarded-Server, X-HTTP-Host-Override and Forwarded.

A forwarded header is only as trustworthy as the last machine that wrote it. RFC 7239 section 8.1 says the Forwarded header field cannot be relied upon to be correct, as it may be modified, whether mistakenly or for malicious reasons, by every node on the way to the server, including the client making the request.

Section 4 says a proxy MAY remove all Forwarded header fields from a request. Apache gives the practical warning for its own headers: be careful when using them on the origin server, since they will contain more than one comma-separated value if the original request already contained one of these headers.

Where applications use the host name

An application that reads its name from the request does so in a handful of places.

Each is harmless while every request carries the right name, and each fails in its own way when one does not.

  • Links in email. A reset, invitation or verification message has to carry a full address, because the reader opens it from outside the site. The next section explains why this is the costly case.
  • Redirects. A redirect to a full address built from the request sends the browser wherever the header said. Apache HTTP Server 2.4 documents its own version: with UseCanonicalName Off, which its core documentation gives as the default, Apache httpd forms self-referential URLs using the hostname and port supplied by the client if any are supplied, and its example is the redirect it sends when a directory is requested without the trailing slash. The WSTG test says that in the simplest case, supplying another domain in the Host header may cause a 302 redirect to the supplied domain. A redirect whose destination comes from a parameter such as next or return_to is a different mistake, covered in the open redirects guide.
  • Canonical and social tags. A canonical link or an Open Graph address written from the request carries whatever name the request did, in a tag whose whole job is to state the page's own address. The first case study in PortSwigger's 2018 paper Practical Web Cache Poisoning was an application that used the X-Forwarded-Host header to generate an Open Graph URL inside a meta tag.
  • Choosing a tenant or a virtual host. Software that serves several customers from one deployment can use the host name to decide whose branding and data to load, and a web server uses it to pick which site answers. The latest edition of the WSTG test describes the second kind going wrong: a server may have virtual hosts that are not intended to be externally accessible, and it may be possible to reach one from outside the network by making a request with its name in the Host header. For a tenant, treat the name as a request to see that tenant's site and not as evidence of who is asking.
  • Cache keys. A cache decides that two requests want the same page by comparing a key. If the name shapes the response but is not in the key, a page built for one name is served under another. The web cache poisoning guide covers unkeyed inputs of every kind, and case 3 of the lab on this page shows this one.

Two vendors' lists

Django's security documentation names three consequences in one sentence: Django uses the Host header provided by the client to construct URLs in certain cases, and while these values are sanitised to prevent cross-site scripting attacks, a fake Host value can be used for cross-site request forgery, cache poisoning attacks, and poisoning links in emails.

Microsoft's ASP.NET Core documentation on proxy servers and load balancers says that proxy servers, load balancers and other network appliances often obscure information about the request before it reaches the app, naming the original scheme and the originating client IP address, and that this information might be important in request processing, for example in redirects, authentication, link generation, policy evaluation and client geolocation.

Password reset poisoning: why the reset link is the costly case

Of everything an application writes with its own name in it, the password reset link is the one that carries a secret.

PortSwigger defines password reset poisoning as a technique whereby an attacker manipulates a vulnerable website into generating a password reset link pointing to a domain under their control, and says this can be leveraged to steal the secret tokens required to reset arbitrary users' passwords and, ultimately, compromise their accounts. It says the technique was first documented in 2013 by its Director of Research, James Kettle.

The mechanism needs no access to the victim's mailbox. In PortSwigger's account, the attacker submits a reset request on the victim's behalf with the Host header changed to a domain they control. The victim receives a genuine password reset email directly from the website, containing a valid reset token associated with their account, in which the domain name in the URL points to the attacker's server.

If the victim clicks the link, or it is fetched in some other way, for example by an antivirus scanner, the token is delivered to that server.

The WSTG test describes the same outcome, and says it is common for password reset functionality to include the Host header value when creating password reset links that use a generated secret token.

Three features of the flow raise the cost, on this page's reading. A forgotten-password form is open to people who are not signed in, so the request needs no account and no session. The message is genuine: your own system sends it, to a customer who has no reason to doubt it.

And the flow rests on one assumption, which PortSwigger states: the security of an emailed reset link relies on the principle that only the intended user has access to their email inbox and, therefore, to their unique token. This technique removes that assumption without touching the inbox.

The fix the sources give

OWASP's Forgot Password Cheat Sheet says not to rely on the Host header while creating the reset URLs, and that the URL should either be hard-coded or validated against a list of trusted domains. PortSwigger gives the same advice in other words: when you have to use absolute URLs, require the current domain to be specified in a configuration file and refer to that value instead of the Host header, an approach it says would eliminate the threat of password reset poisoning, for example.

Cases 2A, 2B and 4B of the lab show both halves. The careless application wrote a reset link beginning with whichever name the request carried. The careful one wrote https://bookings.tideline.example into the link every time, and wrote no message at all for a name it does not serve. Token lifetime, single use and what the form says about unknown addresses belong to the authentication failures guide.

Allowed hosts configuration: how six frameworks decide their own name

Frameworks differ on two questions: whether they check the Host header against a list before your code runs, and whether they read a forwarded header in its place.

Each statement below comes from that product's own documentation as it read on 14 November 2026, for the version named, and says nothing about any other version or product.

Django 6.1

ALLOWED_HOSTS is a list of the host and domain names the site can serve. 1 min read

Ruby on Rails 8.1

Rails checks the host in a middleware, ActionDispatch::HostAuthorization, which the configuration guide describes as preventing against DNS rebinding and other Host header attacks. 1 min read

Laravel 13.x

Laravel's request documentation states its default plainly: Laravel will respond to all requests it receives regardless of the content of the HTTP request's Host header, and the Host header's value will be used when generating absolute URLs to your application during a web request. 1 min read

ASP.NET Core 10.0

Microsoft's host filtering page says that Kestrel, the ASP.NET Core web server, largely ignores the host name and that Host headers aren't validated. 1 min read

Express 5.x

The Express 5.x API reference says req.hostname contains the hostname derived from the Host HTTP header, and that when the trust proxy setting does not evaluate to false the property instead gets the value from the X-Forwarded-Host header field, a header that, in the reference's words, can be set by the client or by the proxy. 1 min read

Next.js

Two statements in the Next.js documentation bear on this. 1 min read

What nginx and Apache do with a name they do not recognise

The web server in front of the application makes its own decision about the name, before any framework sees the request.

By the documentation quoted below, each of these two servers, left alone, hands an unknown name to the site listed first in the configuration.

nginx

nginx's page How nginx processes a request describes three virtual servers listening on one port. nginx tests only the request's Host header field to decide which server the request should be routed to, and if its value does not match any server name, or the request does not contain the header at all, nginx routes the request to the default server for the port.

The default server is the first one, unless the default_server parameter of the listen directive names another.

On this page's reading, an unknown name on nginx is answered by whichever site comes first until someone decides otherwise.

The same page shows a server that just drops requests that have no Host header: its server name is an empty string, and it returns 444, which nginx describes as a special non-standard code that closes the connection. Django's deployment checklist uses the same response for unknown names.

It says the web server in front of Django should respond with a static error page or ignore requests for incorrect hosts instead of forwarding the request to Django, and gives an nginx default server that returns 444 on an unrecognised host as its example.

Apache HTTP Server 2.4

Apache reaches the same place by its own rule. Its name-based virtual host documentation says that if no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.

Django's security documentation carries a separate warning that uses Apache as its example: even if Apache is configured such that a Django site is served from a non-default virtual host with the ServerName set, it is still possible for an HTTP request to match this virtual host and supply a fake Host header. Django therefore requires ALLOWED_HOSTS to be set explicitly rather than relying on web server configuration.

Apache also decides which name its own redirects carry. With UseCanonicalName On, the core documentation says, Apache httpd will use the hostname and port specified in the ServerName directive to construct the canonical name for the server, and this name is used in all self-referential URLs. The default is Off.

Why checking the host at one layer is not enough

A request can pass through a CDN or cache, a reverse proxy and then the application, and each of the three has its own idea of the request's name.

A check in one of them covers the header that layer reads. It says nothing about the header the next layer reads.

The WSTG test names the gap. Under the heading X-Forwarded Host Header Bypass, it says that in the event that Host header injection is mitigated by checking for invalid input injected via the Host header, the value can be supplied to the X-Forwarded-Host header instead. PortSwigger makes a related point about ambiguous requests: the code that validates the host and the code that does something vulnerable with it often reside in different application components or even on separate servers.

It adds that when an X-Forwarded-Host header is present many frameworks will refer to it instead, that this may be observed even when there is no front end that uses the header, and that some websites support the behaviour unintentionally, usually because one or more of these headers is enabled by default in some third-party technology that they use.

Case 4B of the lab is that gap in miniature. The request's Host header was the application's real name from start to finish, so a check on the Host header at any layer would have passed it. The other name travelled in X-Forwarded-Host. The careless proxy passed the client's copy on, the careless application preferred it to the Host header, and the reset link in the outbox began with https://other-host.test.

The gap opens from the other side too. Django's warning is about a web server configuration that seems to validate the Host header and may not in fact do so. Laravel's documentation says that typically the web server should do the filtering, and offers its middleware for when you cannot configure the server.

Where two layers each leave the check to the other, nobody makes it. This page's advice is a rule for every layer that reads a host name: it either checks the name against the list itself, or reads it from a header that the layer directly in front writes on every request.

Two documented behaviours that meet badly

Put two of the pages quoted above side by side. Apache says its forwarded headers will contain more than one comma-separated value if the original request already contained one of them. Express says that for a single X-Forwarded-Host header with comma-separated values, the first value is used.

Neither page mentions the other product, and Apache's does not say which value comes first. On this page's reading, a proxy that adds to a forwarded header and an application that picks one entry from it can between them leave the application reading a value the client wrote.

The careful proxy in the lab replaces the header instead of adding to it for that reason. Test your own pair before relying on either reading.

Host header validation: four controls and what each one covers

The four controls sit at different layers, and the careful version of the lab runs three of them, with the proxy's refusal standing in for the default virtual host.

They are listed in the order to adopt them.

1. One configured origin for every generated link

Hold the site's public origin as a setting and build every absolute URL from it: links in email, redirects to full addresses, canonical and social tags, and return addresses handed to payment or sign-in providers. 1 min read

2. An allow-list of host names, in the framework and at the proxy

List the names the application serves and refuse every other. 1 min read

3. A default virtual host that refuses unknown names

On a web server that picks a site by name, decide what answers when no name matches. 1 min read

4. A proxy that overwrites forwarded host headers

Whichever header your application reads the public name from, the proxy directly in front of it should write that header on every request, replacing any value that arrived from outside, and remove the alternatives it does not use. 1 min read

What a correct refusal looks like

A refusal has three properties, whatever status it uses. 1 min read

Host header testing, one layer at a time

The test pack on this page has four rows: absolute URL generation, password reset links, cache variation and proxy forwarding.

Each row is a request to send to your own application, something to observe, and the result to expect once the controls are in place. The WSTG test puts the starting point in one line: initial testing is as simple as supplying another domain into the Host header field, and it is how the web server processes the header value that dictates the impact.

Keep the testing inside these limits. Use a staging or test copy of an application you own or are authorised in writing to test. Use a test account whose mailbox you control, because two of the rows cause a reset message to be sent, and do not request a reset for a real person's account.

Use an invented name for the other host: the lab uses one under .test, one of the four top level names RFC 2606 reserves. Do not use a real domain you do not control, because a link built from it would point at someone else's site.

And run the cache row only on a path no visitor uses, because a test that succeeds stores a response that later visitors are served.

  • The application alone. If you can reach the application without the proxy, on a developer machine or inside the staging network, send rows 1 and 2 straight to it. This shows what the framework does when nothing in front has filtered the request.
  • The proxy. Send a request for an unknown name through the proxy to a path that exists and note which layer answered: the status, the body and any header that identifies the server. Then send the real name with your own X-Forwarded-Host header, and have the application log the Host and forwarded host headers it received for that one request.
  • The cache. On a test-only path, request the page with the other name, then with the real name, and read the cache status header your product returns. Then read how the cache key is defined.
  • The whole chain. Repeat all four rows through the public address of the test environment, which is the route a request from outside takes.
  • The header rules. Send one request with no Host header and one with two. RFC 9112 section 3.2 says a server MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request message that lacks a Host header field, and to any request message that contains more than one Host header field line.
  • The other forwarded headers. Repeat row 4 with each header your framework or proxy documents, including Forwarded with a host parameter.

Reading the cache key

RFC 9111 section 2 says a cache key is composed from, at a minimum, the request method and target URI. RFC 9112 section 3.3 says that when the request-target is in neither absolute-form nor authority-form, the target URI's authority component is the field value of the Host header field.

On this page's reading, a cache keyed on the whole target URI therefore keeps names apart. Products and configurations vary. Cloudflare's documentation lists the full URL, with scheme and host, and the x-forwarded-host header in its default cache key. nginx's documented default key is the scheme, the proxied host and the request URI, and its documentation defines the proxied host as the name and port of a proxied server as specified in the proxy_pass directive.

On this page's reading, that is not the Host header the client sent.

PortSwigger says standalone caches typically include the Host header in the cache key, so poisoning through the Host header usually works best on integrated, application-level caches. Read the key your own cache uses, including any cache inside the application.

Read the evidence, not the status

A row passes when the origin in what the application generated is the configured one, or the request was refused. A 200 with the right page proves nothing until you have read the links in it. For the two reset rows the evidence is the message that arrived in the test mailbox, or the fact that none did.

Keep each request, its response and the date, because that record is what shows which origin was in use after the fix.

What these tests do not cover

This is a test of your own names against your own configuration. It does not exercise HTTP request smuggling, where two servers disagree about where a request ends and a second request, with a Host header of its own, rides inside the first: the HTTP request smuggling guide covers that.

And it does not find hostnames that point at services you no longer hold, which is the subject of the subdomain takeover guide.

Running the trusted-host lab

The lab on this page is an illustration.

It is an invented booking application, Tideline Tours, with a small reverse proxy and a small cache written for the lab, all held in memory and all listening on 127.0.0.1. It shows the pattern this page describes. It does not show how any real framework, proxy or CDN behaves. The application sends no email: a reset message is an entry in an in-memory outbox, which the lab prints.

Each of the three parts has a careless and a careful version. The careless application builds links from the request, preferring X-Forwarded-Host to Host. The careless proxy passes a client's X-Forwarded-Host on. The careless cache's key is the method and the request's URL, which in every lab request is a path with no query string.

The careful application builds every link from one configured origin, https://bookings.tideline.example, and answers 421 to any Host header that is not one of its two served names.

The careful proxy refuses unknown names itself and replaces X-Forwarded-Host with the name it checked. The careful cache adds the Host header to its key. Every name in the lab is invented and sits under .example or .test.

The lab client opens each connection to the literal address 127.0.0.1 and writes the request as text, with the Host header set in the request itself. It sends its three host names only as header values and never connects to one.

Apart from each other, the lab files import only node:assert/strict, node:http, node:net and node:test, and the lab installs no packages. Each server asks for port 0. Node.js's documentation says the HTTP server's listen() is identical to the one in its net module, where port 0 makes the operating system assign an arbitrary unused port.

The recorded run was made on Node.js 26.0.0 on macOS, the only version and the only system it was run on.

  • Case 1A, a page requested with Host: other-host.test. Careless: status 200, and the links on the page begin with https://other-host.test. Careful: status 421 and no link.
  • Case 1B, the same page requested through the second served name, tideline-tours.example. Careless: the links begin with https://tideline-tours.example. Careful: they begin with the configured origin.
  • Cases 2A and 2B, a reset request with the same two Host headers. Careless: the outbox holds a message whose link begins with the name the request sent. Careful: status 421 and an empty outbox for the unknown name, and a link to the configured origin for the served one.
  • Case 3, three requests for one page through the cache. Careless: the first, for other-host.test, is stored under the key GET /tours/sunrise-paddle, and the next two requests, which sent served names, are answered from the cache with that page. Careful: the first is refused and nothing is stored, the other two are stored under separate keys, and both stored pages link to the configured origin.
  • Case 4A, a reset request through the proxy with an unknown Host header. Careless: it reaches the application and produces a link to the other name. Careful: the proxy answers 421 and the application receives nothing.
  • Case 4B, the served Host header with X-Forwarded-Host: other-host.test. Careless: the application received both headers as the client sent them and wrote a link to https://other-host.test. Careful: the application received X-Forwarded-Host: bookings.tideline.example, and the link carries the configured origin.

The regression tests

The file trusted-host.test.mjs holds one test for each case, written as a statement of what the careful version does. Run against the careless version, all seven fail. Run against the careful version, all seven pass. Node.js's documentation says that if any tests fail, the process exit code is set to 1, and the two recorded runs ended with exit codes 1 and 0. A release pipeline can use that difference to stop a release.

To use the tests on your own application, keep the assertions and replace the lab client with requests to your staging address and a read of your test mailbox. The assertion worth keeping is the same in every row: the origin in whatever was generated is the configured one, or the request was refused.

Where a Clever Ops test fits

One neighbouring guide has not come up yet.

The security misconfiguration guide covers settings that differ between the environment a team tested and the one that is live, which is how a host list that is right on staging can be missing in production.

A Clever Ops web application and API penetration test covers authentication and sessions, what each role can see and change, and the API called directly rather than only through the screens. Read-only code access to named areas can be added to the scope, and a finding that rests on the code alone says so.

Lockout and reset checks run only on test accounts, where the rules of engagement allow them, and no password reset is triggered for a real person's account.

A CDN, load balancer or proxy in front of the application may be run by another business and may have testing terms of its own. When you book a scoping call, say which CDN or reverse proxy sits in front of the application, who controls its settings, and whether a staging copy runs behind the same configuration.

If you want host handling at those layers considered, raise it on the call, so that it is either in the written scope or listed as an exclusion.

Laravel applications sit outside this service. If Clever Ops built or hosts your application, the work is a security review rather than an independent test, and the report says who built it and who hosts it.

Frequently asked questions

It is a weakness in which an application takes its own name from the request, from the Host header or a forwarded copy such as X-Forwarded-Host, and uses it to build a link, a redirect or a cached page. Because the sender of a request writes those headers, the sender chooses the name. OWASP's Web Security Testing Guide v4.2 tests for it as WSTG-INPV-17 and lists four outcomes: requests dispatched to the first virtual host on the list, a redirect to an attacker-controlled domain, web cache poisoning and manipulated password reset functionality.

The two names are used for the same family of problems. PortSwigger's Web Security Academy uses HTTP Host header attacks as the general name for attacks on websites that handle the value of the Host header in an unsafe way, and says those that inject a payload directly into the header are often known as Host header injection attacks. OWASP's testing guide uses the second name for its test.

PortSwigger defines it as a technique whereby an attacker manipulates a vulnerable website into generating a password reset link pointing to a domain under their control. The website sends a genuine email with a valid token, but the link leads to the attacker's server, which receives the token if the link is opened. OWASP's Forgot Password Cheat Sheet gives the fix: do not rely on the Host header while creating reset URLs, and hard-code the URL or validate it against a list of trusted domains.

It closes one door. A list such as Django's ALLOWED_HOSTS refuses names you do not serve, but in the lab the careless application still wrote links to its own second served name, which a list holding that name accepts. Django's documentation adds that its check only applies through get_host(), so code that reads the header directly bypasses it. Build links from one configured origin as well, and have the proxy in front overwrite forwarded host headers.

Only when a proxy you control writes it on every request. RFC 7239 says of the standard Forwarded header that it cannot be relied upon to be correct, because every node on the way to the server may modify it, including the client making the request. Express's guide to running behind proxies warns that with trust proxy set to true, it is important to ensure that the last reverse proxy trusted is removing or overwriting all of X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Proto, otherwise it may be possible for the client to provide any value. Django 6.1 leaves its support for the header off by default (USE_X_FORWARDED_HOST defaults to False in its settings reference) and says to enable it only if a proxy which sets this header is in use.

The products on this page differ. Django's documentation leads to a 400 response, Rails 8.1 answers 403 Forbidden by default, and the nginx examples return 444, a non-standard code that closes the connection. RFC 9110 defines 421 (Misdirected Request) for a request whose target does not match an origin the server has been configured for. The number matters less than the timing and the content: the refusal comes before any link is built, message is sent or response is stored, and nothing in it is built from the name that was sent.

Test your own application in a staging or test environment, with a test account whose mailbox you control and an invented name for the other host. Send a request with that name in the Host header, then one with your real name and an X-Forwarded-Host header, and read the origin in whatever was generated: the page, the redirect and the reset message. Run the cache check only on a path no visitor uses. The four-row test pack on this page sets out each request and the result to expect.

It can keep cached pages for different names apart, and it leaves the links your application writes as they were. Cloudflare's documentation, for example, lists the full URL, with its host, and the x-forwarded-host header in its default cache key. On this page's reading, that separates what that cache stores for one name from what it stores for another. A reset email is not a cached response, so no cache key changes the link inside it.

Scope the right test for your application

Tell us what you have built, who uses it and when it needs to launch. You will get a plain-English recommendation: a website audit, a web application test or a referral to a specialist.