Skip to content
md ~/writing/host-basligi-zehirleme-sifre-sifirlama
All posts

· 6 min read

Host Header Poisoning: Where Does Your Password Reset Link Point?

Host Header Poisoning: Where Does Your Password Reset Link Point?

In most apps, the link in a password reset e-mail is built from the address the request came in on: send the user back to whichever site they came from. It sounds right, but that address is in the hands of whoever sends the request. This post is about the Host header poisoning hole I found in Casus, my party game, during the final review before release: how it put the password reset link at risk and how I closed it. I also show how I applied the same lesson from day one in Tamirhane.

Where does the link come from?

In ASP.NET Core, the short way to build a full URL in a controller is to pass the scheme to Url.Action:

// old code
Url.Action(nameof(Reset), "Account", new { userId = account.Id, code = Encode(token) }, Request.Scheme)!;

That line produces something like https://spygame.miracdeprem.com/account/reset?userId=...&code=.... The domain part comes from the request's Host header. So the link starts not with the site's own address but with the address the request claims to be for.

Host is an ordinary header the browser sends with every request. The browser fills it from the address bar, but an attacker who sends the request from their own program rather than a browser can write any value. If proxies sit in between, headers such as X-Forwarded-Host can play the same role.

The attack

Password reset works like this: the user enters their e-mail, the site sends a link with a one-time code to that address, and opening the link lets them set a new password. The code itself is safe: unguessable and only in the inbox. The problem is which address the link carrying it points to.

  1. The attacker sends a forgot password request with Ayşe's e-mail address and puts their own domain in the Host header: evil.example.
  2. The site generates a real, valid reset code and e-mails Ayşe. The e-mail really comes from Casus; the sender, look and text are right. But the link is https://evil.example/account/reset?userId=...&code=....
  3. Ayşe clicks it, thinking I didn't ask for this, but anyway, or out of curiosity. Her browser goes to the attacker's site and hands over the code in the URL.
  4. The attacker takes the code to the real site and changes Ayşe's password.

The nasty part is that everything the user can check is genuine: the e-mail, the sender, the code. The only fake thing is the link's domain.

Would it have worked in production?

That depends on the infrastructure in between. Casus runs on Render with Cloudflare in front, and a request with a made-up Host is rejected before it reaches the app. I tried it while writing this post: a request with a fake Host header got a 403.

But I did not want a security rule to depend on the hosting platform's routing details of the day. The platform can change, a proxy that trusts X-Forwarded-Host can be added, the app can run somewhere else. As long as the code itself carried the hole, safety was a coincidence.

The fix: the site's address comes from configuration

The principle is simple: the domain of links sent to users comes from the app's own configuration, not from the request. I put that in one place:

/// <summary>
/// The site's own address for links in e-mails and invites. In production it comes from
/// PUBLIC_ORIGIN, never from the request: a forged Host header would otherwise put someone
/// else's site in a password-reset e-mail, with a valid token in it.
/// </summary>
public static class PublicOrigin
{
    public static string Of(HttpContext http)
    {
        var configured = http.RequestServices.GetRequiredService<IConfiguration>()["PUBLIC_ORIGIN"];
        return string.IsNullOrWhiteSpace(configured) ? $"{http.Request.Scheme}://{http.Request.Host}" : configured.TrimEnd('/');
    }
}

Links now start with it, and Url.Action only builds the path:

PublicOrigin.Of(HttpContext) + Url.Action(nameof(Reset), "Account", new { userId = account.Id, code = Encode(token) });

In local development the setting can stay empty; the request's address is used then, because there is only one address locally anyway. In production, forgetting the setting would silently bring the hole back. So the app refuses to start without it:

if (builder.Environment.IsProduction() && string.IsNullOrWhiteSpace(builder.Configuration["PUBLIC_ORIGIN"]))
    throw new InvalidOperationException("PUBLIC_ORIGIN is required in production (e.g. https://spygame.miracdeprem.com).");

As a second layer I limited ASP.NET Core's AllowedHosts setting to the site's own addresses, so a request with a Host not on the list never reaches the application code:

- key: PUBLIC_ORIGIN
  value: "https://spygame.miracdeprem.com"
- key: AllowedHosts
  value: "spygame.miracdeprem.com;spy-game-87jd.onrender.com"

The list also includes the address Render assigned. Render had appended -87jd to the service name because spy-game was taken; in the first release I had listed the name I expected and had to correct it to the real one right after. A list like this is a setting that has to be updated whenever an address changes.

The same class is used for room invite links, so sharing a room code also produces a link that starts with the site's own address.

The test

The test covers an attacker asking for a password reset with a forged host header, and checks that the link in the e-mail starts with the site's own address:

[Fact]
public async Task Links_in_emails_use_the_sites_own_address_even_with_a_forged_host()
{
    var email = NewEmail();
    await ConfirmedAccountAsync(NewName("host"), email);
    var attacker = NewBrowser();
    attacker.Headers["X-Forwarded-Host"] = "evil.example";
    await attacker.SubmitAsync("/account/forgot", "/account/forgot", new() { ["Email"] = email });
    var html = factory.Emails.To(email)[^1].Html;
    Assert.Contains($"href=\"{GameFactory.PublicOrigin}/account/reset?", html);
    Assert.DoesNotContain("evil.example", html);
}

Tamirhane from day one

Having hit this once, I built links from configuration from the first day in Tamirhane, my repair shop app. E-mail links, the customer's tracking page link and the QR code on the intake receipt all start with the same address. The setting is read once at startup and its format is checked too: it must be a full https:// address with no path or query; http is accepted only locally.

if (!Uri.TryCreate(value, UriKind.Absolute, out var uri)
    || uri.AbsolutePath != "/" || !string.IsNullOrEmpty(uri.Query)
    || !(uri.Scheme == Uri.UriSchemeHttps || (uri.Scheme == Uri.UriSchemeHttp && uri.IsLoopback)))
    throw new InvalidOperationException(
        "PUBLIC_ORIGIN geçersiz: \"https://alan.adi\" biçiminde olmalı (http sadece localhost için).");

What I took away

  • The domain of a link sent to a user must not come from the request. Host and X-Forwarded-Host are in the sender's hands. The site's address is configuration.
  • If forgetting a setting could silently bring a hole back, the app should not start without it. The mistake shows up at release, not months later.
  • Protection from the infrastructure is a bonus, not the foundation. The platform may reject forged requests today; the code still has to be right. A second layer such as AllowedHosts is cheap and harmless.
  • The code can be fine while the link it travels in is not. The reset code was unguessable and single-use; the problem was who it was delivered to.