statiqdev / Statiq.Web

Statiq Web is a flexible static site generator written in .NET.
https://statiq.dev/web
Other
1.64k stars 235 forks source link

Preview server appears to redirect preview files to bound hostname #925

Closed daveaglick closed 3 years ago

daveaglick commented 3 years ago

For example when using GitHub Codespaces to forward a port, the host name of the proxy is not localhost. When a URL that relies on default filenames (I.e. an /foo/index.html is requested as /foo) the preview server redirects/rewrites the URL to https://localhost/foo. This doesn’t appear to be happening with other middleware like default extensions (probably because the default file name support is built-in to Kestrel while the other stuff is custom).

daveaglick commented 3 years ago

I'm having trouble replicating this locally, so might be something about the way GitHub Codespaces forwards ports.

daveaglick commented 3 years ago

It appears the 301 is being issued by Kestrel - I can now reproduce (I always could, just didn't realize it). When a URL like /foo/bar is requested and there's no /bar file, a 301 is issued to /foo/bar/ and then the default file logic kicks in. The trick will be to figure out what's issuing the 301 and have it use the current host header instead of localhost.

Possibly related: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-3.1

I imagine this doesn't happen when testing with an alternate DNS name locally because the alternate DNS is actually set by the client in the request. When issued through a proxy, the proxy issues the request for localhost and the server never sees the original host header.

I'll try adding UseForwardedHeaders() and we'll see if that resolves it.

daveaglick commented 3 years ago

It appears to be caused by UseDefaultFiles:

image

Here's the flow:

daveaglick commented 3 years ago

Confirmed working now