Swashbuckle.aspnetcore: Exception within method GetHostOrNullFromRequest in SwaggerMiddleware

Created on 24 Sep 2020  路  9Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Hi,

I get the below exception when running Swagger 5.6.0 on our servers. Probably the load balancer set a bad or empty X-Forwarded-Host header. Can you please surround the method with try/catch and in case of exception, return null? Or, another way would be to optionally set the host in startup for swagger.

System.UriFormatException at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.GetHostOrNullFromRequest

Most helpful comment

@domaindrivendev when do you think the v6.0.0 is going to be released? We cannot use nuget sources other than nuget.org, so it would be great to have the fix in there.

All 9 comments

Hi there,

We also facing same issue and the issue is that our internal proxy adds following headers:

X-Forwarded-Host : <proxy-one>, <proxy-two>
X-Forwarded-Server : <server-one>, <server-two>

The swashbuckle code on the other hand executes following:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/1a2b824ebff66b68e00cbe6d3027a2d070476186/src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs#L72

Takes [0] element but does not split it with comma therefore we get following error:
Unhandled exception caught in API: "Invalid URI: The hostname could not be parsed." at UriFormatException { TargetSite: Void CreateThis(System.String, Boolean, System.UriKind), StackTrace: " at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) at System.Uri..ctor(String uriString, UriKind uriKind) at System.UriBuilder..ctor(String uri) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.GetHostOrNullFromRequest(HttpRequest request) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at DB.MobilePay.Shared.Rest.AspNetCore.ExceptionHandling.Middleware.ExceptionHandlingMiddleware.Invoke(HttpContext httpContext)", Message: "Invalid URI: The hostname could not be parsed.", Data: [], InnerException: null, HelpLink: null, Source: "System.Private.Uri", HResult: -2146233033 }

I believe this has to be fixed to support comma separated host/server list. Let me know if I should raise PR for this because it is blocking update to new version.

Thanks,
Simonas

@domaindrivendev - when can we expect to have v6.0.0 released?

@simluk I forked the SwaggerMiddleware.cs and just return null in GetHostOrNullFromRequest method instead. Maybe this is a mitigation that works for you, too, until the fix is released.

@simluk. It will likely be a week or two but you can pull down preview packages built from latest master from myget.org in the meantime.

See https://myget.org/feed/domaindrivendev/package/nuget/Swashbuckle.AspNetCore

@domaindrivendev when do you think the v6.0.0 is going to be released? We cannot use nuget sources other than nuget.org, so it would be great to have the fix in there.

Hi,
any update on this?

Hi,
any update on this?

hello
same issue here, any update on when this will be fix ?

aha , same mistake,
image

Was this page helpful?
0 / 5 - 0 ratings