Skip to content
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

Reverse proxy handling only takes the first letter of X-Forwarded-Host on Asp.net/Owin #2366

Closed
urhot opened this issue Aug 22, 2019 · 2 comments

Comments

@urhot
Copy link

urhot commented Aug 22, 2019

There seems to be an issue with the reverse proxy handling introduced in PR #2196

If the X-Forwarded-Host header is set, the API document is generated incorrectly with the "host" value being set to a single character.

Without X-Forwarded-Host header:

% curl --silent "http://localhost/SwaggerTest1/swagger/v1/swagger.json"|grep "host"
"host": "localhost",

With X-Forwarded-Host header:

% curl --silent -H "X-Forwarded-Host: example.com" "http://localhost/SwaggerTest1/swagger/v1/swagger.json"|grep "host"
"host": "e",

By looking at the code, I assume the problem is the assumption that request.Headers[key] returns a list, while in fact it returns a string:

6734543#diff-db5f97cf1d06cc084f0b03b789e44bcfR41

var baseUrl = request.Headers.ContainsKey("X-Forwarded-Host") ?
                new Uri($"{request.GetHttpScheme()}://{request.Headers["X-Forwarded-Host"].First()}").ToString().TrimEnd('/') :
                new Uri($"{request.GetHttpScheme()}://{request.Host}").ToString().TrimEnd('/');

I've only tested with Owin, and I don't know if the same issue exists without it.

My test environment (a WebAPI project):

OwinStartup.cs:

[assembly: OwinStartup(typeof(SwaggerTest1.OwinStartup))]
namespace SwaggerTest1
{
    public class OwinStartup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseSwaggerUi3(typeof(OwinStartup).Assembly, settings => {
            });

            if (HostingEnvironment.IsHosted) {
                GlobalConfiguration.Configure(ConfigureWebApi);
            } else {
                var config = new HttpConfiguration();
                ConfigureWebApi(config);
                app.UseWebApi(config);
            }
        }

        public static void ConfigureWebApi(HttpConfiguration config)
        {
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

            config.MapHttpAttributeRoutes();
            config.EnsureInitialized();
        }
    }
}

NuGet packages:

PM> get-package
Id                                  Versions  ProjectName                                                                                                                        
--                                  --------  -----------                                                                                                                        
Microsoft.AspNet.WebApi             {5.2.4}   SwaggerTest1                                                                                                                       
Microsoft.AspNet.WebApi.Client      {5.2.7}   SwaggerTest1                                                                                                                       
Microsoft.AspNet.WebApi.Core        {5.2.7}   SwaggerTest1                                                                                                                       
Microsoft.AspNet.WebApi.Owin        {5.2.7}   SwaggerTest1                                                                                                                       
Microsoft.AspNet.WebApi.WebHost     {5.2.4}   SwaggerTest1                                                                                                                       
Microsoft.CodeDom.Providers.DotN... {2.0.0}   SwaggerTest1                                                                                                                       
Microsoft.Owin                      {4.0.1}   SwaggerTest1                                                                                                                       
Microsoft.Owin.FileSystems          {3.0.1}   SwaggerTest1                                                                                                                       
Microsoft.Owin.Host.SystemWeb       {4.0.1}   SwaggerTest1                                                                                                                       
Microsoft.Owin.StaticFiles          {3.0.1}   SwaggerTest1                                                                                                                       
Namotion.Reflection                 {1.0.6}   SwaggerTest1                                                                                                                       
Newtonsoft.Json                     {11.0.1}  SwaggerTest1                                                                                                                       
NJsonSchema                         {10.0.22} SwaggerTest1                                                                                                                       
NSwag.Annotations                   {13.0.5}  SwaggerTest1                                                                                                                       
NSwag.AspNet.Owin                   {13.0.5}  SwaggerTest1                                                                                                                       
NSwag.Core                          {13.0.5}  SwaggerTest1                                                                                                                       
NSwag.Generation                    {13.0.5}  SwaggerTest1                                                                                                                       
NSwag.Generation.WebApi             {13.0.5}  SwaggerTest1                                                                                                                       
Owin                                {1.0}     SwaggerTest1     
@urhot
Copy link
Author

urhot commented Aug 22, 2019

image

@RicoSuter
Copy link
Owner

Its probably a diff between owin and aspnetcore (string vs string[])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants