Skip to content

Honor Front Door X-Forwarded-Host in StaticHost - #1930

Merged
David Pine (IEvangelist) merged 3 commits into
mainfrom
ievangelist-statichost-forwarded-host
Oct 20, 2026
Merged

David Pine (IEvangelist) merged 3 commits into
mainfrom
ievangelist-statichost-forwarded-host

Conversation

@IEvangelist

@IEvangelist David Pine (IEvangelist) commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Summary

After the aspire.dev cutover to Azure Front Door (AFD), AFD calls the App Service origin with Host: <origin>.azurewebsites.net and sends the requested host in X-Forwarded-Host. StaticHost ignored that header, which breaks two things on the AFD path:

  • The UseDefaultFiles trailing-slash redirect builds an absolute URL from Request.Host, so slashless directory URLs such as aspire.dev/docs, get.aspire.dev (-> /get-started/install-cli) and conf.aspire.dev (-> /aspireconf) end up on the *.azurewebsites.net host.
  • OneDSTelemetryService.TrackDownload only records a download when {Scheme}://{Host} is https://aspire.dev, so /install.sh and /install.ps1 telemetry is silently skipped.

This change makes StaticHost honor X-Forwarded-Host for the hosts it is configured with, and has the AppHost provide them:

  • StaticHost (AddForwardedClientIp) reads ForwardedHeaders:AllowedHosts, separated by ; or ,. Each host is added to ForwardedHeadersOptions.AllowedHosts, and ForwardedHeaders.XForwardedHost is enabled only when that list is non-empty. A forwarded host outside the list is ignored and the request host is kept. The header stays off for an empty list because the middleware treats an empty AllowedHosts as "allow any host". So local runs and any deployment that does not set it keep the previous behavior. Client IP handling (X-Azure-ClientIP, ForwardLimit = 1) is unchanged.
  • AppHost (WithProductionForwardedHosts) adds a forwarded-hosts parameter, default aspire.dev;*.aspire.dev, and passes it to StaticHost as ForwardedHeaders__AllowedHosts. It is publish mode only, so aspire run leaves it unset. The apex is listed because *. only matches subdomains. *.aspire.dev also matches multi-level names such as www.dev.aspire.dev, so one default covers the dev, test and production zones.

Notes:

  • *.azurefd.net and *.azurewebsites.net hostnames are intentionally not listed; requests through them keep the previous (origin host) behavior. Serving another domain means extending the parameter.
  • The root "AllowedHosts": "*" in appsettings.json is the unrelated host filtering setting and is unchanged.
  • The forwarded host is used for redirects and telemetry only, never for authorization.
  • Assumption to confirm after deploy: App Service passes X-Forwarded-Host through unchanged. If it appended its own value, ForwardLimit = 1 would pick the wrong one and the fix would be ineffective but harmless.
  • Follow-up, not in this PR: restrict the origin to Front Door (X-Azure-FDID or App Service access restrictions).

After deploy, verify without any DNS change:

$ip = (Resolve-DnsName aspire.dev -Type A -Server ns1-06.azure-dns.com -DnsOnly | ? Type -eq A | select -First 1).IPAddress
curl.exe -sS -o NUL -w "%{redirect_url}`n" --resolve "aspire.dev:443:$ip" https://aspire.dev/docs

Expected: https://aspire.dev/docs/, not *.azurewebsites.net.

Third-party links and affiliations

None

Validation

  • dotnet test tests\StaticHost.Tests\StaticHost.Tests.csproj --filter "FullyQualifiedName~ForwardedHostTests": passed, 35 of 35.
  • Same command with Extensions.cs taken from main: 18 failed, 17 passed. The 18 failures are the cases that need X-Forwarded-Host honored.
  • Same command with X-Forwarded-Host temporarily enabled even when no hosts are configured (edit reverted): 6 failed, 29 passed, so the "empty means off" guard is covered.
  • Same command with the configured hosts assigned to AllowedHosts instead of added (edit reverted): 1 failed, 34 passed, so keeping hosts set by other startup code is covered.
  • dotnet test tests\StaticHost.Tests\StaticHost.Tests.csproj --filter "FullyQualifiedName!~Live.Redis": passed, 332 of 332.
  • dotnet build src\apphost\Aspire.Dev.AppHost\Aspire.Dev.AppHost.csproj --nologo -v minimal: succeeded, 0 errors.
  • dotnet run --no-build --project src\apphost\Aspire.Dev.AppHost\Aspire.Dev.AppHost.csproj -- --publisher manifest --output-path <temp>\manifest.json: the manifest has the forwarded-hosts parameter (default aspire.dev;*.aspire.dev), and the aspiredev website bicep sets the ForwardedHeaders__AllowedHosts app setting from it.
  • Not run: the Live.Redis tests this time (they need Docker, which was not running locally; an earlier run failed only with Container runtime 'docker' was found but appears to be unhealthy, and they do not touch this code), the frontend build (no frontend files changed), and the post-deploy check above.

Front Door calls the App Service origin with the origin host name and sends the requested host in X-Forwarded-Host. StaticHost ignored it, so the UseDefaultFiles trailing slash redirect (for example /docs and get.aspire.dev) sent browsers to the *.azurewebsites.net host, and the download telemetry origin check never matched https://aspire.dev.

Honor X-Forwarded-Host, restricted to aspire.dev and *.aspire.dev because the origin is also reachable directly. Hosts outside the list are ignored and the origin host is kept.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 90ef1a20-cd65-43f3-97cd-6fa484d8b0a4
Copilot AI balanced review requested due to automatic review settings October 20, 2026 17:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is narrow and has focused regression coverage; live App Service header pass-through remains a documented deployment check.

No open issues 🎉

What changed in this PR

StaticHost now honors allowed Front Door forwarded hosts, preserving public URLs in trailing-slash redirects and restoring download telemetry matching.

Changes:

  • Enable X-Forwarded-Host for aspire.dev and its subdomains.
  • Preserve existing client-IP forwarding behavior.
  • Add regression tests for redirects, rejected hosts, and combined header processing.
File Description
tests/​StaticHost.Tests/​ForwardedHostTests.cs Tests forwarded-host handling and redirect behavior.
tests/​StaticHost.Tests/​ForwardedHeadersTestServer.cs Adds an in-process server using production forwarding configuration.
src/​statichost/​StaticHost/​Extensions.cs Enables host forwarding with an explicit allowlist.

Review effort: Balanced

Read the hosts honored in X-Forwarded-Host from ForwardedHeaders:AllowedHosts
instead of hard-coding aspire.dev and *.aspire.dev. The AppHost sets it for
deployments through the forwarded-hosts parameter (default
aspire.dev;*.aspire.dev), so local runs and other deployments keep the request
host until they opt in.

The middleware treats an empty AllowedHosts as "any host", so X-Forwarded-Host
is only enabled when at least one host is configured.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 90ef1a20-cd65-43f3-97cd-6fa484d8b0a4

if (allowedHosts is { Length: > 0 })
{
options.ForwardedHeaders |= ForwardedHeaders.XForwardedHost;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it weird that these 2 options are coupled together? Should we just always set XForwardedHost unconditionally? And then configure the allowed hosts like we are.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept XForwardedHost conditional on purpose. ASP.NET Core treats an empty AllowedHosts as "allow any host", so enabling it unconditionally would make any deployment without the setting (local runs, a lost env var, a future AppHost change) trust a client-supplied X-Forwarded-Host. The origin is also reachable directly at *.azurewebsites.net, so a spoofed header could steer redirect URLs. In e966844 the allow-list is now configured unconditionally as you suggested, and the header is enabled only when the resulting list is non-empty. Deployed behavior is the same either way because the AppHost always sets the list. Happy to flip it if you still prefer unconditional.

Address review feedback on the forwarded host change: add each configured
host to ForwardedHeadersOptions.AllowedHosts so hosts set by other startup
code are kept, and honor X-Forwarded-Host once the list is non-empty, since
the middleware treats an empty list as "any host".

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 90ef1a20-cd65-43f3-97cd-6fa484d8b0a4
@IEvangelist
David Pine (IEvangelist) merged commit c26b59b into main Oct 20, 2026
11 checks passed
@IEvangelist
David Pine (IEvangelist) deleted the ievangelist-statichost-forwarded-host branch October 20, 2026 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants