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

X-Forwarded-For handling appears incorrect #1840

Closed
leynos opened this issue Sep 22, 2016 · 2 comments
Closed

X-Forwarded-For handling appears incorrect #1840

leynos opened this issue Sep 22, 2016 · 2 comments

Comments

@leynos
Copy link
Contributor

leynos commented Sep 22, 2016

The handling of the X-Forwarded-For header appears to be incorrect.

Squid and Nginx append each new step in the chain to the end of the list. See the Wikipedia article, which cites the Squid documentation here ("header values are iterated in reverse order"). The format given on Wikipedia is:

X-Forwarded-For: client, proxy1, proxy2

This is consistent with my experience of Nginx, which has passed the client IP as the first address in the chain.

The behaviour in question (line 229 of httpserver.py):

ip = ip.split(',')[-1].strip()

A more correct implementation would be ip = ip.split(',')[0].strip()

Furthermore, the value in question may contain a port number. This is handled by Nginx, for example (see here). Tornado strictly rejects this value if it not an ip address. It may be worth stripping off any port suffix, by, e.g.:

if (ip.startswith('[') and not ip.endswith(']')) or ('.' in ip and ':' in ip):
    ip = ip.rsplit(':', 1)[0]

The first clause of the "if" condition checks for IPv6 addresses with a port stated, and the second checks for IPv4 addresses with a port. An alternative might be to use urlparse.

I believe this would provide a more useful implementation of the X-Forwarded-For handler where multiple proxies are chained.

@leynos
Copy link
Contributor Author

leynos commented Sep 22, 2016

My apologies. I see that the ordering issue is already discussed here, and that it is the intended behaviour:

#904

If Tornado were to accept a list of trusted IP addresses and implement a "pruning" capability for X-Forwarded-For comparable to Nginx and Squid's in this respect, would this be an acceptable approach?

I could have a go at implementing a patch to support this if it would be considered.

@bdarnell
Copy link
Member

bdarnell commented Mar 1, 2019

Looks like this can be closed now that #1864 was merged.

@bdarnell bdarnell closed this as completed Mar 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants