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

Respect X-Forward-* headers and generate correct URLs in url_for #1465

Merged
merged 12 commits into from
Jul 4, 2019

Conversation

ZigZagT
Copy link

@ZigZagT ZigZagT commented Jan 8, 2019

@sjsadowski
Copy link
Contributor

I think if you pull the master branch as of this morning with the updated pytest, the build checks that are failing due to pytest version conflict will be resolved.

@codecov
Copy link

codecov bot commented Jan 8, 2019

Codecov Report

Merging #1465 into master will increase coverage by 0.07%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1465      +/-   ##
==========================================
+ Coverage   91.59%   91.67%   +0.07%     
==========================================
  Files          19       19              
  Lines        2082     2101      +19     
  Branches      390      393       +3     
==========================================
+ Hits         1907     1926      +19     
  Misses        137      137              
  Partials       38       38
Impacted Files Coverage Δ
sanic/request.py 97.58% <100%> (+0.2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 966b05b...c2b6451. Read the comment docs.

sanic/request.py Outdated Show resolved Hide resolved
sanic/request.py Show resolved Hide resolved
'X-Forwarded-Proto': 'https',
})
assert app.url_for('view_name') == '/another_view'
assert app.url_for('view_name', _external=True) == 'http:///another_view'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That result is fishy. http:/// ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the missing UT case for Sanic.url_for. But there's no good way to fix it since getting a valid hostname needs Request context as in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can provide some kwargs to url_for, such as _server that can have your valid hostname embed into, like _server=request.headers.get("Host") or _scheme, like _scheme=request.headers.get("X-Forwarded-Proto"). You can check most of them here. I think we should properly document those kwargs from the url_for method.

Copy link
Member

@vltr vltr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First: why is the url_for method mentioned in the PR title?

Furthermore, I think this might be a delicate question to handle. As @harshanarayana mentioned, some may have X-Url-Scheme while others just X-Scheme and I particularly don't like the way Sanic does this right now, to be fair.

I really like the aiohttp-remotes project, which gives you some sort of flexibility over this same issue (amongst others). We could lean toward this direction, at least when it comes to flexibility. Perhaps, instead of have all these header values hard-coded inside Sanic, make them somewhat configurable? This way, I can put for X-My-Special-Scheme if I want to 😉

@ZigZagT
Copy link
Author

ZigZagT commented Jan 9, 2019

Hi, @vltr @harshanarayana thanks for the comments. Regarding the header naming problem, X-Fordered-* are documented in MDN and widely used. X-Scheme is used by HAProxy, which is also a widely used name for years.

There're only a limited number of headers used like de-facto standard, and we don't need to support fully customized headers like X-My-Special-Scheme.

In this PR I'm mainly trying to make Sanic works with Nginx / HAProxy out of the box. The flexibility requirements could be addressed as another issue.

Besides, there's also a standard Forwarded header not implemented in this PR because I personaly don't have much experience handle multi field headers in practice. I would be really appreciating if someone can help.

Also, as meintioned by @andreymal in #801

looks unconfigurable and unsafe

It's surely a security risk using these headers, though in a good production setup it's not a issue since these headers would be filtered out or replaced by a proxy layer, but it still nice to have some configuration options to control this behavior.

@vltr
Copy link
Member

vltr commented Jan 10, 2019

Hi, @vltr @harshanarayana thanks for the comments. Regarding the header naming problem, X-Fordered-* are documented in MDN and widely used. X-Scheme is used by HAProxy, which is also a widely used name for years.

There're only a limited number of headers used like de-facto standard, and we don't need to support fully customized headers like X-My-Special-Scheme.

In this PR I'm mainly trying to make Sanic works with Nginx / HAProxy out of the box. The flexibility requirements could be addressed as another issue.

I understand you completely. There's a lot of other tools out there (such as AWS (ELB), Cloudflare, etc) that might have other headers as well and that's why I thought on making them somewhat configurable: we'll be swiping two problems at once. @huge-success/sanic-core-devs , your input would be mostly welcome on this.

@sjsadowski
Copy link
Contributor

I think we do need to implement X-Forwarded-* - they're included both by common load balancers and for application routers like Traefik. I'm fine with making them configurable in the future, but I don't know that future features should be a blocker for this PR.

@sjsadowski sjsadowski added the needs review the PR appears ready but requires a review label Mar 3, 2019
@sjsadowski
Copy link
Contributor

@vltr @harshanarayana could you review again when you get a chance?

@andreymal
Copy link
Contributor

@BananaWanted can you fix/rebase/rework this PR? I don't know what the Sanic maintainers think about it now, but I think this is an important change, but it seems that it conflicts with my PR #1539

@ahopkins
Copy link
Member

@andreymal Speaking for myself, I agree that it is important.

@ahopkins
Copy link
Member

ahopkins commented Jul 3, 2019

@yunstanford or @sjsadowski Can you run this as a "squash and merge"?

@sjsadowski sjsadowski merged commit 72b4456 into sanic-org:master Jul 4, 2019
@ahopkins ahopkins removed the needs review the PR appears ready but requires a review label Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants