Re: internal vs. external hostname in Request.getHost9)

Glyph Lefkowitz <[email protected]> Tue, 14 Mar 2017 00:10:30 -0700
Newsgroups gmane.comp.python.twisted.web
Message-ID <[email protected]>
--===============2343896208596940366==
Content-Type: multipart/alternative;
 boundary="Apple-Mail=_6943356F-F637-4026-84CE-A901FCF270AC"


--Apple-Mail=_6943356F-F637-4026-84CE-A901FCF270AC
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8


> On Mar 13, 2017, at 11:01 PM, Ilya Skriblovsky =
<[email protected]> wrote:
>=20
> Hi,
>=20
> I'm using Twisted Web server behind Nginx reverse-proxy and I'm =
getting backend's internal host:port from Request.getHost().
>=20
> Seems like Request.host is explicitly set to socket's address (i.e. =
internal address) here: =
=E2=80=8Bhttps://github.com/twisted/twisted/blob/trunk/src/twisted/web/htt=
p.py#L838 =
<https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L83=
8> But comment at =
=E2=80=8Bhttps://github.com/twisted/twisted/blob/trunk/src/twisted/web/htt=
p.py#L1297 =
<https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http.py#L12=
97> and what this method does points that Request.host meant to reflect =
Host header of the request, i.e. user-visible hostname and port.
>=20
> This creates problems for me when using Klein because it correctly =
uses Request.getHost() to create host part of URLs for redirects.
>=20
> 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?


You're absolutely correct!  I even filed a ticket for this =
functionality, 5 years ago: https://twistedmatrix.com/trac/ticket/5807  =
There's even a branch for it.  Oddly enough we do have a private =
_XForwardedForRequest, but... it's only used for logging, for some =
reason.

If you want accurate access logging and request information, =
https://twistedmatrix.com/trac/ticket/7704 will probably also be of =
interest to you.

I'm so sorry you've hit this glaring deficiency in Twisted.

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 incredibly 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.

-glyph=

--Apple-Mail=_6943356F-F637-4026-84CE-A901FCF270AC
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D""><br class=3D""><div><blockquote type=3D"cite" class=3D""><div =
class=3D"">On Mar 13, 2017, at 11:01 PM, Ilya Skriblovsky &lt;<a =
href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div class=3D""><div dir=3D"ltr" =
class=3D""><div class=3D"">Hi,</div><div class=3D""><br =
class=3D""></div><div class=3D"">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=3D""><br class=3D""></div><div =
class=3D"">Seems like Request.host is explicitly set to socket's address =
(i.e. internal address) here: =E2=80=8B<a =
href=3D"https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http=
.py#L838" =
class=3D"">https://github.com/twisted/twisted/blob/trunk/src/twisted/web/h=
ttp.py#L838</a> But comment at =E2=80=8B<a =
href=3D"https://github.com/twisted/twisted/blob/trunk/src/twisted/web/http=
.py#L1297" =
class=3D"">https://github.com/twisted/twisted/blob/trunk/src/twisted/web/h=
ttp.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=3D""><br class=3D""></div><div class=3D"">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=3D""><br class=3D""></div><div class=3D"">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><br =
class=3D""></div><div>You're absolutely correct! &nbsp;I even filed a =
ticket for this functionality, 5 years ago:&nbsp;<a =
href=3D"https://twistedmatrix.com/trac/ticket/5807" =
class=3D"">https://twistedmatrix.com/trac/ticket/5807</a> &nbsp;There's =
even a branch for it. &nbsp;Oddly enough we <i class=3D"">do</i> have a =
<i class=3D"">private</i> _XForwardedForRequest, but... it's only used =
for logging, for some reason.</div><div><br =
class=3D""></div><div><div><div>If you want accurate access logging and =
request information,&nbsp;<a =
href=3D"https://twistedmatrix.com/trac/ticket/7704" =
class=3D"">https://twistedmatrix.com/trac/ticket/7704</a> will probably =
also be of interest to you.</div><div class=3D""><br =
class=3D""></div></div></div><div><div>I'm so sorry you've hit this =
glaring deficiency in Twisted.</div><div><div><br =
class=3D""></div></div><div>On the other hand: I'm so glad that you've =
hit this glaring deficiency in Twisted! &nbsp;I hope you will be =
motivated to fix it :-). &nbsp;It's bothered me for quite some time that =
we don't play nicely with proxying setups, when such setups are so <i =
class=3D"">incredibly</i>&nbsp;common. &nbsp;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><br =
class=3D""></div><div>-glyph</div></body></html>=

--Apple-Mail=_6943356F-F637-4026-84CE-A901FCF270AC--


--===============2343896208596940366==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Twisted-web mailing list
[email protected]
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web

--===============2343896208596940366==--