-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Invalid usage of X-Forwarded-For #1814
Comments
Thinking a little bit more, I don't think those changes were needed (at least not for Host, For, Proto and Port). If there is a need to support X-Forwarded-* headers, then aspnetcore offers the UseForwardedHeaders middleware that sets the correct values in the HttpRequest object. You just have to read them there and not read the headers. |
@NatMarchand thx for pointing out this mistake with |
I have few ideas to share tomorrow (it’s evening for me) in order to properly handle reverse proxies. In my opinion, the less is done in Swashbuckle the better it is as if it’s not done in aspnet pipeline before swashbuckle, same problems could arise further (ex: using urlhelper or using Created method in a controller). Last point I’m not sure it’s Swashbuckle concern to do all this plumbing 😄
|
@NatMarchand I wasn't aware of the
This certainly seems a lot cleaner and takes the responsibility away from SB, which I like :) The one caveat is that I don't see any mention of the |
I recently had to deal with such a prefix, unfortunately there’s no clear standard and the header name is specific to who is using it. I ended up creating a small middleware for this. Application Insights had same problem with this prefix, I contributed to take it in account. IMHO best place to deal with this is in the ForwardedHeader middleware.
|
Ok so, here are my 2cts. When having to deal with a reverse proxy using a prefix in the path, one should be careful.
Second configuration, the proxy is additive (adds the prefix): http://proxy/myroute -> http://server/api/myroute
Third configuration, the proxy is substractive (strips the prefix): http://proxy/api/myroute -> http://server/myroute
If you want to try these middleware you can use an api with this dummy controller:
IMHO, one argument against having it in Swashbuckle is that if you implement this logic inside, some features of aspnet and its ecosystem won't work correctly. For example in the controller, I use the UrlHelper to get the public facing url of the route, it uses the different values of HttpRequest (Scheme, Host, PathBase and Path). Not using one of the middleware above will lead to incorrect urls generated (this helper is used also when using method such as CreatedAt***(), AcceptedAt***() and RedirectTo***() and maybe at other places). Telemetry in Application Insights (and probably other APMs) will also display an incorrect value. Also keep in mind that X-Forwarded-* is a de-facto standard, a RFC (RFC7239) should replace all of this with one header to rule them all™ |
Sounds good. Based on this, my plan is as follows:
This means the |
Hi, Works as expected now. I'll open new ticket with other concerns. |
I'm glad to hear that this addition to 5.6.0 will be removed as it's tripping me up and I didn't previously have an issue. |
@domaindrivendev is this going into the 6.0 release or is this fixed in a >= 5.6.1 release which has no notes? I'm on 5.6.3 and still running into these issues where my server url is |
Hello there!
When I deploy a sample api app to Azure Web App (Windows) using the latest package (5.6.0), a servers property appears in the swagger json :
However, the url in the server is not at all a server : it's my client IP.
I've pinpointed the issue to the following PR : #1801
If you look closely at the "spec" of X-Forwarded-For (see here) the syntax is the following :
X-Forwarded-For: <client>, <proxy1>, <proxy2>
The PR introduced this line where it use the first value of X-Forwaded-For to create a base url. It then use X-Forwarded-Host, if available, to override the host.
However, on Azure WebApp, X-Forwarded-For is set but not X-Forwarded-Host (as the Host header is not rewritten). We end with a server url which in fact is the client. I'm not sure if this line is useful or we can try to use the second value :
In all cases, the first value of the array should never be use as it is the client ip.
I can make the PR if you tell me which solution you like.
The text was updated successfully, but these errors were encountered: