Repository navigation
Honor Front Door X-Forwarded-Host in StaticHost - #1930
Conversation
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
There was a problem hiding this comment.
🟢 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-Hostforaspire.devand 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Summary
After the
aspire.devcutover to Azure Front Door (AFD), AFD calls the App Service origin withHost: <origin>.azurewebsites.netand sends the requested host inX-Forwarded-Host. StaticHost ignored that header, which breaks two things on the AFD path:UseDefaultFilestrailing-slash redirect builds an absolute URL fromRequest.Host, so slashless directory URLs such asaspire.dev/docs,get.aspire.dev(->/get-started/install-cli) andconf.aspire.dev(->/aspireconf) end up on the*.azurewebsites.nethost.OneDSTelemetryService.TrackDownloadonly records a download when{Scheme}://{Host}ishttps://aspire.dev, so/install.shand/install.ps1telemetry is silently skipped.This change makes StaticHost honor
X-Forwarded-Hostfor the hosts it is configured with, and has the AppHost provide them:AddForwardedClientIp) readsForwardedHeaders:AllowedHosts, separated by;or,. Each host is added toForwardedHeadersOptions.AllowedHosts, andForwardedHeaders.XForwardedHostis 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 emptyAllowedHostsas "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.WithProductionForwardedHosts) adds aforwarded-hostsparameter, defaultaspire.dev;*.aspire.dev, and passes it to StaticHost asForwardedHeaders__AllowedHosts. It is publish mode only, soaspire runleaves it unset. The apex is listed because*.only matches subdomains.*.aspire.devalso matches multi-level names such aswww.dev.aspire.dev, so one default covers the dev, test and production zones.Notes:
*.azurefd.netand*.azurewebsites.nethostnames are intentionally not listed; requests through them keep the previous (origin host) behavior. Serving another domain means extending the parameter."AllowedHosts": "*"inappsettings.jsonis the unrelated host filtering setting and is unchanged.X-Forwarded-Hostthrough unchanged. If it appended its own value,ForwardLimit = 1would pick the wrong one and the fix would be ineffective but harmless.X-Azure-FDIDor App Service access restrictions).After deploy, verify without any DNS change:
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.Extensions.cstaken frommain: 18 failed, 17 passed. The 18 failures are the cases that needX-Forwarded-Hosthonored.X-Forwarded-Hosttemporarily enabled even when no hosts are configured (edit reverted): 6 failed, 29 passed, so the "empty means off" guard is covered.AllowedHostsinstead 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 theforwarded-hostsparameter (defaultaspire.dev;*.aspire.dev), and theaspiredevwebsite bicep sets theForwardedHeaders__AllowedHostsapp setting from it.Live.Redistests this time (they need Docker, which was not running locally; an earlier run failed only withContainer 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.