-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a default Host header when it's missing/empty #5909
Comments
As noted in other thread this would fix issues related to using HaProxy and aspnetcore - would like to have a fix sooner rather than later as its a slightly big problem for me right now. All health checks from HaProxy are failing by default - currently investigating workarounds.. including having haproxy use http/1.1 or disabling logging somehow Workaround for HaProxy is like so:
or if like me you are using
|
@Volak you can work around the issue right now by implementing your own |
cc @glennc |
Note to self, Host includes a port that's scheme specific. In a reverse proxy scenario we would not know what that port would be, nor would Hosting be aware of the public scheme as forwarders would not be applied yet. Should apps rely on x-forwarded-host to override the default value in that scenario? If the config value does not include a port then Hosting can add one by looking at the local port. |
Note to you. Any 1/2 decent reverse proxy with rewrite said header if it's
an issue
|
@glennc I found a way to provide an default host value. See the updated proposal above. |
Would that happen on every request? |
Only requests without a host header, and there'd be room for caching. |
Bringing back to triage |
@glennc @shirhatti this feature is more relevant for HTTP/2 where the Host/Authority is optional. |
Kestrel is getting used more as an edge server. Bumping from backlog for re-triage. What do IIS/Http.Sys do? I assume they require a host for routing. |
Thanks for contacting us. We're moving this issue to the |
Just ran into this with a pretty basic Kestrel server on our VMs:
Fiddler confirms this occurs due to requests without a Host header. Eg:
My new workaround, in case it helps anyone, is a small piece of middleware: // Usage: app.UseMiddleware<AddHostHeader>();
public class AddHostHeader
{
public AddHostHeader(RequestDelegate next)
{
Next = next;
}
RequestDelegate Next { get; }
public async Task Invoke(HttpContext httpContext)
{
if (!httpContext.Request.Headers.ContainsKey("Host"))
httpContext.Request.Headers["Host"] = "UNKNOWN-HOST";
await Next(httpContext);
}
} "UNKNOWN-HOST" is what the AppInsights team used in their issue Requests with the url http:/// causes the exception. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
For the empty case, isn't the required behavior called out in the HTTP RFCs as copied below?
|
Also fwiw, the RFCs also call out that compliant implementations MUST reject http/https URIs without a specified authority, like in the
|
Those are referring to requests formatted like this: GET http://host:port/path HTTP/1.1 but the more common format looks like this: GET /path HTTP/1.1 In this case the Host header is required to be present, but it's allowed to be empty: GET /path HTTP/1.1 Luckily few clients actually do this. |
Ah OK, the original post in that appinsights thread was misleading - they initially mentioned that it was doing something like So that's why I was confused, I thought they were talking about an absolute-type request. In a later comment that I missed reading, they clarified that the actual call was more like what you showed. |
Just for my own education I went looking, and it seems that the RFC covers the actual scenario that you're talking about, including substituting a default when the Host header is empty (if that's safe in the given situation)
|
RE: microsoft/ApplicationInsights-aspnetcore#278
HTTP/1.0 requests do not provide Host headers, and HTTP/1.1 requests are required to provide it but it may be empty. This causes failures for components like loggers that want to describe the request with a Uri, or middleware that create absolute links like redirect-to-https. This is a protocol level problem that applies to supported servers.
Proposal: Hosting can provide a default host setting (from config) and apply it whenever the host header is missing or empty. This would happen right when the request is received and before any diagnostics take place:
https://github.com/aspnet/Hosting/blob/9f1e6607dd1b3d15bc6c42146629677c6b455fd0/src/Microsoft.AspNetCore.Hosting/Internal/HostingApplication.cs#L35-L37
Updated proposal
In the absence of config, inspect IServerAddresses for a url that matches the request scheme and port. The new Https redirect middleware does something similar:
https://github.com/aspnet/BasicMiddleware/blob/dd038387285bf9fa8dc52910ef762b9843ff22e4/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionMiddleware.cs#L122-L139
Workarounds:
The text was updated successfully, but these errors were encountered: