#12093 Include client port in WSGI request environment#12096
#12093 Include client port in WSGI request environment#12096adiroiban merged 3 commits intotwisted:trunkfrom
Conversation
22f0d6c to
4876659
Compare
|
Thanks Daniel for the PR. Is this ready for review. No hurry. I just wanted to check the status for this. Cheers |
|
Thanks for checking @adiroiban, yes it is. |
adiroiban
left a comment
There was a problem hiding this comment.
Thanks for the fix.
In order to merge this PR, it needs updated tests
There is this exiting test for REMOTE_ADDR
twisted/src/twisted/web/test/test_wsgi.py
Lines 713 to 719 in 234f378
Can you use it as the foundation for writing a new test for the change from this PR?
Let me know if you need help with the test.
Regards
4876659 to
fcb2ebb
Compare
|
Thanks @adiroiban, I've added a new test for the remote port case (that matches the remote addr case). Let me know if the test is what you had in mind? |
| application contains the port of the client making the request. | ||
| """ | ||
| d = self.render("GET", "1.1", [], [""]) | ||
| d.addCallback(self.environKeyEqual("REMOTE_PORT", "12344")) |
There was a problem hiding this comment.
I bit hard to read and understand how we endedup with 12344.
I prefer an explicit test arrangement like in the IPv6 test.
but I think it's ok. it's not worse than what we have in trunk.
this is a minor comment
There was a problem hiding this comment.
Thanks @adiroiban, I noticed that the DummyChannel class in requesthelper.py set the port to 12344 by default, but I think being more explicit as you've suggested is always better. I've updated it.
Should I update test_remoteAddr() to do the same as well?
There was a problem hiding this comment.
Thanks. I think it would help to also update test_remoteAddr.
Let me know once this PR is ready for re-review.
Thanks again for the update
There was a problem hiding this comment.
Thanks @adiroiban, I've also applied the same update to test_remoteAddr
src/twisted/web/wsgi.py
Outdated
| self.environ = { | ||
| "REQUEST_METHOD": _wsgiString(request.method), | ||
| "REMOTE_ADDR": _wsgiString(request.getClientAddress().host), | ||
| "REMOTE_PORT": _wsgiString(str(request.getClientAddress().port)), |
There was a problem hiding this comment.
Minor comment. have something like remotePeer = request.getClientAddress() so that we call it once.
There was a problem hiding this comment.
Thanks @adiroiban, are you suggesting we add that in addition? i.e. still keep the existing REMOTE_PORT and REMOTE_ADDR and then also add:
"REMOTE_PEER": _wsgiString(request.getClientAddress()),
There was a problem hiding this comment.
My suggestion was something like this
peer = request.getClientAddress()
self.environ = {
"REQUEST_METHOD": _wsgiString(request.method),
"REMOTE_ADDR": _wsgiString(peer.host),
"REMOTE_PORT": _wsgiString(str(peer.port)),
}
There was a problem hiding this comment.
That makes sense, thanks for clarifying! I've made the update.
| @@ -0,0 +1 @@ | |||
| Include client port in WSGI response No newline at end of file | |||
There was a problem hiding this comment.
Is this a request or a response ?
| Include client port in WSGI response | |
| twisted.web.wsgi request environment now contains the peer port number. |
There was a problem hiding this comment.
typo in "containes" and "remote peer" is redundant. 😉
There was a problem hiding this comment.
updated :) thanks for the check.
There was a problem hiding this comment.
You are correct, thanks!
|
What spec does this change conform to? A link to the relevant section might be nice somewhere in here. |
|
As far as I can tell, this is not a standard environment variable https://github.com/joewalnes/websocketd/wiki/Environment-variables#remote_port There is REMOTE_HOST in the original CGI RFC https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.9 But I found this variable in the IIS implementatin https://learn.microsoft.com/en-us/iis/web-dev-reference/server-variables The port is not available in Apache HTTPD https://httpd.apache.org/docs/current/mod/mod_cgi.html But in modern days, I think that it helps to also have the port number ... at least for debug/audit/log |
therefore it inherits the CGI spec'd version of this variable. |
fcb2ebb to
4d566c5
Compare
Just checking - there is no CGI spec'd version of this variable, are we on the same page? |
That is corect. The RFC is only for the The I can see the draft for the CGI started on 1998 and was finalized in 2004. Since we already expose the peer's host, I think it's safe to also expose the port. yes... it will make the implementation non-portable. The following variables are standard / part of the RFC for the client-side part of the socket/connection:
For the server-side socket we have:
|
Sorry, I got confused between REMOTE_HOST and REMOTE_PEER. |
|
OK so here are some references I found for This book on CGI: https://www.cgi101.com/book/ch3/text.html "Abyss Web Server For Windows" https://aprelium.com/data/doc/2/abyssws-win-doc-html/cgivars.html This experimentally-determined environment dump from an Apache 2.4 httpd: https://stackoverflow.com/a/25521262/13564 Which is documented officially as a "variable" you can use in an "expression" https://httpd.apache.org/docs/2.4/expr.html#vars even if this isn't technically CGI IBM's web server: https://www.ibm.com/docs/en/i/7.2?topic=information-environment-variables the US Naval Academy's published course on CGI: https://www.usna.edu/Users/cs/adina/teaching/it350/fall2020/lectures/set06-cgi.html Cloudflare explicitly not supporting it: https://community.cloudflare.com/t/no-port-via-server-remote-port/37682/4 Apparently nginx's set of CGI or FastCGI variables are just specified in your own config, however you like? https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/ but the ASGI specification references it as a part of WSGI compatibility https://asgi.readthedocs.io/en/latest/specs/www.html#wsgi-compatibility So this seems like while it is not technically standardized anywhere, it is ad-hoc copy/pasted around frequently, and everyone seems to assume that it's been inherited from CGI, because some large majority of CGI environments do in fact set this variable, although probably because of a config file that gets distributed with default installs of popular web servers or tutorials for the same. |
|
Does it mean that we can implement it as defacto variable, without having to add any warning extra when someone will use it ? Warning in the sense, that they are using a non-standard variable and that their application might break if they try to migrate to Cloudfare... for example. I am not very familiar with CGI/WSGI. I know what they are expected to do, but I have never created a CGI application... other that the homeworks for CS classes (a very long time ago) :) |
Yeah, I think we can just document it. People seem to mostly expect it, and they can deal with Cloudflare when they use Cloudflare (although don't use Cloudflare, use Fastly…) |
b04eed3 to
7db900d
Compare
Thanks for all extra info and references @glyph. As you've pointed out, it is possible get
As for the documentation changes, is that something I can do during this PR? |
|
Hey @adiroiban, I just want to double-check if there is any other changes or updates I should do here? Thanks! |
adiroiban
left a comment
There was a problem hiding this comment.
Changes look good.
The missing part requested by Glyph
Yeah, I think we can just document it.
As this is a non-standard variable, I think it would help to have it documented in the docstring.
Maybe here ?
twisted/src/twisted/web/wsgi.py
Lines 533 to 545 in 63df84e
Other than that, I think that we can merge this.
Thanks!
| @@ -0,0 +1 @@ | |||
| twisted.web.wsgi request environment now contains the peer port number. No newline at end of file | |||
There was a problem hiding this comment.
Since there is no RFC for REMOTE_PORT , I think it's important to specify the name
| twisted.web.wsgi request environment now contains the peer port number. | |
| twisted.web.wsgi request environment now contains the peer port number as `REMOTE_PORT`. | |
b9f1f06 to
cdb2cc8
Compare
Thanks @adiroiban let me know if that doc was what you had in mind. |
src/twisted/web/wsgi.py
Outdated
| Note that the WSGI response environ includes a non-standard variable | ||
| REMOTE_PORT. | ||
|
|
There was a problem hiding this comment.
It's a bit hard for me to review this as I have never worked with WSGI app
I am reading some docs from https://docs.twisted.org/en/stable/web/howto/web-in-60/wsgi.html
Based on that, maybe we can have it like this.
I think that Note that is kind of redendand for API docs.
All the API doc content is a note that thing :)
| Note that the WSGI response environ includes a non-standard variable | |
| REMOTE_PORT. | |
| The C{environ} argument passed to the application, includes the C{REMOTE_PORT} key to complement the C{REMOTE_ADDR} key. | |
|
Thanks for the update. I left a comment, but I think that also your version is ok. Please review and let me know what you think. We can merge this, even with your version. I think that we all agree that the variable should be available to the applications. If documentation is not right, this can be updated in a follow up PR. Thanks again and sorry for the delay. A final rant :) WSGI is not my jam and I try to avoind using threads. |
9538ce2 to
bd615d6
Compare
for more information, see https://pre-commit.ci
|
Thanks for the review, @adiroiban. I updated the documentation in any case as suggested, with your wording :) Thank you! |
|
Thanks for your help with this patch. Happy to see this merged. @thinkst-daniel. Are you working for Thinkst? we are trying to (re)start a sponsorship program for Twisted. Thanks |
Thanks for the help @adiroiban! Yes, we’re still keen. Who should we reach out to? |
|
Hi The general Python Software Foundation donation email is psf-donations@python.org our contact at PSF is Phyllis Dobbs. You can also CC me adiroiban@gmail.com and I can followup up. Thanks again! |
|
Thanks @adiroiban, we'll reach out! |
Scope and purpose
Fixes #12093
Contributor Checklist:
This process applies to all pull requests - no matter how small.
Have a look at our developer documentation before submitting your Pull Request.
Below is a non-exhaustive list (as a reminder):
please review.Our bot will trigger the review process, by applying the pending review label
and requesting a review from the Twisted dev team.