exception when remote IP makes no sense #85

Closed
dholth opened this Issue Dec 6, 2012 · 6 comments

Comments

Projects
None yet
6 participants

dholth commented Dec 6, 2012

  response = self.handle_request(request)
  File "pyramid_debugtoolbar/toolbar.py", line 108, in toolbar_tween
    if ipaddr.IPAddress(remote_addr) in ipaddr.IPNetwork(host):
  File "pyramid_debugtoolbar/ipaddr.py", line 85, in IPAddress
    address)
ValueError: '192.168.117.153, 216.53.134.86' does not appear to be an IPv4 or IPv6 address

pyramid-debugtoolbar==1.0.2

merwok commented Jan 22, 2013

From a coworker: it looks like the IP is taken from HTTP_X_FORWARDED_FOR and parsed wrongly.

Contributor

hadrien commented Jan 22, 2013

Got it on 1.0.3:

Traceback (most recent call last):
  File "/opt/python-hayk/lib/python2.7/site-packages/PasteDeploy-1.5.0-py2.7.egg/paste/deploy/config.py", line 291, in __call__
    return self.app(environ, start_response)
  File "/opt/python-hayk/lib/python2.7/site-packages/pyramid-1.3.4-py2.7.egg/pyramid/router.py", line 187, in __call__
    response = self.handle_request(request)
  File "/opt/python-hayk/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.3-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 105, in toolbar_tween
    or (not addr_in(remote_addr, hosts)) ):
  File "/opt/python-hayk/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.3-py2.7.egg/pyramid_debugtoolbar/utils.py", line 158, in addr_in
    if ipaddr.IPAddress(addr) in ipaddr.IPNetwork(host):
  File "/opt/python-hayk/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.3-py2.7.egg/pyramid_debugtoolbar/ipaddr.py", line 85, in IPAddress
    address)
ValueError: '172.16.63.156, 64.119.211.105' does not appear to be an IPv4 or IPv6 address
Contributor

hadrien commented Jan 22, 2013

reproduced by:

from pyramid import testing
from pyramid.response import Response
from pyramid.request import Request
from pyramid_debugtoolbar.toolbar import toolbar_tween_factory

config = testing.setUp()

config.add_route('debugtoolbar.root', '/_debug_toolbar')
config.add_static_view('_debugtoolbar/static', '/')

settings = config.registry.settings
settings['debugtoolbar.enabled'] = True
settings['debugtoolbar.hosts'] = ['127.0.0.1']
settings['mako.directories'] = []
settings['debugtoolbar.exclude_prefixes'] = ['/excluded']

registry = config.registry

request = Request.blank('/')

# behind proxies:
request.remote_addr = '172.16.63.156, 64.119.211.105'

def handler(request):
    return Response('OK')
handler = toolbar_tween_factory(handler, registry)

# this call will fail
handler(request)

Will give:

Traceback (most recent call last):
  File "reproduce.py", line 27, in <module>
    handler(request)
  File ".virtualenvs/mp34/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 109, in toolbar_tween
    (not addr_in(remote_addr, hosts))
  File ".virtualenvs/mp34/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/utils.py", line 158, in addr_in
    if ipaddr.IPAddress(addr) in ipaddr.IPNetwork(host):
  File ".virtualenvs/mp34/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.4-py2.7.egg/pyramid_debugtoolbar/ipaddr.py", line 85, in IPAddress
    address)
ValueError: '172.16.63.156, 64.119.211.105' does not appear to be an IPv4 or IPv6 address

hadrien added a commit to hadrien/pyramid_debugtoolbar that referenced this issue Jan 22, 2013

blaflamme added a commit that referenced this issue Jan 29, 2013

Merge pull request #91 from hadrien/master
Fixed bug #85 when request.remote_addr is a comma separated list of proxies IPs

merwok commented Apr 8, 2013

Hi Blaise, thanks for merging this. Could you tell us when this fix will be part of a release?

Owner

blaflamme commented Apr 8, 2013

Let me see if there are other pull requests to merge and do a release

I still receive a similar exception with a pyramid application that runs behind an nginx proxy:

Module weberror.errormiddleware:162 in __call__
>>  app_iter = self.application(environ, sr_checker)
Module pyramid.router:187 in __call__
>>  response = self.handle_request(request)
Module pyramid_debugtoolbar.toolbar:130 in toolbar_tween
>>  return handler(request)
Module pyramid.tweens:20 in excview_tween
>>  response = handler(request)
Module pyramid_tm:95 in tm_tween
>>  response = handler(request)
Module pyramid.router:164 in handle_request
>>  response = view_callable(context, request)
Module pyramid.config.views:307 in attr_view
>>  return view(context, request)
Module pyramid.config.views:279 in predicate_wrapper
>>  if all((predicate(context, request) for predicate in predicates)):
Module pyramid.config.views:279 in <genexpr>
>>  if all((predicate(context, request) for predicate in predicates)):
Module pyramid_debugtoolbar.views:26 in valid_host
>>  return addr_in(request.remote_addr, hosts)
Module pyramid_debugtoolbar.utils:158 in addr_in
>>  if ipaddr.IPAddress(addr) in ipaddr.IPNetwork(host):
Module pyramid_debugtoolbar.ipaddr:85 in IPAddress
>>  address)
ValueError: '188.174.236.210, 192.168.0.1' does not appear to be an IPv4 or IPv6 address

I'm running pyramid_debugtoolbar==1.0.6. It seems the issue has been fixed for one area of the code, but this time the exception happens in another part.

danwerner added a commit to gulp-swe/pyramid_debugtoolbar that referenced this issue Jun 3, 2013

@mcdonc mcdonc closed this Aug 10, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment