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

Web: get real ip (on proxy use) #89

Closed
wants to merge 1 commit into from
Closed

Conversation

treemo
Copy link
Contributor

@treemo treemo commented Jun 10, 2015

No description provided.

@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling e6ab052 on treemo:master into 16d52a1 on circuits:master.

@prologic
Copy link
Member

I think this is fine :) 👍

@prologic
Copy link
Member

@spaceone What are your objections behind this? Sometimes being compliant with RFC(s) is not that useful when the real world is very much different :)

@treemo What problem is this solving? The OSError(s) you were getting?

@spaceone
Copy link
Contributor

@prologic

  1. non-compiant parsing: There is no parsing of header field syntax e.g. if multiple proxies are used.
  2. security. this is an API change which causes user which rely that the field is a real IP address obtained by a connected socket will be vulnerable.

Why don't you add a

class BehindProxy(BaseComponent):

    @handler('request', priority=1)
    def _on_request(self, req. res):
        req.remote = req.headers.elements('X-Forwarded-For', req.remote.ip)[0]

instead of that ungeneric global change.... :(
But as I said: I don't really mind, as I am developing circuits.http which will never contain such a thing.

@treemo
Copy link
Contributor Author

treemo commented Jun 11, 2015

@prologic this commit is my hotfix used for OSError (I use a reverse proxy for resolve my problem)

@spaceone yes this idea (BehindProxy) is à good solution :)

@prologic
Copy link
Member

@spnaceone I like your idea here; (btw: let's keep things positive?). I think we should be moving forward more into this loosely coupled design with circuits.web itself and improve more of the design to be more flexible like this. It is circuits afterall and meant to be completely event-driven and component architectures!

@treemo Would something like BehindProxy() solve your particular issue here (even if we added it as a tool or such?)

@spaceone
Copy link
Contributor

sorry, for ranting when seeing commits i don't like

@prologic
Copy link
Member

It's quite okay :) I think it's important to keep an open mind though and keep communications flowing positively :) I do agree with you and I think your idea of splitting this out into a separate component is a great idea!

TBQH when I originally wrote/designed circuits.web I cobbled together pieces of cherrypy, basehttpserver and a few other things. It could be a lot better and more in-line with the "circuits" way so to speak :)

However; that being said -- I strongly believe we can achieve a better design without necessarily breaking large parts of the higher level API :)

@prologic
Copy link
Member

@treemo I think we could all be happy if you moved this code change into something like circuits.web.tools.ProxyFilter or such; something appropriately named :) That way it's a) loosely coupled and b) optional to the end-user/developer Agreed @spaceone?

@treemo
Copy link
Contributor Author

treemo commented Jun 11, 2015

@spaceone no problem (the comments are designed for make avance the idea) :)
@prologic yes, I'm go test this :)

@prologic
Copy link
Member

Awesome work guys :) 👍

@landscape-bot
Copy link

Code Health
Repository health decreased by 0.05% when pulling 6988bbe on treemo:master into 16d52a1 on circuits:master.

@landscape-bot
Copy link

Code Health
Repository health decreased by 0.04% when pulling ec839e7 on treemo:master into 16d52a1 on circuits:master.

@landscape-bot
Copy link

Code Health
Repository health decreased by 0.06% when pulling 3e2d689 on treemo:master into 16d52a1 on circuits:master.

@landscape-bot
Copy link

Code Health
Repository health decreased by 0.03% when pulling 87ceadd on treemo:master into 16d52a1 on circuits:master.

@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling 0ecfd1f on treemo:master into 16d52a1 on circuits:master.

@@ -13,6 +13,8 @@
from email.utils import formatdate
from datetime import datetime, timedelta
from email.generator import _make_boundary
from circuits import BaseComponent, handler
from circuits.web.wrappers import Host
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these imports down; we like to separate imports:

std lib imports


3rd party imports


local imports

@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling 9aed186 on treemo:master into 40e4e7e on circuits:master.

@prologic
Copy link
Member

@treemo Don't you want to short circuit this and reutrn on the first header found?

@prologic
Copy link
Member

I'm proposing this slight change:

diff --git a/circuits/web/tools.py b/circuits/web/tools.py
index b99203e..1d5a357 100644
--- a/circuits/web/tools.py
+++ b/circuits/web/tools.py
@@ -455,21 +455,14 @@ def gzip(response, level=4, mime_types=("text/html", "text/plain",)):


 class ReverseProxy(BaseComponent):
-    headers = ('X-Real-IP', 'X-Forwarded-For')

-    def __init__(self, headers=None):
-        """ Component for have the original client IP when a reverse proxy is used
-
-        :param headers: List of HTTP headers to read the original client IP
-        """
-        super(ReverseProxy, self).__init__()
+    headers = ("X-Real-IP", "X-Forwarded-For")

+    def init(self, headers=None):
         if headers:
             self.headers = headers

     @handler('request', priority=1)
     def _on_request(self, req, *_):
-        for index in self.headers:
-            real_ip = req.headers.get(index)
-            req.remote = Host(real_ip, '', real_ip)
-            return
+        ip = filter(None, map(req.headers.get, self.headers))
+        req.remote = ip and Host(ip, "", ip) or req.remote

Thoughts?

@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling a93d47a on treemo:master into 40e4e7e on circuits:master.

@prologic
Copy link
Member

If this works for you let's merge it :) 👍

@treemo
Copy link
Contributor Author

treemo commented Jun 12, 2015

it's ok on my server :)

@prologic
Copy link
Member

Merged :) but I don't know wtf I did!

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

Successfully merging this pull request may close these issues.

None yet

4 participants