<div dir="ltr">Tickets you have mentioned and <span style="font-family:"helvetica neue","bitstream vera sans",helvetica,arial,sans-serif">forwarded-for-5807 branch</span> are mostly about parsing X-Forwarded-For in order to obtain correct client IP. While it is valuable task, it is not what strikes me right now.<div><br></div><div>I'm now more concerned with an absence of API for getting user-visible server's name, not client's ip.</div><div><div><br class="inbox-inbox-Apple-interchange-newline">Look, I'm currently porting my app from Django to Klein and noticed strange behavior of Klein. For example:</div><div>@app.route('/alias', alias=True)</div><div>@app.route('/path')</div><div>def path(request): return b'42'</div><div><br></div><div>When /alias is requested werkzeug generates a redirect to /path. But Klein is passing Request.getHost() to Werkzeug, so redirect gets internal hostname and exposes backend's internal hostname and port to the user. Seems like Klein is passing incorrect hostname to Werkzeug. But how can we fix that?</div></div><div><br></div><div>There are two methods in Request:</div><div>• Request.getHost() — "Get my originally requesting transport's host" as doc says. Ok, seems like this method intentionally returns server's internal address.</div><div>• Request.getRequestHostname() —doc says:</div><div>>> "Get the hostname that the user passed in to the request. This will either use the Host: header (if it is available) or the host we are listening on if the header is unavailable."</div><div>Cool, but why does this method only returns a hostname without a port? It intentionally strips out the port number from Host header. What is the point of such implementation? This method is used only a couple of times inside Twisted itself, and in both places Twisted gets what getRequestHostname() returned and mixes it with request.getHost().port which is *definitely* incorrect, because the former is user-visible while latter is internal. So if my backend server is using different port than a fronend, it is impossible to use getRequestHostname() to build user-visible URL. I think current getRequestHostname() implementation is broken.<br></div><div><br></div><div>So I have two proposals:</div><div><br></div><div>Proposal #1 (fixing current behavior):</div><div>• Variant #1: Change Request.getRequestHostname() to return b"hostname:port". I think this is the correct thing to do, but this is a backward-incompatible change.</div><div>- or -</div><div>• Variant #2: Change Klein to use Request.getHeader(b'Host') with fallback to Request.getHost()</div><div><br></div><div>Proposal #2 (adding new feature if Variant #1 is choosed):</div><div>• Add useXForwardedHost=False argument to Request.getRequestHostname() and useXForwardedProto=False to Request.isSecure(). If True is passed, these methods will obey corresponding request headers that are de-facto standard for reverse proxies. Also add corresponding options to Klein app. This can simplify reverse proxy configuration a bit.</div><div><br></div><div>-- ilya<br></div></div><br><div class="gmail_quote"><div dir="ltr">вт, 14 мар. 2017 г. в 10:33, Ilya Skriblovsky <<a href="mailto:ilyaskriblovsky@gmail.com">ilyaskriblovsky@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">Thanks, I will study tickets you mentioned and hopefully fix it.<div class="gmail_msg">Quick-n-dirty fix gave me only two failed tests and in both cases it seems to be a wrong assumption in tests. So I hope this change won't break the world.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">-- ilya<br class="gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg"><div dir="ltr" class="gmail_msg">вт, 14 мар. 2017 г. в 10:12, Glyph Lefkowitz <<a href="mailto:glyph@twistedmatrix.com" class="gmail_msg" target="_blank">glyph@twistedmatrix.com</a>>:<br class="gmail_msg"></div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_quote gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Mar 13, 2017, at 11:01 PM, Ilya Skriblovsky <<a href="mailto:ilyaskriblovsky@gmail.com" class="gmail_msg" target="_blank">ilyaskriblovsky@gmail.com</a>> wrote:</div><br class="m_-8698632494139977386m_-3306553942595082654Apple-interchange-newline gmail_msg"><div class="gmail_msg"><div dir="ltr" class="gmail_msg"><div class="gmail_msg">Hi,</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I'm using Twisted Web server behind Nginx reverse-proxy and I'm getting backend's internal host:port from Request.getHost().</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Seems like Request.host is explicitly set to socket's address (i.e. internal address) here: ​<a href="https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L838" class="gmail_msg" target="_blank">https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L838</a> But comment at ​<a href="https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L1297" class="gmail_msg" target="_blank">https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L1297</a> and what this method does points that Request.host meant to reflect Host header of the request, i.e. user-visible hostname and port.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">This creates problems for me when using Klein because it correctly uses Request.getHost() to create host part of URLs for redirects.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">It seems like inconsistency in Twisted code. I'd expect Request.host should be only set from the Host request header to reflect user-visible hostname, not the internal backend server's address. Or may be I'm missing something?</div></div></div></blockquote></div><div class="gmail_msg"><br class="gmail_msg"></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">You're absolutely correct!  I even filed a ticket for this functionality, 5 years ago: <a href="https://twistedmatrix.com/trac/ticket/5807" class="gmail_msg" target="_blank">https://twistedmatrix.com/trac/ticket/5807</a>  There's even a branch for it.  Oddly enough we <i class="gmail_msg">do</i> have a <i class="gmail_msg">private</i> _XForwardedForRequest, but... it's only used for logging, for some reason.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">If you want accurate access logging and request information, <a href="https://twistedmatrix.com/trac/ticket/7704" class="gmail_msg" target="_blank">https://twistedmatrix.com/trac/ticket/7704</a> will probably also be of interest to you.</div><div class="gmail_msg"><br class="gmail_msg"></div></div></div><div class="gmail_msg"><div class="gmail_msg">I'm so sorry you've hit this glaring deficiency in Twisted.</div><div class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div></div><div class="gmail_msg">On the other hand: I'm so glad that you've hit this glaring deficiency in Twisted!  I hope you will be motivated to fix it :-).  It's bothered me for quite some time that we don't play nicely with proxying setups, when such setups are so <i class="gmail_msg">incredibly</i> common.  If you can write pull requests to fix these issues and put them into review, I'm pretty sure you will find an enthusiastic reviewer quickly.</div></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">-glyph</div></div>_______________________________________________<br class="gmail_msg">
Twisted-web mailing list<br class="gmail_msg">
<a href="mailto:Twisted-web@twistedmatrix.com" class="gmail_msg" target="_blank">Twisted-web@twistedmatrix.com</a><br class="gmail_msg">
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web" rel="noreferrer" class="gmail_msg" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web</a><br class="gmail_msg">
</blockquote></div></div></div></blockquote></div>