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

[Bug?] TooManyHeaders(hdrs.X_FORWARDED_FOR) is raised due to duplicate IPs #79

Closed
mlaradji opened this issue Apr 11, 2019 · 3 comments
Closed

Comments

@mlaradji
Copy link

mlaradji commented Apr 11, 2019

I'm using aiohttp with aiohttp_remotes, and I want to execute the following line:

client_ip = aiohttp_remotes.XForwardedStrict(trusted=trusted_proxies).get_forwarded_for(request.headers)

This raises a TooManyHeaders(hdrs.X_FORWARDED_FOR) error. Upon inspection, I found that the line

request.headers.getall(hdrs.X_FORWARDED_FOR, [])

returns, in my case:

['192.168.0.1', '192.168.0.1']

I am still new to aiohttp_remotes, and I might be misunderstaning the error. However, it seems to me that duplicate IPs should not raise a TooManyHeaders error. If so, an easy fix would be to change the following line in aiohttp_remotes/x_forwarded.py

    def get_forwarded_for(self, headers):
        forwarded_for = headers.getall(hdrs.X_FORWARDED_FOR, [])
...

to

    def get_forwarded_for(self, headers):
        forwarded_for = list(set(headers.getall(hdrs.X_FORWARDED_FOR, [])))
...

System Information

  • aiohttp: 3.5.4
  • aiohttp_remotes: 0.1.2
  • Python: 3.6.7
@mlaradji mlaradji changed the title TooManyHeaders(hdrs.X_FORWARDED_FOR) is raised due to duplicate IPs [Bug?] TooManyHeaders(hdrs.X_FORWARDED_FOR) is raised due to duplicate IPs Apr 11, 2019
@asvetlov
Copy link
Member

Since X-Forwarded-* limitations there is a genuine conflict when any header is encountered more than once.
That's why the library raises an exception for such case.

Standard Forwarded header supports multiple occurrences, it is the preferable approach for very many reasons.

@mlaradji
Copy link
Author

Thanks for the reply. I need the X-Forwarded header since I'm using a reverse proxy.

As it turns out, the issue was my nginx config. It had the following line:

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

which caused duplicate X-Forwarded-For headers.

@asvetlov
Copy link
Member

You may be surprised but Forwarded can be used for reverse proxies as well ;)

Anyway, happy to know that the problem is solved.

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

No branches or pull requests

2 participants